Skip to content

Commit 8949ef1

Browse files
committed
[GR-14662] Fix running the BuildInformationProcessor in IDEA.
PullRequest: truffleruby/727
2 parents 49ae2a4 + 3dbe673 commit 8949ef1

File tree

1 file changed

+58
-32
lines changed

1 file changed

+58
-32
lines changed

src/processor/java/org/truffleruby/processor/BuildInformationProcessor.java

Lines changed: 58 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,19 @@
99
*/
1010
package org.truffleruby.processor;
1111

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;
1322

1423
import javax.annotation.processing.AbstractProcessor;
24+
import javax.annotation.processing.ProcessingEnvironment;
1525
import javax.annotation.processing.RoundEnvironment;
1626
import javax.annotation.processing.SupportedAnnotationTypes;
1727
import javax.lang.model.SourceVersion;
@@ -21,13 +31,8 @@
2131
import javax.lang.model.element.TypeElement;
2232
import javax.tools.Diagnostic.Kind;
2333
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;
3136

3237
@SupportedAnnotationTypes("org.truffleruby.PopulateBuildInformation")
3338
public class BuildInformationProcessor extends AbstractProcessor {
@@ -36,21 +41,51 @@ public class BuildInformationProcessor extends AbstractProcessor {
3641

3742
private final Set<String> processed = new HashSet<>();
3843

44+
private File trufflerubyHome;
3945
private String rubyVersion;
4046
private String rubyBaseVersion;
4147
private int rubyRevision;
4248
private String revision;
4349
private String compileDate;
4450

45-
public BuildInformationProcessor() {
51+
@Override
52+
public synchronized void init(ProcessingEnvironment env) {
53+
super.init(env);
4654
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"));
5059
revision = runCommand("git rev-parse --short=8 HEAD");
5160
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();
5489
}
5590
}
5691

@@ -61,6 +96,7 @@ public SourceVersion getSupportedSourceVersion() {
6196

6297
@Override
6398
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnvironment) {
99+
assert isInitialized();
64100
if (!annotations.isEmpty()) {
65101
for (Element element : roundEnvironment.getElementsAnnotatedWith(PopulateBuildInformation.class)) {
66102
try {
@@ -89,14 +125,14 @@ private void processBuildInformation(TypeElement element) throws Exception {
89125

90126
try (PrintStream stream = new PrintStream(output.openOutputStream(), true, "UTF-8")) {
91127
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+
" */");
100136
stream.println("package " + packageName + ";");
101137
stream.println();
102138
stream.println("// This file is automatically generated from versions.json");
@@ -153,14 +189,4 @@ private void processBuildInformation(TypeElement element) throws Exception {
153189
stream.println("}");
154190
}
155191
}
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-
166192
}

0 commit comments

Comments
 (0)