Fork me on GitHub
#babashka
<
2019-12-28
>
borkdude08:12:59

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

borkdude09:12:22

@deleted-user 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

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

Santiago10:12:38

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

borkdude10:12:46

@deleted-user 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

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

Lyn Headley22: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.