Skip to content

Commit

Permalink
Merge pull request #69 from SentryMan/loadBuildResource
Browse files Browse the repository at this point in the history
Adds a new method for loading build resources
  • Loading branch information
rbygrave authored Jul 1, 2024
2 parents 59fb1ea + f545744 commit 25324e5
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 21 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ build/
*/bin/
.DS_Store

blackbox-test-prism/avaje-prism-core
28 changes: 28 additions & 0 deletions prism-core/src/main/java/io/avaje/prism/internal/APContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.nio.file.Path;
import java.util.Collection;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Stream;

import javax.annotation.processing.Filer;
Expand Down Expand Up @@ -346,4 +349,29 @@ public static BufferedReader getModuleInfoReader() throws IOException {
.openStream();
return new BufferedReader(new InputStreamReader(inputStream));
}

/**
* Given the relative path, gets a {@link Path} from the Maven {@code target}/Gradle {@code build} folder.
* @param path the relative path of the file in the target/build folder
*
* @return the file object
* @throws IOException if unable to retrieve the file
*/
public static Path getBuildResource(String path) throws IOException {

var id = UUID.randomUUID().toString();
final var uri =
filer()
.createResource(StandardLocation.CLASS_OUTPUT, "", path + id)
.toUri()
.toString()
.replaceFirst(id, "")
.replaceFirst("/classes", "")
.replaceFirst("/classes/java/main", "");
var updatedPath = Path.of(URI.create(uri));
if (path.contains("/")) {
updatedPath.getParent().toFile().mkdirs();
}
return updatedPath;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,10 @@ public static void write(PrintWriter out, String packageName) {
+ "\n"
+ "import static java.util.function.Predicate.not;\n"
+ "\n"
+ "import java.io.BufferedReader;\n"
+ "import java.io.IOException;\n"
+ "import java.io.InputStreamReader;\n"
+ "import java.util.Collection;\n"
+ "import java.util.Optional;\n"
+ "import java.util.Set;\n"
+ "import java.io.*;\n"
+ "import java.net.URI;\n"
+ "import java.nio.file.Path;\n"
+ "import java.util.*;\n"
+ "import java.util.stream.Stream;\n"
+ "\n"
+ "import javax.annotation.processing.Filer;\n"
Expand Down Expand Up @@ -384,6 +382,30 @@ public static void write(PrintWriter out, String packageName) {
+ " .toURL()\n"
+ " .openStream();\n"
+ " return new BufferedReader(new InputStreamReader(inputStream));\n"
+ " }\n\n"
+ " /**\n"
+ " * Given the relative path, gets a {@link Path} from the Maven {@code target}/Gradle {@code build} folder.\n"
+ " * @param path the relative path of the file in the target/build folder\n"
+ " *\n"
+ " * @return the file object\n"
+ " * @throws IOException if unable to retrieve the file\n"
+ " */\n"
+ " public static Path getBuildResource(String path) throws IOException {\n"
+ "\n"
+ " var id = UUID.randomUUID().toString();\n"
+ " final var uri =\n"
+ " filer()\n"
+ " .createResource(StandardLocation.CLASS_OUTPUT, \"\", path + id)\n"
+ " .toUri()\n"
+ " .toString()\n"
+ " .replaceFirst(id, \"\")\n"
+ " .replaceFirst(\"/classes\", \"\")\n"
+ " .replaceFirst(\"/classes/java/main\", \"\");\n"
+ " var updatedPath = Path.of(URI.create(uri));\n"
+ " if (path.contains(\"/\")) {\n"
+ " updatedPath.getParent().toFile().mkdirs();\n"
+ " }\n"
+ " return updatedPath;\n"
+ " }\n"
+ "}\n"
+ "");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,23 +243,14 @@ public static void write(PrintWriter out, String packageName) {
+ " }\n"
+ " }\n"
+ "\n"
+ " private static boolean buildPluginAvailable() {\n"
+ " return resource(\"target/avaje-plugin-exists.txt\", \"/target/classes\")\n"
+ " || resource(\"build/avaje-plugin-exists.txt\", \"/build/classes/java/main\");\n"
+ " private static boolean buildPluginAvailable() {\n"
+ " return isPresent(\"avaje-plugin-exists.txt\");\n"
+ " }\n"
+ "\n"
+ " private static boolean resource(String relativeName, String replace) {\n"
+ " try (var inputStream =\n"
+ " new URI(\n"
+ " APContext.filer()\n"
+ " .getResource(StandardLocation.CLASS_OUTPUT, \"\", relativeName)\n"
+ " .toUri()\n"
+ " .toString()\n"
+ " .replace(replace, \"\"))\n"
+ " .toURL()\n"
+ " .openStream()) {\n"
+ "\n"
+ " return inputStream.available() > 0;\n"
+ " private static boolean isPresent(String path) {\n"
+ " try {\n"
+ "\n"
+ " return APContext.getBuildResource(path).toFile().exists();\n"
+ " } catch (Exception e) {\n"
+ " return false;\n"
+ " }\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,14 @@ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
package io.avaje.prism.internal;

import static java.util.function.Predicate.not;
import static java.util.stream.Collectors.joining;
import static io.avaje.prism.internal.APContext.isAssignable;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collection;
Expand All @@ -51,6 +54,7 @@ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Stream;
import java.util.Optional;
import java.util.Set;

Expand All @@ -76,6 +80,7 @@ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
import javax.lang.model.util.Elements;
import javax.lang.model.util.Types;
import javax.tools.Diagnostic;

import io.avaje.spi.ServiceProvider;
/**
* An AnnotationProcessor for generating prisms. Do not use this class directly.
Expand Down Expand Up @@ -113,6 +118,25 @@ public synchronized void init(ProcessingEnvironment env) {
this.elements = env.getElementUtils();
this.types = env.getTypeUtils();
APContext.init(env);
// write a note in target so that other apts can know prisms was running
try {

var file = APContext.getBuildResource("avaje-processors.txt");
var addition = new StringBuilder();
//if file exists, dedup and append current processor
if (file.toFile().exists()) {
var result =
Stream.concat(Files.lines(file), Stream.of("avaje-prism-core"))
.distinct()
.collect(joining("\n"));
addition.append(result);
} else {
addition.append("avaje-prism-core");
}
Files.writeString(file, addition.toString(), StandardOpenOption.CREATE, StandardOpenOption.WRITE);
} catch (IOException e) {
// not an issue worth failing over
}
}

@Override
Expand Down

0 comments on commit 25324e5

Please sign in to comment.