-
Notifications
You must be signed in to change notification settings - Fork 56
/
ReleasePlugin.groovy
339 lines (302 loc) · 14.8 KB
/
ReleasePlugin.groovy
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
* Copyright 2014-2019 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nebula.plugin.release
import nebula.plugin.release.git.base.BaseReleasePlugin
import nebula.plugin.release.git.base.ReleasePluginExtension
import nebula.plugin.release.git.semver.SemVerStrategy
import org.ajoberstar.grgit.Grgit
import org.ajoberstar.grgit.Status
import org.eclipse.jgit.errors.RepositoryNotFoundException
import org.gradle.api.GradleException
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.logging.Logger
import org.gradle.api.logging.Logging
import org.gradle.api.plugins.JavaPlugin
import org.gradle.api.publish.ivy.IvyPublication
import org.gradle.api.publish.ivy.plugins.IvyPublishPlugin
import org.gradle.api.publish.ivy.tasks.GenerateIvyDescriptor
import org.gradle.api.publish.maven.plugins.MavenPublishPlugin
import org.gradle.api.publish.maven.tasks.GenerateMavenPom
import org.gradle.api.publish.maven.tasks.PublishToMavenRepository
class ReleasePlugin implements Plugin<Project> {
public static final String DISABLE_GIT_CHECKS = 'release.disableGitChecks'
Project project
Grgit git
static Logger logger = Logging.getLogger(ReleasePlugin)
static final String SNAPSHOT_TASK_NAME = 'snapshot'
static final String SNAPSHOT_SETUP_TASK_NAME = 'snapshotSetup'
static final String DEV_SNAPSHOT_TASK_NAME = 'devSnapshot'
static final String DEV_SNAPSHOT_SETUP_TASK_NAME = 'devSnapshotSetup'
static final String IMMUTABLE_SNAPSHOT_TASK_NAME = 'immutableSnapshot'
static final String IMMUTABLE_SNAPSHOT_SETUP_TASK_NAME = 'immutableSnapshotSetup'
static final String CANDIDATE_TASK_NAME = 'candidate'
static final String CANDIDATE_SETUP_TASK_NAME = 'candidateSetup'
static final String FINAL_TASK_NAME = 'final'
static final String FINAL_SETUP_TASK_NAME = 'finalSetup'
static final String RELEASE_CHECK_TASK_NAME = 'releaseCheck'
static final String NEBULA_RELEASE_EXTENSION_NAME = 'nebulaRelease'
static final String POST_RELEASE_TASK_NAME = 'postRelease'
static final String GROUP = 'Nebula Release'
@Override
void apply(Project project) {
this.project = project
def gitRoot = project.hasProperty('git.root') ? project.property('git.root') : project.rootProject.projectDir
try {
git = Grgit.open(dir: gitRoot)
}
catch(RepositoryNotFoundException e) {
this.project.version = '0.1.0-dev.0.uncommitted'
logger.warn("Git repository not found at $gitRoot -- nebula-release tasks will not be available. Use the git.root Gradle property to specify a different directory.")
return
}
checkForBadBranchNames()
boolean replaceDevSnapshots = FeatureFlags.isDevSnapshotReplacementEnabled(project)
if (project == project.rootProject) {
project.plugins.apply(BaseReleasePlugin)
ReleasePluginExtension releaseExtension = project.extensions.findByType(ReleasePluginExtension)
SemVerStrategy defaultStrategy = replaceDevSnapshots ? NetflixOssStrategies.IMMUTABLE_SNAPSHOT(project) : NetflixOssStrategies.DEVELOPMENT(project)
releaseExtension.with {
versionStrategy new OverrideStrategies.NoCommitStrategy()
versionStrategy new OverrideStrategies.ReleaseLastTagStrategy(project)
versionStrategy new OverrideStrategies.GradlePropertyStrategy(project)
versionStrategy NetflixOssStrategies.SNAPSHOT(project)
versionStrategy NetflixOssStrategies.IMMUTABLE_SNAPSHOT(project)
versionStrategy NetflixOssStrategies.DEVELOPMENT(project)
versionStrategy NetflixOssStrategies.PRE_RELEASE(project)
versionStrategy NetflixOssStrategies.FINAL(project)
defaultVersionStrategy = defaultStrategy
}
releaseExtension.with {
grgit = git
tagStrategy {
generateMessage = { version ->
StringBuilder builder = new StringBuilder()
builder << "Release of ${version.version}\n\n"
if (version.previousVersion) {
String previousVersion = "v${version.previousVersion}^{commit}"
List excludes = []
if (tagExists(grgit, previousVersion)) {
excludes << previousVersion
}
grgit.log(
includes: ['HEAD'],
excludes: excludes
).inject(builder) { bldr, commit ->
bldr << "- ${commit.id}: ${commit.shortMessage}\n"
}
}
builder.toString()
}
}
}
def nebulaReleaseExtension = project.extensions.create(NEBULA_RELEASE_EXTENSION_NAME, ReleaseExtension)
NetflixOssStrategies.BuildMetadata.nebulaReleaseExtension = nebulaReleaseExtension
def releaseCheck = project.tasks.create(RELEASE_CHECK_TASK_NAME, ReleaseCheck)
releaseCheck.group = GROUP
releaseCheck.grgit = releaseExtension.grgit
releaseCheck.patterns = nebulaReleaseExtension
def postReleaseTask = project.task(POST_RELEASE_TASK_NAME)
postReleaseTask.group = GROUP
postReleaseTask.dependsOn project.tasks.release
def snapshotSetupTask = project.task(SNAPSHOT_SETUP_TASK_NAME)
def immutableSnapshotSetupTask = project.task(IMMUTABLE_SNAPSHOT_SETUP_TASK_NAME)
def devSnapshotSetupTask = project.task(DEV_SNAPSHOT_SETUP_TASK_NAME)
def candidateSetupTask = project.task(CANDIDATE_SETUP_TASK_NAME)
candidateSetupTask.doLast {
project.allprojects.each { it.status = 'candidate' }
}
def finalSetupTask = project.task(FINAL_SETUP_TASK_NAME)
finalSetupTask.doLast {
project.allprojects.each { it.status = 'release' }
}
[snapshotSetupTask, immutableSnapshotSetupTask, devSnapshotSetupTask, candidateSetupTask, finalSetupTask].each {
it.group = GROUP
it.dependsOn releaseCheck
}
def snapshotTask = project.task(SNAPSHOT_TASK_NAME)
snapshotTask.dependsOn snapshotSetupTask
def immutableSnapshotTask = project.task(IMMUTABLE_SNAPSHOT_TASK_NAME)
immutableSnapshotTask.dependsOn immutableSnapshotSetupTask
def devSnapshotTask = project.task(DEV_SNAPSHOT_TASK_NAME)
devSnapshotTask.dependsOn devSnapshotSetupTask
def candidateTask = project.task(CANDIDATE_TASK_NAME)
candidateTask.dependsOn candidateSetupTask
def finalTask = project.task(FINAL_TASK_NAME)
finalTask.dependsOn finalSetupTask
[snapshotTask, immutableSnapshotTask, devSnapshotTask, candidateTask, finalTask].each {
it.group = GROUP
it.dependsOn postReleaseTask
}
def cliTasks = project.gradle.startParameter.taskNames
determineStage(nebulaReleaseExtension, cliTasks, releaseCheck, replaceDevSnapshots)
checkStateForStage()
if (shouldSkipGitChecks()) {
removeReleaseAndPrepLogic(project)
}
} else {
project.version = project.rootProject.version
}
def isParent = project.rootProject.subprojects.any { it.parent == project }
if (!isParent) {
project.plugins.withType(JavaPlugin) {
project.rootProject.tasks.release.dependsOn project.tasks.build
}
}
project.gradle.taskGraph.whenReady { g ->
def tasks = [DEV_SNAPSHOT_TASK_NAME, SNAPSHOT_TASK_NAME].collect { project.getPath() + it }
if(tasks.any { g.hasTask(it) }) {
removeReleaseAndPrepLogic(project)
}
}
configurePublishingIfPresent()
configureBintrayTasksIfPresent()
}
private void removeReleaseAndPrepLogic(Project project) {
project.tasks.release.enabled = false
project.tasks.prepare.enabled = false
}
private void determineStage(ReleaseExtension releaseExtension, List<String> cliTasks, ReleaseCheck releaseCheck, boolean replaceDevSnapshots) {
def hasSnapshot = cliTasks.contains(SNAPSHOT_TASK_NAME)
def hasDevSnapshot = cliTasks.contains(DEV_SNAPSHOT_TASK_NAME)
def hasImmutableSnapshot = cliTasks.contains(IMMUTABLE_SNAPSHOT_TASK_NAME)
def hasCandidate = cliTasks.contains(CANDIDATE_TASK_NAME)
def hasFinal = cliTasks.contains(FINAL_TASK_NAME)
if ([hasSnapshot, hasImmutableSnapshot, hasDevSnapshot, hasCandidate, hasFinal].count { it } > 2) {
throw new GradleException('Only one of snapshot, immutableSnapshot, devSnapshot, candidate, or final can be specified.')
}
releaseCheck.isSnapshotRelease = hasSnapshot || hasDevSnapshot || (!hasCandidate && !hasFinal)
if (hasFinal) {
setupStatus('release')
applyReleaseStage('final')
} else if (hasCandidate) {
setupStatus('candidate')
applyReleaseStage('rc')
} else if (hasImmutableSnapshot) {
applyReleaseStage('snapshot')
} else if (hasSnapshot) {
applyReleaseStage('SNAPSHOT')
} else if (hasDevSnapshot) {
if (replaceDevSnapshots) {
applyReleaseStage('snapshot')
} else {
applyReleaseStage('dev')
}
}
}
private void checkStateForStage() {
if (!project.tasks.releaseCheck.isSnapshotRelease) {
Status status = git.status()
if (!status.isClean()) {
String message = new ErrorMessageFormatter().format(status)
throw new GradleException(message)
}
}
}
private boolean shouldSkipGitChecks() {
def disableGit = project.hasProperty(DISABLE_GIT_CHECKS) && project.property(DISABLE_GIT_CHECKS) as Boolean
def travis = project.hasProperty('release.travisci') && project.property('release.travisci').toBoolean()
disableGit || travis
}
void setupStatus(String status) {
project.plugins.withType(IvyPublishPlugin) {
project.publishing {
publications.withType(IvyPublication) {
descriptor.status = status
}
}
}
}
void applyReleaseStage(String stage) {
final String releaseStage = 'release.stage'
project.allprojects.each { it.ext.set(releaseStage, stage) }
}
void configurePublishingIfPresent() {
project.plugins.withType(MavenPublishPlugin) {
project.tasks.withType(GenerateMavenPom) { task ->
project.rootProject.tasks.postRelease.dependsOn(task)
}
}
project.plugins.withType(IvyPublishPlugin) {
project.tasks.withType(GenerateIvyDescriptor) { task ->
project.rootProject.tasks.postRelease.dependsOn(task)
}
}
}
void configureBintrayTasksIfPresent() {
project.plugins.withId('nebula.nebula-bintray') {
project.tasks.withType(PublishToMavenRepository) { Task task ->
logger.info('Configuring nebula bintray plugin to work with release plugin')
project.plugins.withType(JavaPlugin) {
task.dependsOn(project.tasks.build)
}
project.rootProject.tasks.postRelease.dependsOn(task)
}
}
project.plugins.withId('com.jfrog.bintray') {
project.tasks.withType(Class.forName('com.jfrog.bintray.gradle.tasks.BintrayUploadTask')) { Task task ->
logger.info('Configuring jfrog bintray plugin to work with release plugin')
project.plugins.withType(JavaPlugin) {
task.dependsOn(project.tasks.build)
}
project.rootProject.tasks.postRelease.dependsOn(task)
}
}
project.plugins.withId('com.jfrog.artifactory') {
logger.info('Configuring jfrog artifactory plugin to work with release plugin')
if (isClassPresent('org.jfrog.gradle.plugin.artifactory.task.BuildInfoBaseTask')) {
project.logger.warn 'Please upgrade com.jfrog.artifactory (org.jfrog.buildinfo:build-info-extractor-gradle:) to version 4.6.0 or above'
project.tasks.withType(Class.forName('org.jfrog.gradle.plugin.artifactory.task.BuildInfoBaseTask')) { Task task ->
project.plugins.withType(JavaPlugin) {
task.dependsOn(project.tasks.build)
}
project.rootProject.tasks.postRelease.dependsOn(task)
}
} else if(isClassPresent('org.jfrog.gradle.plugin.artifactory.task.ArtifactoryTask')) {
// JFrog remove BuildInfoBaseTask see https://www.jfrog.com/jira/browse/GAP-281
project.tasks.withType(Class.forName('org.jfrog.gradle.plugin.artifactory.task.ArtifactoryTask')) { Task task ->
project.plugins.withType(JavaPlugin) {
task.dependsOn(project.tasks.build)
}
project.rootProject.tasks.postRelease.dependsOn(task)
}
}
}
}
private boolean tagExists(Grgit grgit, String revStr) {
try {
grgit.resolve.toCommit(revStr)
return true
} catch (e) {
return false
}
}
boolean isClassPresent(String name) {
try {
Class.forName(name)
return true
} catch (Throwable ex) {
logger.debug("Class $name is not present")
return false
}
}
void checkForBadBranchNames() {
if (git.branch.current.name ==~ /release\/\d+(\.\d+)?/) {
throw new GradleException('Branches with pattern release/<version> are used to calculate versions. The version must be of form: <major>.x, <major>.<minor>.x, or <major>.<minor>.<patch>')
}
}
}