babashka

https://github.com/babashka/babashka. Also see #sci, #nbb and #babashka-circleci-builds .
Karol Wójcik 2021-04-22T07:59:27.349100Z

@borkdude I would like to pack some babashka pods into a .zip file in some automatic manner. How would you do it?

borkdude 2021-04-22T08:01:44.349600Z

@karol.wojcik Probably manually download them and zip them

Karol Wójcik 2021-04-22T08:05:24.351500Z

1. Supposing user has the pod defined in that way: (load-pod 'org.babashka/aws "0.0.5") then if I provide pod in .zip via babashka classpath will babashka take it from local filesystem? 2. Can somehow inspect a script and check which pods it declared?

borkdude 2021-04-22T08:09:08.352Z

@karol.wojcik Is the user calling the pod or your framework?

Karol Wójcik 2021-04-22T08:13:11.353700Z

@borkdude Actually babashka-runtime will call the user script like so:

bb -cp src:holy-lambda-babashka-0.1.27.jar:holy-lambda-default-retriever-0.0.4.jar:clojure-1.10.3.jar:core.specs.alpha-0.2.56.jar:spec.alpha-0.2.194.jar:PODS_PATH -m USER_DEFINED_ENTRYPOINT

Karol Wójcik 2021-04-22T08:13:33.354200Z

@borkdude so framework 😄 but only on aws runtime side 🙂

borkdude 2021-04-22T08:14:24.355100Z

what pods is your framework using? btw, pods aren't resolved from the classpath, you can use either a fully qualified symbol so it loads from the registry or a file system path

borkdude 2021-04-22T08:14:34.355300Z

I'm afk for a bit

Karol Wójcik 2021-04-22T08:17:53.357Z

@borkdude Ok so no PODS_PATH then. From which path babashka reads babashka pods? Can the case for fully qualified symbol first check whether pods are available locally? That would make the integration with babashka pods easy on holy-lambda side.

borkdude 2021-04-22T08:46:26.357800Z

@karol.wojcik you can write (load-pod "/tmp/foo/pod") to just load a pod from the file system, there is no special path for this

borkdude 2021-04-22T08:47:59.358800Z

In the case of a fully qualified symbol it is trying to load from ~/.babashka/pods/repository/org.babashka/postgresql/0.0.4 for example when you write (load-pod 'org.babashka/postgresql "0.0.4")

borkdude 2021-04-22T08:48:24.359200Z

and if it is not there, it will try to download it from the pod registry and install it there

borkdude 2021-04-22T08:52:19.359500Z

Does that make sense?

borkdude 2021-04-22T08:52:27.359700Z

It's similar to .m2 but for pods ;)

borkdude 2021-04-22T08:53:25.360100Z

So if you can organize your environment in such a way that these pods are in the right place, this will work

Karol Wójcik 2021-04-22T09:07:07.360700Z

Hah. Eaaaasy!

Karol Wójcik 2021-04-22T09:07:18.361Z

Thank you so much for the explanation @borkdude

Karol Wójcik 2021-04-22T09:15:52.361700Z

@borkdude Where pods are downloaded in Windows?

borkdude 2021-04-22T09:17:21.362100Z

@karol.wojcik To wherever (System/getProperty "user.home") says your home dir is

borkdude 2021-04-22T09:17:38.362400Z

it also respects XDG_CACHE_HOME

Karol Wójcik 2021-04-22T09:19:44.363700Z

Ok got it. I will cp pods from .babashka to zip file and it will just work.

borkdude 2021-04-22T09:20:31.364500Z

@karol.wojcik Except that pods are platform specific. So if you are on Windows, those are .exe files and these do not run on AWS

borkdude 2021-04-22T09:20:51.365100Z

same for macOS

Karol Wójcik 2021-04-22T09:21:06.365700Z

Ups. Right. I need only linux artifacts.

borkdude 2021-04-22T09:21:09.365900Z

There should probably be a step in the docker image to download the pods or something

Karol Wójcik 2021-04-22T09:21:55.366700Z

Is there a way that I can say to babashka "Hey bb just download the pods! Don't do anything else!!"?

borkdude 2021-04-22T09:22:30.367400Z

Not right now. But we could make something like this in bb.edn: you declare the pods there and then you could say bb pods --download or something

Karol Wójcik 2021-04-22T09:22:51.367800Z

Yep. That would be awesome.

borkdude 2021-04-22T09:23:16.368400Z

Currently you can do this by making a script download_pods.clj and just call load-pod there with nothing else

Karol Wójcik 2021-04-22T09:23:58.369100Z

Ok will do so. May I make an issue to support bb pods --download?

👍 1
borkdude 2021-04-22T09:25:50.369300Z

There's already an issue for it: https://github.com/babashka/babashka/issues/768

👍 1
Karol Wójcik 2021-04-22T09:26:57.370200Z

Thanks!

borkdude 2021-04-22T17:14:27.370800Z

Some example scripts: https://github.com/adam-james-v/scripts See https://twitter.com/RustyVermeer/status/1385269161106972673 for more info

2021-04-22T17:30:00.374800Z

Hello. Using babashka.process is it possible to stream the stdout to the parent process whilst also capturing the output? I’m using bb as a task runner and launching the figwheel cljs compilation process. It takes a while so I want the output to display as it occurs, but I want also want to capture output so I can exit with a non zero code if the output contained any warnings.

borkdude 2021-04-22T17:34:04.375700Z

@jamescroft you can display the output as it occurs by setting :out :inherit :err :inherit but I'm not sure why you want to capture the output?

borkdude 2021-04-22T17:34:18.376Z

and why are you not using shell in the task?

borkdude 2021-04-22T17:35:00.376400Z

oh now I get it, you are not using bb's task runner

borkdude 2021-04-22T17:38:22.377400Z

@jamescroft why don't you just exit with the exit code of the process?

borkdude 2021-04-22T17:41:02.377600Z

Something like this:

(require '[babashka.process :refer [process]])

(-> (process '["bb" "-e" (loop [i 0]
                           (if (= i 3)
                             (do (prn :stop) (System/exit 1))
                             (do (Thread/sleep 1000)
                                 (prn :foo)
                                 (recur (inc i)))))]
             {:inherit true})
    deref :exit (System/exit))

borkdude 2021-04-22T17:41:41.378Z

or use check which only throws on a non-zero exit:

(require '[babashka.process :refer [process check]])

(-> (process '["bb" "-e" (loop [i 0]
                           (if (= i 3)
                             (do (prn :stop) (System/exit 1))
                             (do (Thread/sleep 1000)
                                 (prn :foo)
                                 (recur (inc i)))))]
             {:inherit true})
    check)

2021-04-22T17:48:24.380700Z

@borkdude I’m using the bb task runner, but the task definition just calls a function (not using shell ). The reason I want to capture output is that I want to look for the string “Compile Warning” that may occur in the stdout. If I see a “Compile Warning” then I want the process to exit with a non zero code even though the child process would have been a zero code.

2021-04-22T17:51:24.382400Z

So currently I have something like:

(defn throw-if-output-contains [{:keys [out err] :as process} s]
  (when (str/includes? out s)
    (println out)
    (println "Exiting, stdout contains:" s)
    (System/exit 1))
  (when (str/includes? err s)
    (println err)
    (println "Exiting, stderr contains:" s)
    (System/exit 1))
  process)

(defn compile-cljs [{:keys [env]}]
  (-> (sh ["clojure" "-A:frontend:build-min"])
      (throw-if-output-contains "Compile Warning")))
But with that, the output only gets printed after the child process finishes. Ideally i’d like the output to appear as it happens.

borkdude 2021-04-22T17:54:49.382700Z

@jamescroft

(require '[babashka.process :refer [process destroy]]
         '[<http://clojure.java.io|clojure.java.io> :as io]
         '[clojure.string :as str])

(def proc (process '["bb" "-e" (loop [i 0]
                                 (if (= i 3)
                                   (do (prn :compile-warning)
                                       (recur (inc i)))
                                   (do (Thread/sleep 1000)
                                       (prn :foo)
                                       (recur (inc i)))))]
                   {:shutdown destroy}))

(def output (:out proc))

(with-open [rdr (io/reader output)]
  (loop [lines (line-seq rdr)]
    (when-first [l lines]
      (println l)
      (when (str/includes? l "compile-warning")
        (System/exit 1))
      (recur (next lines)))))

borkdude 2021-04-22T17:58:09.383200Z

Perhaps your process writes to stderr, but the same trick applies to that

2021-04-22T17:59:11.384200Z

Thank you! I see the approach now. I’ll try something like that.