Fork me on GitHub
#babashka
<
2019-12-28
>
nate_clojurians04:12:33

so this is useful

nate_clojurians04:12:41

with a deps.edn like this:

{:paths   []
 :deps    {clj-http-lite {:git/url ""
                          :sha "64530ded30d107b1834c3a704f2e087c3324bff9"}}
 :aliases {:depstar
           {:extra-deps
            {seancorfield/depstar {:mvn/version "0.3.4"}}}}}

nate_clojurians04:12:54

you can run:

clojure -A:depstar -m hf.depstar.jar deps.jar

nate_clojurians04:12:04

to build a thin jar with just clj-http-lite in it

nate_clojurians04:12:12

$ unzip -l deps.jar
Archive:  deps.jar
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  12-27-2019 22:13   clj_http/
        0  12-27-2019 22:13   clj_http/lite/
     2397  12-27-2019 19:00   clj_http/lite/util.clj
     2068  12-27-2019 19:00   clj_http/lite/links.clj
     3651  12-27-2019 19:00   clj_http/lite/core.clj
     9608  12-27-2019 19:00   clj_http/lite/client.clj
---------                     -------
    17724                     6 files

nate_clojurians04:12:44

and then you can set the classpath for babashka accordingly:

BABASHKA_CLASSPATH=`pwd`/deps.jar bb "(require '[clj-http.lite.client :as client]) (:status (client/get \"\"))"

nate_clojurians04:12:46

which will return 200

nate_clojurians04:12:47

I'm betting there's some clever way to include the jar in a babashka script but I haven't figured it out yet

nate_clojurians04:12:56

like maybe uuencode/decode to a tmp directory and then set the classpath

borkdude08:12:59

@nate_clojurians as an alternative you can probably build a script which inlines all namespaces into a single file

borkdude09:12:22

@nate_clojurians I've got an experimental thing here: Mac: https://2917-201467090-gh.circle-artifacts.com/0/release/babashka-0.0.51-SNAPSHOT-macos-amd64.zip Linux: https://2915-201467090-gh.circle-artifacts.com/0/release/babashka-0.0.51-SNAPSHOT-linux-amd64.zip

$ export BABASHKA_CLASSPATH="$(clojure -Sdeps '{:deps {clj-http-lite {:git/url "" :sha "f44ebe45446f0f44f2b73761d102af3da6d0a13e"}}}' -Spath)"
$ lein bb --uberscript "(require '[clj-http.lite.client :as client]) (:status (client/get \"\"))"
200
This will put all the required namespaces into one single file in /tmp/uberscript.clj. Now you can:
$ bb /tmp/uberscript.clj
200

slack.jcpsantiago10:12:10

How do I truncate a number e.g. 52.51029159197582 to two decimal places e.g. 52.51 in babashka? e.g. (clojure.pprint/cl-format nil "~,2f" 52.51029159197582) won't work

borkdude10:12:33

@slack.jcpsantiago I think you can do this with format

slack.jcpsantiago10:12:38

ah cool works! I had only ever used cl-format , thanks

borkdude10:12:46

@nate_clojurians New version:

$ bb --classpath test-resources/babashka/src_for_classpath_test -m my.main --uberscript /tmp/script.clj
$ bb /tmp/script.clj 1 2 3
("1" "2" "3")
Linux: https://2921-201467090-gh.circle-artifacts.com/0/release/babashka-0.0.51-SNAPSHOT-linux-amd64.zip Mac: https://2923-201467090-gh.circle-artifacts.com/0/release/babashka-0.0.51-SNAPSHOT-macos-amd64.zip

borkdude10:12:36

Demo:

$ # create project sources
$ mkdir -p project/src
$ echo "(ns project-impl) (defn foo [] ::foo)" > project/src/project_impl.clj
$ echo "(ns project-main (:require [project-impl])) (defn -main [& args] (project-impl/foo))\n"  > project/src/project_main.clj
$ #
$ # run with bb using classpath
$ bb --classpath project/src -m project-main
:project-impl/foo
$ #
$ # create uberscript
$ bb --classpath project/src -m project-main --uberscript uberscript.clj
$ cat uberscript.clj
(ns project-impl) (defn foo [] ::foo)
(ns project-main (:require [project-impl])) (defn -main [& args] (project-impl/foo))

(ns user (:require [project-main])) (apply project-main/-main *command-line-args*)%                                                                                               $ #
$ # run uberscript
$ bb uberscript.clj
:project-impl/foo

borkdude11:12:53

I think I like this idea.

borkdude11:12:11

Using deps.clj (https://github.com/borkdude/deps.clj/):

deps.edn: {:aliases {:script {:main-opts ["-m project-main"]}}}

$ deps.exe -A:script -Scommand "bb --classpath {{classpath}} {{main-opts}} --uberscript uberscript.clj"

$ bb uberscript.clj 
:project-impl/foo

nate_clojurians12:12:14

ooooh very cool

nate13:12:00

This is amazing. Great idea.

borkdude13:12:21

Note that when you already have babashka on your system, installing deps.clj is trivial:

$ curl -sL  -o /tmp/deps.clj && chmod +x /tmp/deps.clj
$ /tmp/deps.clj
Clojure 1.10.1
It only assumes a working java installation, nothing else.

borkdude13:12:39

Personally I find this easier than the official way of installing clojure in CI

borkdude14:12:51

Updated the quickstart, rationale and install sections for deps.clj: https://github.com/borkdude/deps.clj/blob/master/README.md#quickstart

borkdude22:12:30

Babashka v0.0.51 released, which adds the --uberscript option: https://github.com/borkdude/babashka/blob/master/README.md#uberscript

borkdude22:12:41

Cool 🙂 Any reason you're not using distinct?

borkdude22:12:47

Using (System/getenv "PATH") might work for getting the PATH

laheadle22:12:38

great ideas!

laheadle22:12:44

distinct doesn't promise to preserve order. seems to though.

borkdude22:12:49

I think it always does

borkdude22:12:15

Splitting on (System/getProperty "file.separator") is recommended for OS portability

borkdude22:12:45

Sorry, that's path.separator:

borkdude22:12:50

$ bb '(System/getProperty "path.separator")'
":"

borkdude22:12:39

Your remove-dups function might come in handy for reporting which entries are duplicates

borkdude22:12:14

I made this of it:

(require '[clojure.string :as str])

(defn get-path []
  (System/getenv "PATH"))

(defn report-dups
  [coll]
  (reduce
   (fn [acc next]
     (if (contains? acc next)
       (do
         (println "Duplicate PATH entry" next )
         acc)
       (conj acc next)))
   #{}
   coll)
  nil)

(as-> (get-path) $
  (str/split $ (re-pattern (System/getProperty "path.separator")))
  (report-dups $))
and it helped me find that have accidentally doubled my entire PATH string somewhere

borkdude22:12:52

Aah, now I get it. I was in a nested shell, that's why the path got doubled. With a fresh shell, no duplicates.