/
namespace.clj
230 lines (190 loc) · 7.87 KB
/
namespace.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
;; 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 "This namespace is DEPRECATED; most functions have been moved to
other namespaces"
:deprecated "0.2.1"
:added "0.1.0"}
eastwood.copieddeps.dep9.clojure.tools.namespace
(:require [clojure.java.io :as io])
(:import (java.io File FileReader BufferedReader PushbackReader
InputStreamReader)
(java.util.jar JarFile JarEntry)))
;;; Finding namespaces in a directory tree
(defn clojure-source-file?
"DEPRECATED; moved to eastwood.copieddeps.dep9.clojure.tools.namespace.file
Returns true if file is a normal file with a .clj or .cljc extension."
{:deprecated "0.2.1"
:added "0.1.0"}
[^File file]
(and (.isFile file)
(or
(.endsWith (.getName file) ".clj")
(.endsWith (.getName file) ".cljc"))))
(defn find-clojure-sources-in-dir
"DEPRECATED; moved to eastwood.copieddeps.dep9.clojure.tools.namespace.find
Searches recursively under dir for Clojure source files (.clj, .cljc).
Returns a sequence of File objects, in breadth-first sort order."
{:deprecated "0.2.1"
:added "0.1.0"}
[^File dir]
;; Use sort by absolute path to get breadth-first search.
(sort-by #(.getAbsolutePath ^File %)
(filter clojure-source-file? (file-seq dir))))
(defn comment?
"DEPRECATED; moved to eastwood.copieddeps.dep9.clojure.tools.namespace.parse
Returns true if form is a (comment ...)"
{:deprecated "0.2.1"
:added "0.1.0"}
[form]
(and (list? form) (= 'comment (first form))))
(defn ns-decl?
"DEPRECATED; moved to eastwood.copieddeps.dep9.clojure.tools.namespace.parse
Returns true if form is a (ns ...) declaration."
{:deprecated "0.2.1"
:added "0.1.0"}
[form]
(and (list? form) (= 'ns (first form))))
(defn read-ns-decl
"DEPRECATED; moved to eastwood.copieddeps.dep9.clojure.tools.namespace.parse
Attempts to read a (ns ...) declaration from rdr, and returns the
unevaluated form. Returns nil if read fails or if a ns declaration
cannot be found. The ns declaration must be the first Clojure form
in the file, except for (comment ...) forms."
{:deprecated "0.2.1"
:added "0.1.0"}
[^PushbackReader rdr]
(try
(loop [] (let [form (doto (read rdr) str)]
(cond
(ns-decl? form) form
(comment? form) (recur)
:else nil)))
(catch Exception e nil)))
(defn read-file-ns-decl
"DEPRECATED; moved to eastwood.copieddeps.dep9.clojure.tools.namespace.file
Attempts to read a (ns ...) declaration from file, and returns the
unevaluated form. Returns nil if read fails, or if the first form
is not a ns declaration."
{:deprecated "0.2.1"
:added "0.1.0"}
[^File file]
(with-open [rdr (PushbackReader. (BufferedReader. (FileReader. file)))]
(read-ns-decl rdr)))
(defn find-ns-decls-in-dir
"DEPRECATED; moved to eastwood.copieddeps.dep9.clojure.tools.namespace.find
Searches dir recursively for (ns ...) declarations in Clojure
source files; returns the unevaluated ns declarations."
{:deprecated "0.2.1"
:added "0.1.0"}
[^File dir]
(filter identity (map read-file-ns-decl (find-clojure-sources-in-dir dir))))
(defn find-namespaces-in-dir
"DEPRECATED; moved to eastwood.copieddeps.dep9.clojure.tools.namespace.find
Searches dir recursively for (ns ...) declarations in Clojure
source files; returns the symbol names of the declared namespaces."
{:deprecated "0.2.1"
:added "0.1.0"}
[^File dir]
(map second (find-ns-decls-in-dir dir)))
;;; copied from eastwood.copieddeps.dep11.clojure.java.classpath to preserve deprecated API
;;; without an explicit dependency
(defn- loader-classpath [loader]
(when (instance? java.net.URLClassLoader loader)
(map
#(java.io.File. (.toURI ^java.net.URL %))
(.getURLs ^java.net.URLClassLoader loader))))
(defn- classpath
([classloader]
(distinct
(mapcat
loader-classpath
(take-while
identity
(iterate #(.getParent ^ClassLoader %) classloader)))))
([] (classpath (clojure.lang.RT/baseLoader))))
(defn- classpath-directories []
(filter #(.isDirectory ^File %) (classpath)))
(defn- jar-file? [f]
(let [file (io/file f)]
(and (.isFile file)
(or (.endsWith (.getName file) ".jar")
(.endsWith (.getName file) ".JAR")))))
(defn- classpath-jarfiles []
(map #(JarFile. ^File %) (filter jar-file? (classpath))))
(defn- filenames-in-jar [^JarFile jar-file]
(map #(.getName ^JarEntry %)
(filter #(not (.isDirectory ^JarEntry %))
(enumeration-seq (.entries jar-file)))))
;;; Finding namespaces in JAR files
(defn clojure-sources-in-jar
"DEPRECATED; moved to eastwood.copieddeps.dep9.clojure.tools.namespace.find
Returns a sequence of filenames ending in .clj or .cljc found in the JAR file."
{:deprecated "0.2.1"
:added "0.1.0"}
[^JarFile jar-file]
(filter #(or (.endsWith ^String % ".clj") (.endsWith ^String % ".cljc"))
(filenames-in-jar jar-file)))
(defn read-ns-decl-from-jarfile-entry
"DEPRECATED; moved to eastwood.copieddeps.dep9.clojure.tools.namespace.find
Attempts to read a (ns ...) declaration from the named entry in the
JAR file, and returns the unevaluated form. Returns nil if the read
fails, or if the first form is not a ns declaration."
{:deprecated "0.2.1"
:added "0.1.0"}
[^JarFile jarfile ^String entry-name]
(with-open [rdr (PushbackReader.
(BufferedReader.
(InputStreamReader.
(.getInputStream jarfile (.getEntry jarfile entry-name)))))]
(read-ns-decl rdr)))
(defn find-ns-decls-in-jarfile
"DEPRECATED; moved to eastwood.copieddeps.dep9.clojure.tools.namespace.find
Searches the JAR file for Clojure source files containing (ns ...)
declarations; returns the unevaluated ns declarations."
{:deprecated "0.2.1"
:added "0.1.0"}
[^JarFile jarfile]
(filter identity
(map #(read-ns-decl-from-jarfile-entry jarfile %)
(clojure-sources-in-jar jarfile))))
(defn find-namespaces-in-jarfile
"DEPRECATED; moved to eastwood.copieddeps.dep9.clojure.tools.namespace.find
Searches the JAR file for Clojure source files containing (ns ...)
declarations. Returns a sequence of the symbol names of the
declared namespaces."
{:deprecated "0.2.1"
:added "0.1.0"}
[^JarFile jarfile]
(map second (find-ns-decls-in-jarfile jarfile)))
;;; Finding namespaces anywhere on CLASSPATH
(defn find-ns-decls-on-classpath
"DEPRECATED; use eastwood.copieddeps.dep9.clojure.tools.namespace.find/find-ns-decls
and eastwood.copieddeps.dep11.clojure.java.classpath/classpath from
http://github.com/clojure/java.classpath
Searches CLASSPATH (both directories and JAR files) for Clojure
source files containing (ns ...) declarations. Returns a sequence of
the unevaluated ns declaration forms."
{:deprecated "0.2.1"
:added "0.1.0"}
[]
(concat
(mapcat find-ns-decls-in-dir (classpath-directories))
(mapcat find-ns-decls-in-jarfile (classpath-jarfiles))))
(defn find-namespaces-on-classpath
"DEPRECATED; use eastwood.copieddeps.dep9.clojure.tools.namespace.find/find-namespaces
and eastwood.copieddeps.dep11.clojure.java.classpath/classpath from
http://github.com/clojure/java.classpath
Searches CLASSPATH (both directories and JAR files) for Clojure
source files containing (ns ...) declarations. Returns a sequence
of the symbol names of the declared namespaces."
{:deprecated "0.2.1"
:added "0.1.0"}
[]
(map second (find-ns-decls-on-classpath)))