forked from opensha/opensha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-git.gradle
85 lines (79 loc) · 2.39 KB
/
build-git.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
ext.getGitHash = { ->
try {
def stdout = new ByteArrayOutputStream()
exec {
commandLine 'git', 'rev-parse', '--short', 'HEAD'
standardOutput = stdout
}
return stdout.toString().trim()
} catch (Exception e) {
return ""
}
}
ext.getGitLongHash = { ->
try {
def stdout = new ByteArrayOutputStream()
exec {
commandLine 'git', 'rev-parse', 'HEAD'
standardOutput = stdout
}
return stdout.toString().trim()
} catch (Exception e) {
return ""
}
}
ext.getGitBranch = { ->
try {
def stdout = new ByteArrayOutputStream()
exec {
commandLine 'git', 'rev-parse', '--abbrev-ref', 'HEAD'
standardOutput = stdout
}
return stdout.toString().trim()
} catch (Exception e) {
return ""
}
}
ext.getGitRemoteUrl = { ->
try {
def stdout = new ByteArrayOutputStream()
exec {
commandLine 'git', 'name-rev', '--name-only', 'HEAD'
standardOutput = stdout
}
def branch = stdout.toString().trim()
stdout = new ByteArrayOutputStream()
exec {
commandLine 'git', 'config', 'branch.' + branch + '.remote'
standardOutput = stdout
}
def remote = stdout.toString().trim()
stdout = new ByteArrayOutputStream()
exec {
commandLine 'git', 'config', 'remote.' + remote + '.url'
standardOutput = stdout
}
return stdout.toString().trim()
} catch (Exception e) {
return ""
}
}
ext.writeBuildFiles = { prefix='build' ->
logger.info("Writing build.date file")
new File(projectDir, prefix+".date").text = new Date().toString()+"\n"+System.currentTimeMillis()+"\n"
String hash = getGitLongHash()
if (hash?.trim()) {
logger.info("Writing build.githash file: "+hash)
new File(projectDir, prefix+".githash").text = hash+"\n"
}
String branch = getGitBranch()
if (branch?.trim()) {
logger.info("Writing build.gitbranch file: "+branch)
new File(projectDir, prefix+".gitbranch").text = branch+"\n"
}
String remoteUrl = getGitRemoteUrl()
if (remoteUrl?.trim()) {
logger.info("Writing build.gitremoteurl file: "+remoteUrl)
new File(projectDir, prefix+".gitremoteurl").text = remoteUrl+"\n"
}
}