-
Notifications
You must be signed in to change notification settings - Fork 323
/
tools.gradle
86 lines (73 loc) · 2.92 KB
/
tools.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
78
79
80
81
82
83
84
85
86
def getCheckedOutGitCommitHash() {
def gitFolder = project.rootDir.getPath() + "/.git/"
def takeFromHash = 12 // take the first n chars from the commit hash
/*
* Simple access to the current commit-hash, without having to rely on a local git installation
* (based on https://gist.github.com/JonasGroeger/7620911 )
*
* '.git/HEAD' contains either
* in case of detached head: the currently checked out commit hash ("3b3fe8f99d45d48ada83596cee371b21899d97a8")
* otherwise: a reference to a file containing the current commit hash ("ref: refs/heads/master")
*/
def hash = ""
try {
def head = new File(gitFolder + "HEAD").text.split(":") // .git/HEAD
def isCommit = (head.length == 1)
if (isCommit) {
hash = head[0].trim();
} else {
def refHead = new File(gitFolder + head[1].trim()) // .git/refs/heads/master
hash = refHead.text.trim()
}
}catch(IOException ex){
hash = ""
logger.warn("Unable to detect current git commit hash: {}", ex.toString())
}
def version = hash.take(takeFromHash)
logger.debug("Detected '{}' as git commit hash - using {} as version", hash, version)
return version
}
def getGitBranch() {
if (project.hasProperty("branch")) {
// run gradle with -Pbranch=branchName
return project.getProperty("branch")
}
def gitFolder = project.rootDir.getPath() + "/.git/"
/*
* Simple access to the current branch, without having to rely on a local git installation
* (based on https://gist.github.com/JonasGroeger/7620911 )
*
* '.git/HEAD' contains either
* in case of detached head: the currently checked out commit hash ("3b3fe8f99d45d48ada83596cee371b21899d97a8")
* otherwise: a reference to a file containing the current commit hash ("ref: refs/heads/master")
*/
def branch = ""
try {
def head = new File(gitFolder + "HEAD").text.split(":") // .git/HEAD
def isDetached = (head.length == 1)
if (isDetached) {
logger.warn("Unable to detect current git branch, {}", head)
} else {
def refHead = new File(gitFolder + head[1].trim()) // .git/refs/heads/master
branch = head.last().split("/").last().trim();
}
}catch(IOException ex){
logger.warn("Unable to detect current git branch: {}", ex.toString())
}
logger.debug("Detected '{}' as git branch", branch)
return branch
}
def failIfSigningKeyEnforced() {
def warning="Couldn't load release signing keys!"
if (project.hasProperty("enforceReleaseSigning")) {
throw new GradleException(warning)
} else {
logger.warn(warning)
}
}
// export the functions
ext {
failIfSigningKeyEnforced = this.&failIfSigningKeyEnforced
getCheckedOutGitCommitHash = this.&getCheckedOutGitCommitHash
getGitBranch = this.&getGitBranch
}