This repository has been archived by the owner on Jun 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
version.gradle
77 lines (60 loc) · 1.8 KB
/
version.gradle
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
import org.apache.tools.ant.taskdefs.condition.Os
def versionFromFile = file("VERSION").text.trim()
static String getGithubVar(String name) {
return System.getenv("GITHUB_${name}")
}
static boolean isWindows() {
return Os.isFamily(Os.FAMILY_WINDOWS)
}
File getGitExecutable() {
String gitExe = isWindows() ? "git.exe" : "git"
return System.getenv("PATH")?.split(File.pathSeparator)?.collect { file("${it}/${gitExe}") }?.find { it.canExecute() }
}
String getBranchName() {
File gitExe = getGitExecutable()
if (gitExe == null) {
logger.warn("Git not found on path")
return "unknown"
}
return new ByteArrayOutputStream().withStream { out ->
String githubActionsRef = getGithubVar("REF")
if (githubActionsRef != null) {
return githubActionsRef.trim().substring(11).replace('/', '_')
}
exec {
executable gitExe
workingDir rootDir
args "rev-parse", "--abbrev-ref", "HEAD"
standardOutput out
}
return out.toString("UTF-8").trim()
}
}
String getCommit() {
File gitExe = getGitExecutable()
if (gitExe == null) {
logger.warn("Git not found on path")
return "unknown"
}
return new ByteArrayOutputStream().withStream { out ->
exec {
executable gitExe
workingDir rootDir
args "rev-parse", "--short", "HEAD"
standardOutput out
}
return out.toString("UTF-8").trim()
}
}
String branchName = getBranchName()
switch (branchName) {
case "master":
version = versionFromFile
break
case "develop":
version = "${versionFromFile}-SNAPSHOT"
break
default:
version = "${versionFromFile}-${getCommit()}-UNSTABLE"
break
}