-
-
Notifications
You must be signed in to change notification settings - Fork 54
/
classpath.clj
53 lines (46 loc) · 1.71 KB
/
classpath.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
(ns orchard.classpath
"A simple wrapper around `clojure.java.classpath` that is Boot-aware."
(:require
[clojure.java.classpath :as cp]
[clojure.string :as str]
[orchard.classloader :as cl]
[orchard.misc :as u])
(:import
java.io.File
java.util.jar.JarFile))
(defn- ensure-absolute-paths
"Returns a `File` guaranteeing an absolute path for `file`."
[^File file]
(if (.isAbsolute file)
file
(File. (.getAbsolutePath file))))
(defn classpath
"Return a sequence of File objects of elements on the classpath.
It takes into account the classpath trickery performed by Boot."
([]
(classpath (cl/class-loader)))
([classloader]
(let [sep (re-pattern File/pathSeparator)
;; TODO: Check if that's really needed - after all we have a Boot-aware classloader
boot-classpath (u/boot-fake-classpath)
path (if boot-classpath
(map #(File. ^String %) (str/split boot-classpath sep))
;; See https://dev.clojure.org/jira/browse/CLASSPATH-8
(or (seq (cp/classpath classloader))
;; Java 9+
(map ensure-absolute-paths (cp/system-classpath))))]
path)))
(defn classpath-directories
"Returns a sequence of File objects for the directories on classpath.
Uses `classpath` instead of `clojure.java.classpath/classpath`."
([]
(classpath-directories (classpath)))
([path]
(filter #(.isDirectory ^File %) path)))
(defn classpath-jarfiles
"Returns a sequence of JarFile objects for the JAR files on classpath.
Uses `classpath` instead of `clojure.java.classpath/classpath`."
([]
(classpath-jarfiles (classpath)))
([path]
(map #(JarFile. ^File %) (filter cp/jar-file? path))))