-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathbuild.clj
285 lines (250 loc) · 9.96 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
;!zprint {:style [:respect-nl]}
(ns build
"Build scripts for clj-otel-* libraries, examples and tutorials.
For example, to lint all clj-otel-* libraries:
clojure -T:build lint
To see a description of all build scripts:
clojure -A:deps -T:build help/doc"
(:require [clojure.java.io :as io]
[clojure.string :as str]
[clojure.tools.build.api :as b]
[clojure.tools.deps :as t]
[deps-deploy.deps-deploy :as dd]
[org.corfield.log4j2-conflict-handler :refer [log4j2-conflict-handler]])
(:import (java.nio.file FileSystems)))
(def ^:private version
"0.2.8-SNAPSHOT")
;; Later artifacts in this vector may depend on earlier artifacts
(def ^:private library-project-paths
["clj-otel-api"
"clj-otel-sdk-common"
"clj-otel-sdk"
"clj-otel-contrib-aws-resources"
"clj-otel-contrib-aws-xray-propagator"
"clj-otel-sdk-extension-autoconfigure"
"clj-otel-sdk-extension-jaeger-remote-sampler"
"clj-otel-extension-trace-propagators"
"clj-otel-exporter-logging"
"clj-otel-exporter-logging-otlp"
"clj-otel-exporter-otlp"
"clj-otel-exporter-prometheus"
"clj-otel-exporter-zipkin"
"clj-otel-instrumentation-resources"
"clj-otel-instrumentation-runtime-telemetry-java8"
"clj-otel-instrumentation-runtime-telemetry-java17"])
(def ^:private demo-project-paths
["examples/common/core-async.utils"
"examples/common/interceptor.utils"
"examples/common/load-gen"
"examples/common/log4j2.utils"
"examples/common/system"
"examples/countries-service"
"examples/cube-app"
"examples/divisor-app"
"examples/factorial-app"
"examples/microservices/auto-instrument/interceptor/planet-service"
"examples/microservices/auto-instrument/interceptor/solar-system-load-gen"
"examples/microservices/auto-instrument/interceptor/solar-system-service"
"examples/microservices/auto-instrument/middleware/sentence-summary-load-gen"
"examples/microservices/auto-instrument/middleware/sentence-summary-service"
"examples/microservices/auto-instrument/middleware/word-length-service"
"examples/microservices/manual-instrument/interceptor/average-load-gen"
"examples/microservices/manual-instrument/interceptor/average-service"
"examples/microservices/manual-instrument/interceptor/sum-service"
"examples/microservices/manual-instrument/middleware/puzzle-load-gen"
"examples/microservices/manual-instrument/middleware/puzzle-service"
"examples/microservices/manual-instrument/middleware/random-word-service"
"examples/rpg-service"
"examples/square-app"
"tutorial/instrumented"])
(defn- library-project?
[root-path]
(some #{root-path} library-project-paths))
(def ^:private project-paths
(concat library-project-paths
demo-project-paths))
(defn- group-id
[root-path]
(if (library-project? root-path)
"com.github.steffan-westcott"
"org.example"))
(defn- artifact-id
[root-path]
(re-find #"[^/]+$" root-path))
(defn- group-artifact-id
[group-id artifact-id]
(str group-id "/" artifact-id))
(def ^:private group-artifact-ids
(map #(group-artifact-id (group-id %) (artifact-id %)) project-paths))
(def ^:private snapshot?
(str/ends-with? version "-SNAPSHOT"))
(def ^:private head-sha-1
(delay (b/git-process {:git-args "rev-parse HEAD"})))
(defn- artifact-opts
[{:keys [aliases artifact-id group-id root-path tag]}]
{:artifact-id artifact-id
:basis (b/create-basis {:aliases aliases})
:class-dir "target/classes"
:conflict-handlers log4j2-conflict-handler
:group-id group-id
:jar-file (format "target/%s-%s.jar" artifact-id version)
:lib (symbol group-id artifact-id)
:resource-dirs ["resources"]
:root-path root-path
:scm {:tag tag}
:src-dirs ["src"]
:src-pom "template/pom.xml"
:target-dir "target"
:version version})
(defn- project-artifact-opts
[root-path]
(artifact-opts {:artifact-id (artifact-id root-path)
:aliases (when
(library-project? root-path)
[(if snapshot?
:snapshot
:release)])
:group-id (group-id root-path)
:root-path root-path
:tag (when (library-project? root-path)
@head-sha-1)}))
(defn- glob-match
"Returns a predicate which returns true if a single glob `pattern` matches a
`Path` arg and false otherwise. If given several patterns, returns true if
any match and false if none match."
([pattern]
(let [matcher (.getPathMatcher (FileSystems/getDefault) (str "glob:" pattern))]
#(.matches matcher %)))
([pattern & pats]
(let [f (apply some-fn (map glob-match (cons pattern pats)))]
#(boolean (f %)))))
(defn- file-match
"Returns path strings of files in directory tree `root`, with relative paths
filtered by `pred`.
Example: Match all *.clj files in directory tree `src`
`(file-match \"src\" (glob-match \"**.clj\"))`"
[root pred]
(let [root-file (io/file root)
root-path (.toPath root-file)]
(->> root-file
file-seq
(filter #(.isFile %))
(filter #(pred (.relativize root-path (.toPath %))))
(map #(str (.normalize (.toPath %)))))))
(defn- globs
"Returns path strings of files in directory tree `root` with relative paths
matching any of glob `patterns`."
[root & patterns]
(file-match root (apply glob-match patterns)))
(defn- checked-process
[params]
(let [result (b/process params)]
(if (zero? (:exit result))
result
(throw (ex-info "Process returned non-zero exit code" (assoc result :params params))))))
(defn- run-task
"Run a task based on aliases."
[{:keys [main-opts]} aliases]
(let [task (str/join ", " (map name aliases))
_ (println "Running task for:" task)
basis (b/create-basis {:aliases aliases})
combined (t/combine-aliases basis aliases)
command (b/java-command
{:basis basis
:java-opts (:jvm-opts combined)
:main 'clojure.main
:main-args (into (:main-opts combined) main-opts)})]
(checked-process command)))
(defn- clean-artifact
[{:keys [target-dir]}]
(println "Cleaning build dir" target-dir "...")
(b/delete {:path target-dir}))
(defn- jar-artifact
[{:keys [class-dir jar-file resource-dirs src-dirs]
:as opts}]
(clean-artifact opts)
(println "Writing pom.xml ...")
(b/write-pom opts)
(println "Building jar" jar-file "...")
(b/copy-dir {:src-dirs (into src-dirs resource-dirs)
:target-dir class-dir})
(b/jar opts))
(defn- install-artifact
[{:keys [group-id artifact-id]
:as opts}]
(jar-artifact opts)
(println "Installing" (group-artifact-id group-id artifact-id) "...")
(b/install opts))
(defn- deploy-artifact
[{:keys [group-id artifact-id jar-file]
:as opts}]
(install-artifact opts)
(println "Deploying" (group-artifact-id group-id artifact-id) "...")
(dd/deploy {:artifact (b/resolve-path jar-file)
:installer :remote
:pom-file (b/pom-path opts)}))
(defn- tag-release
[tag]
(println "Creating and pushing tag" tag)
(checked-process {:command-args ["git" "tag" "-a" "-m" (str "Release " tag) tag]})
(checked-process {:command-args ["git" "push" "origin" tag]}))
(defn clean
"Delete all build directories."
[_]
(doseq [root-path project-paths]
(b/with-project-root root-path
(clean-artifact {:target-dir "target"}))))
(defn install
"Build all clj-otel-* library JAR files then install them in the local Maven
repository. The libraries are processed in an order such that later libraries
may depend on earlier ones."
([_] (install _ library-project-paths))
([_ root-paths]
(doseq [root-path root-paths]
(b/with-project-root root-path
(install-artifact (project-artifact-opts root-path))))))
(defn deploy
"Build all clj-otel-* library JAR files, install them in the local Maven
repository and deploy them to Clojars. The libraries are processed in an order
such that later libraries may depend on earlier ones.
For non-SNAPSHOT versions, a git tag with the version name is created and
pushed to the origin repository."
[_]
(doseq [root-path library-project-paths]
(b/with-project-root root-path
(deploy-artifact (project-artifact-opts root-path))))
(when-not snapshot?
(tag-release version)))
(defn lint
"Lint all clj-otel-* libraries, example applications and tutorial source
files using clj-kondo. Assumes a working installation of `clj-kondo`
executable binary."
[_]
(let [paths (->> project-paths
(mapcat #(list (str % "/src") (str % "/dev")))
(filter #(.isDirectory (io/file %))))]
(checked-process {:command-args (concat ["clj-kondo" "--lint" "build.clj"] paths)})))
(defn outdated
"Check all clj-otel-* libraries, example applications and tutorials for
outdated dependencies using antq. Dependencies on clj-otel-* projects are
not checked."
[_]
(run-task {:main-opts
["--directory" (str/join ":" project-paths)
"--skip" "pom"
"--exclude" (str/join ":" group-artifact-ids)
"--no-changes"]}
[:antq]))
(defn fmt
"Apply formatting to all *.clj and *.edn source files using zprint. Assumes a
working installation of `zprint` executable binary."
[_]
(let [project-files (mapcat #(globs % "src/**.clj" "dev/**.clj" "*.edn" "resources/**.edn")
project-paths)
other-files (globs "." "*.clj" "*.edn" ".clj-kondo/config.edn" "doc/**.edn")
files (concat project-files other-files)
config-url (-> ".zprint.edn"
io/file
io/as-url
str)]
(checked-process {:command-args (concat ["zprint" "--url-only" config-url "-fsw"] files)})))