Fork me on GitHub
Brandon Stubbs01:07:27

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"))
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"))
7:   (bar/hello))
      ^--- Could not resolve symbol: bar/hello

Brandon Stubbs01:07:17

For now I am just going to use preload to load them all, but was hoping of explicitly loading some scripts

Cora (she/her)02:07:17

wow, it really, really doesn't like conditionally loading a new namespace

Cora (she/her)02:07:02

if you move the load-file out of the when but keep the bar/hello in the when then it works

Cora (she/her)02:07:09

I guess you could use a macro for this, maybe?

Cora (she/her)02:07:44

if you run directly it will print hello twice since it is the main file and it will load bar/hello. if you run directly it will just print the bye from loading and the bye from running

Cora (she/her)02:07:24

You could also add a file like this to that directory

Cora (she/her)02:07:52

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.


As I explained before you probably want to be using add-classpath + require for this

Cora (she/her)06:07:34

oooh bookmarking that one


@U015Y1A1N8Y 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))
(ns bar)

(defn bar-fn []
$ bb /tmp/script/foo.clj

Brandon Stubbs16:07:11

@U8QBZBHGD omg! Thank you so much for spending so much time and sending so many examples across πŸ’™ @U04V15CAJ great thank you for the article! I really was wondering as to they why this wasn't working.

πŸ’œ 2

oh I missed the examples that Cora sent, sorry :)

Cora (she/her)16:07:15

no worries! you knew why it wasn't working which is so much more important, so thank you


Pomodoro timer in babashka:

πŸ… 5
πŸ˜ƒ 4

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

Cora (she/her)20:07:00

does babashka have something like sed?


I think you can use clojure.string/replace for the general case

Cora (she/her)20:07:07

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?


I think the easiest way would be to (-> (slurp "file") (clojure.string/replace #"foo" "bar") (spit "file"))

Cora (she/her)20:07:23

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 @UFDRD93RR wrote, just slurp, str/replace and spit. you could also use Selmer if you need templating.

Cora (she/her)20:07:50

alrighty, I'll give that shot, thanks


I was just thinking about this, Thanks!

Cora (she/her)20:07:10

for editing in place, I mean


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")))

πŸ‘ 2
Cora (she/her)20:07:09

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 ;)

Cora (she/her)20:07:37

but also you'd want to stream to a new file and then move it into place after the fact too


Adam is playing around with babashka and scittle on his stream now: