Skip to content

Commit 25324e5

Browse files
authored
Merge pull request #69 from SentryMan/loadBuildResource
Adds a new method for loading build resources
2 parents 59fb1ea + f545744 commit 25324e5

File tree

5 files changed

+87
-21
lines changed

5 files changed

+87
-21
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ build/
1111
*/bin/
1212
.DS_Store
1313

14+
blackbox-test-prism/avaje-prism-core

prism-core/src/main/java/io/avaje/prism/internal/APContext.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
import java.io.BufferedReader;
66
import java.io.IOException;
77
import java.io.InputStreamReader;
8+
import java.net.URI;
9+
import java.nio.file.Path;
810
import java.util.Collection;
911
import java.util.Optional;
1012
import java.util.Set;
13+
import java.util.UUID;
1114
import java.util.stream.Stream;
1215

1316
import javax.annotation.processing.Filer;
@@ -346,4 +349,29 @@ public static BufferedReader getModuleInfoReader() throws IOException {
346349
.openStream();
347350
return new BufferedReader(new InputStreamReader(inputStream));
348351
}
352+
353+
/**
354+
* Given the relative path, gets a {@link Path} from the Maven {@code target}/Gradle {@code build} folder.
355+
* @param path the relative path of the file in the target/build folder
356+
*
357+
* @return the file object
358+
* @throws IOException if unable to retrieve the file
359+
*/
360+
public static Path getBuildResource(String path) throws IOException {
361+
362+
var id = UUID.randomUUID().toString();
363+
final var uri =
364+
filer()
365+
.createResource(StandardLocation.CLASS_OUTPUT, "", path + id)
366+
.toUri()
367+
.toString()
368+
.replaceFirst(id, "")
369+
.replaceFirst("/classes", "")
370+
.replaceFirst("/classes/java/main", "");
371+
var updatedPath = Path.of(URI.create(uri));
372+
if (path.contains("/")) {
373+
updatedPath.getParent().toFile().mkdirs();
374+
}
375+
return updatedPath;
376+
}
349377
}

prism-core/src/main/java/io/avaje/prism/internal/APContextWriter.java

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,10 @@ public static void write(PrintWriter out, String packageName) {
3131
+ "\n"
3232
+ "import static java.util.function.Predicate.not;\n"
3333
+ "\n"
34-
+ "import java.io.BufferedReader;\n"
35-
+ "import java.io.IOException;\n"
36-
+ "import java.io.InputStreamReader;\n"
37-
+ "import java.util.Collection;\n"
38-
+ "import java.util.Optional;\n"
39-
+ "import java.util.Set;\n"
34+
+ "import java.io.*;\n"
35+
+ "import java.net.URI;\n"
36+
+ "import java.nio.file.Path;\n"
37+
+ "import java.util.*;\n"
4038
+ "import java.util.stream.Stream;\n"
4139
+ "\n"
4240
+ "import javax.annotation.processing.Filer;\n"
@@ -384,6 +382,30 @@ public static void write(PrintWriter out, String packageName) {
384382
+ " .toURL()\n"
385383
+ " .openStream();\n"
386384
+ " return new BufferedReader(new InputStreamReader(inputStream));\n"
385+
+ " }\n\n"
386+
+ " /**\n"
387+
+ " * Given the relative path, gets a {@link Path} from the Maven {@code target}/Gradle {@code build} folder.\n"
388+
+ " * @param path the relative path of the file in the target/build folder\n"
389+
+ " *\n"
390+
+ " * @return the file object\n"
391+
+ " * @throws IOException if unable to retrieve the file\n"
392+
+ " */\n"
393+
+ " public static Path getBuildResource(String path) throws IOException {\n"
394+
+ "\n"
395+
+ " var id = UUID.randomUUID().toString();\n"
396+
+ " final var uri =\n"
397+
+ " filer()\n"
398+
+ " .createResource(StandardLocation.CLASS_OUTPUT, \"\", path + id)\n"
399+
+ " .toUri()\n"
400+
+ " .toString()\n"
401+
+ " .replaceFirst(id, \"\")\n"
402+
+ " .replaceFirst(\"/classes\", \"\")\n"
403+
+ " .replaceFirst(\"/classes/java/main\", \"\");\n"
404+
+ " var updatedPath = Path.of(URI.create(uri));\n"
405+
+ " if (path.contains(\"/\")) {\n"
406+
+ " updatedPath.getParent().toFile().mkdirs();\n"
407+
+ " }\n"
408+
+ " return updatedPath;\n"
387409
+ " }\n"
388410
+ "}\n"
389411
+ "");

prism-core/src/main/java/io/avaje/prism/internal/ModuleInfoReaderWriter.java

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -243,23 +243,14 @@ public static void write(PrintWriter out, String packageName) {
243243
+ " }\n"
244244
+ " }\n"
245245
+ "\n"
246-
+ " private static boolean buildPluginAvailable() {\n"
247-
+ " return resource(\"target/avaje-plugin-exists.txt\", \"/target/classes\")\n"
248-
+ " || resource(\"build/avaje-plugin-exists.txt\", \"/build/classes/java/main\");\n"
246+
+ " private static boolean buildPluginAvailable() {\n"
247+
+ " return isPresent(\"avaje-plugin-exists.txt\");\n"
249248
+ " }\n"
250249
+ "\n"
251-
+ " private static boolean resource(String relativeName, String replace) {\n"
252-
+ " try (var inputStream =\n"
253-
+ " new URI(\n"
254-
+ " APContext.filer()\n"
255-
+ " .getResource(StandardLocation.CLASS_OUTPUT, \"\", relativeName)\n"
256-
+ " .toUri()\n"
257-
+ " .toString()\n"
258-
+ " .replace(replace, \"\"))\n"
259-
+ " .toURL()\n"
260-
+ " .openStream()) {\n"
261-
+ "\n"
262-
+ " return inputStream.available() > 0;\n"
250+
+ " private static boolean isPresent(String path) {\n"
251+
+ " try {\n"
252+
+ "\n"
253+
+ " return APContext.getBuildResource(path).toFile().exists();\n"
263254
+ " } catch (Exception e) {\n"
264255
+ " return false;\n"
265256
+ " }\n"

prism-core/src/main/java/io/avaje/prism/internal/PrismGenerator.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,14 @@ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
3737
package io.avaje.prism.internal;
3838

3939
import static java.util.function.Predicate.not;
40+
import static java.util.stream.Collectors.joining;
4041
import static io.avaje.prism.internal.APContext.isAssignable;
4142

4243
import java.io.IOException;
4344
import java.io.PrintWriter;
4445
import java.io.UncheckedIOException;
46+
import java.nio.file.Files;
47+
import java.nio.file.StandardOpenOption;
4548
import java.util.ArrayDeque;
4649
import java.util.ArrayList;
4750
import java.util.Collection;
@@ -51,6 +54,7 @@ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
5154
import java.util.List;
5255
import java.util.Map;
5356
import java.util.Map.Entry;
57+
import java.util.stream.Stream;
5458
import java.util.Optional;
5559
import java.util.Set;
5660

@@ -76,6 +80,7 @@ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
7680
import javax.lang.model.util.Elements;
7781
import javax.lang.model.util.Types;
7882
import javax.tools.Diagnostic;
83+
7984
import io.avaje.spi.ServiceProvider;
8085
/**
8186
* An AnnotationProcessor for generating prisms. Do not use this class directly.
@@ -113,6 +118,25 @@ public synchronized void init(ProcessingEnvironment env) {
113118
this.elements = env.getElementUtils();
114119
this.types = env.getTypeUtils();
115120
APContext.init(env);
121+
// write a note in target so that other apts can know prisms was running
122+
try {
123+
124+
var file = APContext.getBuildResource("avaje-processors.txt");
125+
var addition = new StringBuilder();
126+
//if file exists, dedup and append current processor
127+
if (file.toFile().exists()) {
128+
var result =
129+
Stream.concat(Files.lines(file), Stream.of("avaje-prism-core"))
130+
.distinct()
131+
.collect(joining("\n"));
132+
addition.append(result);
133+
} else {
134+
addition.append("avaje-prism-core");
135+
}
136+
Files.writeString(file, addition.toString(), StandardOpenOption.CREATE, StandardOpenOption.WRITE);
137+
} catch (IOException e) {
138+
// not an issue worth failing over
139+
}
116140
}
117141

118142
@Override

0 commit comments

Comments
 (0)