Thank again for the help yesterday, I did a bit more digging... I am not sure if this is an issue or not?
Say I have two scripts:
<dir>/foo
<dir>/bar
bar
contains:
#!/usr/bin/env bb
(ns bar)
(defn hello [] (println "test"))
foo
this works load-file
before ns
#!/usr/bin/env bb
(require '[babashka.fs :as fs])
(load-file (str (fs/parent *file*) "/bar"))
(ns foo)
(when (= *file* (System/getProperty "babashka.file"))
(bar/hello))
foo
this doesn't work
#!/usr/bin/env bb
(ns foo
(:require [babashka.fs :as fs]))
(when (= *file* (System/getProperty "babashka.file"))
(load-file (str (fs/parent *file*) "/bar"))
(bar/hello))
7: (bar/hello))
^--- Could not resolve symbol: bar/hello
@brandon746 Here is an example: /tmp/script/foo.clj:
(ns foo
(:require [babashka.classpath :refer [add-classpath]]
[babashka.fs :as fs]))
(add-classpath (str (fs/parent *file*)))
(require '[bar :as bar])
(prn (bar/bar-fn))
/tmp/script/bar.clj:
(ns bar)
(defn bar-fn []
:bar)
$ bb /tmp/script/foo.clj
:bar
@corasaurus-hex omg! Thank you so much for spending so much time and sending so many examples across π @borkdude great thank you for the article! I really was wondering as to they why this wasn't working.
oh I missed the examples that Cora sent, sorry :)
no worries! you knew why it wasn't working which is so much more important, so thank you
For now I am just going to use preload to load them all, but was hoping of explicitly loading some scripts
wow, it really, really doesn't like conditionally loading a new namespace
if you move the load-file out of the when but keep the bar/hello in the when then it works
I guess you could use a macro for this, maybe?
if you run http://foo.bb directly it will print hello twice since it is the main file and it will load bar/hello. if you run http://baz.bb directly it will just print the bye from loading http://foo.bb and the bye from running http://baz.bb
You could also add a file like this to that directory
and then stick this at the top of each file...
This is normal behavior, you will get this with JVM Clojure as well. Itβs called the Gilardi scenario. https://technomancy.us/143
As I explained before you probably want to be using add-classpath + require for this
oooh bookmarking that one
Pomodoro timer in babashka: https://github.com/babashka/babashka/discussions/910
What would this look like in Tasks?
bb -cp $(clojure -Spath -Aremove-clojure) --uberscript data-loader -m append-hash
@bocaj you don't have to set the classpath anymore if you have a bb.edn
, also the remove-clojure thing is done automatically
oh, cool.
probably: bb uberscript data-loader -m append-hand
from within tasks you could just use shell
for this
ok, got it. Nice to have the classpath handled
does babashka have something like sed?
for editing in place, I mean
I think you can use clojure.string/replace
for the general case
but to edit in place, though?
It doesn't do everything sed
does, but it should work in the most common cases as a substitute for s/something/somethingelse//
> but to edit in place, though?
Ah, you mean like sed -i
?
yep!
I think the easiest way would be to (-> (slurp "file") (clojure.string/replace #"foo" "bar") (spit "file"))
it's a common thing I need to do. glob a bunch of filenames based on search criteria and then replace values
(Not tested so the arguments may be wrong)
yes, I agree with what @thiagokokada wrote, just slurp, str/replace and spit. you could also use Selmer if you need templating.
alrighty, I'll give that shot, thanks
I was just thinking about this, Thanks!
Is there a convenient way to pre-pend #!/usr/bin/env bb
to for example the output file from uberscript. Related to sed, above, thinking concat
with spit and slurp
The reason it does not do this by default is that it's not cross platform. To do this using a Clojure expression, you can do:
(spit "uberscript.clj" (str "#!/usr/bin/env bb\n\n" (slurp "uberscript.clj")))
not a great solution if your files are exceptionally large but that's not a problem for me π
you can probably optimize that using some streaming solution if needed, but in a lot of cases this does the job fine ;)
absolutely
but also you'd want to stream to a new file and then move it into place after the fact too
right
Adam is playing around with babashka and scittle on his stream now: https://www.twitch.tv/adam_james_tv