Fork me on GitHub
#babashka
<
2021-08-24
>
bherrmann01:08:40

Humm... Is there a simple (easy) way to execute babashka scripts off of github? aka something like

#!/usr/bin/env bb
(add-classpath "")  ;; has project "myutility" with src/mynamespace.clj
(require '[mynamespace])
(mynamspace/main *command-line-args* )
perhaps using bb.edn so I can pull in dependencies as needed too... sort of like clojure's
clojure -Sdeps '{:deps {cljfmt {:mvn/version "0.8.0"}}}' -m cljfmt.main myInputFile 
but for bb.... and github...

Bob B04:08:45

The short(ish) answer is yes, assuming you have a deps.edn file in your repo. In the same way that you can specify git deps for clj, you can do it either in bb.edn or using babashka.deps:

(ns something
  (:require [babashka.deps :as deps]))

(deps/add-deps '{:deps {io.github.bobisageek/bb-test-thing
                             {:git/url ""
                              :sha "5a7eb8fb71665cbee2c6d5c90c38ea9fda48f7a6"}}})

(require '[io.github.bobisageek.stuff :as s])

(s/do-a-thing)

Bob B04:08:14

... please ignore the ugliness of the code - I just threw the repo up there for a really simple repro

donavan10:08:36

☝️ you can also look at the repos I posted three messages above that used git deps in a bb.edn file

John Conti21:05:58

I followed this recipe but the dependency resolution either didn't work or I didn't use it properly:

#!/usr/bin/env bb
(ns fmt-check
  (:require [babashka.deps :as deps]))
(deps/add-deps '{:deps {cljfmt/cljfmt {:mvn/version "0.8.0"}}})
(require '[cljfmt.main :as cljfmt])
(apply cljfmt/-main *command-line-args*)

John Conti21:05:32

% ./fmt-check
----- Error --------------------------------------------------------------------
Type:     java.lang.Exception
Message:  Unable to resolve classname: difflib.DiffUtils
Location: cljfmt/diff.clj:2:3

----- Context ------------------------------------------------------------------
1: (ns cljfmt.diff
2:   (:import [difflib DiffUtils]
     ^--- Unable to resolve classname: difflib.DiffUtils
3:            [ File]
4:            [java.util.regex Pattern])
5:   (:require [ :as io]
6:             [clojure.string :as str]))
7: 

----- Stack trace --------------------------------------------------------------
cljfmt.diff - cljfmt/diff.clj:2:3
cljfmt.main - cljfmt/main.clj:3:3
fmt-check   - /Users/jconti/code/reifyhealth/fine-grained-authorization/./fmt-check:5:1

John Conti21:05:48

But Clojure resolves the deps and is able to run:

% clojure -Sdeps '{:deps {cljfmt/cljfmt {:mvn/version "0.8.0"}}}' -m cljfmt.main
WARNING: Implicit use of clojure.main with options is deprecated, use -M
cljfmt [OPTIONS] COMMAND [PATHS ...]
      --help
      --file-pattern FILE_PATTERN                  \.clj[csx]?$
      --indents INDENTS_PATH
      --alias-map ALIAS_MAP_PATH
      --project-root PROJECT_ROOT                  .
      --[no-]ansi
      --[no-]indentation
      --[no-]remove-multiple-non-indenting-spaces
      --[no-]remove-surrounding-whitespace
      --[no-]remove-trailing-whitespace
      --[no-]insert-missing-whitespace
      --[no-]remove-consecutive-blank-lines

dabrazhe15:08:02

Can I make the bb repl share the same environment with current shell? eg When I export AWS_SECRET_ACCESS_KEY="5beQdsugRmMy1" I'd like the bb repl to be aware of it.

borkdude15:08:56

export FOO=1 bb repl ?

dabrazhe16:08:34

This'd mean I have to restart the repl every time I change the variables. I am looking for a more scalable option @borkdude

borkdude17:08:20

Unfortunately the JVM doesn't let you change environment variables within the same process, this also applies to bb

borkdude17:08:27

You can however spawn new processes from within bb with env vars

dabrazhe10:08:28

Is there a way to 'persist' the process environment, so that i can issue repl functions calls outside of the process form?

borkdude10:08:30

System/getenv gives you the entire map

dabrazhe10:08:01

Ok. How can I do this correctly, it fails ?

(-> (process ["export" "FOO" "1"] {:out :string}) check :out str/split-lines first)

borkdude10:08:50

(process [...cmd...] {:extra-env {"FOO" "1"}})

borkdude10:08:28

export is a bash thing. process doesn't start bash necessarily

👍 3
borkdude10:08:31

unless you tell it to

borkdude10:08:05

you can find the process docs over here: https://github.com/babashka/process

borkdude11:08:07

Example:

$ bb -e '(keys (edn/read-string (:out @(babashka.process/process ["bb" "-e" "(System/getenv)"] {:out :string :err :string :env (assoc (select-keys (System/getenv) ["PATH"]) "FOO" "bar")}))))'
("__CF_USER_TEXT_ENCODING" "PATH" "FOO")

borkdude11:08:49

There is :env which replaces the environment variables and :extra-env which only adds environment variables (or replaces existing ones)

dabrazhe11:08:17

Great, thanks a lot!

Adam Stokes16:08:41

@borkdude are you worried that nbb would take away from adoption of babashka?

Adam Stokes16:08:03

I wonder if making babashka support pluggable backends would be helpful

Adam Stokes16:08:17

bb --provider=nodejs or something similar

borkdude17:08:54

@adam.stokes I'm not worried about that at all. bb is to Clojure JVM what nbb is to CLJS on Node.JS. A convenient fast starting environment for scripting without needed to build anything. For most things bb is sufficient, but there are sometimes situations in which a library e.g. for Excel or whatever is needed which isn't in bb, in those cases you can perhaps consider nbb.

👍 6
3
lispyclouds17:08:16

Also bb still remains as the only true multithreaded scripting environment across any language! babashka

💯 21