Skip to content

Commit

Permalink
feat: Add port-auto-detecting-interceptor
Browse files Browse the repository at this point in the history
  • Loading branch information
liquidz committed Jan 16, 2024
1 parent 9f21322 commit a6d74d4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 18 deletions.
29 changes: 18 additions & 11 deletions src/elin/handler/core.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(ns elin.handler.core
(:require
[clojure.core.async :as async]
[elin.interceptor.connect :as e.i.connect]
[elin.log :as e.log]
[elin.nrepl.client.manager :as e.n.c.manager]
[elin.protocol.nrepl :as e.p.nrepl]
Expand All @@ -21,17 +22,23 @@
true)

(defmethod handler* :connect
[{:keys [params]}]
(let [[host port] params]
(-> {:host host :port port}
(e.u.interceptor/execute
[]
(fn [{:as ctx :keys [host port]}]
(let [client (e.p.nrepl/add-client! client-manager host port)]
(e.p.nrepl/switch-client! client-manager client)
(assoc ctx :client client)))))
(e.log/info "Connected")
"Connected"))
[{:as msg :keys [cwd params]}]
(let [[host port] (condp = (count params)
0 [nil nil]
1 [nil (first params)]
params)
result (-> {:cwd cwd :host host :port port}
(e.u.interceptor/execute
[e.i.connect/port-auto-detecting-interceptor]
(fn [{:as ctx :keys [host port]}]
(if (and host port)
(let [client (e.p.nrepl/add-client! client-manager host port)]
(e.p.nrepl/switch-client! client-manager client)
(assoc ctx :client client))
ctx))))]
(if (contains? result :client)
(e.log/info msg (format "Connected to %s:%s" (:host result) (:port result)))
(e.log/warning msg "Host or port is not specified." (pr-str (select-keys result [:host :port]))))))

(defmethod handler* :evaluate
[{:keys [params]}]
Expand Down
19 changes: 12 additions & 7 deletions src/elin/interceptor/connect.clj
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
(ns elin.interceptor.connect
(:require
[elin.protocol.nrepl :as e.p.nrepl]))
[elin.util.file :as e.u.file]))

(def connect-interceptor
{:name ::connect
:enter (fn [{:as ctx :keys [client-manager host port]}]
(let [client (e.p.nrepl/add-client! client-manager host port)]
(e.p.nrepl/switch-client! client-manager client)
(assoc ctx :client client)))})
(def port-auto-detecting-interceptor
{:name ::port-auto-detecting-interceptor
:enter (fn [{:as ctx :keys [cwd host port]}]
(if (and host port)
ctx
(let [nrepl-port-file (e.u.file/find-file-in-parent-directories cwd ".nrepl-port")
host' (or host "localhost")
port' (some-> nrepl-port-file
(slurp)
(Long/parseLong))]
(assoc ctx :host host' :port port'))))})

0 comments on commit a6d74d4

Please sign in to comment.