Fork me on GitHub
#babashka
<
2023-04-02
>
Stephan Renatus18:04:13

as a bb excercise for myself, I’ve been trying to construct an argument set that would start portal in vscode with just one call:

bb --prn -cp `clj -Spath -Sdeps '{:deps {djblue/portal {:mvn/version "0.36.0"}}}'` --exec portal.api/open :launcher :vs-code :port 5678
this is my attempt so far, but it’s got shortcomings: by using clojure.core/identity instead of portal.api/open, I found that (a) “vs-code” is ":vs-code", not :vs-code; and (b) it just prints whatever is returned and exits.

Stephan Renatus18:04:46

(a) is a bit of a surprise to me given that babashka/cli is supposed to (?) auto-coerce the args, and they do start with :

Stephan Renatus18:04:11

for (b), I suppose I’d have to get it to block in some way; @(promise)-style? :thinking_face:

borkdude18:04:24

$ bb -cp `clj -Spath -Sdeps '{:deps {djblue/portal {:mvn/version "0.36.0"}}}'` --exec clojure.core/prn :launcher :vs-code :port 5678
{:launcher :vs-code, :port 5678}

borkdude18:04:54

What version of babashka are you using, perhaps an older one?

Stephan Renatus18:04:12

babashka v1.1.173

Stephan Renatus18:04:23

thanks for your quick help. updating…

borkdude18:04:37

$ bb -Sdeps '{:deps {djblue/portal {:mvn/version "0.36.0"}}}' -x portal.api/open :launcher :vs-code :port 5678

----- Error --------------------------------------------------------------------
Type:     clojure.lang.ExceptionInfo
Message:  No config file found: vs-code.edn

Stephan Renatus18:04:44

hrm. So I’ve been doing this in a repl and it worked; I wanted to condense it into one call:

$ bb -cp `clj -Spath -Sdeps '{:deps {djblue/portal {:mvn/version "0.36.0"}}}'`
Downloading: djblue/portal/0.36.0/portal-0.36.0.pom from clojars
Downloading: djblue/portal/0.36.0/portal-0.36.0.jar from clojars
Babashka v1.1.173 REPL.
Use :repl/quit or :repl/exit to quit the REPL.
Clojure rocks, Bash reaches.

user=> (require '[portal.api :as p])
nil
user=> (def p (p/open {:launcher :vs-code :port 5678}))

borkdude18:04:13

you don't need to do the clj -Spath -Sdeps thing, bb supports -Sdeps directly

borkdude18:04:59

gotcha. perhaps @U1G869VNV could update this.

borkdude18:04:07

or you could submit a PR

😄 2
Stephan Renatus18:04:12

or I can PR it tomorrow.

borkdude18:04:30

But this should work, probably?

bb -Sdeps '{:deps {djblue/portal {:mvn/version "0.36.0"}}}' -x portal.api/open :launcher :vs-code :port 5678

borkdude18:04:47

I get an error about a vscode config file but this is probably expected

borkdude18:04:12

and yes, the @promise is necessary

Stephan Renatus18:04:40

possibly. I get it to open the tab in vscode, but it’s empty. probably the @promise thing

Stephan Renatus18:04:51

can I… emulate that in the CLI? :thinking_face:

borkdude18:04:46

hmm, no, maybe we need another flag like --prn , like --block or so

borkdude18:04:02

or you can write an exec function which calls the other function and then blocks

Stephan Renatus18:04:28

(just checking, tasks also can’t do this, right? I’ve been wondering if I should define a task for this…)

Stephan Renatus18:04:55

> or you can write an exec function which calls the other function and then blocks can you point me to an example, please? 🙂

borkdude18:04:42

$ BABASHKA_PRELOADS='(ns exec) (defn block [{:keys [f] :as opts}] ((requiring-resolve (symbol f)) opts) @(promise))' bb -Sdeps '{:deps {djblue/portal {:mvn/version "0.36.0"}}}' -x exec/block -f portal.api/open :port 5678

Stephan Renatus18:04:12

oh wow. thank you

Stephan Renatus18:04:23

hm for some reason, adding the :launcher :vs-code doesn’t work but I’ll try to take it from there. thanks again!

borkdude18:04:09

what does not work about it?

Stephan Renatus18:04:45

it’s suppose to open the portal UI in a vscode tab, but it opens it in a standalone window — like the :launcher arg was malformed or missing

borkdude18:04:21

Oh I see, babashka.cli doesn't like if you mix -- and : style params, so this works:

$ BABASHKA_PRELOADS='(ns exec) (defn block [{:keys [f] :as opts}] (prn opts) ((requiring-resolve (symbol f)) opts) @(promise))' bb -Sdeps '{:deps {djblue/portal {:mvn/version "0.36.0"}}}' -x exec/block :f portal.api/open :port 5678 :launcher :vscode

borkdude18:04:04

At least I see this printed:

{:f "portal.api/open", :port 5678, :launcher :vscode}

Stephan Renatus18:04:14

it works beautifully!

🎉 4
Stephan Renatus18:04:38

(btw the latest portal is 0.38.1; I had started out with an old version above — it doesn’t matter for any of this, though.)

Matt00:04:55

Oooh, this is so cool @U04GFG14FCP. Added this to my nostromo aliases. Also, as a thank you for sharing this, here is https://github.com/djblue/portal/pull/176 for portal's README. 🙇 thanks3

Stephan Renatus07:04:08

thanks @U04CYFD1CDN, for noticing that I had dropped the ball on this 😅

Matt12:04:34

LOLOLOL. Let's give you the benefit of the doubt. In a few years when an interviewer is looking at this monstrous PR, wide eyed in disbelief at its shear elegance, I will smile knowing that I STOLE this work from you and Michiel.

Stephan Renatus12:04:04

wait is this even correct? I think it should be

bb -Sdeps '{:deps {djblue/portal {:mvn/version "0.38.2"}}}'
without any of the backticks and the call to clj

Matt12:04:15

2 shakes, brb

Matt12:04:53

@U04GFG14FCP, Is the --classpath /`-cp` flag redundant as well

Stephan Renatus12:04:17

bb -Sdeps '{:deps {djblue/portal {:mvn/version "0.38.2"}}}'
yes, I believe so

borkdude12:04:28

Yes, you can use -Sdeps directly with bb

Matt12:04:23

Updated. I'm glad I double checked with you gents, the version number needed a bump as well.