-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathnakefile.nim
151 lines (130 loc) · 4.52 KB
/
nakefile.nim
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
import nake
import os
import nimblepkg/cli
import times
import re
import ospaths
import strformat
import strutils
import sequtils
import algorithm
import wiish/building/buildutil
proc basename(x:string):string =
let split = x.splitFile
result = split.name & split.ext
proc getLatestVersion():string =
## Return the latest released version
let guts = readFile("wiish.nimble")
if guts =~ re(".*?version\\s*=\\s*\"(.*?)\"", {reMultiLine, reDotAll}):
var version = matches[0]
if version.endsWith("-dev"):
version = version[0..^5]
return version
else:
raise newException(CatchableError, "Version not detected")
proc updateVersion(newversion:string) =
## Write the new version to the nimble package
let guts = readFile("wiish.nimble")
let newguts = guts.replacef(re("(.*?version\\s*=\\s*)\"(.*?)\"", {reMultiLine, reDotAll}), "$1\"" & newversion & "\"")
writeFile("wiish.nimble", newguts)
proc possibleNextVersions(baseversion:string, has_new:bool, has_break:bool):seq[string] =
## Suggest three possible next versions
let
parts = baseversion.split(".")
major = &"{parts[0].parseInt+1}.0.0"
minor = &"{parts[0]}.{parts[1].parseInt+1}.0"
fix = &"{parts[0]}.{parts[1]}.{parts[2].parseInt+1}"
if has_break:
if parts[0] != "0":
# still in the 0 series
result.add(@[minor, fix, major])
else:
result.add(@[major, minor, fix])
elif has_new:
result.add(@[minor, fix, major])
else:
result.add(@[fix, minor, major])
template possibleNextVersions(baseversion:string):seq[string] =
possibleNextVersions(baseversion, has_new = hasChangesOfType("new"), has_break = hasChangesOfType("break"))
template currentDate():string =
format(now(), "YYYY-MM-dd")
proc hasChangesOfType(kind:string):bool =
toSeq(walkFiles(&"changes/{kind}-*.md")).len > 0
proc changelogHeader(version:string = ""):string =
var version = if version.len == 0: getLatestVersion() else: version
result.add(&"# v{version} - {currentDate()}\L")
proc combineChanges():string =
var
news: seq[string]
fixes: seq[string]
breaks: seq[string]
misc: seq[string]
for thing in os.walkDir(currentSourcePath.parentDir/"changes"):
if thing.kind == pcFile:
if thing.path.endsWith(".md"):
let guts = readFile(thing.path).strip()
let changetype = thing.path.basename().split("-")[0]
var tag:string
case changetype
of "fix":
fixes.add(guts)
of "new":
news.add(guts)
of "break":
breaks.add(guts)
else:
misc.add(guts)
for item in breaks:
result.add(&"- **BREAKING CHANGE** {item}\L")
for item in fixes:
result.add(&"- **FIX** {item}\L")
for item in news:
result.add(&"- **NEW** {item}\L")
for item in misc:
result.add(&"- {item}\L")
proc removeChanges() =
for thing in os.walkDir(currentSourcePath.parentDir/"changes"):
if thing.kind == pcFile:
if thing.path.endsWith(".md"):
removeFile(thing.path)
echo "rm " & thing.path
proc updateChangelog(version:string) =
echo "Updating CHANGELOG.md ..."
let oldlog = readFile("CHANGELOG.md")
let newchanges = combineChanges()
let header = changelogHeader(version)
echo header
echo newchanges
writeFile("CHANGELOG.md", header & "\L" & newchanges & "\L\L" & oldlog)
echo "Wrote to CHANGELOG.md"
removeChanges()
task "chlog-echo", "Print the combined CHANGELOG changes to stdout":
let nextVersion = possibleNextVersions(getLatestVersion())[0]
echo changelogHeader(nextVersion)
echo combineChanges()
task "release", "Bump the version and update the CHANGELOG":
var revert:seq[string]
defer:
if revert.len > 0:
echo "To revert, run the following:\L"
for item in reversed(revert):
echo &"{item}"
echo ""
let lastVersion = getLatestVersion()
echo "Last version: ", lastVersion
echo "Unreleased changes:\L"
echo combineChanges()
let nextVersion = promptList(dontForcePrompt, "Next version?", possibleNextVersions(lastVersion))
echo "Updating to: ", nextVersion
let gitTag = "v" & nextVersion
updateVersion(nextVersion)
revert.add("git checkout -- wiish.nimble")
updateChangelog(nextVersion)
revert.add("git checkout -- CHANGELOG.md changes")
run(@["git", "add", "wiish.nimble", "changes", "CHANGELOG.md"])
run("git", "status")
revert.add("git reset HEAD -- wiish.nimble changes CHANGELOG.md")
run(@["git", "commit", "-m", &"Bump to v{nextVersion}"])
revert.add("git reset --soft HEAD~1")
run(@["git", "tag", gitTag])
revert.add(&"git tag -d {gitTag}")