-
Notifications
You must be signed in to change notification settings - Fork 0
/
pom_patch.clj
281 lines (248 loc) · 10.2 KB
/
pom_patch.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
(ns ivarref.pom-patch
(:require [babashka.process :refer [$ check]]
[babashka.process.pprint]
[clojure.data.xml :as xml]
[clojure.java.io :as io]
[clojure.string :as str]
[clojure.walk :as walk]
[clojure.zip :as zip])
(:import (java.util.regex Pattern)))
; adapted from https://ravi.pckl.me/short/functional-xml-editing-using-zippers-in-clojure/
(defn tree-edit
"Take a zipper, a function that matches a pattern in the tree,
and a function that edits the current location in the tree. Examine the tree
nodes in depth-first order, determine whether the matcher matches, and if so
apply the editor."
[zipper matcher]
(loop [loc zipper]
(if (zip/end? loc)
(zip/root loc)
(if-let [matcher-result (matcher loc)]
(recur (zip/remove loc))
(recur (zip/next loc))))))
(defn simplify-node [n]
(walk/prewalk
(fn [x]
(if (keyword? x)
(keyword (name x))
x))
n))
(defn match-repository? [loc]
(let [{:keys [tag content]} (simplify-node (zip/node loc))]
(when (= :repository tag)
(->> content
(filter map?)
(filter #(= :url (:tag %)))
(first)
:content
(str/join "")
(str/trim)
(not= "https://repo.clojars.org/")))))
(defn clojars-repo-only! [{:keys [input-file output-file]
:or {input-file "pom.xml"
output-file "pom.xml"}}]
(with-open [input (io/input-stream (io/file input-file))]
(let [root (zip/xml-zip (xml/parse input))
new-content (->> (xml/indent-str (tree-edit root match-repository?))
(str/split-lines)
(remove (comp empty? str/trim))
(str/join "\n"))]
(spit output-file new-content))))
(defn tree-editor
"Take a zipper, a function that matches a pattern in the tree,
and a function that edits the current location in the tree. Examine the tree
nodes in depth-first order, determine whether the matcher matches, and if so
apply the editor."
[zipper matcher editor]
(loop [loc zipper]
(if (zip/end? loc)
(zip/root loc)
(if-let [matcher-result (matcher loc)]
(recur (zip/next (zip/edit loc editor)))
(recur (zip/next loc))))))
(defn match-version? [loc]
(let [{:keys [tag]} (simplify-node (zip/node loc))]
(and (= :version tag)
(= :project (:tag (simplify-node (zip/node (zip/up loc))))))))
(defn editor [new-patch node]
(let [curr-content (str/join "" (:content node))
[major minor _old-patch] (str/split curr-content #"\.")]
(assoc node :content (list (str major
"."
minor
"."
new-patch)))))
(defn file->current-version [input-file]
(with-open [input (io/input-stream (io/file input-file))]
(let [root (zip/xml-zip (xml/parse input))]
(->> root
(zip/children)
(remove string?)
(filter #(= :version (:tag (simplify-node %))))
(first)
:content
(str/join "")))))
(defn match-scm-tag? [loc]
(let [{:keys [tag]} (simplify-node (zip/node loc))]
(and (= :tag tag)
(= :scm (:tag (simplify-node (zip/node (zip/up loc))))))))
(defn update-tag! [version-prefix input-str]
(with-open [input (io/reader (char-array input-str))]
(let [root (zip/xml-zip (xml/parse input))
current-version (->> root
(zip/children)
(remove string?)
(filter #(= :version (:tag (simplify-node %))))
(first)
:content
(str/join ""))
new-content (->> (tree-editor root match-scm-tag?
(fn [node]
(assoc node :content (list (str version-prefix current-version)))))
(xml/indent-str)
(str/split-lines)
(remove (comp empty? str/trim))
(str/join "\n"))]
new-content)))
(defn ->number [^String s]
(Long/valueOf s))
(defn commit-count []
(-> ^{:out :string} ($ git rev-list --count HEAD) check :out str/split-lines first ->number))
(defn git-sha []
(-> ^{:out :string} ($ git rev-parse --short=7 HEAD) check :out str/split-lines first))
(defn set-patch-version! [{:keys [input-file output-file
patch
version-prefix]
:or {input-file "pom.xml"
output-file "pom.xml"
version-prefix "v"}
:as opts}]
(with-open [input (io/input-stream (io/file input-file))]
(let [root (zip/xml-zip (xml/parse input))
patch (cond (= :commit-count+1 patch)
(inc (commit-count))
(= :commit-count patch)
(commit-count)
:else
patch)
new-content (->> (tree-editor root match-version? (partial editor (str patch)))
(xml/indent-str)
(str/split-lines)
(remove (comp empty? str/trim))
(str/join "\n")
(update-tag! version-prefix))]
(if (= :repl output-file)
new-content
(do
(shutdown-agents)
(spit output-file new-content)
(println (file->current-version output-file)))))))
(defn set-version [v node]
(assoc node :content (list (str v))))
(defn set-version! [{:keys [input-file output-file version-prefix version]
:or {input-file "pom.xml"
output-file "pom.xml"
version-prefix "v"}}]
(with-open [input (io/input-stream (io/file input-file))]
(let [root (zip/xml-zip (xml/parse input))
new-content (->> (tree-editor root match-version? (partial set-version (str version)))
(xml/indent-str)
(str/split-lines)
(remove (comp empty? str/trim))
(str/join "\n")
(update-tag! version-prefix))]
(if (= :repl output-file)
new-content
(do
(shutdown-agents)
(spit output-file new-content)
(println (file->current-version output-file)))))))
(comment
(set-version! {:output-file :repl :version "DEV"}))
(defn get-version [{:keys [input-file]
:or {input-file "pom.xml"}
:as opts}]
(println (file->current-version input-file)))
(defn get-quote-value [line k]
(cond
(str/starts-with? line k)
(let [rst (subs line (count k))]
(str/join "" (->> (seq rst)
(drop-while #(contains? #{\" \space} %))
(take-while #(not= \" %)))))
(= 0 (count line))
nil
:else
(get-quote-value (subs line 1) k)))
(defn replace-quote-value [line k new-val]
(cond
(not (str/includes? line k))
line
(str/starts-with? line k)
(let [rst (subs line (count k))
rest-of-line (str/join "" (->> (seq rst)
(drop-while #(contains? #{\" \space} %))
(drop-while #(not (contains? #{\" \space} %)))
(drop-while #(contains? #{\" \space} %))))]
(str k
" \""
new-val
"\""
(when-not (str/starts-with? rest-of-line "}")
" ")
rest-of-line))
(= 0 (count line))
nil
:else
(str (first line) (replace-quote-value (subs line 1) k new-val))))
(comment
(count "8b242e8"))
(comment
(replace-quote-value
"com.github.sikt-no/datomic-testcontainers {:git/tag \"0.1.1\" :git/sha \"8b242e8\"}"
":git/tag"
"janei"))
(defn get-kv [inp what]
(first (mapcat (fn [lin]
(when (str/includes? lin what)
[(get-quote-value lin what)]))
(str/split-lines inp))))
(defn update-readme! [{:keys [git-cmd input-md out-file dry?]
:or {git-cmd "git"
out-file "README.md"
dry? false}}]
(println "Updating README.md")
(let [[major minor _patch :as old-tag] (str/split (get-kv input-md ":git/tag")
(Pattern/compile (Pattern/quote ".")))
new-tag (str major "." minor "." (commit-count))
git-sha-str (git-sha)
update-line (fn [line]
(-> line
(replace-quote-value ":git/tag" new-tag)
(replace-quote-value ":git/sha" git-sha-str)))
org-lines (str/split-lines (or input-md (slurp "README.md")))
git-cmd (if dry? "echo" git-cmd)
lines (->> org-lines
(mapv update-line)
(vec))]
(println "Creating new git tag" new-tag "for sha" git-sha-str)
(-> ^{:out :string} ($ ~git-cmd commit -a ~(str new-tag) -m ~(str "Release " new-tag)) check :out println)
(-> ^{:out :string} ($ ~git-cmd push --follow-tags) check :out println)
(when (string? out-file)
(when-not dry?
(spit out-file (str/join "\n" lines)))
(-> ^{:out :string} ($ ~git-cmd add ~out-file) check :out println)
(-> ^{:out :string} ($ ~git-cmd commit "Update git tag and git sha") check :out println)
(-> ^{:out :string} ($ ~git-cmd push) check :out println))
nil))
(comment
(update-readme!
{:dry? true
:out-file "README.md"
:input-md (slurp (str "/home/ire/code/datomic-testcontainers" "/README.md"))}))
(comment
(set-patch-version! {:output-file :repl
:patch :commit-count+1}))
(comment
(clojars-repo-only! {:input-file "pom1.xml"
:output-file "pom2.xml"}))