Skip to content

Commit 39016f7

Browse files
arichiardibbatsov
authored andcommitted
Move part of the resource implementation to orchard
This patch moves the code for the resource op down into the orchard.
1 parent f431370 commit 39016f7

File tree

3 files changed

+6
-69
lines changed

3 files changed

+6
-69
lines changed
Lines changed: 4 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,10 @@
11
(ns cider.nrepl.middleware.resource
22
(:require
33
[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]))
546

557
(defn handle-resource [handler msg]
568
(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))}))

src/cider/nrepl/middleware/stacktrace.clj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
[nrepl.misc :refer [response-for]]
1111
[nrepl.transport :as t]
1212
[orchard.info :as info]
13-
[orchard.resource :as resource]
1413
[orchard.java :as java]
1514
[orchard.misc :as u]
16-
[orchard.namespace :as namespace])
15+
[orchard.namespace :as namespace]
16+
[orchard.resource :as resource])
1717
(:import
1818
(clojure.lang Compiler$CompilerException)
1919
(java.io StringWriter)))
Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
(ns cider.nrepl.middleware.resource-test
22
(:require
33
[cider.nrepl.test-session :as session]
4-
[cider.nrepl.middleware.resource :as r]
54
[clojure.test :refer :all]))
65

76
(use-fixtures :once session/session-fixture)
@@ -19,17 +18,3 @@
1918
(:resources-list response))))
2019
(is (seq (filter #(re-matches #".*test/resources/test\.txt" (:file %))
2120
(:resources-list response)))))))
22-
23-
(deftest resource-op-error-handling-test
24-
(with-redefs [r/resource-path (fn [& _] (throw (Exception. "resource")))]
25-
(let [response (session/message {:op "resource" :name "test.txt"})]
26-
(is (= "class java.lang.Exception" (:ex response)))
27-
(is (= #{"done" "resource-error"} (:status response)))
28-
(is (:pp-stacktrace response)))))
29-
30-
(deftest resources-list-op-error-handling-test
31-
(with-redefs [r/resources-list (fn [& _] (throw (Exception. "resources list")))]
32-
(let [response (session/message {:op "resources-list"})]
33-
(is (= "class java.lang.Exception" (:ex response)))
34-
(is (= #{"done" "resources-list-error"} (:status response)))
35-
(is (:pp-stacktrace response)))))

0 commit comments

Comments
 (0)