-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuild.clj
106 lines (88 loc) · 3.42 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
(ns build
(:require [clojure.string :as str]
[clojure.tools.build.api :as b]))
(def version-marker #"--TRANSIT_CLJD_VERSION--")
(def deps-marker #"--TRANSIT_CLJD_DEPS--")
(def version-pattern #"0\.8\.\d{1,4}")
;; Helper functions
(defn version
"Runs git command `describe` to find the number of commits since the earliest,
which is tagged with 'v0.8.0'."
[]
(let [ref (b/git-process {:git-args "describe --tags --match v0.8.0"})
cnt (-> ref (str/split #"-") (get 1))]
(format "0.8.%s" cnt)))
(defn sha
"Returns the commit sha for the commit with tag 'v<ver>'."
[ver]
(b/git-process {:git-args ["rev-parse" "--short" (format "v%s^{commit}" ver)]}))
(defn update-version
"Finds lines containing `marker` and replaces version number."
[lines ver & {:keys [pattern marker]
:or {pattern version-pattern
marker version-marker}}]
(map #(if (re-find marker %) (str/replace % pattern ver) %) lines))
(defn update-deps-entry
"Splits lines at marker and then replaces the four lines after the split with
updated deps.edn git coordinates."
[lines ver & {:keys [marker] :or {marker deps-marker}}]
(let [sha (sha ver)
[bef aft] (split-with #(not (re-find marker %)) lines)
[ss rr] (split-at 4 aft)
ss (assoc (vec ss) 2 (format "io.github.wevre/transit-cljd {:git/tag \"v%s\" :git/sha \"%s\"}" ver sha))]
(concat bef ss rr)))
(defn add-build-notes
"Splits lines after title line, inserts section for new version and notes."
[lines ver notes]
(let [[bef aft] (split-at 1 lines)
hdr ["" (str "### " ver) ""]]
(concat bef hdr (map #(str "- " %) notes) aft)))
;; Update README.md
(defn update-readme
"Updates the version and the deps.edn git coordinates in `README.md`."
[ver & {:keys [filename] :or {filename "README.md"}}]
(->> (-> (slurp filename)
str/split-lines
(update-version ver)
(update-deps-entry ver))
(str/join "\n")
(spit filename)))
(comment
(update-readme "0.8.12" :filename "TEST_README.md")
)
;; Update pubspec.yaml
(defn update-pubspec
"Updates the version in `pubspec.yaml`."
[ver & {:keys [filename] :or {filename "pubspec.yaml"}}]
(->> (-> (slurp filename)
str/split-lines
(update-version ver))
(str/join "\n")
(spit filename)))
(comment
(update-pubspec "0.8.12" :filename "pubspec copy.yaml")
)
;; Update CHANGELOG.md
(defn update-changelog
"Add an entry for newest version, with notes, in `CHANGELOG.md`."
[ver notes & {:keys [filename] :or {filename "CHANGELOG.md"}}]
(->> (-> (slurp filename)
str/split-lines
(add-build-notes ver notes))
(str/join "\n")
(spit filename)))
(comment
(update-changelog "0.8.13" ["first note" "second note"] :filename "CHANGELOG copy.md"))
;; Build target: 'release'
(defn release
"Adds a tag to the latest commit and pushes that, then updates version
references in README.md, pubspec.yaml, and CHANGELOG.md and pushes those."
[{:keys [notes]}]
(let [ver (version)]
(b/git-process {:git-args ["tag" "-a" (format "v%s" ver) "-m" (format "Release %s" ver)]})
(b/git-process {:git-args "push"})
(update-readme ver)
(update-pubspec ver)
(update-changelog ver notes)
(b/git-process {:git-args ["commit" "-a" "-m" (format "update doc refs to ver %s" ver)]})
(b/git-process {:git-args "push"})))