This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-08-12
Channels
- # aleph (3)
- # announcements (15)
- # architecture (6)
- # babashka (35)
- # babashka-sci-dev (10)
- # biff (5)
- # calva (9)
- # cherry (1)
- # cider (44)
- # clj-kondo (31)
- # cljfx (1)
- # clojure (108)
- # clojure-europe (32)
- # clojure-norway (12)
- # clojurescript (15)
- # conjure (3)
- # cursive (8)
- # datahike (1)
- # datalevin (19)
- # datascript (1)
- # datomic (59)
- # emacs (4)
- # graphql (3)
- # jobs (1)
- # luminus (6)
- # meander (9)
- # membrane (45)
- # nbb (67)
- # off-topic (16)
- # portal (3)
- # remote-jobs (1)
- # scittle (8)
- # shadow-cljs (46)
- # test-check (7)
- # tools-deps (5)
- # vim (63)
- # web-security (11)
- # xtdb (15)
Hi :)
I want:
1. To write a CLI tool with babashka
2. To have it on PATH so that I can run it from any folder on my system
3. To declare dependencies only in bb.edn
, and avoid needing a "generate script" step (like in Neil)
4. To be able to split my script into multiple files / namespaces, like a normal Clojure project
Currently, the only option I see is to use a shell wrapper. Something like:
#!/usr/bin/env bash
SRC_PATH="/home/teodorlu/dev/ORG/MYTOOL/SRC"
exec bb --classpath $SRC_PATH -m MYTOOL "$@"
It seems to run my script. But I'm linking straight into MYTOOL/SRC
, so I don't think I'm picking up dependencies.
Is there an endorsed way to do this?Hmm, I'm wondering whether just using babashka itself for the wrapper would be a solution. Something like:
(require '[babashka.deps :as deps])
(deps/add-deps '{:deps {mytool {:local/root "/home/teodorlu/dev/ORG/MYTOOL/SRC"}}})
(require 'mytool)
(apply mytool/main *command-line-args*)
Right! That looks like exactly what I need. Not sure why I missed that.
Trying --config
. Thanks!
so your shell wrapper would look like this:
SRC_PATH="/home/teodorlu/dev/ORG/MYTOOL"
exec bb --config $SRC_PATH -m MYTOOL "$@"
This is also what I'm considering for bb install
, that it would generate a shell wrapper like that
That would be great 🙂 Feels like I'm writing platform specific boilerplate right now.
you could make a bb script/tool which generates that boilerplate, e.g. a bb install
prototype in userspace
would be good to experiment with that to see if it works and to inform what bb install
will look like
bb install
prototype, local only, --dry-run only:
$ bb -m bb-install bb.edn --main ucad --install-dir ~/.local/bin --dry-run
#!/usr/bin/env bash
# This script was generated by `bb install`.
# TODO link to `bb install` docs
exec bb --config /home/teodorlu/dev/iterate/squirreltime/ucad/bb.edn -m ucad "$@"
Source (WIP):
(ns bb-install
(:require [babashka.cli :as cli]
[babashka.fs :as fs]
[clojure.string :as str]))
;; Example usage:
;;
;; bb-install BB_EDN_PATH --main MAIN --wrapper-script-type bash --dir ~/.local/bin
(defn- lines [& ls]
(str/join "\n" (map str ls)))
(defn shell-escape-string [s]
;; TODO, not sure how
;; Otherwise, we'll fail on spaces in file names
s)
(defn run [opts]
(let [opts (merge {:wrapper-script-type :bash ; :wrapper-script-type should default to batch on Windows
:install-dir (fs/expand-home "~/.local/bin")}
opts)
{:keys [dry-run main wrapper-script-type install-dir bb-edn-path]} opts]
(assert dry-run "Only dry-run is supported for now")
(assert main)
(assert wrapper-script-type)
(assert (#{:bash} wrapper-script-type) "Only bash wrappers are supported for now")
(assert install-dir)
(assert bb-edn-path)
;; without dry-run, should write to a file.
(println (lines
"#!/usr/bin/env bash"
""
"# This script was generated by `bb install`."
"# TODO link to `bb install` docs"
""
(str "exec bb --config " (shell-escape-string (fs/absolutize bb-edn-path)) " -m " main " \"$@\"")))))
(defn -main [& args]
(let [opts (cli/parse-opts args
{:alias {:m :main}
:coerce {:main :string
:wrapper-script-type :keyword
:dry-run :boolean
:install-dir :string}
:args->opts [:bb-edn-path]})]
(run opts)))
Did some more polish, added install instructions: https://play.teod.eu/bb-install/ No helptext, and few guardrails. But works for installing a local bb script.
So one of the ideas in bb install
is that you can install from a url, not from a local project