Skip to content

Commit

Permalink
Allow extending an existing Catalog
Browse files Browse the repository at this point in the history
  • Loading branch information
ppalaga committed Sep 3, 2021
1 parent 510a4b9 commit 68be716
Show file tree
Hide file tree
Showing 10 changed files with 465 additions and 216 deletions.
3 changes: 3 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@
<wiremock.version>2.27.2</wiremock.version>
<zt-exec.version>1.11</zt-exec.version>

<!-- Tooling dependency versions (keep sorted alphabetically) -->
<maven-utils.version>0.1.0</maven-utils.version>

<!-- Maven plugin versions (keep sorted alphabetically) -->
<cq-plugin.version>0.40.0</cq-plugin.version>
<build-helper-maven-plugin.version>3.1.0</build-helper-maven-plugin.version>
Expand Down
10 changes: 10 additions & 0 deletions tooling/maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@
<artifactId>quarkus-extension-processor</artifactId>
<version>${quarkus.version}</version>
</dependency>
<dependency>
<groupId>org.l2x6.maven-utils</groupId>
<artifactId>maven-utils</artifactId>
<version>${maven-utils.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
Expand Down Expand Up @@ -87,6 +92,11 @@
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.l2x6.maven-utils</groupId>
<artifactId>maven-utils</artifactId>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,12 @@
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugins.annotations.Parameter;

/**
* Base for {@link CheckExtensionPagesMojo} and {@link UpdateExtensionDocPageMojo}.
*/
abstract class AbstractDocGeneratorMojo extends AbstractMojo {
abstract class AbstractDocGeneratorMojo extends AbstractExtensionListMojo {
public static final String DEFAULT_TEMPLATES_URI_BASE = "classpath:/doc-templates";
/**
*/
Expand All @@ -42,19 +41,6 @@ abstract class AbstractDocGeneratorMojo extends AbstractMojo {
@Parameter(property = "camel-quarkus.basedir", defaultValue = "${project.basedir}")
File baseDir;

/**
* Encoding to read and write files in the current source tree
*/
@Parameter(defaultValue = "${project.build.sourceEncoding}", required = true, property = "camel-quarkus.encoding")
String encoding;
/**
* The root directory of the Camel Quarkus source tree.
*
* @since 1.0.0-M8
*/
@Parameter(defaultValue = "${maven.multiModuleProjectDirectory}", required = true, property = "camel-quarkus.multiModuleProjectDirectory")
File multiModuleProjectDirectory;

protected static <T extends Writer> T evalTemplate(Configuration cfg, String templateUri, Map<String, Object> model,
T out) {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.quarkus.maven;

import java.io.File;
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.util.List;
import java.util.stream.Stream;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.l2x6.maven.utils.MavenSourceTree;

public abstract class AbstractExtensionListMojo extends AbstractMojo {

/**
* A set of regular expressions to match against artifactIdBases. The matching extensions will not be processed by
* this mojo.
*
* @since 0.1.0
*/
@Parameter(property = "cq.skipArtifactIdBases")
protected List<String> skipArtifactIdBases;
private PatternSet skipArtifactIdBasePatterns;

/**
* The root directory of the Camel Quarkus source tree.
*
* @since 0.41.0
*/
@Parameter(defaultValue = "${maven.multiModuleProjectDirectory}", readonly = true)
protected File multiModuleProjectDirectory;
private Path rootModuleDirectory;

/**
* Encoding to read and write files in the current source tree
*
* @since 0.18.0
*/
@Parameter(defaultValue = "utf-8", required = true, property = "cq.encoding")
String encoding;
private Charset charset;

private MavenSourceTree tree;

Path getRootModuleDirectory() {
if (rootModuleDirectory == null) {
rootModuleDirectory = multiModuleProjectDirectory.toPath().toAbsolutePath().normalize();
}
return rootModuleDirectory;
}

Charset getCharset() {
if (charset == null) {
charset = Charset.forName(encoding);
}
return charset;
}

Stream<ExtensionModule> findExtensions() {
getSkipArtifactIdBases();
return CqUtils.findExtensions(
getRootModuleDirectory(),
getTree().getModulesByGa().values(),
artifactIdBase -> !skipArtifactIdBasePatterns.matchesAny(artifactIdBase));
}

PatternSet getSkipArtifactIdBases() {
if (skipArtifactIdBasePatterns == null) {
skipArtifactIdBasePatterns = skipArtifactIdBases == null ? PatternSet.empty() : new PatternSet(skipArtifactIdBases);
}
return skipArtifactIdBasePatterns;
}

public MavenSourceTree getTree() {
if (tree == null) {
tree = MavenSourceTree.of(getRootModuleDirectory().resolve("pom.xml"), getCharset());
}
return tree;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -62,7 +60,7 @@ public class CheckExtensionPagesMojo extends AbstractDocGeneratorMojo {
/**
* The directory relative to which the catalog data is read.
*/
@Parameter(defaultValue = "${project.build.directory}/classes", property = "camel-quarkus.catalogBaseDir")
@Parameter(defaultValue = "${project.build.directory}/classes", property = "cq.catalogBaseDir")
File catalogBaseDir;

/**
Expand All @@ -71,18 +69,6 @@ public class CheckExtensionPagesMojo extends AbstractDocGeneratorMojo {
@Parameter(defaultValue = "${maven.multiModuleProjectDirectory}/docs")
File docsBaseDir;

/**
* List of directories that contain extensions
*/
@Parameter(property = "cq.extensionDirectories", required = true)
List<File> extensionDirectories;

/**
* A set of artifactIdBases that are not extensions and should be excluded from the catalog
*/
@Parameter(property = "cq.skipArtifactIdBases")
Set<String> skipArtifactIdBases;

/**
* The version of Camel we depend on
*/
Expand All @@ -108,9 +94,6 @@ public class CheckExtensionPagesMojo extends AbstractDocGeneratorMojo {
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
final Path docsBasePath = docsBaseDir.toPath();
if (skipArtifactIdBases == null) {
skipArtifactIdBases = Collections.emptySet();
}

camelBits(docsBasePath);
extensions(docsBasePath);
Expand Down Expand Up @@ -186,17 +169,13 @@ void extensions(Path docsBasePath) {

final Set<String> artifactIdBases = new HashSet<>();
final Set<CamelQuarkusExtension> extensions = new TreeSet<>(Comparator.comparing(e -> e.getName().get()));
extensionDirectories.stream()
.map(File::toPath)
.forEach(extDir -> {
CqUtils.findExtensionArtifactIdBases(extDir)
.filter(artifactIdBase -> !skipArtifactIdBases.contains(artifactIdBase))
.forEach(artifactIdBase -> {
artifactIdBases.add(artifactIdBase);
final Path runtimePomXmlPath = extDir.resolve(artifactIdBase).resolve("runtime/pom.xml")
.toAbsolutePath().normalize();
extensions.add(CamelQuarkusExtension.read(runtimePomXmlPath));
});
findExtensions()
.forEach(ext -> {
final String artifactIdBase = ext.getArtifactIdBase();
artifactIdBases.add(artifactIdBase);
final Path runtimePomXmlPath = ext.getExtensionDir().resolve("runtime/pom.xml")
.toAbsolutePath().normalize();
extensions.add(CamelQuarkusExtension.read(runtimePomXmlPath));
});

final String extLinks = extensions.stream()
Expand All @@ -223,12 +202,14 @@ void extensions(Path docsBasePath) {

void replace(Path path, String replacementKey, String value) {
try {
String document = new String(Files.readAllBytes(path), encoding);
document = replace(document, path, replacementKey, value);
try {
Files.write(path, document.getBytes(encoding));
} catch (IOException e) {
throw new RuntimeException("Could not write to " + path, e);
final String oldDocument = new String(Files.readAllBytes(path), encoding);
final String newDocument = replace(oldDocument, path, replacementKey, value);
if (!oldDocument.equals(newDocument)) {
try {
Files.write(path, newDocument.getBytes(encoding));
} catch (IOException e) {
throw new RuntimeException("Could not write to " + path, e);
}
}
} catch (IOException e) {
throw new RuntimeException("Could not read from " + path, e);
Expand Down

0 comments on commit 68be716

Please sign in to comment.