| 1 |
(ns cpath-clj.core |
| 2 |
(:require [clojure.java |
| 3 |
[io :as io] |
| 4 |
[classpath :as cp]]) |
| 5 |
(:import [java.net URL URI] |
| 6 |
[java.util.jar JarFile] |
| 7 |
[java.io File])) |
| 8 |
|
| 9 |
;; ## Read Directories from Classpath |
| 10 |
|
| 11 |
(defmulti ^:private resource-file-uris |
| 12 |
"Create a seq of all files that are children of the given URI (e.g. files |
| 13 |
within a directory, files within a JAR, ...). The result will be a seq of |
| 14 |
pairs, the first element being the path relative to the base, the second |
| 15 |
one being a `java.net.URI`." |
| 16 |
(fn [^URL directory-url] |
| 17 |
(.getProtocol directory-url)) |
| 18 |
:default nil) |
| 19 |
|
| 20 |
(defmethod resource-file-uris nil |
| 21 |
[_] |
| 22 |
[]) |
| 23 |
|
| 24 |
;; ### Local Directory/File |
| 25 |
|
| 26 |
(defn- recursive-directory-files |
| 27 |
"Recursively list the files in the given directory. Returns a single-element |
| 28 |
vector if the given File is not a directory." |
| 29 |
[prefix ^File f] |
| 30 |
(let [new-prefix (str prefix "/" (.getName f))] |
| 31 |
(cond (.isFile f) [[new-prefix f]] |
| 32 |
(.isDirectory f) (vec |
| 33 |
(mapcat |
| 34 |
#(recursive-directory-files new-prefix %) |
| 35 |
(.listFiles f))) |
| 36 |
:else []))) |
| 37 |
|
| 38 |
(defmethod resource-file-uris "file" |
| 39 |
[^URL url] |
| 40 |
(let [^File f (io/file url)] |
| 41 |
(if (.isDirectory f) |
| 42 |
(->> (.listFiles f) |
| 43 |
(mapcat #(recursive-directory-files nil %)) |
| 44 |
(mapv |
| 45 |
(fn [[path ^File f]] |
| 46 |
[path (.toURI f)]))) |
| 47 |
[[(str "/" (.getName f)) (.toURI f)]]))) |
| 48 |
|
| 49 |
;; ### JAR Files |
| 50 |
|
| 51 |
(defn- jar-content-uri |
| 52 |
"Create URI pointing to a file within the given JAR." |
| 53 |
[^URL jar-location ^String path] |
| 54 |
(->> (str "jar:" (.toURI jar-location) "!/" path) |
| 55 |
(URL.) |
| 56 |
(.toURI))) |
| 57 |
|
| 58 |
(defmethod resource-file-uris "jar" |
| 59 |
[^URL url] |
| 60 |
(let [^String path (.getPath url) |
| 61 |
idx (.lastIndexOf path "!")] |
| 62 |
(if (neg? idx) |
| 63 |
(->> (cp/filenames-in-jar (JarFile. (io/file url))) |
| 64 |
(map |
| 65 |
(juxt |
| 66 |
#(str "/" %) |
| 67 |
#(jar-content-uri url %)))) |
| 68 |
(let [jar-location (URL. (subs path 0 idx)) |
| 69 |
jar-file (JarFile. (io/file jar-location)) |
| 70 |
prefix (subs path (+ idx 2)) |
| 71 |
prefix-length (count prefix)] |
| 72 |
(->> (cp/filenames-in-jar jar-file) |
| 73 |
(filter #(.startsWith ^String % prefix)) |
| 74 |
(map |
| 75 |
(juxt |
| 76 |
(if (= prefix "") |
| 77 |
#(str "/" %) |
| 78 |
#(subs % prefix-length)) |
| 79 |
#(jar-content-uri jar-location %)))))))) |
| 80 |
|
| 81 |
;; ## Classpath Inspection |
| 82 |
|
| 83 |
(defn resources* |
| 84 |
"Find all resources with the given path on the classpath. |
| 85 |
Returns a seq of resource URLs." |
| 86 |
[path] |
| 87 |
(-> (Thread/currentThread) |
| 88 |
(.getContextClassLoader) |
| 89 |
(.getResources path) |
| 90 |
(enumeration-seq))) |
| 91 |
|
| 92 |
;; ## Resource Lookup Protocol |
| 93 |
|
| 94 |
(defprotocol ResourceLookup |
| 95 |
"Protocol for values that allow resource lookups." |
| 96 |
(child-resources [this] |
| 97 |
"Return a seq of path/URI pairs describing resources.")) |
| 98 |
|
| 99 |
(extend-protocol ResourceLookup |
| 100 |
URL |
| 101 |
(child-resources [url] |
| 102 |
(resource-file-uris url)) |
| 103 |
|
| 104 |
URI |
| 105 |
(child-resources [uri] |
| 106 |
(resource-file-uris (.toURL ^URI uri))) |
| 107 |
|
| 108 |
File |
| 109 |
(child-resources [f] |
| 110 |
(-> (.toURI ^File f) |
| 111 |
(.toURL) |
| 112 |
(resource-file-uris))) |
| 113 |
|
| 114 |
String |
| 115 |
(child-resources [path] |
| 116 |
(mapcat resource-file-uris (resources* path)))) |
| 117 |
|
| 118 |
;; ## Resource Inspection |
| 119 |
|
| 120 |
(defn resource-uris |
| 121 |
"Create a seq of URIs matching all children of resources with the given |
| 122 |
base." |
| 123 |
([] (resource-uris "")) |
| 124 |
([base] |
| 125 |
(map second (child-resources base)))) |
| 126 |
|
| 127 |
(defn resources |
| 128 |
"Find all files on the classpath that are children of resources |
| 129 |
with the given base. The result will be a map associating the path |
| 130 |
relative to the given base with a seq of URIs pointing to the resources." |
| 131 |
([] (resources "")) |
| 132 |
([base] |
| 133 |
(->> (child-resources base) |
| 134 |
(reduce |
| 135 |
(fn [m [^String path ^URI uri]] |
| 136 |
(update-in m [path] (fnil conj []) uri)) |
| 137 |
{})))) |