-
Notifications
You must be signed in to change notification settings - Fork 175
/
resource.clj
59 lines (52 loc) · 1.86 KB
/
resource.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
(ns cider.nrepl.middleware.resource
(:require
[cider.nrepl.middleware.util.error-handling :refer [with-safe-transport]]
[clojure.java.io :as io]
[orchard.classloader :refer [class-loader]]
[orchard.classpath :as cp]
[orchard.misc :as u]))
(defn- trim-leading-separator
[s]
(if (.startsWith s java.io.File/separator)
(subs s 1)
s))
(defn- get-project-resources
[]
(mapcat
(fn [directory]
(->> directory
(file-seq)
(filter (memfn isFile))
(map (fn [file]
(let [relpath (-> file
(.getPath)
(.replaceFirst
(.getPath directory)
"")
(trim-leading-separator))]
{:root directory
:file file
:relpath relpath
:url (io/resource relpath)})))
(remove #(.startsWith (:relpath %) "META-INF/"))
(remove #(re-matches #".*\.(clj[cs]?|java|class)" (:relpath %)))))
(filter (memfn isDirectory)
(cp/classpath (class-loader)))))
(defn resource-path [name]
(when-let [resource (io/resource name (class-loader))]
(.getPath resource)))
(defn resources-list
"Return a list of dictionaries containing file and relpath: file is the
absolute path to the resource, relpath is the path of the resource relative
to the classpath."
[_]
(map #(select-keys % [:file :relpath])
(get-project-resources)))
(defn resource-reply [{:keys [name] :as msg}]
{:resource-path (resource-path name)})
(defn resources-list-reply [msg]
{:resources-list (u/transform-value (resources-list msg))})
(defn handle-resource [handler msg]
(with-safe-transport handler msg
"resource" resource-reply
"resources-list" resources-list-reply))