Skip to content

Commit

Permalink
Correctly resolve resource full path in Boot projects
Browse files Browse the repository at this point in the history
  • Loading branch information
antonyshchenko committed Nov 11, 2015
1 parent 0447b81 commit fbcecba
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
35 changes: 30 additions & 5 deletions src/cider/nrepl/middleware/info.clj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,31 @@
[clojure.tools.nrepl.middleware :refer [set-descriptor!]]
[clojure.tools.nrepl.misc :refer [response-for]]))

(defn- boot-project? []
;; fake.class.path under boot contains the original directories with source
;; files, see https://github.com/boot-clj/boot/issues/249
(not (nil? (System/getProperty "fake.class.path"))))

(defn- boot-class-loader
"Creates a class-loader that knows original source files paths in Boot project."
[]
(let [class-path (System/getProperty "fake.class.path")
dir-separator (System/getProperty "file.separator")
paths (str/split class-path (re-pattern (System/getProperty "path.separator")))
urls (map
(fn [path]
(let [url (if (re-find #".jar$" path)
(str "file:" path)
(str "file:" path dir-separator))]
(new java.net.URL url)))
paths)]
(new java.net.URLClassLoader (into-array java.net.URL urls))))

(defn- resource-full-path [relative-path]
(if (boot-project?)
(io/resource relative-path (boot-class-loader))
(io/resource relative-path)))

(defn maybe-protocol
[info]
(if-let [prot-meta (meta (:protocol info))]
Expand Down Expand Up @@ -93,7 +118,7 @@
(defn find-cljx-source
"If file was cross-compiled using CLJX, return path to original file."
[filename]
(let [file (if-let [rsrc (io/resource filename)]
(let [file (if-let [rsrc (resource-full-path filename)]
(when (= "file" (.getProtocol rsrc))
(io/as-file rsrc))
(io/as-file filename))]
Expand Down Expand Up @@ -148,14 +173,14 @@
(defn resource-path
"If it's a resource, return a tuple of the relative path and the full resource path."
[x]
(or (if-let [full (io/resource x)]
(or (if-let [full (resource-full-path x)]
[x full])
(if-let [[_ relative] (re-find #".*jar!/(.*)" x)]
(if-let [full (io/resource relative)]
(if-let [full (resource-full-path relative)]
[relative full]))
;; handles load-file on jar resources from a cider buffer
(if-let [[_ relative] (re-find #".*jar:(.*)" x)]
(if-let [full (io/resource relative)]
(if-let [full (resource-full-path relative)]
[relative full]))))

(defn file-path
Expand All @@ -182,7 +207,7 @@
classes. If no source is available, return the relative path as is."
[path]
{:javadoc
(or (io/resource path)
(or (resource-full-path path)
(when (re-find #"^(java|javax|org.omg|org.w3c.dom|org.xml.sax)/" path)
(format "http://docs.oracle.com/javase/%s/docs/api/%s"
u/java-api-version path))
Expand Down
16 changes: 16 additions & 0 deletions test/clj/cider/nrepl/middleware/info_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,22 @@
(is (relative "clojure/core.clj"))
(is (nil? (relative "notclojure/core.clj"))))

(deftest test-boot-resource-path
(let [tmp-dir-name (System/getProperty "java.io.tmpdir")
tmp-file-name "boot-test.txt"
tmp-file-path (str tmp-dir-name (System/getProperty "file.separator") tmp-file-name)]
(spit tmp-file-path "test")
(testing "when fake.class.path is not set"
(is (not (= (class (file tmp-file-name))
java.net.URL)))
(is (= (file tmp-file-name) tmp-file-name)))
(testing "when fake.class.path is set"
(System/setProperty "fake.class.path" tmp-dir-name)
(is (= (class (file tmp-file-name))
java.net.URL))
(is (= (.getPath (file tmp-file-name))
tmp-file-path)))))

(deftype T [])

(deftest test-info
Expand Down

0 comments on commit fbcecba

Please sign in to comment.