Fork me on GitHub
#babashka
<
2024-04-08
>
Stephan Renatus12:04:28

babashka is beautiful. I wanted to be able to run make foo anywhere in my repo, and it was a perfect chance to write some clojure+bb once again 😄 if someone wants to critique my attempt, you’re welcome to (🧵)

😀 1
🎉 1
🚀 1
Stephan Renatus12:04:33

#!/usr/bin/env bb
(require '[babashka.fs :as :fs]
         '[babashka.process :refer [shell]])

(def make "/usr/bin/make")

(defn find-root
  ([]   (find-root (fs/cwd)))
  ([wd] (cond
          (fs/exists? (fs/path wd "Makefile")) {:dir wd}
          (fs/same-file? (fs/home) wd)         :not-found
          (fs/exists? (fs/path wd ".git"))     :not-found
          :else                                (find-root (fs/parent wd)))))

(defn run-make [wd & args]
  (if (= :not-found wd)
    (throw (Exception. "Makefile not found"))
    (apply shell wd make args)))

(when (= *file* (System/getProperty "babashka.file"))
  (apply run-make (find-root) *command-line-args*))

nate13:04:00

Neat idea. A couple suggestions: • Replace the recursive find-root call with a call to recur. • Replace (throw (Exception. "Makefile not found")) with (throw (ex-info "Makefile not found" {:babashka/exit 1})), which will make Babashka print out the error message cleaner than the exception stack trace stuff.

🙌 1
John14:04:31

I think babashka can exec? That might be better than subprocess

👍 2
borkdude14:04:54

There is a PR for a similar function as find-root here: https://github.com/babashka/fs/issues/112 Something here tells me that I was right about not including it yet, since it seems the requirements of the above variant are slightly different

Stephan Renatus16:04:31

thank you all for the feedback, this is very valuable to me 😃

Stephan Renatus07:04:44

> I think babashka can exec? That might be better than subprocess AFAICT it doesn’t support :dir, though. I could be missing something

borkdude07:04:44

yeah, that's a good point

Stephan Renatus07:04:42

I’ve looked into the source, but a “fix” for that is beyond me 😅

borkdude07:04:37

I don't think it's fixable unless graalvm supports it.

Stephan Renatus07:04:26

yeah for my purposes, the extra shell doesn’t matter. the time the actual make invocation takes dominates the script 🙃

Stephan Renatus13:04:49

changed it now to throw an exception when the make call itself errors:

(-> (apply shell (assoc wd :continue true) make args)
      :exit
      System/exit)))

🎉 1
mukundzare21:04:17

I upgraded to the latest version of bb today and suddenly slurp didn't work to fetch data from apis. So I switched to http-client and still it fails to fetch. For example: Why does (http/get "") throw a null exception but I am able to fetch from the same address via Curl cli?

mukundzare21:04:34

I am behind a company firewall

borkdude21:04:17

please use a thread for addressing one topic

borkdude21:04:13

did you set the correct JVM properties for accessing the firewall?

mukundzare21:04:43

Woops, sorry I thought messaging below myself would merge the messges into a thread (like MS teams).

mukundzare21:04:00

I will try the jvm settings

borkdude21:04:56

also maybe re-download the previous version of bb manually via github releases into another directory and try it as well. I have no idea why things should have changed, but perhaps you can find the exact version when it changed

mukundzare21:04:26

Now, I am not quite familiar I am doing this right. Is this the way?

mukundzare21:04:30

{:paths ["src"] :deps {tick/tick {:mvn/version "RELEASE"} org.babashka/http-client {:mvn/version "0.3.11"}} :jvm-opts ["-Dhttp.proxyHost=127.0.0.1" "-Dhttp.proxyPort=3128" "-Dhttps.proxyHost=127.0.0.1" "-Dhttps.proxyPort=3128" "-Dhttps.nonProxyHosts=localhost"]}

borkdude21:04:27

bb.edn doesn't respect jvm-opts currently, also deps.edn doesn't support them at the top level. you can set them either on the command line or programmatically

borkdude21:04:39

bb -Dhttp.proxyPort=.... 

borkdude21:04:57

or (System/setProperty "http.proxyPort" ...)

1
borkdude21:04:09

gotta go now, will check back tomorrow

mukundzare07:04:37

That worked. Thank you!

👍 1