Fork me on GitHub
#babashka
<
2021-07-01
>
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"))
  (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

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

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...

borkdude05:07:58

This is normal behavior, you will get this with JVM Clojure as well. It’s called the Gilardi scenario. https://technomancy.us/143

borkdude05:07:39

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

borkdude10:07:49

@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))
/tmp/script/bar.clj:
(ns bar)

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

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
borkdude16:07:10

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

borkdude12:07:55

Pomodoro timer in babashka: https://github.com/babashka/babashka/discussions/910

πŸ… 5
πŸ˜ƒ 4
bocaj19:07:56

What would this look like in Tasks? bb -cp $(clojure -Spath -Aremove-clojure) --uberscript data-loader -m append-hash

borkdude19:07:01

@bocaj you don't have to set the classpath anymore if you have a bb.edn, also the remove-clojure thing is done automatically

bocaj19:07:38

oh, cool.

borkdude19:07:39

probably: bb uberscript data-loader -m append-hand

borkdude19:07:51

from within tasks you could just use shell for this

bocaj19:07:05

ok, got it. Nice to have the classpath handled

Cora (she/her)20:07:00

does babashka have something like sed?

kokada20:07:33

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

Cora (she/her)20:07:07

but to edit in place, though?

kokada20:07:09

It doesn't do everything sed does, but it should work in the most common cases as a substitute for s/something/somethingelse//

kokada20:07:34

> but to edit in place, though? Ah, you mean like sed -i?

kokada20:07:21

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

kokada20:07:37

(Not tested so the arguments may be wrong)

borkdude20:07:36

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

bocaj20:07:24

I was just thinking about this, Thanks!

Cora (she/her)20:07:10

for editing in place, I mean

bocaj20:07:50

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

borkdude20:07:01

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 πŸ™‚

borkdude20:07:15

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

borkdude20:07:23

Adam is playing around with babashka and scittle on his stream now: https://www.twitch.tv/adam_james_tv