Fork me on GitHub
#cider
<
2021-10-25
>
dakra07:10:16

Maybe it's useful for some. I wrote a simple cider-jack-in-babashkafunction that's handy when you want to have a repl quick:

(defun cider-jack-in-babashka ()
  (interactive)
  (let* ((default-directory (project-root (project-current t)))
         (port (+ 1024 (random 5000)))
         (params `(:host "localhost"
                   :port ,port
                   :project-dir ,default-directory)))
    (start-process-shell-command "babashka-nrepl" "*babashka-nrepl*"
                                 (concat "bb nrepl-server " (number-to-string port)))
    (sleep-for 0.5)
    (cider-connect-clj params)))

borkdude07:10:32

This seems useful to contribute to the http://book.babashka.org nREPL section

dakra08:10:05

Should I make a PR or you want to just add it? Also, if someone has a better idea to find a free port and check when the babashka process is really running please say 🙂 random port and just sleeping for 0.5 seconds doesn't sound ideal but was enough for me personally.

borkdude08:10:12

A PR is fine or just an issue

bozhidar13:10:51

@UFAP0C8KU If the server writes the port you can just wait a bit a read it from the .nrepl_port file (or however it was named).

bozhidar13:10:10

Or you can just extract it from the server output, as we do for the Clojure nREPL server.

bozhidar13:10:04

Anyways, at some point I should add something similar to CIDER.

dakra13:10:42

Thanks. I think parsing it from the output is a good idea. Then I don't need the sleep kludge as the server should be ready when I'm able to read the port.

dakra13:10:18

@U04V15CAJ is there a simple way to start babashka on the command line to always start on a free port (e.g. if 1667 is already in use, just use 1668 etc)? I don't really want to use a bb.ednas described in https://book.babashka.org/#_nrepl

dakra13:10:17

Very nice. I'll update my snippet tonight.

solf13:10:46

I have one similar:

(defun my-babashka-connect--process-filter (proc string)
  "Run cider-connect once babashka nrepl server is ready."
  (when (string-match "Started nREPL server at .+:\\([0-9]+\\)" string)
    (cider-connect-clj (list :host "localhost" :port (match-string 1 string))))
  ;; Default behavior: write to process buffer
  (internal-default-process-filter proc string))

(defun my-babashka-connect ()
  "Start babashka on a random port."
  (interactive)
  (let ((port (+ 1230 (cl-random 300))))
    (set-process-filter
     (start-process "babashka"
		    "*babashka*"
		    "bb" "--nrepl-server" (number-to-string port))
     'my-babashka-connect--process-filter)))

(defun my-babashka-quit ()
  "Quit cider and kill babashka process."
  (interactive)
  (cider-quit)
  (kill-process (get-process "babashka"))
  (message "babashka is kill"))

borkdude13:10:14

We could also post this on the babashka wiki if there not a single right approach (the wiki is open for anyone to post)

borkdude13:10:27

or ... someone can make a babashka elisp package ;)

solf14:10:18

Making a PR for CIDER is probably the way to go?

borkdude14:10:40

yeah why not

dakra14:10:30

@U7S5E44DB nice, that looks like something I would have ended up with anyway. Just that port number can be 0 now instead of random. And an improvement would maybe to remap cider-quit to your babashka quit function.

dakra14:10:17

For cider itself there is already an open issue https://github.com/clojure-emacs/cider/issues/2947 But if I read it correctly the plan is to not add a special babashka jack-in function

bozhidar16:10:09

My point was that I was hoping we could detect that something is a babashka project and use the existing jack-in function which simply runs bababashka's nREPL server then.

bozhidar16:10:04

Seems like a better user experience than one more function that people will have to know about, but I don't have any fundamental issues with a dedicated babashka command.

richiardiandrea20:10:56

yeah agree with Bozhidar, we could detect if the project contains bb.edn IIRC we do a similar thing for Shadow Cljs

dakra20:10:11

Jep, I agree. One jack-in command where someone can choose the repl backend(?) is probably more user-friendly.

dakra20:10:08

But lets start with putting a simple command in the babashka wiki or http://book.babashka.org This is basically the version from @U7S5E44DB but with port 0, the default directory is set to the project root and it should work over tramp (untested):

(defun cider-jack-in-babashka ()
    "Start an babashka nREPL server for the current project and connect to it."
    (interactive)
    (let* ((default-directory (project-root (project-current t)))
           (process-filter (lambda (proc string)
                             "Run cider-connect once babashka nrepl server is ready."
                             (when (string-match "Started nREPL server at .+:\\([0-9]+\\)" string)
                               (cider-connect-clj (list :host "localhost"
                                                        :port (match-string 1 string)
                                                        :project-dir default-directory)))
                             ;; Default behavior: write to process buffer
                             (internal-default-process-filter proc string))))
      (set-process-filter
       (start-file-process "babashka"
		           "*babashka*"
		           "bb" "--nrepl-server" "0")
       process-filter)))

🙌 2
borkdude20:10:06

Perhaps start with wiki so we can tweak the code easier, and we can link in the book to the wiki

dakra20:10:10

Ok. I think that's a good idea.

borkdude20:10:37

ok, go ahead

dakra20:10:46

@U0C8489U6 the thing with bb.edn is that it's not always there. And the main reason I even wrote this snippet was that often I simply wanted a quick repl to experiment in my project. Now I can just create a buffer playground.clj and M-x cider-jack-in-babashka and I have a working nrepl session in my editor in milliseconds preloaded with lots of useful libs to manipulate all kinds of files etc 🙂

🎉 1
richiardiandrea20:10:04

@UFAP0C8KU I see yeah that makes sense for random stuff. I am actually using both in project and random...however for the latter use case I have this in my conf and connect directly:

(cider-known-endpoints '(("localhost" "1667") ;; babashka
                         ("localhost" "5555") ;; common
                           ))

dakra20:10:47

Could probably use more text but here's a start: https://github.com/babashka/babashka/wiki/GNU-Emacs

bozhidar08:10:37

FYI - I plan to cut CIDER 1.2 in a week or two, so now's a good time to test the current master. It seems to be in a good shape overall, but a bit of extra testing never hurts. 🙂

☝️ 2
dakra20:10:51

I just saw https://github.com/babashka/neil/blob/main/neil#L2 and made https://github.com/clojure-emacs/clojure-mode/pull/604 so that's not necessary anymore. I would say bb is unique enough for us to always add to interpreter-mode-alist when the user has clojure-mode installed.

👍 3