This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-09-09
Channels
- # aws (1)
- # babashka (61)
- # bangalore-clj (5)
- # beginners (83)
- # biff (2)
- # calva (4)
- # cider (6)
- # clara (5)
- # clj-kondo (72)
- # cljs-dev (31)
- # cljsrn (28)
- # clojure (8)
- # clojure-australia (1)
- # clojure-europe (19)
- # clojure-france (1)
- # clojure-losangeles (21)
- # clojure-nl (2)
- # clojure-spec (2)
- # clojure-uk (9)
- # clojurescript (13)
- # clojureverse-ops (5)
- # code-reviews (1)
- # conjure (7)
- # cursive (4)
- # datascript (2)
- # datomic (8)
- # depstar (1)
- # emacs (3)
- # etaoin (1)
- # events (3)
- # exercism (7)
- # fulcro (6)
- # girouette (2)
- # graalvm (125)
- # honeysql (19)
- # integrant (1)
- # jobs (12)
- # lsp (1)
- # numerical-computing (1)
- # off-topic (23)
- # portal (12)
- # practicalli (1)
- # re-frame (35)
- # reitit (5)
- # releases (1)
- # remote-jobs (1)
- # shadow-cljs (51)
- # tools-deps (14)
- # vim (3)
- # xtdb (20)
I want to have a global bb script, say its in /src/project1/bin/bbscript
, and I want to call it from anywhere (project1/bin is in my path) and give it a file as an argument like:
$ cd src/mynewproject
$ ls
file.edn
$ bbscript -f file.edn
My problem is that I have a bb.edn
file in /src/project1
so I need babashka to pick that up somehow. I tried this
#_( ;; Allow this script to be executed directly
"cd" "$(readlink -f $(dirname $0)/..)"
"exec" "bb" "$0" "$(readlink -f $(dirname $0)/..)" "$@" "$PWD"
)
which picks up the bb.edn but now it doesn’t get the right file passed in (because its looking in the wrong directory)@alex395 this is an interesting problem. I think we could make bb pick up on the bb.edn relative to the script but this requires some thinking, e.g. local paths in bb.edn like src
, should they be resolved also relative to the script or not. this is what makes this a difficult issue
perhaps the local paths should always be resolved relative to the bb.edn
, but I'm afraid I'll break something if I introduce this behavior
I've done something similar with nbb
though: nbb /tmp/project/foobar.cljs
picks up on /tmp/project/node_modules
Yeah.. my quick fix was just to copy the bb.edn file into each directory I want to execute it, which works but isn’t great for obvious reasons
There is an issue about it here: https://github.com/babashka/babashka/discussions/869
so my suggestion just now is basically in this comment as well: https://github.com/babashka/babashka/discussions/869#discussioncomment-1115234
You can also set BABASHKA_EDN
but when you have deps that you want to utilize I ran into problems with this approach and just cd'ed into the top level directory where bb.end resides
BABASHKA_EDN
is not a public/documented thing, only internally used right now for testing, it might disappear
I re-iterated my suggestion here: https://github.com/babashka/babashka/discussions/869#discussioncomment-1301059
@alex395 I guess you could make a bash wrapper like this:
export old_dir=$pwd
cd script_dir
bb "$@"
and then use old_dir
to resolve any relative file argumentsThat was my only question, will that still work even though you aren't executing the script from where it resides?
I would love to have that
script.clj a
and then (babashka.tasks/run (symbol (first *command-line-args*)))
to run a
?
Not sure if this helps but I use this pattern for the subcommand stuff:
(defn -main
[& args]
(let [opts (parse-opts args cli-opts :in-order true)
config (:config (:options opts))
subcmd (first (:arguments opts))
subcmd-args (rest (:arguments opts))]
(cond
(:help (:options opts)) (println usage)
(= "help" subcmd) (println usage)
(= "cluster" subcmd) (apply cluster/parse subcmd-args)
(= "vm" subcmd) (apply vm/parse subcmd-args)
:else (println usage))
(config/save config/config)
(shutdown-agents)))
couldn't we do something like that?
The command parsing isn't what I'm interested in right now, just interested if deftasks
would solve the global script + bb.edn problem(s)
This would allow you to make a global bb.edn with multiple global scripts that can use it
That would be super cool
I agree. This solution only occurred to me after working on #nbb so there is a nice cross-pollination there
e.g.:
/foo/node_modules
/foo/script.js
/bar # no node_modules
/bar $ node ../foo/script.js # will use /foo/node_modules
They also have this helper npx
too that does some magic to find the right node_modules for whatever you are executing
unless we want to support executing scripts from your deps, but we already do this using bb -m foo.bar/some-fn
Could probably just post that as a question to get feedback. I'm guessing most people are used to passing it in like youve described in the clj world
hi - I'm just learning babashka and I'm finding there are times when I'd like to "shell out" a command within bb. Do I need to require the sh
library somehow or use babashka/process? A small example of how to just call ls -hlah
from bb
would be ideal. Thanks in advance
also - is babashka designed to replace things that might go into a .profile or .bashrc file (e.g. common aliases?)
Take a look at https://book.babashka.org/#child_processes
Hi Dorab - I've seen the link you show which shows a clojure process. Looking for a bb
example
or maybe I need to study it more carefully. Is process
not a "batteries included" part of babashka?
getting closer with bb -e "(require '[babashka.process :as p]) (p/process \"ls\" ) "
. I'll fiddle with it a bit more. Thanks
@U0CDMAKD0 you can also use (babashka.tasks/shell "ls")
which is more like how you do it in a shell: it prints the output
I have this: bb -e "(require '[babashka.process :as p]) (-> (p/process [\"ls\" \"-la\"] {:out :string}) p/check :out println ) "
is there a better way?
you can use :inherit true
if you want the printing to happen from the process immediately
nice. thank-you, I'll tinker a bit more. Ultimately, I'd like to replace some of my lengthly .bashrc commands and aliases with babashka so understanding this will come in handy
bb -e '(-> (babashka.tasks/shell "ls" "-halt") :out )'
works well, yet it also prints #object[java.lang.ProcessBuilder$NullInputStream 0x4fb6cba8 "java.lang.ProcessBuilder$NullInputStream@4fb6cba8"]
after the directory listing.
d'oh.. this solves it`bb -e '(-> (babashka.tasks/shell "ls" "-halt") {:out :string} )'`
Wrote a project that combines tools.build and bb.edn: https://github.com/clj-easy/graal-build-time