Skip to content

Commit 8931f93

Browse files
authored
Merge pull request #22 from metabase/bb-dev-facelift
bb dev tasks facelift
2 parents 26e77c0 + 6441d46 commit 8931f93

File tree

8 files changed

+102
-151
lines changed

8 files changed

+102
-151
lines changed

β€Žbb.ednβ€Ž

Lines changed: 63 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
{:min-bb-version "0.9.162"
2-
:paths ["bb"]
2+
:paths ["."]
33
:deps {escherize/bask {:git/url "https://github.com/escherize/bask.git"
44
:git/sha "81cc9af3021d7689cfbddf0518a1e828f785f006"}
55
table/table {:mvn/version "0.5.0"}}
6-
:tasks {:requires [[bask.colors :as c]
6+
:tasks {:requires [[babashka.process :refer [shell]]
7+
[bask.colors :as c]
78
[bb.cli :as cli]
89
[bb.tasks :as t]
910
[clojure.string :as str]]
@@ -28,64 +29,75 @@
2829
{:id :port :msg "What port should we run it on?" :short "-p" :long "--port PORT" :prompt :number :default 3337}
2930
{:id :socket-repl :msg "What port shall we open a socket repl on?" :short "-s" :long "--socket-repl SOCKETPORT"}))))}
3031

31-
check-branch
32-
{:requires [[bb.dl-and-run :as dl]]
33-
:doc "Check the status of a branch in CI."
34-
:examples [["bb check-branch --branch master" "Check master branch github checks"]]
35-
:task
36-
(do (dl/check-gh-token!)
37-
(let [branch-info (cli/menu! (current-task)
38-
{:id :branch
39-
:msg "What branch should we check?"
40-
:short "-b"
41-
:long "--branch BRANCH"
42-
:choices (delay (t/list-branches (t/mb-env)))
43-
:choices-doc "list of branches"
44-
:prompt :select}
45-
{:id :watch?
46-
:msg "Watch until success?"
47-
:short "-w"
48-
:long "--watch"})]
49-
(if (:watch? branch-info)
50-
(loop []
51-
(let [check (dl/checks-for-branch branch-info)]
52-
(println (str "\n----------[ "
53-
(.format (java.text.SimpleDateFormat. "hh:mm:ss a") (java.util.Date.))
54-
" ]"))
55-
(doseq [[s c] check]
56-
(println (dl/pretty s) s (str "(" c ") ")))
57-
(when-not (= (keys check) [:success])
58-
(Thread/sleep 5000)
59-
(recur))))
60-
(doseq [[s c] (dl/checks-for-branch branch-info)]
61-
(println (dl/pretty s) s (str "(" c ") "))))))}
62-
6332
metabuild
6433
{:doc "Starts metabase locally in dev mode. Set FORCE_MB_DB_CONNECTION_URI to override connection string building"
6534
:requires [[bb.meta :as meta]]
6635
:examples [["FORCE_MB_DB_CONNECTION_URI=mysql://localhost:3308/metabase_test?user=root bb metabuild -d mysql"
6736
"Connect to MYSQL, running against run-mariadb-latest.sh"]]
68-
:task (let [{:keys [app-db user-name password extensions] :as p}
37+
:task (let [_ (println (c/red "Welcome to " (c/on-white (c/blue " MetaBuilder "))))
38+
{:keys [app-db user-name password extensions] :as p}
6939
(cli/menu! (current-task)
70-
{:id :app-db :short "-d" :long "--database-type DB" :title "Which DB should we use for metabase's data?" :choices ["postgres" "h2" "mysql"] :required? true :prompt :select}
71-
{:id :user-name :short "-u" :long "--username USER" :default (t/whoami)}
72-
{:id :password :short "-p" :long "--pw PW" :default "password"}
73-
{:id :extensions :short "-e" :long "--exensions EXT" :default ["dev" "ee" "ee-dev" "drivers" "drivers-dev" "cider/nrepl"] :prompt :multi}
74-
{:id :db-name :short "-n" :long "--name DB_NAME" :default "metabase" :required? true :title "Name of the database to connect to." :prompt :text})]
40+
{:id :app-db
41+
:short "-d"
42+
:long "--database-type DB"
43+
:title "Pick Metabase's app-db"
44+
:choices ["postgres" "h2" "mysql"]
45+
:prompt :select}
46+
{:id :user-name :short "-u" :long "--username USER" :default (t/whoami)}
47+
{:id :password :short "-p" :long "--pw PW" :default "password"}
48+
{:id :extensions :short "-e" :long "--extensions EXT" :default ["dev" "ee" "ee-dev" "drivers" "drivers-dev" "cider/nrepl"] :prompt :multi}
49+
{:id :db-name
50+
:short "-n"
51+
:long "--name DB_NAME"
52+
:default "metabase"
53+
:title "Name of the database to connect to."
54+
:prompt :text})]
7555
;; (prn p)
7656
(meta/build app-db user-name password extensions))}
7757

78-
quick-test {:doc "Quickly run a test against a namespace."
79-
:requires [[bb.quick-test :as qt]]
80-
:task (qt/go!
81-
(:test-namespaces (cli/menu! (current-task)
82-
{:id :test-namespaces
83-
:msg "What namespace(s) to test?"
84-
:long "--ns NS"
85-
:required? true
86-
:prompt :multi
87-
:choices (delay (qt/test-nss))
88-
:choices-doc "a list of clojure test namespaces"})))}
58+
quick-test
59+
{:doc "Quickly run a test against a namespace."
60+
:requires [[bb.quick-test :as qt]]
61+
:task (qt/go!
62+
(:test-namespaces (cli/menu! (current-task)
63+
{:id :test-namespaces
64+
:msg "What namespace(s) to test?"
65+
:long "--ns NS"
66+
:prompt :multi
67+
:choices (delay (qt/test-nss))
68+
:choices-doc "a list of clojure test namespaces"})))}
69+
70+
watch-ci
71+
{:requires [[bb.dl-and-run :as dl]
72+
[clojure.pprint :as pp]]
73+
:doc "Watch the CI status for a branch until it passes."
74+
:examples [["bb watch-ci --branch current" "Check currently checked out branch in MB_DIR repo"]]
75+
:task
76+
(do (dl/check-gh-token!)
77+
(let [{:keys [branch]} (cli/menu! (current-task)
78+
{:id :branch
79+
:msg "What branch should we check?"
80+
:short "-b"
81+
:long "--branch BRANCH"
82+
:choices (delay (t/list-branches (t/mb-env)))
83+
:choices-doc "list of branches"
84+
:prompt :select})
85+
branch (if (= "current" branch)
86+
(let [current (str/trim
87+
(:out (shell {:dir (t/env "MB_DIR" (fn []
88+
(println "Please set MB_DIR to your metabase repo!")
89+
(System/exit 1))) :out :string}
90+
"git rev-parse --abbrev-ref HEAD")))]
91+
(do (println (c/green (c/bold "Checking current branch: " current)))
92+
current))
93+
branch)]
94+
(pp/pprint dl/pretty)
95+
(loop []
96+
(let [check (dl/checks-for-branch branch)]
97+
(print (str "\n[ " (.format (java.text.SimpleDateFormat. "hh:mm:ss a") (java.util.Date.)) " ]"))
98+
(doseq [[status count] check] (print (str/join (repeat count (dl/pretty status)))) (print "|")) (flush)
99+
(when-not (= (keys check) [:success])
100+
(Thread/sleep 10000) (recur))))))}
89101

90102
install-autotab {:doc "Prints shell code to autocomplete tasks using bb.
91103
Note: for fish shell please make sure ~/.config/fish/completions exists."
@@ -96,7 +108,6 @@
96108
(cli/menu! (current-task)
97109
{:id :shell-type
98110
:long "--shell SHELL"
99-
:required? true
100111
:title "What kind of shell?"
101112
:choices ["zsh" "bash" "fish"]
102113
:prompt :select})]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
(ns bb.build-and-run-jar-locally)
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@
4141
(println (c/cyan " accepts no command line arguments.")))
4242
(System/exit 0)))
4343

44-
(defn ->ask [{:keys [id msg prompt choices] :as _option}]
44+
(defn ->ask [{:keys [id title prompt choices] :as _option}]
4545
{:id id
46-
:msg msg
46+
:msg title
4747
:type prompt
4848
:choices (if (delay? choices) @choices choices)})
4949

@@ -73,8 +73,10 @@
7373
(System/exit 1))
7474
asked-opts (into {} (for [hybrid-option missing-and-askable]
7575
(println "todo: ask (menu-ask hybrid-option)" (pr-str hybrid-option))))
76-
cli (assoc (merge parsed-opts asked-opts) :args arguments)]
77-
(ask-unknown! cli opts)))
76+
cli (assoc (merge parsed-opts asked-opts) :args arguments)
77+
out (ask-unknown! cli opts)]
78+
(println out)
79+
out))
7880

7981
(defn add-parsing-for-multi [option]
8082
(if (= :multi (:prompt option))
Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
(ns bb.dl-and-run
2-
(:require [babashka.tasks :refer [shell]]
3-
[babashka.curl :as curl]
4-
[bask.colors :as c]
5-
[bb.tasks :as t]
6-
[selmer.parser :refer [<<]]
7-
[cheshire.core :as json]
8-
[clojure.edn :as edn]))
2+
(:require
3+
[babashka.curl :as curl]
4+
[babashka.tasks :refer [shell]]
5+
[bask.colors :as c]
6+
[bb.tasks :as t]
7+
[cheshire.core :as json]
8+
[clojure.edn :as edn]
9+
[clojure.string :as str]
10+
[selmer.parser :refer [<<]]))
911

1012
(defn- keep-first
1113
"like (fn [f coll] (first (keep f coll))) but does not do chunking."
1214
[f coll]
13-
(reduce (fn [_ element]
14-
(when-let [resp (f element)] (reduced resp)))
15-
nil
16-
coll))
15+
(reduce (fn [_ element] (when-let [resp (f element)] (reduced resp))) nil coll))
1716

1817
(defn- gh-get [url]
1918
(try (-> url
@@ -79,14 +78,15 @@
7978
(println (c/bold "Be sure to tick the *repo* permission."))
8079
(System/exit 1))))
8180

82-
(defn download-and-run-latest-jar! [{:keys [branch port socket-repl]}]
81+
(defn download-latest-jar! [{:keys [branch]}]
8382
(let [finished (t/wait (str "Finding uberjar for branch" (c/green branch)) "πŸ“ž")
8483
{artifact-id :id
8584
created-at :created_at
8685
dl-url :archive_download_url
8786
sha :head_sha
88-
:as info} (branch->latest-artifact branch)
89-
branch-dir (str download-dir branch)]
87+
:as info_} (branch->latest-artifact branch)
88+
branch-dir (str download-dir branch)
89+
info (into (sorted-map) (assoc info_ :branch branch :branch-dir branch-dir))]
9090
(finished)
9191
(println (c/cyan "Found latest artifact!"))
9292
(println (c/magenta (str " git SHA: " (c/green sha))))
@@ -99,6 +99,7 @@
9999
;; - _or_ use the older artifact?
100100
(println (c/magenta (str " Download Url: " (c/green dl-url))))
101101
(prn info)
102+
(println "Download directory: " branch-dir)
102103
(shell (str "mkdir -p " branch-dir))
103104
(if (= (try (edn/read-string (slurp (str branch-dir "/info.edn")))
104105
(catch Throwable _ ::nothing-there))
@@ -109,6 +110,10 @@
109110
(download-mb-jar! branch-dir dl-url)))
110111
(println "Artifact download complete.")
111112
(spit (str branch-dir "/info.edn") info)
113+
info))
114+
115+
(defn run-jar! [info port socket-repl]
116+
(let [{:keys [branch branch-dir]} info]
112117
(println "Unzipping artifact...")
113118
(try
114119
(shell {:dir branch-dir :out nil} "unzip -o metabase.zip")
@@ -133,13 +138,17 @@
133138
(t/print-env "mb" env+)
134139
(shell {:dir branch-dir :out :inherit :env env+} cmd))))
135140

141+
(defn download-and-run-latest-jar! [{:keys [branch port socket-repl] :as args}]
142+
(let [info (download-latest-jar! args)]
143+
(run-jar! info port socket-repl)))
144+
136145
(def pretty {:success "βœ…"
137146
:skipped "⏭️ "
138-
:cancelled "⏹️ "
147+
:cancelled "⏹️"
139148
:in-progress "πŸ”„"
140149
:failure "❌"})
141150

142-
(defn checks-for-branch [{branch :branch}]
151+
(defn checks-for-branch [branch]
143152
;; note: this is a ref, so it can e.g. also be a sha.
144153
(->> (str "https://api.github.com/repos/metabase/metabase/commits/" branch "/check-runs")
145154
gh-get
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,20 @@
2222
(println (c/green "[bb metabuild] βœ… Done."))))
2323

2424
(defn build [app-db user-name password extensions]
25-
(let [env+ (assoc (t/env) "MB_DB_CONNECTION_URI"
25+
(let [env+ (assoc (t/env)
26+
"MB_DB_CONNECTION_URI"
2627
(or (t/env "FORCE_MB_DB_CONNECTION_URI" (constantly false))
2728
(case app-db
28-
"postgres" (str "postgres://" user-name ":" password "@localhost:5432/metabase")
2929
"mysql" (str "mysql://" user-name ":" password "@localhost:3306/metabase_test")
30-
"h2" "" )))
30+
"postgres" (str "postgres://" user-name ":" password "@localhost:5432/metabase")
31+
"h2" "" ))
32+
"MB_DB_TYPE" app-db)
3133
cmd (str "clj -M" (str/join (map (fn [s-or-kw] (keyword (name s-or-kw))) extensions)))]
3234
(println (:out (shell {:out :string} "java -version")))
3335
(t/print-env "mb" env+)
3436
(println (c/green "\n ==== Starting Metabase with: ==== \n"))
3537
(println (c/green cmd))
3638
(println (c/green "\n ================================= \n"))
37-
(println (c/magenta "In directory: " (t/env "MB_DIR")))
39+
(println (c/magenta "In directory πŸ“‚:" (t/env "MB_DIR")))
3840
(future (listen-for-nrepl-and-init!))
3941
(shell {:extra-env env+ :dir (t/env "MB_DIR")} cmd)))
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

3636
(defn list-branches [mb-dir]
3737
(git-fetch mb-dir)
38-
(letfn [(remove-origin [b] (str/replace (str/trim b) (re-pattern "^origin/") ""))]
38+
(let [remove-origin (fn remove-origin [b] (str/replace (str/trim b) (re-pattern "^origin/") ""))]
3939
(mapv remove-origin
4040
(str/split-lines
4141
(->> "git branch -r" (shell {:dir mb-dir :out :string}) :out)))))

β€Žwatch_ci.cljβ€Ž

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

0 commit comments

Comments
Β (0)