|
19 | 19 | InputStreamReader)
|
20 | 20 | (java.util.jar JarFile JarEntry)))
|
21 | 21 |
|
| 22 | +(def clojure-platform |
| 23 | + "Definition of file extensions and reader options for Clojure (.clj |
| 24 | + and .cljc) source files." |
| 25 | + {:read-opts parse/clojure-read-opts |
| 26 | + :extensions file/clojure-extensions}) |
| 27 | + |
| 28 | +(def clojurescript-platform |
| 29 | + "Definition of file extensions and reader options for ClojureScript |
| 30 | + (.cljs and .cljc) source files." |
| 31 | + {:read-opts parse/clojurescript-read-opts |
| 32 | + :extensions file/clojurescript-extensions}) |
| 33 | + |
22 | 34 | ;;; Finding namespaces in a directory tree
|
23 | 35 |
|
| 36 | +(defn- sort-files-breadth-first |
| 37 | + [files] |
| 38 | + (sort-by #(.getAbsolutePath ^File %) files)) |
| 39 | + |
| 40 | +(defn find-sources-in-dir |
| 41 | + "Searches recursively under dir for source files. Returns a sequence |
| 42 | + of File objects, in breadth-first sort order." |
| 43 | + ([dir] |
| 44 | + (find-sources-in-dir dir nil)) |
| 45 | + ([^File dir platform] |
| 46 | + (let [{:keys [extensions]} (or platform clojure-platform)] |
| 47 | + (->> (file-seq dir) |
| 48 | + (filter #(file/file-with-extension? % extensions)) |
| 49 | + sort-files-breadth-first)))) |
| 50 | + |
24 | 51 | (defn find-clojure-sources-in-dir
|
25 |
| - "Searches recursively under dir for Clojure source files (.clj, .cljc). |
| 52 | + "DEPRECATED: replaced by find-sources-in-dir |
| 53 | +
|
| 54 | + Searches recursively under dir for Clojure source files (.clj, .cljc). |
26 | 55 | Returns a sequence of File objects, in breadth-first sort order."
|
27 | 56 | [^File dir]
|
28 |
| - ;; Use sort by absolute path to get breadth-first search. |
29 |
| - (sort-by #(.getAbsolutePath ^File %) |
30 |
| - (filter file/clojure-file? (file-seq dir)))) |
| 57 | + (find-sources-in-dir dir clojure-platform)) |
31 | 58 |
|
32 | 59 | (defn find-ns-decls-in-dir
|
33 | 60 | "Searches dir recursively for (ns ...) declarations in Clojure
|
34 | 61 | source files; returns the unevaluated ns declarations."
|
35 |
| - [^File dir] |
36 |
| - (keep file/read-file-ns-decl (find-clojure-sources-in-dir dir))) |
| 62 | + ([dir] (find-ns-decls-in-dir dir nil)) |
| 63 | + ([dir options] |
| 64 | + (keep #(file/read-file-ns-decl % options) |
| 65 | + (find-clojure-sources-in-dir dir)))) |
37 | 66 |
|
38 | 67 | (defn find-namespaces-in-dir
|
39 | 68 | "Searches dir recursively for (ns ...) declarations in Clojure
|
40 | 69 | source files; returns the symbol names of the declared namespaces."
|
41 |
| - [^File dir] |
42 |
| - (map second (find-ns-decls-in-dir dir))) |
| 70 | + ([dir] (find-namespaces-in-dir dir)) |
| 71 | + ([dir options] |
| 72 | + (map second (find-ns-decls-in-dir dir options)))) |
43 | 73 |
|
44 | 74 | ;;; Finding namespaces in JAR files
|
45 | 75 |
|
| 76 | +(defn- ends-with-extension |
| 77 | + [^String filename extensions] |
| 78 | + (some #(.endsWith filename %) extensions)) |
| 79 | + |
| 80 | +(defn sources-in-jar |
| 81 | + "Returns a sequence of source file names found in the JAR file. |
| 82 | + Optional second argument is either clojure-platform or |
| 83 | + clojurescript-platform, both defined in this namespace.. Controls |
| 84 | + which file extensions are included." |
| 85 | + ([jar-file] |
| 86 | + (sources-in-jar jar-file nil)) |
| 87 | + ([^JarFile jar-file platform] |
| 88 | + (let [{:keys [extensions]} (or platform clojure-platform)] |
| 89 | + (filter #(ends-with-extension % extensions) |
| 90 | + (classpath/filenames-in-jar jar-file))))) |
| 91 | + |
46 | 92 | (defn clojure-sources-in-jar
|
47 |
| - "Returns a sequence of filenames ending in .clj or .cljc found in the JAR file." |
48 |
| - [^JarFile jar-file] |
49 |
| - (filter #(or (.endsWith ^String % ".clj") (.endsWith ^String % ".cljc")) |
50 |
| - (classpath/filenames-in-jar jar-file))) |
| 93 | + "DEPRECATED: replaced by sources-in-jar |
| 94 | +
|
| 95 | + Returns a sequence of filenames ending in .clj or .cljc found in the |
| 96 | + JAR file." |
| 97 | + [jar-file] |
| 98 | + (sources-in-jar jar-file clojure-platform)) |
51 | 99 |
|
52 | 100 | (defn read-ns-decl-from-jarfile-entry
|
53 | 101 | "Attempts to read a (ns ...) declaration from the named entry in the
|
54 | 102 | JAR file, and returns the unevaluated form. Returns nil if the read
|
55 | 103 | fails, or if the first form is not a ns declaration."
|
56 |
| - [^JarFile jarfile ^String entry-name] |
57 |
| - (with-open [rdr (PushbackReader. |
58 |
| - (io/reader |
59 |
| - (.getInputStream jarfile (.getEntry jarfile entry-name))))] |
60 |
| - (parse/read-ns-decl rdr))) |
| 104 | + ([jarfile entry-name] |
| 105 | + (read-ns-decl-from-jarfile-entry jarfile entry-name nil)) |
| 106 | + ([^JarFile jarfile ^String entry-name platform] |
| 107 | + (let [{:keys [read-opts]} (or platform clojure-platform)] |
| 108 | + (with-open [rdr (PushbackReader. |
| 109 | + (io/reader |
| 110 | + (.getInputStream jarfile (.getEntry jarfile entry-name))))] |
| 111 | + (parse/read-ns-decl rdr read-opts))))) |
61 | 112 |
|
62 | 113 | (defn find-ns-decls-in-jarfile
|
63 |
| - "Searches the JAR file for Clojure source files containing (ns ...) |
| 114 | + "Searches the JAR file for platform source files containing (ns ...) |
64 | 115 | declarations; returns the unevaluated ns declarations."
|
65 |
| - [^JarFile jarfile] |
66 |
| - (filter identity |
67 |
| - (map #(read-ns-decl-from-jarfile-entry jarfile %) |
68 |
| - (clojure-sources-in-jar jarfile)))) |
| 116 | + ([jarfile] |
| 117 | + (find-ns-decls-in-jarfile jarfile nil)) |
| 118 | + ([^JarFile jarfile platform] |
| 119 | + (keep #(read-ns-decl-from-jarfile-entry jarfile % platform) |
| 120 | + (sources-in-jar jarfile platform)))) |
69 | 121 |
|
70 | 122 | (defn find-namespaces-in-jarfile
|
71 |
| - "Searches the JAR file for Clojure source files containing (ns ...) |
| 123 | + "Searches the JAR file for platform source files containing (ns ...) |
72 | 124 | declarations. Returns a sequence of the symbol names of the
|
73 | 125 | declared namespaces."
|
74 |
| - [^JarFile jarfile] |
75 |
| - (map second (find-ns-decls-in-jarfile jarfile))) |
| 126 | + ([jarfile] |
| 127 | + (find-namespaces-in-jarfile jarfile nil)) |
| 128 | + ([^JarFile jarfile platform] |
| 129 | + (map second (find-ns-decls-in-jarfile jarfile platform)))) |
76 | 130 |
|
77 | 131 |
|
78 | 132 | ;;; Finding namespaces
|
79 | 133 |
|
80 | 134 | (defn find-ns-decls
|
81 | 135 | "Searches a sequence of java.io.File objects (both directories and
|
82 |
| - JAR files) for .clj or .cljc source files containing (ns...) declarations. |
83 |
| - Returns a sequence of the unevaluated ns declaration forms. Use with |
84 |
| - clojure.java.classpath to search Clojure's classpath." |
85 |
| - [files] |
86 |
| - (concat |
87 |
| - (mapcat find-ns-decls-in-dir (filter #(.isDirectory ^File %) files)) |
88 |
| - (mapcat find-ns-decls-in-jarfile (filter classpath/jar-file? files)))) |
| 136 | + JAR files) for platform source files containing (ns...) |
| 137 | + declarations. Returns a sequence of the unevaluated ns declaration |
| 138 | + forms. Use with clojure.java.classpath to search Clojure's |
| 139 | + classpath." |
| 140 | + ([files] |
| 141 | + (find-ns-decls files nil)) |
| 142 | + ([files platform] |
| 143 | + (concat |
| 144 | + (mapcat #(find-ns-decls-in-dir % platform) |
| 145 | + (filter #(.isDirectory ^File %) files)) |
| 146 | + (mapcat #(find-ns-decls-in-jarfile % platform) |
| 147 | + (filter classpath/jar-file? files))))) |
89 | 148 |
|
90 | 149 | (defn find-namespaces
|
91 | 150 | "Searches a sequence of java.io.File objects (both directories and
|
92 |
| - JAR files) for .clj or .cljc source files containing (ns...) declarations. |
93 |
| - Returns a sequence of the symbol names of the declared |
| 151 | + JAR files) for platform source files containing (ns...) |
| 152 | + declarations. Returns a sequence of the symbol names of the declared |
94 | 153 | namespaces. Use with clojure.java.classpath to search Clojure's
|
95 | 154 | classpath."
|
96 |
| - [files] |
97 |
| - (map second (find-ns-decls files))) |
| 155 | + ([files] |
| 156 | + (find-namespaces files nil)) |
| 157 | + ([files platform] |
| 158 | + (map second (find-ns-decls files platform)))) |
98 | 159 |
|
0 commit comments