Skip to content

Commit

Permalink
Move from quarkus-extension.json to .yaml
Browse files Browse the repository at this point in the history
Why:

 * Humans read and write .yaml better than .json
   and yaml is a superset of json.

This change addreses the need by:

 * ExtensionDescriptorMojo
   - updated to read .yaml OR .json and then still massage
     the structure into new structure for backwards compatability.
 * GenerateExtensonsMojo
   - reads .yaml or .json per extension. Still some javax.json code
     present so do a double-parse to convert back to java.json.
     Not pretty but minimal change for v1 that can be cleaned up
     afterwards.
 * ValidateExtensionJsonMojo
   - just updated to check if .yaml or .json file exists and can be
     read.
  • Loading branch information
maxandersen committed Oct 28, 2019
1 parent 222edef commit d4f4da2
Show file tree
Hide file tree
Showing 153 changed files with 351 additions and 844 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
metadata: {}
7 changes: 6 additions & 1 deletion devtools/platform-descriptor-json-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,12 @@
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
</dependency>


<dependency>
<groupId>org.jboss.slf4j</groupId>
<artifactId>slf4j-jboss-logging</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -44,6 +45,14 @@
import org.eclipse.aether.resolution.ArtifactResolutionException;
import org.eclipse.aether.resolution.ArtifactResult;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;

import io.quarkus.bootstrap.BootstrapConstants;
import io.quarkus.bootstrap.util.ZipUtils;
import io.quarkus.dependencies.Extension;
Expand Down Expand Up @@ -208,6 +217,19 @@ public void execute() throws MojoExecutionException, MojoFailureException {
}

private JsonObject processDependency(Artifact artifact) throws IOException {
JsonNode onode = processDependencyToObjectNode(artifact);

if (onode != null) {
// TODO: this is a dirty hack to avoid redoing existing javax.json code
String json = getMapper(false).writeValueAsString(onode);
JsonReader jsonReader = Json.createReader(new StringReader(json));
return jsonReader.readObject();
} else {
return null;
}
}

private JsonNode processDependencyToObjectNode(Artifact artifact) throws IOException {
final Path path = artifact.getFile().toPath();
if (Files.isDirectory(path)) {
return processMetaInfDir(artifact, path.resolve(BootstrapConstants.META_INF));
Expand All @@ -218,37 +240,70 @@ private JsonObject processDependency(Artifact artifact) throws IOException {
}
}

private JsonObject processMetaInfDir(Artifact artifact, Path metaInfDir)
/**
* Load and return javax.jsonObject based on yaml, json or properties file.
*
* @param artifact
* @param metaInfDir
* @return
* @throws IOException
*/
private JsonNode processMetaInfDir(Artifact artifact, Path metaInfDir)
throws IOException {

ObjectMapper mapper = null;

if (!Files.exists(metaInfDir)) {
return null;
}
final Path p = metaInfDir.resolve(BootstrapConstants.EXTENSION_PROPS_JSON_FILE_NAME);
if (!Files.exists(p)) {
final Path props = metaInfDir.resolve(BootstrapConstants.DESCRIPTOR_FILE_NAME);
if (Files.exists(props)) {
return Json.createObjectBuilder()
.add(Extension.ARTIFACT_ID, artifact.getArtifactId())
.add(Extension.GROUP_ID, artifact.getGroupId())
.add("version", artifact.getVersion())
.add("name", artifact.getArtifactId())
.build();
Path jsonOrYaml = null;

Path yaml = metaInfDir.resolve(BootstrapConstants.QUARKUS_EXTENSION_FILE_NAME);
if (Files.exists(yaml)) {
mapper = getMapper(true);
jsonOrYaml = yaml;
} else {
Path json = metaInfDir.resolve(BootstrapConstants.EXTENSION_PROPS_JSON_FILE_NAME);
if (!Files.exists(json)) {
final Path props = metaInfDir.resolve(BootstrapConstants.DESCRIPTOR_FILE_NAME);
if (Files.exists(props)) {
return mapper.createObjectNode()
.put(Extension.ARTIFACT_ID, artifact.getArtifactId())
.put(Extension.GROUP_ID, artifact.getGroupId())
.put("version", artifact.getVersion())
.put("name", artifact.getArtifactId());
} else {
return null;
}
} else {
jsonOrYaml = json;
mapper = getMapper(false);
}
return null;
}
return processPlatformArtifact(artifact, p);
return processPlatformArtifact(artifact, jsonOrYaml, mapper);
}

private JsonObject processPlatformArtifact(Artifact artifact, Path descriptor)
private JsonNode processPlatformArtifact(Artifact artifact, Path descriptor, ObjectMapper mapper)
throws IOException {
try (InputStream is = Files.newInputStream(descriptor)) {
try (JsonReader reader = Json.createReader(is)) {
final JsonObject object = reader.readObject();
debug("Adding Quarkus extension %s:%s", object.get(Extension.GROUP_ID), object.get(Extension.ARTIFACT_ID));
return object;
}
} catch (IOException e) {
throw new IOException("Failed to parse " + descriptor, e);
JsonNode object = mapper.readTree(is);
debug("Adding Quarkus extension %s:%s", object.get(Extension.GROUP_ID), object.get(Extension.ARTIFACT_ID));
return object;
} catch (IOException io) {
throw new IOException("Failed to parse " + descriptor, io);
}
}

private ObjectMapper getMapper(boolean yaml) {

if (yaml) {
YAMLFactory yf = new YAMLFactory();
return new ObjectMapper(yf)
.setPropertyNamingStrategy(PropertyNamingStrategy.KEBAB_CASE);
} else {
return new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT)
.enable(JsonParser.Feature.ALLOW_COMMENTS).enable(JsonParser.Feature.ALLOW_NUMERIC_LEADING_ZEROS)
.setPropertyNamingStrategy(PropertyNamingStrategy.KEBAB_CASE);
}
}

Expand All @@ -265,6 +320,27 @@ private String extensionId(JsonObject extObject) {
}
}

private String extensionId(ObjectNode extObject) {
String artId = extObject.get(Extension.ARTIFACT_ID).asText("");
if (artId.isEmpty()) {
getLog().warn("Missing artifactId in extension overrides in " + extObject.toString());
}
String groupId = extObject.get(Extension.GROUP_ID).asText("");
if (groupId.isEmpty()) {
return artId;
} else {
return extObject.get(Extension.GROUP_ID).asText("") + ":" + artId;
}
}

private String safeGet(ObjectNode extObject, String field, String defaultValue) {
if (extObject.has(field)) {
return extObject.asText(defaultValue);
} else {
return defaultValue;
}
}

private JsonObject mergeObject(JsonObject extObject, JsonObject extOverride) {
final JsonObjectBuilder mergedObject = Json.createObjectBuilder();
for (Map.Entry<String, JsonValue> e : extOverride.entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,18 +153,34 @@ private void analyzeArtifact(Artifact artifact, Map<String, Artifact> extensions
if (!file.exists()) {
throw new MojoExecutionException("Failed to locate " + artifact + " at " + file);
}

if (!doesDescriptorExistAndCanBeRead(artifact, extensions, file, BootstrapConstants.QUARKUS_EXTENSION_FILE_NAME) &&
!doesDescriptorExistAndCanBeRead(artifact, extensions, file,
BootstrapConstants.EXTENSION_PROPS_JSON_FILE_NAME)) {

throw new MojoExecutionException("Failed to locate and read neither "
+ BootstrapConstants.QUARKUS_EXTENSION_FILE_NAME
+ " or " + BootstrapConstants.EXTENSION_PROPS_JSON_FILE_NAME
+ " for '" + artifact + "' in " + file);
}
}

private boolean doesDescriptorExistAndCanBeRead(Artifact artifact, Map<String, Artifact> extensions, final File file,
String descriptorName)
throws MojoExecutionException {
if (file.isDirectory()) {
processExtensionDescriptor(artifact, file.toPath().resolve(BootstrapConstants.META_INF)
.resolve(BootstrapConstants.EXTENSION_PROPS_JSON_FILE_NAME), extensions);
.resolve(descriptorName), extensions);
} else {
try (FileSystem fs = FileSystems.newFileSystem(file.toPath(), null)) {
processExtensionDescriptor(artifact,
fs.getPath("/", BootstrapConstants.META_INF, BootstrapConstants.EXTENSION_PROPS_JSON_FILE_NAME),
fs.getPath("/", BootstrapConstants.META_INF, descriptorName),
extensions);
} catch (IOException e) {
throw new MojoExecutionException("Failed to read " + file, e);
return false;
}
}
return true;
}

private void processExtensionDescriptor(Artifact artifact, Path p, Map<String, Artifact> extensions) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
metadata: {}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
metadata: {}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
metadata: {}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
metadata: {}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
metadata: {}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
metadata: {}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
metadata: {}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
metadata: {}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
metadata: {}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
metadata: {}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
metadata: {}
Loading

0 comments on commit d4f4da2

Please sign in to comment.