20
20
import java .security .CodeSource ;
21
21
import java .util .Calendar ;
22
22
import java .util .HashSet ;
23
+ import java .util .Optional ;
23
24
import java .util .Set ;
24
25
import java .util .regex .Matcher ;
25
26
import java .util .regex .Pattern ;
@@ -46,8 +47,9 @@ public class BuildInformationProcessor extends TruffleRubyProcessor {
46
47
private String buildName ;
47
48
private String shortRevision ;
48
49
private String fullRevision ;
50
+ private boolean isDirty ;
49
51
private String compileDate ;
50
- private String copyrightYear ;
52
+ private int copyrightYear ;
51
53
private String kernelMajorVersion ;
52
54
53
55
@ Override
@@ -56,10 +58,13 @@ public synchronized void init(ProcessingEnvironment env) {
56
58
try {
57
59
trufflerubyHome = findHome ();
58
60
buildName = System .getenv ("TRUFFLERUBY_BUILD_NAME" );
59
- fullRevision = runCommand ("git rev-parse HEAD" );
61
+ fullRevision = runCommand ("git rev-parse HEAD" )
62
+ .orElseThrow (() -> new Error ("git rev-parse command failed" ));
60
63
shortRevision = fullRevision .substring (0 , 8 );
61
- compileDate = runCommand ("git log -1 --date=short --pretty=format:%cd" );
62
- copyrightYear = compileDate .split ("\\ -" )[0 ];
64
+ isDirty = runCommand ("git diff --quiet" ).isEmpty ();
65
+ compileDate = runCommand ("git log -1 --date=short --pretty=format:%cd" )
66
+ .orElseThrow (() -> new Error ("git log command failed" ));
67
+ copyrightYear = Integer .parseInt (compileDate .split ("\\ -" )[0 ]);
63
68
kernelMajorVersion = findKernelMajorVersion ();
64
69
} catch (Throwable e ) {
65
70
throw new Error (e );
@@ -98,23 +103,32 @@ private File findHome() throws URISyntaxException {
98
103
}
99
104
100
105
private String findKernelMajorVersion () throws IOException , InterruptedException {
101
- final String kernelVersion = runCommand ("uname -r" );
106
+ final String kernelVersion = runCommand ("uname -r" ). orElseThrow (() -> new Error ( "uname -r command failed" )) ;
102
107
return kernelVersion .split (Pattern .quote ("." ))[0 ];
103
108
}
104
109
105
- private String runCommand (String command ) throws IOException , InterruptedException {
106
- final Process git = new ProcessBuilder (command .split ("\\ s+" )).directory (trufflerubyHome ).start ();
107
- final String firstLine ;
110
+ private Optional <String > runCommand (String command ) throws IOException , InterruptedException {
111
+ final Process process = new ProcessBuilder (command .split ("\\ s+" )).directory (trufflerubyHome ).start ();
112
+
113
+ String firstLine ;
108
114
try (BufferedReader reader = new BufferedReader (
109
- new InputStreamReader (git .getInputStream (), StandardCharsets .UTF_8 ))) {
115
+ new InputStreamReader (process .getInputStream (), StandardCharsets .UTF_8 ))) {
110
116
firstLine = reader .readLine ();
117
+
118
+ while (process .isAlive ()) {
119
+ reader .readLine ();
120
+ }
121
+ }
122
+
123
+ if (process .waitFor () != 0 ) {
124
+ return Optional .empty ();
111
125
}
112
126
113
- final int exitCode = git .waitFor ();
114
- if (exitCode != 0 ) {
115
- throw new Error ("Command " + command + " failed with exit code " + exitCode );
127
+ if (firstLine == null ) {
128
+ firstLine = "" ;
116
129
}
117
- return firstLine ;
130
+
131
+ return Optional .of (firstLine );
118
132
}
119
133
120
134
@ Override
@@ -174,7 +188,7 @@ private void processBuildInformation(TypeElement element) throws Exception {
174
188
if (e instanceof ExecutableElement ) {
175
189
final String name = e .getSimpleName ().toString ();
176
190
177
- final String value ;
191
+ final Object value ;
178
192
switch (name ) {
179
193
case "getBuildName" :
180
194
value = buildName ;
@@ -185,6 +199,9 @@ private void processBuildInformation(TypeElement element) throws Exception {
185
199
case "getFullRevision" :
186
200
value = fullRevision ;
187
201
break ;
202
+ case "isDirty" :
203
+ value = isDirty ;
204
+ break ;
188
205
case "getCopyrightYear" :
189
206
value = copyrightYear ;
190
207
break ;
@@ -199,11 +216,13 @@ private void processBuildInformation(TypeElement element) throws Exception {
199
216
}
200
217
201
218
stream .println (" @Override" );
202
- stream .println (" public String " + name + "() {" );
219
+ stream .println (" public " + (( ExecutableElement ) e ). getReturnType () + " " + name + "() {" );
203
220
if (value == null ) {
204
221
stream .println (" return null;" );
205
- } else {
222
+ } else if ( value instanceof String ) {
206
223
stream .println (" return \" " + value + "\" ;" );
224
+ } else {
225
+ stream .println (" return " + value + ";" );
207
226
}
208
227
stream .println (" }" );
209
228
stream .println ();
0 commit comments