|
1 | 1 | (ns clojure-cli.repl.client |
2 | 2 | (:require |
3 | 3 | [clojure.edn :as edn] |
4 | | - [clojure.java.shell :as sh] |
5 | 4 | [clojure.string :as str] |
6 | 5 | [clojure.tools.deps.config :as dc] |
| 6 | + [clojure-cli.repl.host :as host] |
7 | 7 | [clojure-cli.repl.nrepl :as nrepl] |
8 | 8 | [clojure-cli.repl.api :as api] |
9 | 9 | [clojure-cli.repl.paredit :as paredit] |
|
145 | 145 | [{:text @api/current-ns :style {:fg :blue :bold true}} |
146 | 146 | {:text " => "}]) |
147 | 147 |
|
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 | | - |
154 | 148 | (defn render-prompt ^String [prompt-fn] |
155 | 149 | (.toAnsi (color/build-attrib-str (prompt-fn)))) |
156 | 150 |
|
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 | | - |
189 | 151 | (defn history-file |
190 | 152 | "REPL history file, location is user configurable [:project|:user] under |
191 | 153 | :history key, defaults to :user. Creates the parent dir if necessary." |
|
386 | 348 | (exit-alt-screen term) |
387 | 349 | (.callWidget rdr "redisplay"))))) |
388 | 350 |
|
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 | | - |
396 | 351 | (defn auto-require |
397 | 352 | "Requires libspecs in ns-name on the server. |
398 | 353 | Prints the error if a libspec fails to load." |
|
402 | 357 | (println "clojure-cli.repl: :auto-require failed:" error)))) |
403 | 358 |
|
404 | 359 | (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) |
407 | 362 | port (Integer/parseInt (str/trim (or (first args) (slurp ".nrepl-port")))) |
408 | 363 | {:keys [session]} (nrepl/connect port) |
409 | 364 | libspecs (:auto-require config) |
|
417 | 372 | (when-let [keyseq (:inspect config)] |
418 | 373 | (api/bind-key rdr (api/key-sequence keyseq) (inspect rdr))) |
419 | 374 | (paredit/install config rdr) |
420 | | - (apply-keybindings config rdr) |
| 375 | + (host/apply-keybindings config rdr) |
421 | 376 | ;; ctrl-c during an eval outside of .readLine needs to interrupt the server |
422 | 377 | (.handle (.getTerminal rdr) Terminal$Signal/INT |
423 | 378 | (reify Terminal$SignalHandler |
|
0 commit comments