-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbb.edn
149 lines (128 loc) · 6.77 KB
/
bb.edn
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
{:deps {cldwalker/bb-clis {:git/url "https://github.com/cldwalker/bb-clis"
:sha "393a97cb55be57c6b9312ccec5959e247f282fdb"
:deps/manifest :deps}}
;; For local development
;; :deps {cldwalker/bb-clis {:local/root "../bb-clis"}}
:tasks
{:requires ([babashka.fs :as fs]
[cldwalker.bb-clis.babashka.task :as task]
[clojure.string :as str])
:init (do
;; Define helpers
(defn -exists? [path]
(let [ret (fs/exists? path)]
(when ret (println "Path" (str path) "already installed"))
ret))
(def repo-paths
"Repo paths relative to $HOME"
(merge
{"cldwalker/hammerspoon-files" ".hammerspoon"
"cldwalker/keyboard" ".keyboard"
;"cldwalker/atomfiles" "code/repo/atomfiles"
"cldwalker/logseq-config" "code/repo/logseq-config"
"cldwalker/bb-clis" "code/repo/bb-clis"
"cldwalker/nbb-clis" "code/repo/nbb-clis"
"cldwalker/logseq-clis" "code/repo/logseq-clis"
"cldwalker/dotfiles" "code/repo/dotfiles"
"cldwalker/irbfiles" "code/repo/irbfiles"
"cldwalker/vimfiles" "code/repo/vimfiles"}
;; Allow for repos with differing paths and private repos
(when (fs/exists? "repos.edn")
(-> "repos.edn" slurp clojure.edn/read-string))))
(defn -github-repo [gh-repo]
(str "[email protected]:" gh-repo ".git"))
(defn- clone-repo-and-setup
([gh-repo] (clone-repo-and-setup gh-repo (fn [_])))
([gh-repo setup-fn]
(let [path (str (fs/path (System/getenv "HOME") (repo-paths gh-repo)))]
(when-not (-exists? path)
(shell (format "git clone %s %s" (-github-repo gh-repo) path))
(setup-fn path)))))
(defn- active-packages []
(keep (fn [l]
(let [pkg (second (re-find (re-pattern "^(?:cask|brew)\\s*'([^']+)'\\s*") l))]
;; This is really only for babashka
(when pkg
(or (second (re-find (re-pattern "/([^/]+)$") pkg))
pkg))))
(str/split-lines (slurp "Brewfile"))))
(defn- optional-packages
[]
(->> (slurp "optional-packages.edn")
clojure.edn/read-string
vals
flatten
(map name)))
)
:enter (def ^:dynamic *parsed-args*
(task/parse-options))
hammerspoon-repo {:doc "Install cldwalker/hammerspoon-files"
:task (clone-repo-and-setup "cldwalker/hammerspoon-files")}
keyboard-repo {:doc "Install cldwalker/keyboard"
:depends [hammerspoon-repo]
:task (clone-repo-and-setup "cldwalker/keyboard"
(fn [p] (shell {:dir p} "script/setup")))}
atomfiles-repo {:doc "Install cldwalker/atomfiles"
:task (clone-repo-and-setup
"cldwalker/atomfiles"
(fn [p] (shell {:dir p} "bb setup")))}
bb-clis-repo {:doc "Install cldwalker/bb-clis"
:task (clone-repo-and-setup "cldwalker/bb-clis")}
nbb-clis-repo {:doc "Install cldwalker/bb-clis"
:task (clone-repo-and-setup "cldwalker/nbb-clis")}
dotfiles-repo {:doc "Install cldwalker/dotfiles"
:task (clone-repo-and-setup "cldwalker/dotfiles"
(fn [p] (shell {:dir p} "ruby install.rb")))}
irbfiles-repo {:doc "Install cldwalker/irbfiles"
:task (clone-repo-and-setup
"cldwalker/irbfiles"
(fn [path]
(shell {:dir path} "bundle install --system")
(shell {:dir path} "ruby install.rb")))}
vimfiles-repo {:doc "Install cldwalker/vimfiles"
:task (clone-repo-and-setup "cldwalker/vimfiles"
(fn [p] (shell {:dir p} "rake install")))}
repo-update {:doc "Update/pull all repos"
:task (doseq [[gh-repo path] repo-paths]
(let [full-path (str (fs/path (System/getenv "HOME") path))]
;; Only print and git pull on behind repos
(when (zero? (:exit
(clojure.java.shell/sh
"bash" "-c" "git fetch origin && git status |grep 'branch is behind'"
:dir full-path)))
(println "Update" gh-repo "...")
(shell {:dir full-path} "git pull"))))}
repo-status {:doc "Check git status of all repos"
:task (doseq [[gh-repo path] repo-paths]
(let [full-path (str (fs/path (System/getenv "HOME") path))]
(let [status (:out (shell {:out :string :dir full-path} "git status"))]
(when-not (and (str/includes? status "branch is up to date")
(str/includes? status "nothing to commit"))
(print (str "Repo " full-path ":"))
(println "\n" status)))))}
autocomplete-packages
{:doc "List brew packages that have oh-my-zsh autocompletion"
:options [["-o" "--optional"]]
:requires ([clojure.set :as set])
:task (let [active (active-packages)
packages-to-analyze (if (get-in *parsed-args* [:options :optional])
(into active (optional-packages)) active)
autocomplete (->> (fs/list-dir (str (System/getenv "HOME") "/.oh-my-zsh/plugins"))
(map (fn [p] (-> p .getFileName str)))
set)]
(println (set/intersection autocomplete (set packages-to-analyze))))}
unknown-packages
{:doc "List brew packages that in Brewfile or optional-packages.edn"
:requires ([clojure.edn :as edn]
[clojure.set :as set])
:task (let [installed (map (fn [l]
(or (second (re-find (re-pattern "/([^/]+)$") l))
l))
(str/split-lines
(:out (shell {:out :string} "brew leaves"))))
active (active-packages)
optional (optional-packages)]
(println (set/difference (set installed) (set active) (set optional))))}
setup {:doc "Sets up all repositories for osx"
:depends [keyboard-repo dotfiles-repo bb-clis-repo atomfiles-repo
nbb-clis-repo irbfiles-repo vimfiles-repo]}}}