[camel] branch master updated: [CAMEL-14560] Use a custom xref checker as the antora one is very slow

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[camel] branch master updated: [CAMEL-14560] Use a custom xref checker as the antora one is very slow

Guillaume Nodet-2
This is an automated email from the ASF dual-hosted git repository.

gnodet pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/master by this push:
     new 7a00de4  [CAMEL-14560] Use a custom xref checker as the antora one is very slow
7a00de4 is described below

commit 7a00de4da0f9c6055cb55a0a30890e58d6e648d5
Author: Guillaume Nodet <[hidden email]>
AuthorDate: Thu Feb 13 16:25:26 2020 +0100

    [CAMEL-14560] Use a custom xref checker as the antora one is very slow
---
 docs/pom.xml                                       |  15 +--
 parent/pom.xml                                     |   1 +
 tooling/maven/camel-package-maven-plugin/pom.xml   |  19 ++-
 .../camel/maven/packaging/XRefCheckMojo.java       | 133 +++++++++++++++++++++
 4 files changed, 155 insertions(+), 13 deletions(-)

diff --git a/docs/pom.xml b/docs/pom.xml
index 56f1a15..09477cd 100644
--- a/docs/pom.xml
+++ b/docs/pom.xml
@@ -80,17 +80,18 @@
                             <commandlineArgs>${project.basedir}/node/yarn/dist/bin/yarn.js --non-interactive gulp</commandlineArgs>
                         </configuration>
                     </execution>
+                </executions>
+            </plugin>
+            <plugin>
+                <groupId>org.apache.camel</groupId>
+                <artifactId>camel-package-maven-plugin</artifactId>
+                <executions>
                     <execution>
-                        <id>antora-xref-check</id>
+                        <id>xref-check</id>
                         <goals>
-                            <goal>exec</goal>
+                            <goal>xref-check</goal>
                         </goals>
                         <phase>verify</phase>
-                        <configuration>
-                            <executable>${project.basedir}/node/node</executable>
-                            <commandlineArgs>${project.basedir}/node/yarn/dist/bin/yarn.js --non-interactive antora --generator @antora/xref-validator site.yml</commandlineArgs>
-                            <skip>${fastinstall}</skip>
-                        </configuration>
                     </execution>
                 </executions>
             </plugin>
diff --git a/parent/pom.xml b/parent/pom.xml
index 344e9d1..bbcc766 100644
--- a/parent/pom.xml
+++ b/parent/pom.xml
@@ -4670,6 +4670,7 @@
                         <plugin>
                             <groupId>org.apache.camel</groupId>
                             <artifactId>camel-package-maven-plugin</artifactId>
+                            <version>${project.version}</version>
                             <dependencies>
                                 <dependency>
                                     <groupId>jakarta.xml.bind</groupId>
diff --git a/tooling/maven/camel-package-maven-plugin/pom.xml b/tooling/maven/camel-package-maven-plugin/pom.xml
index 15514eb..e1b7b6f 100644
--- a/tooling/maven/camel-package-maven-plugin/pom.xml
+++ b/tooling/maven/camel-package-maven-plugin/pom.xml
@@ -167,6 +167,19 @@
             <version>7.0</version>
         </dependency>
 
+        <!-- camel -->
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>spi-annotations</artifactId>
+        </dependency>
+
+        <!-- yaml -->
+        <dependency>
+            <groupId>org.snakeyaml</groupId>
+            <artifactId>snakeyaml-engine</artifactId>
+            <version>2.1</version>
+        </dependency>
+
         <!-- logging -->
         <dependency>
             <groupId>org.apache.logging.log4j</groupId>
@@ -184,12 +197,6 @@
             <scope>test</scope>
         </dependency>
 
-        <!-- camel -->
-        <dependency>
-            <groupId>org.apache.camel</groupId>
-            <artifactId>spi-annotations</artifactId>
-        </dependency>
-
         <!-- testing -->
         <dependency>
             <groupId>org.junit.jupiter</groupId>
diff --git a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/XRefCheckMojo.java b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/XRefCheckMojo.java
new file mode 100644
index 0000000..1ab48a5
--- /dev/null
+++ b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/XRefCheckMojo.java
@@ -0,0 +1,133 @@
+/*
+ * 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.maven.packaging;
+
+import java.io.IOError;
+import java.io.IOException;
+import java.io.Reader;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import org.apache.camel.tooling.util.PackageHelper;
+import org.apache.maven.plugin.AbstractMojo;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugin.MojoFailureException;
+import org.apache.maven.plugins.annotations.Mojo;
+import org.apache.maven.plugins.annotations.Parameter;
+import org.apache.maven.project.MavenProject;
+import org.snakeyaml.engine.v2.api.Load;
+import org.snakeyaml.engine.v2.api.LoadSettings;
+
+@Mojo(name = "xref-check", threadSafe = true)
+public class XRefCheckMojo extends AbstractMojo {
+
+    /**
+     * The maven project.
+     */
+    @Parameter(property = "project", required = true, readonly = true)
+    protected MavenProject project;
+
+    @Override
+    public void execute() throws MojoExecutionException, MojoFailureException {
+        try {
+            List<String> unresolved = checkXRef(project.getBasedir().toPath());
+            if (!unresolved.isEmpty()) {
+                getLog().error("Unresolved xrefs:");
+                for (String ref : unresolved) {
+                    getLog().error("  " + ref);
+                }
+                throw new MojoFailureException("Unresolved xrefs");
+            }
+        } catch (IOException e) {
+            throw new MojoFailureException("Error checking xref", e);
+        }
+    }
+
+    public List<String> checkXRef(Path path) throws IOException {
+        List<String> unresolved = new ArrayList<>();
+        Load yaml = new Load(LoadSettings.builder().build());
+        Map site;
+        try (Reader r = Files.newBufferedReader(path.resolve("site.yml"))) {
+            site = (Map) yaml.loadFromReader(r);
+        }
+        Map<String, Path> pages = new HashMap<>();
+        for (Map component : (List<Map>) ((Map) site.get("content")).get("sources")) {
+            String url = (String) component.get("url");
+            String startPath = (String) component.get("start_path");
+            Path root = path.resolve(url).resolve(startPath).normalize();
+            Map antora;
+            try (Reader r = Files.newBufferedReader(root.resolve("antora.yml"))) {
+                antora = (Map) yaml.loadFromReader(r);
+            }
+            String name = (String) antora.get("name");
+            List<Path> navs = ((List<String>) antora.get("nav")).stream()
+                    .map(root::resolve)
+                    .collect(Collectors.toList());
+            for (Path nav : navs) {
+                pages.put(name + "::" + nav.getFileName().toString(), nav);
+            }
+            Files.list(root.resolve("modules"))
+                    .filter(Files::isDirectory)
+                    .filter(p -> Files.isDirectory(p.resolve("pages")))
+                    .forEach(module -> {
+                        Path pagesDir = module.resolve("pages");
+                        walk(pagesDir)
+                                .filter(Files::isRegularFile)
+                                .forEach(page -> {
+                                    Path rel = pagesDir.relativize(page);
+                                    pages.put(name + "::" + rel.toString(), page);
+                                });
+                    });
+        }
+
+        Pattern xref = Pattern.compile("\\b(?<all>xref:(?<link>[^\\[]+.adoc)\\[[^\\]]*\\])");
+        for (Map.Entry<String, Path> page : pages.entrySet()) {
+            String str = PackageHelper.loadText(page.getValue());
+            Matcher m = xref.matcher(str);
+            while (m.find()) {
+                String link = m.group("link");
+                String all = m.group("all");
+                if (!link.contains("::")) {
+                    link = page.getKey().substring(0, page.getKey().indexOf("::")) + "::" + link;
+                }
+                if (!pages.containsKey(link)) {
+                    long line = str.chars().limit(m.start()).filter(c -> c == '\n').count() + 1;
+                    unresolved.add(page.getKey() + " (" + page.getValue() + ") at line " + line + ": " + all);
+                }
+            }
+        }
+
+        return unresolved;
+    }
+
+    private Stream<Path> walk(Path p) {
+        try {
+            return Files.walk(p);
+        } catch (IOException e) {
+            throw new IOError(e);
+        }
+    }
+
+}