|
1 | 1 | (ns cider.nrepl.middleware.resource
|
2 | 2 | (:require
|
3 | 3 | [cider.nrepl.middleware.util.error-handling :refer [with-safe-transport]]
|
4 |
| - [clojure.java.io :as io] |
5 |
| - [clojure.string :as str] |
6 |
| - [orchard.classpath :as cp] |
7 |
| - [orchard.misc :as u])) |
8 |
| - |
9 |
| -(defn- trim-leading-separator |
10 |
| - [s] |
11 |
| - (if (.startsWith s java.io.File/separator) |
12 |
| - (subs s 1) |
13 |
| - s)) |
14 |
| - |
15 |
| -(defn- get-project-resources |
16 |
| - [] |
17 |
| - (mapcat |
18 |
| - (fn [directory] |
19 |
| - (->> directory |
20 |
| - (file-seq) |
21 |
| - (filter (memfn isFile)) |
22 |
| - (map (fn [file] |
23 |
| - (let [relpath (-> file |
24 |
| - (.getPath) |
25 |
| - (.replaceFirst |
26 |
| - (.getPath directory) |
27 |
| - "") |
28 |
| - (trim-leading-separator))] |
29 |
| - {:root directory |
30 |
| - :file file |
31 |
| - :relpath relpath |
32 |
| - :url (io/resource relpath)}))) |
33 |
| - (remove #(.startsWith (:relpath %) "META-INF/")) |
34 |
| - (remove #(re-matches #".*\.(clj[cs]?|java|class)" (:relpath %))))) |
35 |
| - (filter (memfn isDirectory) (map io/as-file (cp/classpath (cp/boot-aware-classloader)))))) |
36 |
| - |
37 |
| -(defn resource-path [name] |
38 |
| - (when-let [resource (io/resource name (cp/boot-aware-classloader))] |
39 |
| - (.getPath resource))) |
40 |
| - |
41 |
| -(defn resources-list |
42 |
| - "Return a list of dictionaries containing file and relpath: file is the |
43 |
| - absolute path to the resource, relpath is the path of the resource relative |
44 |
| - to the classpath." |
45 |
| - [_] |
46 |
| - (map #(select-keys % [:file :relpath]) |
47 |
| - (get-project-resources))) |
48 |
| - |
49 |
| -(defn resource-reply [{:keys [name] :as msg}] |
50 |
| - {:resource-path (resource-path name)}) |
51 |
| - |
52 |
| -(defn resources-list-reply [msg] |
53 |
| - {:resources-list (u/transform-value (resources-list msg))}) |
| 4 | + [orchard.misc :as u] |
| 5 | + [orchard.resource :as resource])) |
54 | 6 |
|
55 | 7 | (defn handle-resource [handler msg]
|
56 | 8 | (with-safe-transport handler msg
|
57 |
| - "resource" resource-reply |
58 |
| - "resources-list" resources-list-reply)) |
| 9 | + "resource" {:resource-path (resource/resource-path (:name msg))} |
| 10 | + "resources-list" {:resources-list (u/transform-value (resource/resource-maps))})) |
0 commit comments