Skip to content

Commit 5ca8dd2

Browse files
committed
Relocate JVM host dependent config loading code to new host ns
1 parent f301904 commit 5ca8dd2

4 files changed

Lines changed: 83 additions & 50 deletions

File tree

‎client/deps.edn‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{:paths ["src"]
1+
{:paths ["src" "src-jvm"]
22
:deps {org.clojure/clojure {:mvn/version "1.12.5"}
33
org.clojure/tools.reader {:mvn/version "1.6.0"}
44
org.clojure/tools.deps.config {:mvn/version "0.1.5"}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
(ns clojure-cli.repl.host
2+
(:require
3+
[clojure.java.shell :as sh]
4+
[clojure.string :as str]
5+
[clojure.tools.deps.config :as dc]))
6+
7+
(set! *warn-on-reflection* true)
8+
9+
(defn cli-classpath
10+
"CP for the config's deps.edn -Srepro excludes global ~/.clojure/deps.edn"
11+
[location lib]
12+
(when (.isFile (dc/data-file location lib "deps.edn"))
13+
(let [dir (dc/data-dir location lib)
14+
{:keys [exit out err]} (sh/sh "clojure" "-Srepro" "-A:clojure-cli.repl/client" "-Spath" :dir (str dir))]
15+
(if (zero? exit)
16+
(str/trim out)
17+
(println "clojure-cli.repl: could not resolve deps.edn in" (str dir) "-" (str/trim err))))))
18+
19+
(defn classpath-files [^java.io.File dir ^String cp]
20+
(map #(.toFile (.resolve (.toPath dir) ^String %))
21+
(.split cp java.io.File/pathSeparator)))
22+
23+
(defn config-classpath [location lib]
24+
(when-let [cp (cli-classpath location lib)]
25+
(classpath-files (dc/data-dir location lib) cp)))
26+
27+
(defn load-config
28+
"Puts the config classpath on a DynamicClassLoader installed as the thread's
29+
context loader. A new loader is required because the app loader cannot be
30+
extended. Returns the config map."
31+
[lib]
32+
(let [entries (distinct (mapcat #(config-classpath % lib) [:project :user]))]
33+
(when (seq entries)
34+
(let [thread (Thread/currentThread)
35+
loader (clojure.lang.DynamicClassLoader. (.getContextClassLoader thread))]
36+
(doseq [^java.io.File f entries]
37+
(.addURL loader (.toURL (.toURI f))))
38+
(.setContextClassLoader thread loader))))
39+
(dc/config lib))
40+
41+
(defn resolve-prompt
42+
"Resolves the configured :prompt var, or nil when none is configured."
43+
[config]
44+
(when-let [prompt (:prompt config)]
45+
(or (requiring-resolve prompt)
46+
(throw (ex-info (str "clojure-cli.repl: :prompt var not found: " prompt) {:prompt prompt})))))
47+
48+
(defn apply-keybindings
49+
[config reader]
50+
(when-let [keybindings (:keybindings config)]
51+
(if-let [f (requiring-resolve keybindings)]
52+
(f reader)
53+
(throw (ex-info (str "clojure-cli.repl: :keybindings var not found: " keybindings) {:keybindings keybindings})))))

‎client/src/clojure_cli/repl/client.clj‎

Lines changed: 4 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
(ns clojure-cli.repl.client
22
(:require
33
[clojure.edn :as edn]
4-
[clojure.java.shell :as sh]
54
[clojure.string :as str]
65
[clojure.tools.deps.config :as dc]
6+
[clojure-cli.repl.host :as host]
77
[clojure-cli.repl.nrepl :as nrepl]
88
[clojure-cli.repl.api :as api]
99
[clojure-cli.repl.paredit :as paredit]
@@ -145,47 +145,9 @@
145145
[{:text @api/current-ns :style {:fg :blue :bold true}}
146146
{:text " => "}])
147147

148-
(defn resolve-prompt [config]
149-
(if-let [prompt (:prompt config)]
150-
(or (requiring-resolve prompt)
151-
(throw (ex-info (str "clojure-cli.repl: :prompt var not found: " prompt) {:prompt prompt})))
152-
default-prompt))
153-
154148
(defn render-prompt ^String [prompt-fn]
155149
(.toAnsi (color/build-attrib-str (prompt-fn))))
156150

157-
(defn cli-classpath
158-
"CP for the config's deps.edn -Srepro excludes global ~/.clojure/deps.edn"
159-
[location lib]
160-
(when (.isFile (dc/data-file location lib "deps.edn"))
161-
(let [dir (dc/data-dir location lib)
162-
{:keys [exit out err]} (sh/sh "clojure" "-Srepro" "-A:clojure-cli.repl/client" "-Spath" :dir (str dir))]
163-
(if (zero? exit)
164-
(str/trim out)
165-
(println "clojure-cli.repl: could not resolve deps.edn in" (str dir) "-" (str/trim err))))))
166-
167-
(defn classpath-files [^java.io.File dir ^String cp]
168-
(map #(.toFile (.resolve (.toPath dir) ^String %))
169-
(.split cp java.io.File/pathSeparator)))
170-
171-
(defn config-classpath [location lib]
172-
(when-let [cp (cli-classpath location lib)]
173-
(classpath-files (dc/data-dir location lib) cp)))
174-
175-
(defn load-config
176-
"Puts the config classpath on a DynamicClassLoader installed as the thread's
177-
context loader. A new loader is required because the app loader cannot be
178-
extended. Returns the config map."
179-
[lib]
180-
(let [entries (distinct (mapcat #(config-classpath % lib) [:project :user]))]
181-
(when (seq entries)
182-
(let [thread (Thread/currentThread)
183-
loader (clojure.lang.DynamicClassLoader. (.getContextClassLoader thread))]
184-
(doseq [^java.io.File f entries]
185-
(.addURL loader (.toURL (.toURI f))))
186-
(.setContextClassLoader thread loader))))
187-
(dc/config lib))
188-
189151
(defn history-file
190152
"REPL history file, location is user configurable [:project|:user] under
191153
:history key, defaults to :user. Creates the parent dir if necessary."
@@ -386,13 +348,6 @@
386348
(exit-alt-screen term)
387349
(.callWidget rdr "redisplay")))))
388350

389-
(defn apply-keybindings
390-
[config reader]
391-
(when-let [keybindings (:keybindings config)]
392-
(if-let [f (requiring-resolve keybindings)]
393-
(f reader)
394-
(throw (ex-info (str "clojure-cli.repl: :keybindings var not found: " keybindings) {:keybindings keybindings})))))
395-
396351
(defn auto-require
397352
"Requires libspecs in ns-name on the server.
398353
Prints the error if a libspec fails to load."
@@ -402,8 +357,8 @@
402357
(println "clojure-cli.repl: :auto-require failed:" error))))
403358

404359
(defn -main [& args]
405-
(let [config (load-config 'org.clojure/clojure-cli.repl)
406-
prompt-fn (resolve-prompt config)
360+
(let [config (host/load-config 'org.clojure/clojure-cli.repl)
361+
prompt-fn (or (host/resolve-prompt config) default-prompt)
407362
port (Integer/parseInt (str/trim (or (first args) (slurp ".nrepl-port"))))
408363
{:keys [session]} (nrepl/connect port)
409364
libspecs (:auto-require config)
@@ -417,7 +372,7 @@
417372
(when-let [keyseq (:inspect config)]
418373
(api/bind-key rdr (api/key-sequence keyseq) (inspect rdr)))
419374
(paredit/install config rdr)
420-
(apply-keybindings config rdr)
375+
(host/apply-keybindings config rdr)
421376
;; ctrl-c during an eval outside of .readLine needs to interrupt the server
422377
(.handle (.getTerminal rdr) Terminal$Signal/INT
423378
(reify Terminal$SignalHandler

‎doc/operations.md‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,31 @@ Reply:
3333
{:doc "clojure.core/map\n..." :status ["done"]}
3434
```
3535

36+
## auto-require
37+
38+
Require libspecs in a namespace. The client sends it on startup and on each namespace change.
39+
40+
Request:
41+
42+
| Key | Value |
43+
|-----|-------|
44+
| `:op` | `"auto-require"` |
45+
| `:libspecs` | the libspecs to require, as an EDN string |
46+
| `:ns` | the namespace to require them in |
47+
48+
Reply:
49+
50+
| Key | Value |
51+
|-----|-------|
52+
| `:error` | the failure message, present only when a libspec fails to load |
53+
54+
```clojure
55+
;; request
56+
{:op "auto-require" :libspecs "[[clojure.string :as s]]" :ns "user"}
57+
;; reply
58+
{:status ["done"]}
59+
```
60+
3661
## inspect
3762

3863
The server is responsible for holding and modifying the state of the inspector.

0 commit comments

Comments
 (0)