Skip to content

Instantly share code, notes, and snippets.

@borkdude
Last active September 21, 2021 11:43
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save borkdude/5f3019d6666953f3b8f533a70eb067d2 to your computer and use it in GitHub Desktop.
Babashka tasks + JVM Clojure pREPL
{:tasks
{:requires ([babashka.deps :as deps]
[babashka.process :refer [tokenize]]
[clojure.edn :as edn]
[clojure.java.io :as io])
:init
(do
(def prepl
(delay
(do
(println "Launching prepl!")
(let [proc (deps/clojure (tokenize "-M -e '(clojure.core.server/io-prepl)'")
{:in nil
:err :inherit
:out nil})]
{:in (io/writer (:in proc))
:out (io/reader (:out proc))}))))
(defn run-expr [form]
(let [form (pr-str form)]
(do (binding [*out* (:in (deref prepl))]
(println form))
(binding [*in* (:out (deref prepl))]
(loop []
(let [res (edn/read-string (read-line))]
(when-let [v (:val res)]
(if-let [ex (:exception res)]
(let [ex (edn/read-string v)
msg (get-in ex [:via 0 :message])]
(throw (ex-info msg ex)))
(when (not= "nil" v)
(print v))))
(when (not= form (:form res))
(recur)))))
(println)))))
f {:task (run-expr '(defn f [] (+ 1 2 3)))}
g {:depends [f]
:task (run-expr '(do (prn (f)) (def data {:x 1})))}
h {:depends [f g]
:task (do
(prn :yolo)
(run-expr 'data))}}}
;; Usage:
;; $ bb h
;; Launching prepl!
;; #'user/f
;; 6
;; #'user/data
;; {:x 1}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment