@@ -24,47 +24,117 @@ ext {
24
24
25
25
task configureVersionInfo {
26
26
try {
27
+ // Users can configure behavior by setting properties on the command
28
+ // line:
29
+ //
30
+ // jmeVersionName:
31
+ // If set this will override all automatic version detection.
32
+ //
33
+ // useCommitHashAsVersionName:
34
+ // If there is no jmeVersionName set and the current commit has no
35
+ // specific tag then setting this to 'true' will cause the version to
36
+ // be the full commit ID.
37
+ //
38
+ // includeBranchInVersion:
39
+ // Set to true if a non-master branch name should be included in the automatically
40
+ // generated version.
41
+
27
42
def grgit = Grgit . open(project. file(' .' ))
28
43
def head = grgit. head()
29
44
jmeRevision = grgit. log(includes : [head]). size()
30
45
jmeGitHash = head. id
31
46
jmeShortGitHash = head. abbreviatedId
32
47
jmeBranchName = grgit. branch. current. name
48
+
49
+ // This code will find an exact-match tag if the current
50
+ // commit is the same as the tag commit.
33
51
jmeGitTag = grgit. tag. list(). find { it. commit == head }
52
+ def latestTag;
53
+ if ( jmeGitTag ) {
54
+ // Just use the name. We keep jmeGitTag separate because there
55
+ // is some logic that wants to know if this specific commit has
56
+ // a tag versus 'whatever tag we are a child of'... which is what
57
+ // 'latestTag' will be.
58
+ jmeGitTag = jmeGitTag. name
59
+ latestTag = jmeGitTag;
60
+ } else {
61
+ // Use describe to find the most recent tag. Unfortunately,
62
+ // in this version of grgit, we don't have the 'always' options
63
+ // so we can't make as many assumptions about the format of the
64
+ // string.
65
+ // If the commit is an exact match then it will return just the
66
+ // tag name... else it will be tagName-commitCount-abbreviatedId
67
+ // We'll use some groovy regex magic to get the tag either way.
68
+ def describe = grgit. describe()
69
+ def fullDescribe = (describe =~ / (.*?)-(\d +)-g$jmeShortGitHash/ )
70
+ latestTag = fullDescribe ? fullDescribe[0 ][1 ] : describe
71
+ println " Latest tag:" + latestTag
72
+ }
73
+
74
+ // We could enhance this with some more regex if we wanted to sanity
75
+ // check that it was formatted like our versions.
76
+ def tagVersion = (latestTag =~ / v?(.*)/ )[0 ][1 ];
77
+ // If the branch is not master then use the tag.
78
+ if ( jmeBranchName != " master" ) {
79
+ jmeVersion = tagVersion
80
+ }
34
81
35
- if (jmeVersionName== " " ){
36
- if (jmeGitTag != null ) {
37
- jmeGitTag = jmeGitTag. name
38
- jmeFullVersion = jmeGitTag
82
+ // Parse out just the base version part. -SNAPSHOT versions
83
+ // shouldn't really include our release suffixes
84
+ def baseVersion = (tagVersion =~ / (\d +\.\d +.\d +)/ )
85
+ baseVersion = baseVersion. size() > 0 ? baseVersion[0 ][0 ] : tagVersion
86
+
87
+ if ( ! jmeVersionName ) {
88
+ // If there is a specific tag for the top commit then we always
89
+ // use that.
90
+ if ( jmeGitTag ) {
91
+ println " Using GIT tag as version"
92
+ jmeFullVersion = tagVersion; // already cleaned up
93
+ jmeVersionTag = " " // and no SNAPSHOT suffix for an exact version tag
94
+
95
+ // Note: this will not automatically add SNAPSHOT if the user has
96
+ // local changes that they haven't committed. Technically, only
97
+ // real CI builds should be using non-SNAPSHOT versions so we may
98
+ // eventually want to change the script to always use -SNAPSHOT with
99
+ // a CI option to turn it off.
100
+ // We could also check the grgit.status for unstaged modified/removed files.
101
+ // def unstaged = grgit.status().unstaged;
102
+ // def modCount = unstaged.modified.size() + unstaged.removed.size()
103
+ // ...but that seems like a wasteful check considering only official
104
+ // version builds should not have a -SNAPSHOT.
105
+
106
+ } else if ( useCommitHashAsVersionName == " true" && jmeGitHash ) {
107
+ // The user has opted to use the hash... and we actually have
108
+ // a hash.
109
+ println " Using commit ID as version"
110
+ jmeFullVersion = jmeGitHash;
39
111
jmeVersionTag = " "
40
- if (jmeFullVersion. startsWith(" v" )&& Character . isDigit(jmeFullVersion. charAt(1 ))){
41
- jmeFullVersion= jmeFullVersion. substring(1 );
42
- }
43
112
} else {
44
- if (useCommitHashAsVersionName== " true" && jmeGitHash!= null &&! jmeGitHash. equals(" " )){
45
- jmeFullVersion = jmeGitHash
46
- jmeVersionTag = " "
47
- }else {
48
- jmeFullVersion= " ${ jmeVersion} -" ;
49
- if (jmeBranchName!= " master" )jmeFullVersion+ = " ${ jmeBranchName} -" ;
50
- jmeFullVersion+ = " SNAPSHOT"
51
- jmeVersionTag= " SNAPSHOT"
113
+ println " Auto-detecting version"
114
+ jmeVersionTag = " SNAPSHOT"
115
+
116
+ if ( includeBranchInVersion == " true" && jmeBranchName != " master" ) {
117
+ jmeFullVersion = baseVersion + " -" + jmeBranchName + " -" + jmeVersionTag;
118
+ } else {
119
+ jmeFullVersion = baseVersion + " -" + jmeVersionTag;
52
120
}
53
121
}
54
- }else {
55
- jmeVersionTag= " "
122
+ } else {
123
+ // Just use defaults
124
+ println " Using user-defined version"
56
125
jmeFullVersion= jmeVersionName
126
+ jmeVersionTag = " SNAPSHOT"
57
127
}
58
128
59
-
60
129
println (" Revision: ${ jmeRevision} " )
61
130
println (" Hash: ${ jmeGitHash} " )
62
131
println (" Short Hash: ${ jmeShortGitHash} " )
63
132
println (" Tag: ${ jmeGitTag} " )
64
133
println (" Build Date: ${ jmeBuildDate} " )
65
134
println (" Build Branch: ${ jmeBranchName} " )
66
135
println (" Use commit hash as version ${ useCommitHashAsVersionName} " )
67
- println (" Build Tag: ${ jmeVersionTag} " )
136
+ println (" Base Version: ${ baseVersion} " )
137
+ println (" Build Suffix: ${ jmeVersionTag} " )
68
138
println (" Build Version: ${ jmeFullVersion} " )
69
139
70
140
} catch (ex) {
0 commit comments