11import java.net.InetAddress ;
22
33task writeBuildInfo {
4- def hasGit = { ->
5- try {
6- exec {
7- executable ' git'
8- delegate. args ' --version'
9- standardOutput = OutputStream . nullOutputStream()
10- }
11- } catch (Exception e) {
12- return false
13- }
14- return true
15- }
4+ def hasGit = { ->
5+ try {
6+ def result = exec {
7+ executable ' git'
8+ args ' --version'
9+ standardOutput = OutputStream . nullOutputStream()
10+ ignoreExitValue = true
11+ }
12+ return result. exitValue == 0
13+ } catch (Exception e) {
14+ return false
15+ }
16+ }
1617
1718 def addGitOutput = { PrintStream output , String name , String ... args ->
1819 output. println (" ### $name (git ${ String.join(' ', args)} )" )
@@ -39,30 +40,30 @@ task writeBuildInfo {
3940 return path
4041 }
4142
42- doLast {
43- try ( PrintStream output = new PrintStream (' src/main/deploy/build-info.txt' )) {
44- output. println (" ### Host name (InetAddress.localHost.hostName)" )
45- output. println (InetAddress . localHost. hostName)
46- output. println (" ### Project path (convertPath(projectDir.path))" )
47- output. println (convertPath(projectDir. path))
48- if (! hasGit()) {
49- // ANSI escape codes
50- def WARNINGFMT = " \0 33[43;30m"
51- def CLEAR = " \0 33[m"
52- println (" ${ WARNINGFMT} WARNING: Could not find git! Git info will not be sent to the robot.${ CLEAR} " )
53- output. println (" ### <could not run git>" )
54- } else {
55- addGitOutput(output, ' Commit hash' , ' log' , ' -1' , ' --format=%H' )
56- addGitOutput(output, ' Refs to latest commit' , ' log' , ' -1' , ' --format=%D' )
57- addGitOutput(output, ' Commit time' , ' log' , ' -1' , ' --format=%ad' )
58- addGitOutput(output, ' Commit message' , ' log' , ' -1' , ' --format=%B' )
59- addGitOutput(output, ' Remotes' , ' remote' , ' -v' )
60- addGitOutput(output, ' Changed files' , ' diff-index' , ' --name-only' , ' HEAD' , ' --' )
61- addGitOutput(output, ' Untracked files' , ' ls-files' , ' --exclude-standard' , ' --others' )
62- }
63- output. println (' ### (END)' )
64- }
65- }
43+ doLast {
44+ new PrintStream (' src/main/deploy/build-info.txt' ). withStream { output ->
45+ output. println (" ### Host name (InetAddress.localHost.hostName)" )
46+ output. println (InetAddress . localHost. hostName)
47+ output. println (" ### Project path (convertPath(projectDir.path))" )
48+ output. println (convertPath(projectDir. path))
49+ if (! hasGit()) {
50+ // ANSI escape codes
51+ def WARNINGFMT = " \0 33[43;30m"
52+ def CLEAR = " \0 33[m"
53+ println (" ${ WARNINGFMT} WARNING: Could not find git! Git info will not be sent to the robot.${ CLEAR} " )
54+ output. println (" ### <could not run git>" )
55+ } else {
56+ addGitOutput(output, ' Commit hash' , ' log' , ' -1' , ' --format=%H' )
57+ addGitOutput(output, ' Refs to latest commit' , ' log' , ' -1' , ' --format=%D' )
58+ addGitOutput(output, ' Commit time' , ' log' , ' -1' , ' --format=%ad' )
59+ addGitOutput(output, ' Commit message' , ' log' , ' -1' , ' --format=%B' )
60+ addGitOutput(output, ' Remotes' , ' remote' , ' -v' )
61+ addGitOutput(output, ' Changed files' , ' diff-index' , ' --name-only' , ' HEAD' , ' --' )
62+ addGitOutput(output, ' Untracked files' , ' ls-files' , ' --exclude-standard' , ' --others' )
63+ }
64+ output. println (' ### (END)' )
65+ }
66+ }
6667}
6768if (getProjectBooleanProperty(' jarWriteBuildInfo' , true )) {
6869 jar. dependsOn(writeBuildInfo)
0 commit comments