-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.clj
155 lines (137 loc) · 5.17 KB
/
build.clj
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
150
151
152
153
154
155
(ns build
(:require
[babashka.process :as p]
[babashka.process.pprint]
[clojure.java.io :as io]
[clojure.string :as str]
[clojure.tools.build.api :as b]))
(def lib 'codes.stel/nuzzle)
(def version (str (-> ".VERSION_PREFIX" slurp str/trim) "." (b/git-count-revs nil)))
(def class-dir "target/classes")
(def basis (b/create-basis {:project "deps.edn"}))
(def uber-file (format "target/%s-%s-standalone.jar" (name lib) version))
(def jar-file (format "target/%s-%s.jar" (name lib) version))
(def git-tag (str "v" version))
(defn render-templates [& _]
(println "Rendering templates")
(letfn [(slurp-match [matches]
(-> matches
second
slurp
str/trim))
(render-template [file]
(spit (str/replace file ".template" "")
(-> file
slurp
(str/replace #"\{\{(\S+)\}\}" slurp-match))))]
(render-template "README.template.md"))
(println "Finished rendering templates"))
(comment (render-templates))
(defn get-latest-version []
(->> @(p/process ["git" "describe" "--tags" "--abbrev=0"])
:out
slurp
(re-find #"[0-9\.]+")))
(comment (get-latest-version))
(defn update-example-deps []
(println "Updating example deps.edn files")
(let [latest-version (get-latest-version)]
(->> (io/file "examples")
file-seq
(filter #(= "deps.edn" (.getName %)))
(reduce
(fn [_ file]
(spit file
(str/replace-first
(slurp file)
#"codes\.stel/nuzzle \{:mvn/version \"[0-9\.]+\"\}"
(str "codes.stel/nuzzle {:mvn/version \"" latest-version "\"}"))))
nil)))
(when (-> @(p/process ["git" "diff" "--quiet"]) :exit (= 1))
(println "Commiting docs changes")
(p/check (p/process ["git" "commit" "--all" "-m" "Update Nuzzle version in example deps.edn"]
{:out :inherit :err :inherit})))
(println "Finished updating example deps.edn files"))
(comment (update-example-deps))
(defn ensure-clean-tree [& _]
(println "Checking if working tree is clean")
(try
;; Check if working tree has staged changes
(p/check (p/process ["git" "diff-index" "--quiet" "--cached" "HEAD" "--"]))
;; Check if working tree has meaningful changes that could be staged
;; Not using git diff-files here bc it returns 0 when file metadata changed
(p/check (p/process ["git" "diff" "--quiet"]))
(catch Throwable e
(println "Working tree is dirty")
(throw e)))
(println "Working tree is clean"))
(defn ensure-tests [& _]
(println "Checking if tests pass")
(p/check (p/process ["bb" "clojure" "-M:test"] {:out :inherit :err :inherit}))
(println "Tests are passing"))
(defn ensure-lint [& _]
(println "Checking for linter warnings")
(p/check (p/process ["bb" "clojure" "-M:clj-kondo" "--lint" "src" "test"] {:out :inherit :err :inherit}))
(println "No linter warnings"))
(defn tag-latest [& _]
(println "Attempting to tag" git-tag)
(if (= git-tag (-> @(p/process ["git" "describe" "--tags"]) :out slurp str/trim))
(println "HEAD already has tag" git-tag)
(do (println "Tagging latest commit")
(p/check (p/process ["git" "tag" "-a" "-m" (str "Bump version to " version) git-tag]
{:out :inherit :err :inherit}))
(println "Tagged latest commit with" git-tag))))
(defn update-templated-files [& _]
(println "Updating templated files")
(ensure-clean-tree)
(render-templates)
(when (-> @(p/process ["git" "diff" "--quiet"]) :exit (= 1))
(println "Commiting templated file changes")
(p/check (p/process ["git" "commit" "--all" "-m" "Update templated files"]
{:out :inherit :err :inherit})))
(println "Finished updating templated files"))
(defn push-commits-and-tags [& _]
(println "Pushing commits")
(ensure-clean-tree)
(p/check (p/process ["git" "push" "origin"] {:out :inherit :err :inherit}))
(println "Pushing tags")
(p/check (p/process ["git" "push" "--tags" "origin"] {:out :inherit :err :inherit}))
(println "Finished pushing commits and tags"))
(defn clean [_]
(b/delete {:path "target"}))
(defn jar [_]
(clean nil)
(b/write-pom {:class-dir class-dir
:lib lib
:version version
:basis basis
:src-dirs ["src"]})
(b/copy-dir {:src-dirs ["src" "resources"]
:target-dir class-dir})
(b/jar {:class-dir class-dir
:jar-file jar-file}))
(defn uber [_]
(clean nil)
(b/copy-dir {:src-dirs ["src" "resources"]
:target-dir class-dir})
(b/compile-clj {:basis basis
:src-dirs ["src"]
:class-dir class-dir})
(b/uber {:class-dir class-dir
:uber-file uber-file
:basis basis}))
(defn deploy [opts]
(ensure-clean-tree)
(ensure-lint)
(ensure-tests)
(update-templated-files)
(tag-latest)
(jar opts)
((requiring-resolve 'deps-deploy.deps-deploy/deploy)
(merge {:installer :remote
:artifact jar-file
:pom-file (b/pom-path {:lib lib :class-dir class-dir})}
opts))
(update-example-deps)
(push-commits-and-tags)
opts)