Fork me on GitHub
#babashka
<
2022-10-07
>
Ferdinand Beyer08:10:32

I've created a tiny VS-Code extension to run Babashka Tasks. First release v0.1.0 is out! https://marketplace.visualstudio.com/items?itemName=fbeyer.babashka-tasks

🎉 4
Søren Sjørup11:10:19

What is a good way of working with urls in babashka? I’m looking specifically for a nice way of adding query parameters.

borkdude11:10:45

Lambdaisland has a lib for this:

(require '[babashka.deps :as deps])

(deps/add-deps '{:deps {lambdaisland/uri {:mvn/version "1.13.95"}}})

(require '[lambdaisland.uri :as uri :refer [uri]])

(-> (uri "")
    (uri/assoc-query {:foo :bar})
    uri/uri-str prn)

borkdude11:10:28

@UE21H2HHD Perhaps lambdaisland/uri wants to take a logo PR ;)

babashka 1
Søren Sjørup12:10:41

Nice! Thank you very much for the complete example with add-deps and everything!

hlship16:10:04

I didn't appreciate that if you have a script, say <dir>/bin/my-command, that Babashka will look upwards for bb.edn, so that can be in <dir>/bb.edn. This makes it really easy to write scripts with dependencies. Full example here: https://github.com/hlship/cli-tools#defcommand

borkdude16:10:10

To my knowledge, bb doesn't look upwards for a bb.edn

hlship16:10:33

But it works ...

borkdude16:10:46

It picks the bb.edn from the current working directory

borkdude16:10:35

if you execute <dir>/bin/my-command and you have <dir>/bb.edn that works, because <dir> is the current working directory

hlship16:10:37

Shoot. Was running from the root directory, and forgot to check if it works from elsewhere. Ok, gotta fix that.

hlship16:10:57

I just shouldn't multi-task.

hlship16:10:11

I ended up with:

#!/usr/bin/env bb

(require '[babashka.deps :as deps])

(let [root (-> *file*
              fs/parent
              fs/parent)
      paths [(fs/path root "src")
             (fs/path root "resources")]]
  (deps/add-deps `{:paths ~paths
                   :deps {io.github.hlship/cli-tools {:mvn/version "0.5"}}}))

(require '[net.lewisship.cli-tools :as cli])

(cli/dispatch {:tool-name "flow"
               :namespaces '[flow.commands]})

borkdude16:10:11

you might have to stringify those paths

hlship16:10:38

It's working without doing that, haven't traced down why.

borkdude16:10:09

maybe because you're not requiring anything locally

hlship16:10:49

Well, it's able to load namespaces under the root src dir, so it's working for whatever reason.

hlship16:10:09

Yes, I could (map str paths) but I'm trying to keep this simple.

borkdude16:10:45

I enjoyed the podcast on the cognicast btw, thanks for mentioning bb

gratitude-thank-you 1
hlship16:10:50

Would you consider an extra switch on bb to look upwards from the script file to find the bb.edn?

hlship16:10:37

Or, I suppose, could move the bb.edn to the bin dir, but that feels dirty for some reason.

borkdude16:10:39

I wonder what edge cases that might introduce. clj doesn't support this either right?

hlship16:10:03

No I don't think clj does this.

borkdude16:10:23

You can also use --config to specify an alternative bb.edn - not sure if that helps

hlship16:10:34

I've tried a bunch of variations of #!/usr/bin/env bb --config "$(dirname $0)/../bb.edn" but it doesn't look like you can do a shell expansion there, so I get error Message: File does not exist: $0)/../bb.edn"

teodorlu17:10:20

I'm also curious about the space between "full blown project in its own git repo" (at least its own folder with a bb.edn file) and "single file self contained script". I feel like I have to pick one or the other. Not that I have any solutions to suggest 😄😅

borkdude17:10:35

Another thing: you can re-invoke bb itself using process/exec

borkdude18:10:11

Or just babashka.process/shell: So instead of the shebang you could do:

(babashka.process/shell "bb" "--config" "...")

borkdude18:10:11

But I wonder what is the problem behind the problem here. I never have this problem since I always launch scripts from the root:

script/foo.clj
bin/foo.clj
Not:
cd bin && ./foo.clj

hlship18:10:15

I think I mentioned flow in the 'cast. It's a tool I use all over the place (it's that personal tool you don't necessarily share with anyone). So I have that bin directory on my $PATH but I'm almost never in that root directory to execute it, so it has to be able to find that directory to find src , resources and dependencies.

borkdude18:10:56

Can you be more specific: is flow in the bin directory? And what does it do exactly, invoke bb?