-
Notifications
You must be signed in to change notification settings - Fork 56
/
ReleasePluginExtension.groovy
131 lines (114 loc) · 4.27 KB
/
ReleasePluginExtension.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
/*
* Copyright 2012-2017 the original author or authors.
*
* 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.git.base
import org.ajoberstar.grgit.Grgit
import org.gradle.api.GradleException
import org.gradle.api.Project
import org.gradle.util.ConfigureUtil
import org.slf4j.Logger
import org.slf4j.LoggerFactory
/**
* Extension providing configuration options for gradle-git's release plugins.
*
* <p>
* Sets the version to a {@link DelayedVersion} which will infer the version
* when {@code toString()} is called on it. A strategy will be selected from the
* ones configured on this extension and then used to infer the version.
* </p>
*
* @see BaseReleasePlugin
* @see nebula.plugin.release.git.opinion.OpinionReleasePlugin
*/
class ReleasePluginExtension {
private static final Logger logger = LoggerFactory.getLogger(ReleasePluginExtension)
protected final Project project
private final Map<String, VersionStrategy> versionStrategies = [:]
/**
* The strategy to use when creating a tag for the inferred version.
*/
final TagStrategy tagStrategy = new TagStrategy()
/**
* The strategy to use if all of the ones in {@code versionStrategies} return
* false from their {@code selector()} methods. This strategy can be, but is
* not required to be, one from {@code versionStrategies}.
*/
VersionStrategy defaultVersionStrategy
/**
* The repository to infer the version from.
*/
Grgit grgit
/**
* The remote to fetch changes from and push changes to.
*/
String remote = 'origin'
ReleasePluginExtension(Project project) {
this.project = project
def sharedVersion = new DelayedVersion()
project.rootProject.allprojects {
version = sharedVersion
}
}
/**
* Gets all strategies in the order they were inserted into the extension.
*/
List<VersionStrategy> getVersionStrategies() {
return versionStrategies.collect { key, value -> value }.asImmutable()
}
/**
* Adds a strategy to the extension. If the strategy has the same name as
* one already configured, it will replace the existing one.
*/
void versionStrategy(VersionStrategy strategy) {
versionStrategies[strategy.name] = strategy
}
/**
* Configures the tag strategy with the provided closure.
*/
void tagStrategy(Closure closure) {
ConfigureUtil.configure(closure, tagStrategy)
}
// TODO: Decide if this should be thread-safe.
private class DelayedVersion implements Serializable {
ReleaseVersion inferredVersion
private void infer() {
VersionStrategy selectedStrategy = versionStrategies.find { strategy ->
strategy.selector(project, grgit)
}
if (!selectedStrategy) {
boolean useDefault
if (defaultVersionStrategy instanceof DefaultVersionStrategy) {
useDefault = defaultVersionStrategy.defaultSelector(project, grgit)
} else {
useDefault = defaultVersionStrategy?.selector(project, grgit)
}
if (useDefault) {
logger.info('Falling back to default strategy: {}', defaultVersionStrategy.name)
selectedStrategy = defaultVersionStrategy
} else {
throw new GradleException('No version strategies were selected. Run build with --info for more detail.')
}
}
inferredVersion = selectedStrategy.infer(project, grgit)
}
@Override
String toString() {
if (!inferredVersion) {
infer()
}
return inferredVersion.version
}
}
}