Fork me on GitHub
#babashka
<
2021-01-31
>
Eamonn Sullivan13:01:11

Hi, complete newby here. I've decided to explore this one whole babashka thing that I hear so many great things about. I'm a (maybe) advanced beginner (at best) on Clojure and I'm going to try this afternoon to rewrite my very crufty bash backup script into something more fun to maintain and change. But what do you people use for tooling? I tried to create a backup.clj file in emacs and my Clojure set up kicked in, with clojure-lsp complaining about the 35,000+ files in this "project" (i.e., my home directory). I then tried to use clojure -X:new ... to create a simple small deps.edn project to work in, so I'd get all the niceness of CIDER, but the syntax is a bit different, isn't it, for things like imports and requires. How do people generally write these? Do you write them first in Clojure (or ClojureScript) and then make the last-minute adjustments?

borkdude13:01:56

@eamonn.sullivan The simplest way to get started is just write a script.clj with a shebang or just execute it with bb script.clj

borkdude13:01:20

The syntax for babashka is exactly the same as clojure

Eamonn Sullivan13:01:28

OK, so I just be mostly OK working in Clojure (to use Cider and LSP) and then adding the shebang at the end, to make it stand-alone?

Eamonn Sullivan13:01:53

Ah, I will have a read of the manual too!

borkdude13:01:14

#!/usr/bin/env bb
(println "Hello world!")

Eamonn Sullivan13:01:41

Yes, I got that far fairly quickly from the examples. And I got it kind of working in a short period of time. What I'm running into is that when I open the bb script in Emacs to edit it, my configuration gets confused. I have so many things set up to start when the extension in the buffer ends in *.clj, and those don't expect to work with a shell script. Clojure-lsp is trying to find the project definition, for example.

borkdude13:01:49

I think that might be good to mention in the #lsp channel. LSP should support scripts. You can write standalone scripts in JVM clojure too. cc @ericdallo

borkdude13:01:27

I think @pez also asked about this once (he is the originating author of Calva, which is now also using clojure-lsp)

borkdude13:01:46

Anyway, maybe for now you can turn lsp off 🤷

borkdude13:01:47

@eamonn.sullivan Just clojure-mode and clj-kondo should be good enough for babashka, for small scripts. Babashka also supports a REPL, but for small scripts I find it sufficient to just run it from the terminal each time

Eamonn Sullivan13:01:27

:thumbsup: Giving it a go now.

pez14:01:12

Something has changed in clojure-lsp since I asked about that. Now it just works. I have code navigation, documentation lookup, outline, clj-kondo linting, … the lot, when opening my babashka scripts, even though they have shebangs. I start bb with the --nrepl-server and enjoy the REPL workflow I am used to after connecting Calva. At the end of some “entry” scripts I have check if it is running as a script or in the repl. That is mainly so that loading the file in the REPL is a way to perform some basic smoke testing.

borkdude14:01:14

Hmm, maybe that got fixed in LSP then. Maybe upgrade @eamonn.sullivan?

pez14:01:56

It is so sweet I have a hard time fathoming it. Maybe babashka is about to position itself as Clojure’s killer app? It is hard to see who, in their right mind, and after some carefully designed introduction, would choose to script in anything but babashka.

💖 1
💯 5
Eamonn Sullivan14:01:11

Yeah, I literally just discovered (from the aforementioned manual) bb --nrepl-server and now have a full cider/lsp experience. Very nice! Thanks again for this. I might never write another bash script.

☝️ 1
ericdallo15:01:52

Cool, this should work with lsp indeed, we support both project and single file clojure code, any issues LMK in #lsp :)

Eamonn Sullivan20:01:40

Hi, all, a question: I'm writing my first babashka script and I'd like to write some tests for it. I think I've followed the instructions in the babashka book about my project structure/test_runner, but I'm getting a strange error when I try to redefine a function in the script using with-redefs: clojure.lang.ExceptionInfo: Method format on class sci.impl.fns$fun$arity_0__16223 not allowed! . My repo is here: https://github.com/eamonnsullivan/backup-scripts. I'm just trying to write my first, simple test around date handling before I tackle some of the harder ones. Can anyone spot my simple mistake?

borkdude21:01:06

@eamonn.sullivan The reason you are getting this error is that you are re-defining date as a function in your tests, while in your normal code it's a date, not a function

Eamonn Sullivan21:01:00

Doh! Thanks. It's too late to still be working on this...

borkdude21:01:09

No worries, the error message is a bit cryptic here

borkdude22:01:20

Added lots of functions to babashka.fs today. You can already use this lib as a git lib and it's built into bb in the fs branch (see #babashka_circleci_builds or visit the builds via Github). I want to wait on some feedback for a while before it will end up in babashka. Please test and review! https://github.com/babashka/fs#usage API docs: https://babashka.org/fs/

🚀 4