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

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

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

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

deleted20:07:00

does babashka have something like sed?

kokada20:07:33

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

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

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.

bocaj20:07:24

I was just thinking about this, Thanks!

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

borkdude20:07:23

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