Skip to content

Commit 8905b3d

Browse files
committed
Refactored how versions are auto-built to provide more normal versions
when building locally. Normal auto-detected versions will be based on the base version parsed from the most recent tag on the branch with a -SNAPSHOT appended. If the current commit is the tagged commit then it is used directly to preserve backwards compatibility... but really that should be a CI only option for most use-cases. A new includeBranchInVersion option was added to allow the old behavior of including the branch name in a munged version string for those wanting to keep their experimental branch builds separate from their normal master/version-branch builds.
1 parent a216999 commit 8905b3d

File tree

2 files changed

+93
-19
lines changed

2 files changed

+93
-19
lines changed

gradle.properties

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ jmeVersionName =
88
# If true, the version name will contain the commit hash
99
useCommitHashAsVersionName = false
1010

11+
# Set to true if a non-master branch name should be included in the automatically
12+
# generated version.
13+
includeBranchInVersion = false
14+
1115
# specify if JavaDoc should be built
1216
buildJavaDoc = true
1317

version.gradle

+89-19
Original file line numberDiff line numberDiff line change
@@ -24,47 +24,117 @@ ext {
2424

2525
task configureVersionInfo {
2626
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+
2742
def grgit = Grgit.open(project.file('.'))
2843
def head = grgit.head()
2944
jmeRevision = grgit.log(includes: [head]).size()
3045
jmeGitHash = head.id
3146
jmeShortGitHash = head.abbreviatedId
3247
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.
3351
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+
}
3481

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;
39111
jmeVersionTag = ""
40-
if(jmeFullVersion.startsWith("v")&&Character.isDigit(jmeFullVersion.charAt(1))){
41-
jmeFullVersion=jmeFullVersion.substring(1);
42-
}
43112
} 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;
52120
}
53121
}
54-
}else{
55-
jmeVersionTag=""
122+
} else {
123+
// Just use defaults
124+
println "Using user-defined version"
56125
jmeFullVersion=jmeVersionName
126+
jmeVersionTag = "SNAPSHOT"
57127
}
58128

59-
60129
println("Revision: ${jmeRevision}")
61130
println("Hash: ${jmeGitHash}")
62131
println("Short Hash: ${jmeShortGitHash}")
63132
println("Tag: ${jmeGitTag}")
64133
println("Build Date: ${jmeBuildDate}")
65134
println("Build Branch: ${jmeBranchName}")
66135
println("Use commit hash as version ${useCommitHashAsVersionName}")
67-
println("Build Tag: ${jmeVersionTag}")
136+
println("Base Version: ${baseVersion}")
137+
println("Build Suffix: ${jmeVersionTag}")
68138
println("Build Version: ${jmeFullVersion}")
69139

70140
} catch (ex) {

0 commit comments

Comments
 (0)