Skip to content

Commit d0a3f64

Browse files
committed
Add babashka CLI explorations
1 parent d0aba07 commit d0a3f64

File tree

7 files changed

+103
-37
lines changed

7 files changed

+103
-37
lines changed

bb/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Basic scripting
2+
* Entrypoints are defined as tasks in bb.edn.
3+
4+
```
5+
bb nrepl-server 1667
6+
```
7+
8+
Nice walkthrough of conjure:
9+
```
10+
:ConjureSchool
11+
```

bb/bb.edn

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
{:paths [""]
2-
:tasks {
3-
default (prn "Hello, World!")
4-
findenv hello/venv-test
5-
}
6-
}
2+
:tasks {venv-find-activate-script dot.cli/venv-find-activate-script
3+
venv-find dot.cli/venv-find}}

bb/bb.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function source-env {
2+
local from_dir=${1:-$PWD}
3+
local activate_file="$(bb venv-find-activate-script ${from_dir})"
4+
if [[ -f $activate_file ]]; then
5+
echo "Sourcing ${activate_file}"
6+
source $activate_file
7+
else
8+
echo "No virtualenv found"
9+
fi
10+
}

bb/dot/cli.clj

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
(ns dot.cli
2+
(:require [clojure.tools.cli :refer [parse-opts]]
3+
[clojure.pprint]
4+
[dot.venv]))
5+
6+
;; CLI adapters
7+
(defn venv-find-activate-script [& args]
8+
(let [path (first args)]
9+
(-> path dot.venv/find-env :activate-path print)))
10+
11+
(defn venv-find [& args]
12+
(let [path (first args)]
13+
(-> path dot.venv/find-env clojure.pprint/pprint)))
14+
15+
(def cli-options
16+
;; An option with a required argument
17+
[["-p" "--port PORT" "Port number"
18+
:default 80
19+
:parse-fn #(Integer/parseInt %)
20+
:validate [#(< 0 % 0x10000) "Must be a number between 0 and 65536"]]
21+
;; A non-idempotent option (:default is applied first)
22+
["-v" nil "Verbosity level"
23+
:id :verbosity
24+
:default 0
25+
:update-fn inc] ; Prior to 0.4.1, you would have to use:
26+
;; :assoc-fn (fn [m k _] (update-in m [k] inc))
27+
;; A boolean option defaulting to nil
28+
["-h" "--help"]])
29+
30+
(defn -main [& args]
31+
(let [opts (parse-opts args cli-options)]
32+
(print opts)))

bb/dot/util.clj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
(ns dot.util
2+
(:require [babashka.fs :as fs]))
3+
4+
(defn pwd []
5+
(System/getProperty "user.dir"))
6+
7+
(defn ancestors-and-self [path]
8+
(if (nil? path)
9+
[]
10+
(lazy-seq (cons (str path) (ancestors-and-self (fs/parent path))))))

bb/dot/venv.clj

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
(ns dot.venv
2+
(:require [dot.util :refer [pwd ancestors-and-self]]
3+
[babashka.fs :as fs]
4+
[babashka.process :refer [shell]]))
5+
6+
(defn- resolve-path [path]
7+
(if (nil? path) (pwd) path))
8+
9+
(defn dir-candidates [dirseq]
10+
(for [dir dirseq
11+
env-dir-name [".venv" "venv" "env"]]
12+
(str dir "/" env-dir-name)))
13+
14+
(defn env-binary-path [env-path binary]
15+
(str env-path "/bin/" binary))
16+
17+
(defn python-site-packages-dir [python-exe]
18+
(let [python-exe (if (nil? python-exe) "python" python-exe)
19+
python-code "import site; print(site.getsitepackages()[-1])"
20+
process (shell {:out :string :err :string} python-exe "-c" python-code)]
21+
(:out process)))
22+
23+
(defn find-env [path]
24+
(let [path (resolve-path path)
25+
dirseq (ancestors-and-self path)
26+
env (->> dirseq
27+
(dir-candidates)
28+
(filter #(fs/exists? (env-binary-path % "activate")))
29+
first)]
30+
(if env
31+
{:root (str (fs/parent env))
32+
:venv-path env
33+
:activate-path (env-binary-path env "activate")
34+
:python-exe-path (env-binary-path env "python")
35+
:site-packages-dir (python-site-packages-dir (env-binary-path env "python"))}
36+
nil)))
37+
38+
;; (python-site-packages-dir nil)

bb/hello.bb

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)