This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
Hi all, Please can you help advise if/how the following three code reuse scenarios would be done in babashka:
• [1] "Require" a babashka script from another babashka script in a different directory.
◦ looks like https://book.babashka.org/#_add_classpath is the solution
▪︎ (cp/add-classpath "other-dir")(require 'your.test-a 'your.test-b)
• [2] Call a secondary bashka script multiple times from a babashka script
◦ e.g. my-babashka-script.clj
calls https://github.com/babashka/babashka/blob/master/examples/tree.clj on 5 different directories.
• [3] From a babashka script, call a function in another babashka script (e.g. call file-tree
in https://github.com/babashka/babashka/blob/master/examples/tree.clj) without running the file (causing it to create/print a tree).
are all the files in the same root dir? or some are in a parent or sibling dir?
I guess sibling dir would be most flexible scenario.
by sibling i meant its not in a subdir but outside the parent. i think you mean they all are in the same file tree?
if yes have you tried the project setup?https://book.babashka.org/#project-setup
Thanks for the link. I don't think that helps with scenario [2] or [3]?
for 2 when you say call, you still mean require or execute?
3 can be addressed by the project setup
Yes - for scenario [2] I want to do it multiple times (e.g. I have 5 directories I want to run tree.clj
). I think it will only get run once.
For [3] - I want to use an existing script as a library and not trigger the side effects.
right, the 3rd one is a problem as the print is right at the top and require will cause it and no way to avoid it. id recommend to copy the functions needed and expose an fn which you require and call. that script isnt intended for requiring
Sounds like a serious shame since many/most babashka scripts are written like this.
sure but this is standard clojure behaviour
Yes - but most clojure scripts aren't written like that only babashka scripts are.
You can introduce a function and invoke it with bb -m your-ns/your-fn
or bb -x your-ns/your-fn
. If you want to have globally invokable scripts, use #C0400TZENE7
You can also use (when (= *file* (System/getProperty "babashka.file"))) (do-my-side-effects))
Yup - that would make the scripts more reusable - but the examples in github and common usage don't do that. Which means that a lot of the code so far can't be re-used as a library. Good to see there is a way to do it though.
yes, they are meant for direct invocation.
How about scenario [2]?
You can also use (load-file ...)
but this is less common and not recommend for a real project
I'm thinking about using scripts that I don't own and don't want to change the code for.
Cool - load-file seems like the way to go for that
"Scripts you don't own and don't want to change the code for" are usually libraries you include in bb.edn
and call as functions
I assume I can (binding[*input* ...] (load-file "")
? There's a note that it won't work Note that *input* is not available in preloads.
?
Nearly all Clojure code (even apps) I can reuse as libraries. However, there is an encouragement for bb scripts to be one file solutions (so the library code is often bundled into the execution code).
Just hoping we don't end up with lots of bb code that we can't reuse because it executes.
e.g. there are useful functions in the https://github.com/babashka/babashka/tree/master/examples but none can be used without causing the file to run.
If I would want to expose tree.clj
as a library I would put it in a deps.edn
project and wouldn't call any top level side effects. Then I would expose a -main
function that does CLI parsing and calls the library logic. You can invoke the tree functions from bb.edn
tasks or you can install it as a global script with #C0400TZENE7. This covers as far as I know all major use cases.
@U7ERLH6JX Maybe this is something you could go into a little bit of detail in in the workshop
This requires users to make the decision up front for their code to be reusable. Seems like the babshka should heavily encourage scripts to be reusable by default.
There is a major issue in the re-frame space where there is loads of code written but very low reusability across projects/companies due to the design of the framework. It's really held clojure frontends back because so little sharing/reuse. Just concerned the default option in BB is encouraging the same outcome.
This is how it goes with all projects: you start off with little one-off hacks and if it becomes more widely useful, you make libraries. There's nothing stopping you from making your code re-usable.
also the primary intention is that these are examples, intended for inspiration not as libraries
Yes for code you own - I'm thinking about BB code that is shared online. People create cool things not realising it can't easily be built on top of.
@UJVEQPAKS You can see a combination of non-reusable code + reusable code here: https://github.com/borkdude/tools
The .clj
files themselves cause top level side effects, but they call into the re-usable libraries
Yup - agree these examples are reusable.
Maybe we should update the main examples with (when (= *file* (System/getProperty "babashka.file"))) (do-my-side-effects))
and promote its usage more commonly?
the examples were written a long time ago before most of the bb.edn
stuff and bbin stuff existed
I don't have time at the mo - but will do if I get time down the line.
Thanks @U7ERLH6JX and @U04V15CAJ very much for all the help this morning. Have a good rest of your Sundays.
Hi, after some commits in 1.0.174-SNAPSHOT I noticed that
clojure.lang.ExceptionInfo: Method submit on class java.util.concurrent.ThreadPerTaskExecutor not allowed!
when I try to run virtual threads. May be I should use another branch than master?That class isn't available on the master branch. As mentioned several times already, you should use the jdk19-loom branch, but it isn't in sync with master. You can help with merging master into that branch via a PR.
Ah, got it. Thank you.
The relevant change is this, probably: https://github.com/babashka/babashka/compare/master...jdk19-loom#diff-10889a6dbab396cc4a07d142a6ddc22e589081f73c25b382b14a5b0b6a3560b1