-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathautoversion.gradle
More file actions
81 lines (74 loc) · 2.62 KB
/
autoversion.gradle
File metadata and controls
81 lines (74 loc) · 2.62 KB
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
def gitCurrentShortHash = { ->
return 'git rev-parse --short HEAD'.execute().text.trim()
}
def gitCurrentBranch = { ->
def stdout = new ByteArrayOutputStream()
exec {
commandLine 'git', 'rev-parse', '--abbrev-ref', 'HEAD'
standardOutput = stdout
}
return stdout.toString().trim()
}
def getBranchPrefix = { ->
def branchName = gitCurrentBranch()
if(branchName.startsWith("master")) {
return "master"
} else if (branchName.startsWith("develop")) {
return "develop"
} else if (branchName.startsWith("feature")) {
return "feature"
} else if (branchName.startsWith("release")) {
return "release"
} else if (branchName.startsWith("hotfix")) {
return "hotfix"
} else if (branchName.startsWith("support")) {
return "support"
} else {
return "temp"
}
}
def getLatestRelateReleaseTag = { ->
def commonMergeBaseCommit = 'git merge-base HEAD master'.execute().text.trim()
def releaseTagDescribe = ('git describe --tags --abbrev=0 --contains ' + commonMergeBaseCommit).execute().text.trim()
def endIndex = releaseTagDescribe.indexOf("^")
if(endIndex > 0)
return releaseTagDescribe.substring(0, endIndex)
else
return releaseTagDescribe
}
def getRCOrdinal = { ->
def parentCommitHash = 'git merge-base HEAD develop'.execute().text.trim()
def stdout = new ByteArrayOutputStream()
exec {
commandLine 'git', 'rev-list', parentCommitHash + '..HEAD', '--count'
standardOutput = stdout
}
return (stdout.toString().trim().toInteger() + 1)
}
ext.generateVersionName = { ->
def versionTag = getLatestRelateReleaseTag()
def branchType = getBranchPrefix();
if(branchType.equalsIgnoreCase("master")) {
return versionTag
} else if(branchType.equalsIgnoreCase("release")) {
def branchName = gitCurrentBranch()
def slashPosition = branchName.indexOf("/");
def newReleaseVersion = branchName.substring(slashPosition + 1)
return newReleaseVersion + '-rc' + getRCOrdinal()
} else {
return versionTag + '-dev+' + gitCurrentShortHash()
}
}
ext.generateVersionCode = { ->
def branchType = getBranchPrefix();
def tagName;
if(branchType.equalsIgnoreCase("release")) {
def branchName = gitCurrentBranch()
def slashPosition = branchName.indexOf("/");
tagName = branchName.substring(slashPosition + 1)
} else {
tagName = getLatestRelateReleaseTag();
}
def (major, minor, patch) = tagName.tokenize('.')
return major.toInteger() * 10000 + minor.toInteger() * 100 + patch.toInteger()
}