9
9
*/
10
10
package org .truffleruby .processor ;
11
11
12
- import org .truffleruby .PopulateBuildInformation ;
12
+ import java .io .BufferedReader ;
13
+ import java .io .File ;
14
+ import java .io .IOException ;
15
+ import java .io .InputStreamReader ;
16
+ import java .io .PrintStream ;
17
+ import java .net .URISyntaxException ;
18
+ import java .security .CodeSource ;
19
+ import java .util .Calendar ;
20
+ import java .util .HashSet ;
21
+ import java .util .Set ;
13
22
14
23
import javax .annotation .processing .AbstractProcessor ;
24
+ import javax .annotation .processing .ProcessingEnvironment ;
15
25
import javax .annotation .processing .RoundEnvironment ;
16
26
import javax .annotation .processing .SupportedAnnotationTypes ;
17
27
import javax .lang .model .SourceVersion ;
21
31
import javax .lang .model .element .TypeElement ;
22
32
import javax .tools .Diagnostic .Kind ;
23
33
import javax .tools .JavaFileObject ;
24
- import java .io .BufferedReader ;
25
- import java .io .File ;
26
- import java .io .InputStreamReader ;
27
- import java .io .PrintStream ;
28
- import java .util .Calendar ;
29
- import java .util .HashSet ;
30
- import java .util .Set ;
34
+
35
+ import org .truffleruby .PopulateBuildInformation ;
31
36
32
37
@ SupportedAnnotationTypes ("org.truffleruby.PopulateBuildInformation" )
33
38
public class BuildInformationProcessor extends AbstractProcessor {
@@ -36,21 +41,51 @@ public class BuildInformationProcessor extends AbstractProcessor {
36
41
37
42
private final Set <String > processed = new HashSet <>();
38
43
44
+ private File trufflerubyHome ;
39
45
private String rubyVersion ;
40
46
private String rubyBaseVersion ;
41
47
private int rubyRevision ;
42
48
private String revision ;
43
49
private String compileDate ;
44
50
45
- public BuildInformationProcessor () {
51
+ @ Override
52
+ public synchronized void init (ProcessingEnvironment env ) {
53
+ super .init (env );
46
54
try {
47
- rubyVersion = runCommand ("../../../tool/query-versions-json.rb ruby.version" );
48
- rubyBaseVersion = runCommand ("../../../tool/query-versions-json.rb ruby.base" );
49
- rubyRevision = Integer .parseInt (runCommand ("../../../tool/query-versions-json.rb ruby.revision" ));
55
+ trufflerubyHome = findHome ();
56
+ rubyVersion = runCommand ("tool/query-versions-json.rb ruby.version" );
57
+ rubyBaseVersion = runCommand ("tool/query-versions-json.rb ruby.base" );
58
+ rubyRevision = Integer .parseInt (runCommand ("tool/query-versions-json.rb ruby.revision" ));
50
59
revision = runCommand ("git rev-parse --short=8 HEAD" );
51
60
compileDate = runCommand ("git log -1 --date=short --pretty=format:%cd" );
52
- } catch (Exception e ) {
53
- processingEnv .getMessager ().printMessage (Kind .ERROR , e .getClass () + " " + e .getMessage ());
61
+ } catch (Throwable e ) {
62
+ env .getMessager ().printMessage (Kind .ERROR , e .getClass () + " " + e .getMessage ());
63
+ }
64
+ }
65
+
66
+ private File findHome () throws URISyntaxException {
67
+ CodeSource codeSource = getClass ().getProtectionDomain ().getCodeSource ();
68
+ if (codeSource == null ) {
69
+ throw new RuntimeException ("Could not find the source code for " + getClass ());
70
+ }
71
+ File source = new File (codeSource .getLocation ().toURI ());
72
+ // this is probably `mxbuild/org.truffleruby.processor/bin` or `mxbuild/dists/jdk1.8/truffleruby-processor.jar`
73
+ // let's try to find `mxbuild`
74
+ while (!source .getName ().equals ("mxbuild" )) {
75
+ source = source .getParentFile ();
76
+ if (source == null ) {
77
+ throw new RuntimeException ("Could not find `mxbuild` in the source path for " + getClass () + ": " + codeSource .getLocation ());
78
+ }
79
+ }
80
+ return source .getParentFile ();
81
+ }
82
+
83
+ private String runCommand (String command ) throws IOException {
84
+ final Process git = new ProcessBuilder (command .split ("\\ s+" ))
85
+ .directory (trufflerubyHome )
86
+ .start ();
87
+ try (BufferedReader reader = new BufferedReader (new InputStreamReader (git .getInputStream ()))) {
88
+ return reader .readLine ();
54
89
}
55
90
}
56
91
@@ -61,6 +96,7 @@ public SourceVersion getSupportedSourceVersion() {
61
96
62
97
@ Override
63
98
public boolean process (Set <? extends TypeElement > annotations , RoundEnvironment roundEnvironment ) {
99
+ assert isInitialized ();
64
100
if (!annotations .isEmpty ()) {
65
101
for (Element element : roundEnvironment .getElementsAnnotatedWith (PopulateBuildInformation .class )) {
66
102
try {
@@ -89,14 +125,14 @@ private void processBuildInformation(TypeElement element) throws Exception {
89
125
90
126
try (PrintStream stream = new PrintStream (output .openOutputStream (), true , "UTF-8" )) {
91
127
stream .println ("/*\n " +
92
- " * Copyright (c) " + Calendar .getInstance ().get (Calendar .YEAR ) + " Oracle and/or its affiliates. All rights reserved. This\n " +
93
- " * code is released under a tri EPL/GPL/LGPL license. You can use it,\n " +
94
- " * redistribute it and/or modify it under the terms of the:\n " +
95
- " *\n " +
96
- " * Eclipse Public License version 1.0, or\n " +
97
- " * GNU General Public License version 2, or\n " +
98
- " * GNU Lesser General Public License version 2.1.\n " +
99
- " */" );
128
+ " * Copyright (c) " + Calendar .getInstance ().get (Calendar .YEAR ) + " Oracle and/or its affiliates. All rights reserved. This\n " +
129
+ " * code is released under a tri EPL/GPL/LGPL license. You can use it,\n " +
130
+ " * redistribute it and/or modify it under the terms of the:\n " +
131
+ " *\n " +
132
+ " * Eclipse Public License version 1.0, or\n " +
133
+ " * GNU General Public License version 2, or\n " +
134
+ " * GNU Lesser General Public License version 2.1.\n " +
135
+ " */" );
100
136
stream .println ("package " + packageName + ";" );
101
137
stream .println ();
102
138
stream .println ("// This file is automatically generated from versions.json" );
@@ -153,14 +189,4 @@ private void processBuildInformation(TypeElement element) throws Exception {
153
189
stream .println ("}" );
154
190
}
155
191
}
156
-
157
- private String runCommand (String command ) throws Exception {
158
- final Process git = new ProcessBuilder (command .split ("\\ s+" ))
159
- .directory (new File (getClass ().getProtectionDomain ().getCodeSource ().getLocation ().toURI ()).getParentFile ())
160
- .start ();
161
- try (BufferedReader reader = new BufferedReader (new InputStreamReader (git .getInputStream ()))) {
162
- return reader .readLine ();
163
- }
164
- }
165
-
166
192
}
0 commit comments