17
17
import java.util.regex.Matcher
18
18
19
19
plugins {
20
- id ' org.ajoberstar.grgit' version ' 2.0.0-rc. 1'
20
+ id ' org.ajoberstar.grgit' version ' 2.0.1'
21
21
}
22
22
23
- ext. gitTag = getGitTag ()
23
+ ext. scmInfo = getScmInfo ()
24
24
25
25
allprojects {
26
26
group = ' org.culturegraph'
27
- version = getVersionFrom(rootProject . ext . gitTag) ?: ' 5.0.0-SNAPSHOT '
27
+ version = scmInfo . version
28
28
ext. mavenName = null
29
29
}
30
30
@@ -82,14 +82,13 @@ subprojects {
82
82
83
83
signing {
84
84
required {
85
- rootProject. ext. gitTag != null &&
86
- gradle. taskGraph. hasTask(tasks. uploadArchives)
85
+ scmInfo. isRelease() && gradle. taskGraph. hasTask(tasks. uploadArchives)
87
86
}
88
87
sign configurations. archives
89
88
}
90
89
91
90
def mavenProjectDescription = {
92
- name project. ext . mavenName ?: project. name
91
+ name project. mavenName ?: project. name
93
92
if (project. description) {
94
93
description project. description
95
94
}
@@ -118,7 +117,7 @@ subprojects {
118
117
connection ' scm:git:https://github.com/culturegraph/metafacture-core.git'
119
118
developerConnection ' scm:git:https://github.com/culturegraph/metafacture-core.git'
120
119
url ' https://github.com/culturegraph/metafacture-core'
121
- tag rootProject . ext . gitTag ?: ' HEAD'
120
+ tag project . scmInfo . tag ?: ' HEAD'
122
121
}
123
122
issueManagement {
124
123
system ' Github'
@@ -175,36 +174,91 @@ subprojects {
175
174
}
176
175
}
177
176
177
+ class ScmInfo {
178
+ def version
179
+ def tag
180
+
181
+ ScmInfo (version , tag ) {
182
+ this . version = version
183
+ this . tag = tag
184
+ }
185
+
186
+ def isRelease () {
187
+ return tag != null
188
+ }
189
+ }
190
+
191
+ def getScmInfo () {
192
+ def version = null
193
+ def tag = getGitTag()
194
+ if (tag != null ) {
195
+ logger. lifecycle(' SCM tag found. Making a release build' )
196
+ version = extractVersionFromTag(tag)
197
+ } else {
198
+ logger. lifecycle(' No SCM tag found. Making a snapshot build' )
199
+ version = getSnapshotVersion()
200
+ }
201
+ logger. lifecycle(" Version is $version " )
202
+ return new ScmInfo (version, tag)
203
+ }
204
+
205
+ def getSnapshotVersion () {
206
+ if (grgit == null ) {
207
+ logger. warn(' No Git repository found' )
208
+ return ' non-scm-build-SNAPSHOT'
209
+ }
210
+ if (grgit. branch. current(). fullName. equals(' HEAD' )) {
211
+ logger. lifecycle(' Detached HEAD found' )
212
+ return " commit-${ grgit.head().id} -SNAPSHOT"
213
+ }
214
+ if (grgit. branch. current(). name. equals(' master' )) {
215
+ logger. lifecycle(' On master branch' )
216
+ return ' master-SNAPSHOT'
217
+ }
218
+ if (grgit. branch. current(). name. startsWith(' releases/' )) {
219
+ logger. lifecycle(' Release branch found' )
220
+ return " ${ extractVersionFromBranch(grgit.branch.current().name)} -SNAPSHOT"
221
+ }
222
+ logger. lifecycle(' Feature branch found' )
223
+ return " feature-${ grgit.branch.current().name} -SNAPSHOT"
224
+ }
225
+
178
226
def getGitTag () {
179
- def tags = getGitTags(grgit)
227
+ if (grgit == null ) {
228
+ logger. warn(' No Git repository found' )
229
+ return null
230
+ }
231
+ if (! grgit. status(). isClean()) {
232
+ logger. warn(' Working copy has modifications. Will not look for tags' )
233
+ return null
234
+ }
235
+ def tags = getAnnotatedTags()
180
236
if (tags. isEmpty()) {
181
- logger. lifecycle(" HEAD is not tagged. Making a snapshot build " )
237
+ logger. lifecycle(' HEAD has no annotated tags ' )
182
238
return null
183
239
}
184
240
if (tags. size() > 1 ) {
185
- logger. warn(" HEAD has ${ tags.size()} tags. Making a snapshot build " )
241
+ logger. warn(" HEAD has ${ tags.size()} annotated tags " )
186
242
return null
187
243
}
188
244
def tag = tags[0 ]
189
- if (tag. tagger == null || tag. dateTime == null ) {
190
- logger. warn(" Found lightweight tag ${ tag.name} . Making a snapshot build" )
191
- return null
192
- }
193
- logger. lifecycle(" Found annotated tag ${ tag.name} . Making a release build" )
245
+ logger. lifecycle(" Found annotated tag $tag . name " )
194
246
return tag. name
195
247
}
196
248
197
- def static getGitTags ( grgit ) {
249
+ def getAnnotatedTags ( ) {
198
250
def tags = []
199
251
for (tag in grgit. tag. list()) {
200
- if (tag. commit == grgit. head()) {
252
+ if (tag. commit == grgit. head()
253
+ && tag. tagger != null
254
+ && tag. dateTime != null ) {
201
255
tags. add tag
202
256
}
203
257
}
204
258
return tags
205
259
}
206
260
207
- def static extractVersion (tag ) {
261
+ def static extractVersionFromTag (tag ) {
208
262
Matcher matcher =
209
263
tag =~ / metafacture-core-(\d +\.\d +\.\d +(-[-A-Za-z0-9]+)?)/
210
264
if (! matcher. matches()) {
@@ -218,9 +272,16 @@ def static extractVersion(tag) {
218
272
return matcher. group(1 )
219
273
}
220
274
221
- def static getVersionFrom (tag ) {
222
- if (tag != null ) {
223
- return extractVersion(tag)
275
+ def static extractVersionFromBranch (branch ) {
276
+ Matcher matcher =
277
+ branch =~ / releases\/ metafacture-core-(\d +\.\d +\.\d +(-[-A-Za-z0-9]+)?)/
278
+ if (! matcher. matches()) {
279
+ throw new GradleException (""" \
280
+ Unsupported branch format: $branch
281
+ Could not extract version from branch. Supported branch formats are
282
+ releases/metafacture-core-X.Y.Z and
283
+ releases/metafacture-core-X.Y.Z-QUALIFIER
284
+ """ . stripIndent())
224
285
}
225
- return null
286
+ return matcher . group( 1 )
226
287
}
0 commit comments