forked from auth0/java-jwt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrelease.gradle
More file actions
170 lines (145 loc) · 5.42 KB
/
release.gradle
File metadata and controls
170 lines (145 loc) · 5.42 KB
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
import java.text.SimpleDateFormat
apply plugin: ReleasePlugin
class Semver {
String version
def snapshot
def getStringVersion() {
return snapshot ? "$version-SNAPSHOT" : version
}
def nextPatch() {
def parts = version.split("\\.")
def patch = Integer.parseInt(parts[2]) + 1
return "${parts[0]}.${parts[1]}.${patch}"
}
def nextMinor() {
def parts = version.split("\\.")
def minor = Integer.parseInt(parts[1]) + 1
return "${parts[0]}.${minor}.0"
}
}
class ChangeLogTask extends DefaultTask {
def current
def next
@TaskAction
def update() {
def repository = project.auth0.repo
def file = new File('CHANGELOG.md')
def output = new File('CHANGELOG.md.release')
output.newWriter().withWriter { writer ->
file.eachLine { line, number ->
if (number == 0 && !line.startsWith('# Change Log')) {
throw new GradleException('Change Log file is not properly formatted')
}
writer.println(line)
if (number == 0 || number > 1) {
return
}
def formatter = new SimpleDateFormat('yyyy-MM-dd')
writer.println()
writer.println("## [${next}](https://github.com/auth0/${repository}/tree/${next}) (${formatter.format(new Date())})")
writer.println("[Full Changelog](https://github.com/auth0/${repository}/compare/${current}...${next})")
def command = ["curl", "https://webtask.it.auth0.com/api/run/wt-hernan-auth0_com-0/oss-changelog.js?webtask_no_cache=1&repo=${repository}&milestone=${next}", "-f", "-s", "-H", "Accept: text/markdown"]
def content = command.execute()
content.consumeProcessOutputStream(writer)
if (content.waitFor() != 0) {
throw new GradleException("Failed to request changelog for version ${next}")
}
}
}
file.delete()
output.renameTo('CHANGELOG.md')
}
}
class ReleaseTask extends DefaultTask {
def tagName
@TaskAction
def perform() {
def path = project.getRootProject().getProjectDir().path
project.exec {
commandLine 'git', 'add', 'README.md'
workingDir path
}
project.exec {
commandLine 'git', 'add', 'CHANGELOG.md'
workingDir path
}
project.exec {
commandLine 'git', 'commit', '-m', "Release ${tagName}"
workingDir path
}
project.exec {
commandLine 'git', 'tag', "${tagName}"
workingDir path
}
}
}
class ReadmeTask extends DefaultTask {
def current
def next
final filename = 'README.md'
@TaskAction
def update() {
def file = new File(filename)
def gradleUpdated = "compile '${project.group}:${project.name}:${next}'"
def oldSingleQuote = "compile '${project.group}:${project.name}:${current}'"
def oldDoubleQuote = "compile \"${project.group}:${project.name}:${current}\""
def mavenUpdated = "<version>${next}</version>"
def mavenOld = "<version>${current}</version>"
def contents = file.getText('UTF-8')
contents = contents.replace(oldSingleQuote, gradleUpdated).replace(oldDoubleQuote, gradleUpdated).replace(mavenOld, mavenUpdated)
file.write(contents, 'UTF-8')
}
}
class ReleasePlugin implements Plugin<Project> {
void apply(Project target) {
def semver = current()
target.version = semver.stringVersion
def version = semver.version
def nextMinor = semver.nextMinor()
def nextPatch = semver.nextPatch()
target.task('changelogMinor', type: ChangeLogTask) {
current = version
next = nextMinor
}
target.task('changelogPatch', type: ChangeLogTask) {
current = version
next = nextPatch
}
target.task('readmeMinor', type: ReadmeTask, dependsOn: 'changelogMinor') {
current = version
next = nextMinor
}
target.task('readmePatch', type: ReadmeTask, dependsOn: 'changelogPatch') {
current = version
next = nextPatch
}
target.task('releaseMinor', type: ReleaseTask, dependsOn: 'readmeMinor') {
tagName = nextMinor
}
target.task('releasePatch', type: ReleaseTask, dependsOn: 'readmePatch') {
tagName = nextPatch
}
}
static def current() {
def current = describeGit(false)
def snapshot = current == null
if (snapshot) {
current = describeGit(snapshot, '0.0.1')
}
return new Semver(snapshot: snapshot, version: current)
}
static def describeGit(boolean snapshot, String defaultValue = null) {
def command = ['git', 'describe', '--tags', (snapshot ? '--abbrev=0' : '--exact-match')].execute()
def stdout = new ByteArrayOutputStream()
def errout = new ByteArrayOutputStream()
command.consumeProcessOutput(stdout, errout)
if (command.waitFor() != 0) {
Logging.getLogger(ReleasePlugin.class).debug(errout.toString())
return defaultValue
}
if (stdout.toByteArray().length > 0) {
return stdout.toString().replace('\n', "")
}
return defaultValue
}
}