forked from clojure/tools.namespace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
find.clj
194 lines (165 loc) · 7.15 KB
/
find.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
;; Copyright (c) Stuart Sierra, 2012. All rights reserved. The use and
;; distribution terms for this software are covered by the Eclipse
;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
;; which can be found in the file epl-v10.html at the root of this
;; distribution. By using this software in any fashion, you are
;; agreeing to be bound by the terms of this license. You must not
;; remove this notice, or any other, from this software.
(ns
^{:author "Stuart Sierra",
:doc "Search for namespace declarations in directories and JAR files."}
lambdaisland.tools.namespace.find
(:require [clojure.java.classpath :as classpath]
[clojure.java.io :as io]
[clojure.set :as set]
[lambdaisland.tools.namespace.file :as file]
[lambdaisland.tools.namespace.parse :as parse])
(:import (java.io File FileReader BufferedReader PushbackReader
InputStreamReader)
(java.util.jar JarFile JarEntry)))
(def ^{:added "0.3.0"}
clj
"Platform definition of file extensions and reader options for
Clojure (.clj and .cljc) source files."
{:read-opts parse/clj-read-opts
:extensions file/clojure-extensions})
(def ^{:added "0.3.0"}
cljs
"Platform definition of file extensions and reader options for
ClojureScript (.cljs and .cljc) source files."
{:read-opts parse/cljs-read-opts
:extensions file/clojurescript-extensions})
(defmacro ^:private detect-reader-exception
"If body throws an exception caused by a syntax error (from
tools.reader), returns nil. Rethrows other exceptions."
[file & body]
`(try ~@body
(catch Exception e#
(if (= :reader-exception (:type (ex-data e#)))
[::reader-exception ~file e#]
(throw e#)))))
;;; Finding namespaces in a directory tree
(defn- sort-files-breadth-first
[files]
(sort-by #(.getAbsolutePath ^File %) files))
(defn find-sources-in-dir
"Searches recursively under dir for source files. Returns a sequence
of File objects, in breadth-first sort order.
Optional second argument is either clj (default) or cljs, both
defined in lambdaisland.tools.namespace.find."
{:added "0.3.0"}
([dir]
(find-sources-in-dir dir nil))
([^File dir platform]
(let [{:keys [extensions]} (or platform clj)]
(->> (file-seq dir)
(filter #(file/file-with-extension? % extensions))
sort-files-breadth-first))))
(defn find-ns-decls-in-dir
"Searches dir recursively for (ns ...) declarations in Clojure
source files; returns the unevaluated ns declarations.
Optional second argument platform is either clj (default) or cljs,
both defined in lambdaisland.tools.namespace.find."
{:added "0.2.0"}
([dir] (find-ns-decls-in-dir dir nil))
([dir platform]
(keep #(detect-reader-exception
%
(file/read-file-ns-decl % (:read-opts platform)))
(find-sources-in-dir dir platform))))
(defn reader-exception? [sym-or-vec]
(and (vector? sym-or-vec)
(= ::reader-exception (first sym-or-vec))))
(defn maybe-parse-name-from-ns-decl [ns-form]
(if (reader-exception? ns-form)
ns-form
(parse/name-from-ns-decl ns-form)))
(defn find-namespaces-in-dir
"Searches dir recursively for (ns ...) declarations in Clojure
source files; returns the symbol names of the declared namespaces.
Optional second argument platform is either clj (default) or cljs,
both defined in lambdaisland.tools.namespace.find."
{:added "0.3.0"}
([dir] (find-namespaces-in-dir dir nil))
([dir platform]
(map maybe-parse-name-from-ns-decl (find-ns-decls-in-dir dir platform))))
;;; Finding namespaces in JAR files
(defn- ends-with-extension
[^String filename extensions]
(some #(.endsWith filename %) extensions))
(defn sources-in-jar
"Returns a sequence of source file names found in the JAR file.
Optional second argument platform is either clj (default) or cljs,
both defined in lambdaisland.tools.namespace.find."
{:added "0.3.0"}
([jar-file]
(sources-in-jar jar-file nil))
([^JarFile jar-file platform]
(let [{:keys [extensions]} (or platform clj)]
(filter #(ends-with-extension % extensions)
(classpath/filenames-in-jar jar-file)))))
(defn read-ns-decl-from-jarfile-entry
"Attempts to read a (ns ...) declaration from the named entry in the
JAR file, and returns the unevaluated form. Returns nil if read
fails due to invalid syntax or if a ns declaration cannot be found.
Optional third argument platform is either clj (default) or cljs,
both defined in lambdaisland.tools.namespace.find."
([jarfile entry-name]
(read-ns-decl-from-jarfile-entry jarfile entry-name nil))
([^JarFile jarfile ^String entry-name platform]
(let [{:keys [read-opts]} (or platform clj)]
(with-open [rdr (PushbackReader.
(io/reader
(.getInputStream jarfile (.getEntry jarfile entry-name))))]
(detect-reader-exception
(parse/read-ns-decl rdr read-opts))))))
(defn find-ns-decls-in-jarfile
"Searches the JAR file for source files containing (ns ...)
declarations; returns the unevaluated ns declarations.
Optional second argument platform is either clj (default) or cljs,
both defined in lambdaisland.tools.namespace.find."
([jarfile]
(find-ns-decls-in-jarfile jarfile nil))
([^JarFile jarfile platform]
(keep #(read-ns-decl-from-jarfile-entry jarfile % platform)
(sources-in-jar jarfile platform))))
(defn find-namespaces-in-jarfile
"Searches the JAR file for platform source files containing (ns ...)
declarations. Returns a sequence of the symbol names of the
declared namespaces.
Optional second argument platform is either clj (default) or cljs,
both defined in lambdaisland.tools.namespace.find."
([jarfile]
(find-namespaces-in-jarfile jarfile nil))
([^JarFile jarfile platform]
(map maybe-parse-name-from-ns-decl (find-ns-decls-in-jarfile jarfile platform))))
;;; Finding namespaces
(defn find-ns-decls
"Searches a sequence of java.io.File objects (both directories and
JAR files) for platform source files containing (ns...)
declarations. Returns a sequence of the unevaluated ns declaration
forms. Use with clojure.java.classpath to search Clojure's
classpath.
Optional second argument platform is either clj (default) or cljs,
both defined in lambdaisland.tools.namespace.find."
([files]
(find-ns-decls files nil))
([files platform]
(concat
(mapcat #(find-ns-decls-in-dir % platform)
(filter #(.isDirectory ^File %) files))
(mapcat #(find-ns-decls-in-jarfile % platform)
(map #(JarFile. (io/file %))
(filter classpath/jar-file? files))))))
(defn find-namespaces
"Searches a sequence of java.io.File objects (both directories and
JAR files) for platform source files containing (ns...)
declarations. Returns a sequence of the symbol names of the declared
namespaces. Use with clojure.java.classpath to search Clojure's
classpath.
Optional second argument platform is either clj (default) or cljs,
both defined in lambdaisland.tools.namespace.find."
([files]
(find-namespaces files nil))
([files platform]
(map parse/name-from-ns-decl (find-ns-decls files platform))))