Skip to content

Writes a file to signal inject's presence #634

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ build/
*.processors
*/bin/

inject-generator/avaje-inject
inject-generator/avaje-inject-generator
inject-generator/avaje-processors.txt
4 changes: 2 additions & 2 deletions inject-generator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<name>avaje inject generator</name>
<description>annotation processor generating source code for avaje-inject dependency injection</description>
<properties>
<avaje.prisms.version>1.27</avaje.prisms.version>
<avaje.prisms.version>1.28</avaje.prisms.version>
</properties>
<dependencies>

Expand All @@ -34,7 +34,7 @@
<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-spi-service</artifactId>
<version>2.0</version>
<version>2.1</version>
</dependency>
<!-- test dependencies -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ private static boolean moduleCP() {

static void registerModuleProvidedTypes(Set<String> providedTypes) {
if (!injectAvailable) {
if (!pluginExists("build/avaje-module-provides.txt")
&& !pluginExists("target/avaje-module-provides.txt")) {
if (!pluginExists("avaje-module-provides.txt")) {
APContext.logNote("Unable to detect Avaje Inject in Annotation Processor ClassPath, use the Avaje Inject Maven/Gradle plugin for detecting Inject Modules from dependencies");
}
return;
Expand Down Expand Up @@ -129,14 +128,7 @@ static void registerPluginProvidedTypes(ScopeInfo defaultScope) {

private static boolean pluginExists(String relativeName) {
try {
final String resource =
APContext.filer()
.getResource(StandardLocation.CLASS_OUTPUT, "", relativeName)
.toUri()
.toString()
.replaceFirst("/target/classes", "")
.replaceFirst("/build/classes/java/main", "");
return Paths.get(new URI(resource)).toFile().exists();
return APContext.getBuildResource(relativeName).toFile().exists();
} catch (final Exception e) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
import javax.lang.model.element.TypeElement;
import javax.lang.model.util.ElementFilter;
import javax.lang.model.util.Elements;
import javax.tools.StandardLocation;

import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toSet;

import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand Down Expand Up @@ -71,28 +73,42 @@ public synchronized void init(ProcessingEnvironment processingEnv) {
this.defaultScope = allScopes.defaultScope();
ExternalProvider.registerPluginProvidedTypes(defaultScope);
pluginFileProvided.forEach(defaultScope::pluginProvided);

// write a note in target so that other apts can know inject is 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-inject-generator"))
.distinct()
.collect(joining("\n"));
addition.append(result);
} else {
addition.append("avaje-inject-generator");
}
Files.writeString(file, addition.toString(), StandardOpenOption.CREATE, StandardOpenOption.WRITE);
} catch (IOException e) {
// not an issue worth failing over
}
}

/**
* Loads provider files generated by avaje-inject-maven-plugin
*/
void loadProvidedFiles() {
this.performModuleValidation =
lines("target/avaje-plugin-exists.txt").isEmpty()
&& lines("build/avaje-plugin-exists.txt").isEmpty();
pluginFileProvided.addAll(lines("target/avaje-plugin-provides.txt"));
moduleFileProvided.addAll(lines("target/avaje-module-provides.txt"));
pluginFileProvided.addAll(lines("build/avaje-plugin-provides.txt"));
moduleFileProvided.addAll(lines("build/avaje-module-provides.txt"));
performModuleValidation = lines("avaje-plugin-exists.txt").isEmpty();
pluginFileProvided.addAll(lines("avaje-plugin-provides.txt"));
moduleFileProvided.addAll(lines("avaje-module-provides.txt"));
}

/**
* Loads order files generated by avaje-inject-maven-plugin
*/
private void loadOrderFiles() {
Stream.concat(
lines("target/avaje-module-dependencies.csv").stream().skip(1),
lines("build/avaje-module-dependencies.csv").stream().skip(1))
lines("avaje-module-dependencies.csv").stream().skip(1),
lines("avaje-module-dependencies.csv").stream().skip(1))
.filter(s -> !s.startsWith("External Module Type"))
.distinct()
.map(l -> l.split("\\|"))
Expand All @@ -102,15 +118,7 @@ private void loadOrderFiles() {

private List<String> lines(String relativeName) {
try {
final String resource =
processingEnv
.getFiler()
.getResource(StandardLocation.CLASS_OUTPUT, "", relativeName)
.toUri()
.toString()
.replaceFirst("/target/classes", "")
.replaceFirst("/build/classes/java/main", "");
return Files.readAllLines(Paths.get(new URI(resource)));
return Files.readAllLines(APContext.getBuildResource(relativeName));
} catch (final Exception e) {
return Collections.emptyList();
}
Expand Down
Loading