Fork me on GitHub
#babashka
<
2022-11-08
>
Piotr Kaznowski08:11:44

Hi all! Is it possible in bb tasks to evaluate a symbol? Like instead of:

# bb.edn
{:tasks
 foo-bar foo.bar/-main}
to have something like:
{:tasks
 foo-bar (symbol "foo.bar" "-main")}
It's not returning errors, but it doesn't seem to do anything either (like it's rather returning the symbol than evaluating it). Using resolve returns nil.

borkdude08:11:05

Hi @UUAKPEMJ9! Is your goal to execute a main function dynamically?

borkdude08:11:59

resolve returns nil because you should in this case load the namespace yourself

Piotr Kaznowski08:11:36

Hmm, I'm still learning clj, so I may be confusing something. My goal is actually to create the symbol to be evaluated from the command line args (context: writing a setup for Advent of Code in bb, and want to have task to run solution for a puzzle of a given day, like bb solve 1. I'd have a namespace day1.solution with a -main func there. But each day will add another namespace, and I don't want to update tasks each day bc I want to automate stuff 🙂).

borkdude08:11:16

You could use requiring-resolve:

(requiring-resolve (symbol "foo.bar" "-main"))

borkdude08:11:50

that gets you the var, but then you still need to call that a function:

(apply (requiring-resolve (symbol "foo.bar" "-main")) *command-line-args*)

borkdude08:11:28

or just

((requiring-resolve (symbol "foo.bar" "-main")))
if you don't want to feed it any command line args

Piotr Kaznowski08:11:58

Excellent -- that works! Thank you 🙂

borkdude08:11:07

You can also try:

{:task (exec (symbol "foo.bar" "baz"))}

👏 1
borkdude08:11:37

What this does is it parses the command line arguments with babashka.cli and then executes the function denoted by the symbol foo.bar/baz with those arguments

Piotr Kaznowski08:11:39

And that works, too