Created
October 28, 2021 19:13
-
-
Save viesti/8959327b298950cd9c9574ce46265591 to your computer and use it in GitHub Desktop.
bb task to run nrepl server and send initialization code to the server when it starts
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
;; Task to run nrepl server and send some init code into it, when the nrepl server starts | |
{:tasks {dev {:requires ([bencode.core :as b] | |
[babashka.wait :as wait] | |
[babashka.process :as p :refer [process]]) | |
:task (do | |
(let [;; Put the command to start nrepl server here, along any other aliases | |
proc (process "clj -M:dev/nrepl:dev/hashp" {:inherit true})] | |
(wait/wait-for-path ".nrepl-port") | |
(try | |
(let [port (Integer/parseInt (slurp ".nrepl-port"))] | |
(with-open [s (java.net.Socket. "localhost" port)] | |
(let [out (.getOutputStream s) | |
in (java.io.PushbackInputStream. (.getInputStream s)) | |
;; Code to send for initializing the app | |
init-code "(do (require '[myname.myapp]) (in-ns 'myname.myapp) (def init :done) :init-done)" | |
_ (b/write-bencode out {"op" "eval" "code" init-code}) | |
bytes (get (b/read-bencode in) "value")] | |
(when bytes | |
(println (String. bytes)))))) | |
(catch Throwable _ | |
(println "Failed to send init code"))) | |
(.addShutdownHook (Runtime/getRuntime) | |
(Thread. (fn [] | |
(when (.isAlive (:proc proc)) | |
(println "Kill the app") | |
(.destroy (:proc proc))) | |
(println "thx, bye")))) | |
(System/exit (.waitFor (:proc proc)))))}}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment