-
-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathcmdline.clj
More file actions
304 lines (277 loc) · 10.7 KB
/
Copy pathcmdline.clj
File metadata and controls
304 lines (277 loc) · 10.7 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
(ns nrepl.cmdline
"A proof-of-concept command-line client for nREPL. Please see
e.g. REPL-y for a proper command-line nREPL client @
https://github.com/trptcolin/reply/"
{:author "Chas Emerick"}
(:require
[clojure.java.io :as io]
[clojure.edn :as edn]
[clojure.string :as str]
[nrepl.config :as config]
[nrepl.core :as nrepl]
[nrepl.ack :refer [send-ack]]
[nrepl.server :refer [start-server]]
[nrepl.transport :as transport]
[nrepl.version :as version]))
(defn- clean-up-and-exit
"Performs any necessary clean up and calls `(System/exit status)`."
[status]
(shutdown-agents)
(flush)
(binding [*out* *err*] (flush))
(System/exit status))
(defn exit
"Requests that the process exit with the given `status`. Does not
return."
[status]
(throw (ex-info nil {::kind ::exit ::status status})))
(defn die
"`Print`s items in `msg` to *err* and then exits with a status of 2."
[& msg]
(binding [*out* *err*]
(doseq [m msg] (print m)))
(exit 2))
(defmacro ^{:author "Colin Jones"} set-signal-handler!
[signal f]
(if (try (Class/forName "sun.misc.Signal")
(catch Throwable e))
`(try
(sun.misc.Signal/handle
(sun.misc.Signal. ~signal)
(proxy [sun.misc.SignalHandler] []
(handle [signal#] (~f signal#))))
(catch Throwable e#))
`(println "Unable to set signal handlers.")))
(def colored-output
{:err #(binding [*out* *err*]
(print "\033[31m")
(print %)
(print "\033[m")
(flush))
:out print
:value (fn [x]
(print "\033[34m")
(print x)
(println "\033[m")
(flush))})
(def running-repl (atom {:transport nil
:client nil}))
(defn- done?
[input]
(some (partial = input)
['exit 'quit '(exit) '(quit)]))
(defn- run-repl
([host port]
(run-repl host port nil))
([host port {:keys [prompt err out value transport]
:or {prompt #(print (str % "=> "))
err print
out print
value println
transport #'transport/bencode}}]
(let [transport (nrepl/connect :host host :port port :transport-fn transport)
client (nrepl/client-session (nrepl/client transport Long/MAX_VALUE))
ns (atom "user")]
(swap! running-repl assoc :transport transport)
(swap! running-repl assoc :client client)
(println (format "nREPL %s" (:version-string version/version)))
(println (str "Clojure " (clojure-version)))
(println (System/getProperty "java.vm.name") (System/getProperty "java.runtime.version"))
(println (str "Interrupt: Control+C"))
(println (str "Exit: Control+D or (exit) or (quit)"))
(loop []
(prompt @ns)
(flush)
(let [input (read *in* false 'exit)]
(if (done? input)
(System/exit 0)
(do (doseq [res (nrepl/message client {:op "eval" :code (pr-str input)})]
(when (:value res) (value (:value res)))
(when (:out res) (out (:out res)))
(when (:err res) (err (:err res)))
(when (:ns res) (reset! ns (:ns res))))
(recur))))))))
(def #^{:private true} option-shorthands
{"-i" "--interactive"
"-r" "--repl"
"-c" "--connect"
"-b" "--bind"
"-h" "--host"
"-p" "--port"
"-m" "--middleware"
"-t" "--transport"
"-n" "--handler"
"-v" "--version"})
(def #^{:private true} unary-options
#{"--interactive"
"--connect"
"--color"
"--help"
"--version"})
(defn- expand-shorthands
"Expand shorthand options into their full forms."
[args]
(map (fn [arg] (or (option-shorthands arg) arg)) args))
(defn- keywordize-options [options]
(reduce-kv
#(assoc %1 (keyword (str/replace-first %2 "--" "")) %3)
{}
options))
(defn- split-args
"Convert `args` into a map of options + a list of args.
Unary options are set to true during this transformation.
Returns a vector combining the map and the list."
[args]
(loop [[arg & rem-args :as args] args
options {}]
(if-not (and arg (re-matches #"-.*" arg))
[options args]
(if (unary-options arg)
(recur rem-args
(assoc options arg true))
(recur (rest rem-args)
(assoc options arg (first rem-args)))))))
(defn- display-help
[]
(println "Usage:
-i/--interactive Start nREPL and connect to it with the built-in client.
-c/--connect Connect to a running nREPL with the built-in client.
-C/--color Use colors to differentiate values from output in the REPL. Must be combined with --interactive.
-b/--bind ADDR Bind address, by default \"127.0.0.1\".
-h/--host ADDR Host address to connect to when using --connect. Defaults to \"127.0.0.1\".
-p/--port PORT Start nREPL on PORT. Defaults to 0 (random port) if not specified.
--ack ACK-PORT Acknowledge the port of this server to another nREPL server running on ACK-PORT.
-n/--handler HANDLER The nREPL message handler to use for each incoming connection; defaults to the result of `(nrepl.server/default-handler)`.
-m/--middleware MIDDLEWARE A sequence of vars, representing middleware you wish to mix in to the nREPL handler.
-t/--transport TRANSPORT The transport to use. By default that's nrepl.transport/bencode.
--help Show this help message.
-v/--version Display the nREPL version."))
(defn- require-and-resolve
"Attempts to resolve the config `key`'s `value` as a namespaced symbol
and returns the related var if successful. Otherwise calls `die`."
[key sym]
(when-not (symbol? sym)
(die (format "nREPL %s: %s is not a symbol\n" (name key) (pr-str sym))))
(let [space (some-> (namespace sym) symbol)]
(when-not space
(die (format "nREPL %s: %s has no namespace\n" (name key) sym)))
(require space)
(or (ns-resolve space (-> sym name symbol))
(die (format "nREPL %s: unable to resolve %s\n" (name key) sym)))))
(def ^:private resolve-mw-xf
(comp (map #(require-and-resolve :middleware %))
(keep identity)))
(defn- handle-seq-var
[var]
(let [x @var]
(if (sequential? x)
(into [] resolve-mw-xf x)
[var])))
(defn- handle-interrupt
[signal]
(let [transport (:transport @running-repl)
client (:client @running-repl)]
(if (and transport client)
(doseq [res (nrepl/message client {:op :interrupt})]
(when (= ["done" "session-idle"] (:status res))
(System/exit 0)))
(System/exit 0))))
(def ^:private mw-xf
(comp (map symbol)
resolve-mw-xf
(mapcat handle-seq-var)))
(defn- ->mw-list
[middleware-var-strs]
(into [] mw-xf middleware-var-strs))
(defn- build-handler
"Build an nREPL handler from `middleware`.
`middleware` is a sequence of vars or string which can be resolved
to vars, representing middleware you wish to mix in to the nREPL
handler. Vars can resolve to a sequence of vars, in which case
they'll be flattened into the list of middleware."
[middleware]
(apply nrepl.server/default-handler (->mw-list middleware)))
(defn- ->int [x]
(cond
(nil? x) x
(number? x) x
:else (Integer/parseInt x)))
(defn- sanitize-middleware-option
"Sanitize the middleware option. In the config it can be either a
symbol or a vector of symbols."
[mw-opt]
(if (symbol? mw-opt)
[mw-opt]
mw-opt))
(defn- parse-cli-values
"Converts relevant command line argument values to their config
representation."
[options]
(reduce-kv (fn [result k v]
(case k
(:handler :transport :middleware) (assoc result k (edn/read-string v))
result))
options
options))
(defn- run
[args]
(set-signal-handler! "INT" handle-interrupt)
(let [[options _args] (split-args (expand-shorthands args))
options (keywordize-options options)
options (parse-cli-values options)
options (merge config/config options)]
;; we have to check for --help first, as it's special
(when (:help options)
(display-help)
(exit 0))
(when (:version options)
(println (:version-string version/version))
(exit 0))
;; then we check for --connect
(let [port (->int (:port options))
host (:host options)
transport (or (some->> (:transport options) (require-and-resolve :transport))
#'transport/bencode)]
(when (:connect options)
(run-repl host port {:transport transport})
(exit 0))
;; otherwise we assume we have to start an nREPL server
(let [bind (:bind options)
;; if some handler was explicitly passed we'll use it, otherwise we'll build one
;; from whatever was passed via --middleware
middleware (sanitize-middleware-option (:middleware options))
handler (some->> (:handler options) (require-and-resolve :handler))
handler (or handler (build-handler middleware))
greeting-fn (if (= transport #'transport/tty) #'transport/tty-greeting)
server (start-server :port port :bind bind :handler handler
:transport-fn transport :greeting-fn greeting-fn)]
(when-let [ack-port (some-> (:ack options) ->int)]
(binding [*out* *err*]
(println (format "ack'ing my port %d to other server running on port %d"
(:port server) ack-port)
(:status (send-ack (:port server) ack-port transport)))))
(let [port (:port server)
^java.net.ServerSocket ssocket (:server-socket server)
host (.getHostName (.getInetAddress ssocket))]
;; The format here is important, as some tools (e.g. CIDER) parse the string
;; to extract from it the host and the port to connect to
(println (format "nREPL server started on port %d on host %s - %s://%s:%d"
port host (transport/uri-scheme transport) host port))
;; Many clients look for this file to infer the port to connect to
(let [port-file (io/file ".nrepl-port")]
(.deleteOnExit port-file)
(spit port-file port))
(if (:interactive options)
(run-repl host port (merge (when (:color options) colored-output)
{:transport transport}))
;; need to hold process open with a non-daemon thread -- this should end up being super-temporary
(Thread/sleep Long/MAX_VALUE)))))))
(defn -main
[& args]
(try
(run args)
(catch clojure.lang.ExceptionInfo ex
(let [{:keys [::kind ::status]} (ex-data ex)]
(when (= kind ::exit)
(clean-up-and-exit status))
(throw ex)))))