Skip to content

Commit

Permalink
Allow not detecting the extension
Browse files Browse the repository at this point in the history
We actually have some level of support for building an extension with
Gradle so we need to make it work.
However, at the moment, it's close to impossible to determine the
extension we are in in a Gradle extension.
So we allow not detecting the extension and disable the config doc
generation in this case.
  • Loading branch information
gsmet committed Aug 6, 2024
1 parent 0c6c204 commit b4b2ffe
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import javax.tools.Diagnostic;
import javax.tools.Diagnostic.Kind;

import org.jboss.jdeparser.JDeparser;

Expand All @@ -38,19 +39,6 @@ public class ExtensionAnnotationProcessor extends AbstractProcessor {
public synchronized void init(ProcessingEnvironment processingEnv) {
super.init(processingEnv);

List<ExtensionProcessor> extensionProcessors = new ArrayList<>();
extensionProcessors.add(new ExtensionBuildProcessor());

boolean skipDocs = Boolean.getBoolean("skipDocs") || Boolean.getBoolean("quickly");
boolean generateDoc = !skipDocs && !"false".equals(processingEnv.getOptions().get(Options.GENERATE_DOC));

// for now, we generate the old config doc by default but we will change this behavior soon
if (generateDoc) {
extensionProcessors.add(new ConfigDocExtensionProcessor());
}

this.extensionProcessors = Collections.unmodifiableList(extensionProcessors);

utils = new Utils(processingEnv);

boolean useConfigMapping = !Boolean
Expand All @@ -65,7 +53,25 @@ public synchronized void init(ProcessingEnvironment processingEnv) {
+ " config implementation is deprecated. Please migrate to use @ConfigMapping: https://quarkus.io/guides/writing-extensions#configuration");
}

for (ExtensionProcessor extensionProcessor : extensionProcessors) {
List<ExtensionProcessor> extensionProcessors = new ArrayList<>();
extensionProcessors.add(new ExtensionBuildProcessor());

boolean skipDocs = Boolean.getBoolean("skipDocs") || Boolean.getBoolean("quickly");
boolean generateDoc = !skipDocs && !"false".equals(processingEnv.getOptions().get(Options.GENERATE_DOC));

// for now, we generate the old config doc by default but we will change this behavior soon
if (generateDoc) {
if (extension.detected()) {
extensionProcessors.add(new ConfigDocExtensionProcessor());
} else {
processingEnv.getMessager().printMessage(Kind.WARNING,
"We could not detect the groupId and artifactId of this module (maybe you are using Gradle to build your extension?). The generation of the configuration documentation has been disabled.");
}
}

this.extensionProcessors = Collections.unmodifiableList(extensionProcessors);

for (ExtensionProcessor extensionProcessor : this.extensionProcessors) {
extensionProcessor.init(config, utils);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
import com.fasterxml.jackson.annotation.JsonIgnore;

public record Extension(String groupId, String artifactId, String name,
NameSource nameSource) implements Comparable<Extension> {
NameSource nameSource, boolean detected) implements Comparable<Extension> {

public static Extension createNotDetected() {
return new Extension("not.detected", "not.detected", "Not detected", NameSource.NONE, false);
}

@Override
public final String toString() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import java.util.Map;
import java.util.Optional;

import javax.annotation.processing.ProcessingEnvironment;
import javax.tools.Diagnostic.Kind;
import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
Expand All @@ -26,9 +28,11 @@ public final class ExtensionUtil {
private static final String NAME_COMMON_SUFFIX = " - Common";
private static final String NAME_INTERNAL_SUFFIX = " - Internal";

private final ProcessingEnvironment processingEnv;
private final FilerUtil filerUtil;

ExtensionUtil(FilerUtil filerUtil) {
ExtensionUtil(ProcessingEnvironment processingEnv, FilerUtil filerUtil) {
this.processingEnv = processingEnv;
this.filerUtil = filerUtil;
}

Expand All @@ -37,21 +41,25 @@ public final class ExtensionUtil {
* One option would be to pass it through the annotation processor but it's not exactly ideal.
*/
public Extension getExtension() {
Path pom = filerUtil.getPomPath();
Optional<Path> pom = filerUtil.getPomPath();

if (pom.isEmpty()) {
return Extension.createNotDetected();
}

Document doc;

try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
DocumentBuilder db = dbf.newDocumentBuilder();
doc = db.parse(pom.toFile());
doc = db.parse(pom.get().toFile());
doc.getDocumentElement().normalize();
} catch (Exception e) {
throw new IllegalStateException("Unable to parse pom file: " + pom, e);
}

return getExtensionFromPom(pom, doc);
return getExtensionFromPom(pom.get(), doc);
}

private Extension getExtensionFromPom(Path pom, Document doc) {
Expand Down Expand Up @@ -99,7 +107,8 @@ private Extension getExtensionFromPom(Path pom, Document doc) {
}

if (groupId == null || groupId.isBlank() || artifactId == null || artifactId.isBlank()) {
throw new IllegalStateException("Unable to determine artifact coordinates from: " + pom);
processingEnv.getMessager().printMessage(Kind.WARNING, "Unable to determine artifact coordinates from: " + pom);
return Extension.createNotDetected();
}

boolean commonOrInternal = false;
Expand Down Expand Up @@ -142,7 +151,7 @@ private Extension getExtensionFromPom(Path pom, Document doc) {
}
}

return new Extension(groupId, artifactId, name, nameSource);
return new Extension(groupId, artifactId, name, nameSource, true);
}

private Optional<String> getExtensionNameFromExtensionMetadata() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,18 @@ public Path getTargetPath() {
}
}

public Path getPomPath() {
public Optional<Path> getPomPath() {
try {
return Paths.get(processingEnv.getFiler().getResource(StandardLocation.CLASS_OUTPUT, "", "dummy").toUri())
.getParent().getParent().getParent().resolve("pom.xml").toAbsolutePath();
Path pomPath = Paths.get(processingEnv.getFiler().getResource(StandardLocation.CLASS_OUTPUT, "", "dummy").toUri())
.getParent().getParent().getParent().resolve("pom.xml");

if (!Files.isReadable(pomPath)) {
return Optional.empty();
}

return Optional.of(pomPath.toAbsolutePath());
} catch (IOException e) {
throw new IllegalStateException("Unable to determine path to pom.xml");
return Optional.empty();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public Utils(ProcessingEnvironment processingEnv) {
this.elementUtil = new ElementUtil(processingEnv);
this.accessorGenerator = new AccessorGenerator(processingEnv, elementUtil);
this.filerUtil = new FilerUtil(processingEnv);
this.extensionUtil = new ExtensionUtil(filerUtil);
this.extensionUtil = new ExtensionUtil(processingEnv, filerUtil);
}

public ElementUtil element() {
Expand Down

0 comments on commit b4b2ffe

Please sign in to comment.