exupero's blog
RSSApps

quickbb script for Babashka

Inspired by quick, below is a script that launches a Babashka nREPL in the background before opening Neovim. Forms in the editor can then be evaluated via Conjure, which (unlike quick) allows holding expensive calculations in memory, rather than re-evaluating them every time the script is saved. When Neovim exits, the nREPL is killed.

#!/bin/bash

nrepl() {
  bb << EOF
#!/usr/bin/env bb

(require '[babashka.fs :as fs]
         '[babashka.process :as p :refer [process]]
         '[babashka.wait :as wait])

(let [port (with-open [sock (java.net.ServerSocket. 0)] (.getLocalPort sock))
      proc (process (str "bb nrepl-server " port) {:inherit true})]
  (wait/wait-for-port "localhost" port)
  (spit ".nrepl-port" port)
  (deref proc))
EOF
}

nrepl &
pid=$!
echo nrepl running in PID $pid

script=$(mktemp)
printf '#!/usr/bin/env bb\n\n\n' > $script
chmod +x $script
nvim +'set ft=clojure' +3 $script

rm .nrepl-port && kill $pid && echo killed nrepl