-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathgenbuildinfo.gradle
60 lines (56 loc) · 2.3 KB
/
genbuildinfo.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
def runShellCommand(cmd, theargs) {
def os = new ByteArrayOutputStream()
exec {
executable cmd
args theargs
standardOutput = os
}
return os.toString().replace("\n", "");
}
import java.text.SimpleDateFormat
def buildTime(){
def df = new SimpleDateFormat("yyyy-MM-dd:HH:mm:ss")
df.setTimeZone(TimeZone.getTimeZone("UTC"))
return df.format(new Date())
}
def genBuildInfo(String czName, String pkgName, String baseDir) {
String header = "// " + czName + ".java generated by build script"
def commit = runShellCommand('git', ['rev-parse', 'HEAD'])
def branch = runShellCommand('git', ['rev-parse', '--abbrev-ref', 'HEAD'])
def tag = runShellCommand('git', ['name-rev', '--tags', '--name-only', commit])
def commitinfo = runShellCommand('git', ['show-branch', 'HEAD'])
def server = InetAddress.getLocalHost().getHostName()
def builder = System.getProperty('user.name')
def buildos = System.getProperty('os.name')
def buildtime = buildTime()
String modif = " public static final String "
String fname = baseDir + pkgName.replace(".", "/") + "/" + czName + ".java"
println "Generating build information to " + fname
new File(fname).withWriter { out ->
out.writeLine(header)
out.writeLine("package " + pkgName + ";")
out.writeLine("public final class " + czName + " {")
out.writeLine(modif + "VERSION = \"$version\";");
out.writeLine(modif + "BRANCH = \"$branch\";");
out.writeLine(modif + "TAG = \"$tag\";");
out.writeLine(modif + "COMMIT = \"$commit\";");
out.writeLine(modif + "COMMIT_INFO = \"$commitinfo\";");
out.writeLine(modif + "BUILD_SERVER = \"$server\";");
out.writeLine(modif + "BUILD_OS = \"$buildos\";");
out.writeLine(modif + "BUILD_USER = \"$builder\";");
out.writeLine(modif + "BUILD_TIME = \"$buildtime\";");
out.writeLine("}")
}
/*
exec {
executable 'cat'
args fname
}
*/
}
task genBuildInfo << {
String pkgName = 'feiw'
String gendir = '/generated-sources/'
new File(project.buildDir.toString() + gendir + pkgName.replace(".", "/")).mkdirs();
genBuildInfo('BuildInfo', pkgName, project.buildDir.toString() + gendir)
}