Created
January 5, 2021 22:57
-
-
Save hiredman/a2630ea6153d06840a2723d5b2c9698c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn send-repl [& [obj]] | |
(let [in-in (java.io.PipedInputStream.) | |
in-out (java.io.PipedOutputStream. in-in) | |
out-in (java.io.PipedInputStream.) | |
out-out (java.io.PipedOutputStream. out-in) | |
err-in (java.io.PipedInputStream.) | |
err-out (java.io.PipedOutputStream. err-in)] | |
(tap> {:repl/in (clojure.java.io/writer in-out) | |
:repl/out (clojure.java.io/reader out-in) | |
:repl/err (clojure.java.io/reader err-in)}) | |
(with-open [out (java.io.PrintWriter. (clojure.java.io/writer out-out)) | |
err (java.io.PrintWriter. (clojure.java.io/writer err-out)) | |
in (clojure.lang.LineNumberingPushbackReader. | |
(clojure.java.io/reader in-in))] | |
(binding [*out* out | |
*err* err | |
*in* in | |
*1 obj] | |
(clojure.main/repl | |
:init (fn [] | |
(clojure.core.server/repl-init) | |
(set! *1 obj)) | |
:read clojure.core.server/repl-read))))) | |
(defn wait-for-repl [] | |
(let [p (promise) | |
f (fn [msg] | |
(when (and (contains? msg :repl/in) | |
(contains? msg :repl/out) | |
(contains? msg :repl/err)) | |
(deliver p msg)))] | |
(try | |
(add-tap f) | |
(let [{:repl/keys [in out err]} @p | |
o (future | |
(let [buf (char-array 1024)] | |
(loop [] | |
(let [n (.read out buf)] | |
(if-not (neg? n) | |
(do | |
(.write *out* buf 0 n) | |
(.flush *out*) | |
(recur)) | |
nil))))) | |
e (future | |
(let [buf (char-array 1024)] | |
(loop [] | |
(let [n (.read err buf)] | |
(if-not (neg? n) | |
(do | |
(.write *err* buf 0 n) | |
(.flush *err*) | |
(recur)) | |
nil))))) | |
i (future | |
(let [buf (char-array 1024)] | |
(loop [] | |
(let [n (.read *in* buf)] | |
(if-not (neg? n) | |
(do | |
(.write in buf 0 n) | |
(.flush in) | |
(recur)) | |
(.close in))))))] | |
@o | |
@e | |
@i) | |
(finally | |
(remove-tap f))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Context by hiredman shared on Slack: