Skip to content

Commit 3125f4b

Browse files
committed
TNS-55: Added jar/dir and file metadata to ns declaration synbol name in find-ns-decls and test.
1 parent 56b23af commit 3125f4b

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/main/clojure/clojure/tools/namespace/find.clj

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
InputStreamReader)
2020
(java.util.jar JarFile JarEntry)))
2121

22+
(set! *warn-on-reflection* true)
23+
2224
(def ^{:added "0.3.0"}
2325
clj
2426
"Platform definition of file extensions and reader options for
@@ -77,14 +79,20 @@
7779
(defn find-ns-decls-in-dir
7880
"Searches dir recursively for (ns ...) declarations in Clojure
7981
source files; returns the unevaluated ns declarations.
82+
The symbol name in the returned ns declaration will contain metadata
83+
for the corresponding directory and located file within at keys
84+
:dir and :file.
8085
8186
Optional second argument platform is either clj (default) or cljs,
8287
both defined in clojure.tools.namespace.find."
8388
{:added "0.2.0"}
8489
([dir] (find-ns-decls-in-dir dir nil))
8590
([dir platform]
8691
(keep #(ignore-reader-exception
87-
(file/read-file-ns-decl % (:read-opts platform)))
92+
(let [[_ nom & more] (file/read-file-ns-decl % (:read-opts platform))]
93+
(list* 'ns (with-meta nom
94+
{:dir (.getName ^java.io.File dir) :file (.getName ^java.io.File %)})
95+
more)))
8896
(find-sources-in-dir dir platform))))
8997

9098
(defn find-namespaces-in-dir
@@ -131,6 +139,9 @@
131139
"Attempts to read a (ns ...) declaration from the named entry in the
132140
JAR file, and returns the unevaluated form. Returns nil if read
133141
fails due to invalid syntax or if a ns declaration cannot be found.
142+
The symbol name in the returned ns declaration will contain metadata
143+
for the corresponding jar filename and located file within at keys
144+
:jar and :file.
134145
135146
Optional third argument platform is either clj (default) or cljs,
136147
both defined in clojure.tools.namespace.find."
@@ -142,7 +153,10 @@
142153
(io/reader
143154
(.getInputStream jarfile (.getEntry jarfile entry-name))))]
144155
(ignore-reader-exception
145-
(parse/read-ns-decl rdr read-opts))))))
156+
(let [[_ nom & more] (parse/read-ns-decl rdr read-opts)]
157+
(list* 'ns (with-meta nom
158+
{:jar (.getName ^java.io.File jarfile) :file entry-name})
159+
more)))))))
146160

147161
(defn find-ns-decls-in-jarfile
148162
"Searches the JAR file for source files containing (ns ...)

src/test/clojure/clojure/tools/namespace/find_test.clj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,13 @@
2727
(is (help/same-files?
2828
[main-cljs one-cljc two-cljs]
2929
(find/find-sources-in-dir dir find/cljs)))))
30+
31+
(deftest t-find-ns-decl-meta
32+
(let [dir (help/create-temp-dir "t-find-clj-and-cljc-files")
33+
main-clj (help/create-source dir 'example.main :clj '[example.one])
34+
one-cljc (help/create-source dir 'example.one :cljc '[example.two])
35+
two-clj (help/create-source dir 'example.two :clj)
36+
two-cljs (help/create-source dir 'example.two :cljs)]
37+
(is (every? #{(.getName ^java.io.File dir)}
38+
(map #(-> % second meta :dir)
39+
(find/find-ns-decls [dir]))))))

0 commit comments

Comments
 (0)