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

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

commit 8c445094268188a640444d7987adaa30f231911c
Author: Claus Ibsen <claus.ib...@gmail.com>
AuthorDate: Mon Jun 15 14:43:09 2020 +0200

    CAMEL-15190: Generate table of options for camel-main
---
 core/camel-main/src/main/docs/main.adoc            |  16 ++-
 .../apache/camel/maven/packaging/GenerateMojo.java |   2 +
 .../maven/packaging/PrepareCamelMainDocMojo.java   | 132 +++++++++++++++++++++
 .../maven/packaging/PrepareCamelMainMojo.java      |   4 +-
 .../src/main/resources/main-options.mvel           |   7 ++
 5 files changed, 158 insertions(+), 3 deletions(-)

diff --git a/core/camel-main/src/main/docs/main.adoc 
b/core/camel-main/src/main/docs/main.adoc
index 90ec50c..e39644a 100644
--- a/core/camel-main/src/main/docs/main.adoc
+++ b/core/camel-main/src/main/docs/main.adoc
@@ -10,4 +10,18 @@
 
 *Since Camel {since}*
 
-This module is used for running Camel standalone via a main class extended 
from `camel-main`.
\ No newline at end of file
+This module is used for running Camel standalone via a main class extended 
from `camel-main`.
+
+== Configuration options
+
+When running Camel via `camel-main` you can configure Camel in the 
`application.properties` file.
+
+The following table lists all the options:
+
+// main options: START
+// main options: END
+
+== Examples
+
+You can find a set of examples using `camel-main` in [Camel 
Examples](https://github.com/apache/camel-examples)
+which demonstrate running Camel in standalone with `camel-main`.
diff --git 
a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/GenerateMojo.java
 
b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/GenerateMojo.java
index dba8ac7..e4d2c10 100644
--- 
a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/GenerateMojo.java
+++ 
b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/GenerateMojo.java
@@ -48,6 +48,8 @@ public class GenerateMojo extends AbstractGenerateMojo {
         invoke(PrepareComponentMojo.class);
         // prepare-main
         invoke(PrepareCamelMainMojo.class);
+        // prepare-main-doc
+        invoke(PrepareCamelMainDocMojo.class);
         // generate-xml-parser
         invoke(ModelXmlParserGeneratorMojo.class);
         // generate-legal
diff --git 
a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PrepareCamelMainDocMojo.java
 
b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PrepareCamelMainDocMojo.java
new file mode 100644
index 0000000..2dde0a8
--- /dev/null
+++ 
b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PrepareCamelMainDocMojo.java
@@ -0,0 +1,132 @@
+/*
+ * 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.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Collections;
+
+import org.apache.camel.tooling.model.JsonMapper;
+import org.apache.camel.tooling.model.MainModel;
+import org.apache.camel.tooling.util.PackageHelper;
+import org.apache.camel.tooling.util.Strings;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugin.MojoFailureException;
+import org.apache.maven.plugins.annotations.LifecyclePhase;
+import org.apache.maven.plugins.annotations.Mojo;
+import org.apache.maven.plugins.annotations.Parameter;
+import org.apache.maven.plugins.annotations.ResolutionScope;
+import org.apache.maven.project.MavenProject;
+import org.apache.maven.project.MavenProjectHelper;
+import org.mvel2.templates.TemplateRuntime;
+import org.sonatype.plexus.build.incremental.BuildContext;
+
+/**
+ * Prepares camel-main by updating main documentation.
+ */
+@Mojo(name = "prepare-main", defaultPhase = LifecyclePhase.PROCESS_CLASSES, 
threadSafe = true, requiresDependencyResolution = ResolutionScope.COMPILE)
+public class PrepareCamelMainDocMojo extends AbstractGeneratorMojo {
+
+    /**
+     * The documentation directory
+     */
+    @Parameter(defaultValue = "${project.basedir}/src/main/docs")
+    protected File docDocDir;
+
+    /**
+     * The documentation directory
+     */
+    @Parameter(defaultValue = 
"${project.basedir}/src/generated/resources/META-INF/camel-main-configuration-metadata.json")
+    protected File mainJsonFile;
+
+    @Override
+    public void execute(MavenProject project, MavenProjectHelper 
projectHelper, BuildContext buildContext) throws MojoFailureException, 
MojoExecutionException {
+        super.execute(project, projectHelper, buildContext);
+    }
+
+    @Override
+    public void execute() throws MojoExecutionException, MojoFailureException {
+        if (!mainJsonFile.exists()) {
+            throw new MojoExecutionException("Main json file does not exist: " 
+ mainJsonFile);
+        }
+
+        File file = new File(docDocDir, "main.adoc");
+        boolean exists = file.exists();
+        boolean updated;
+        try {
+            String json = PackageHelper.loadText(mainJsonFile);
+            MainModel model = JsonMapper.generateMainModel(json);
+            String options = evaluateTemplate("main-options.mvel", model);
+            updated = updateOptionsIn(file, "main", options);
+        } catch (IOException e) {
+            throw new MojoExecutionException("Error preparing main docs", e);
+        }
+
+        if (updated) {
+            getLog().info("Updated doc file: " + file);
+        } else if (exists) {
+            getLog().debug("No changes to doc file: " + file);
+        } else {
+            getLog().warn("No main doc file: " + file);
+        }
+    }
+
+    private static String evaluateTemplate(final String templateName, final 
Object model) throws MojoExecutionException {
+        try (InputStream templateStream = 
UpdateReadmeMojo.class.getClassLoader().getResourceAsStream(templateName)) {
+            String template = PackageHelper.loadText(templateStream);
+            return (String) TemplateRuntime.eval(template, model, 
Collections.singletonMap("util", MvelHelper.INSTANCE));
+        } catch (IOException e) {
+            throw new MojoExecutionException("Error processing mvel template 
`" + templateName + "`", e);
+        }
+    }
+
+    private boolean updateOptionsIn(final File file, final String kind, final 
String changed) throws MojoExecutionException {
+        if (!file.exists()) {
+            return false;
+        }
+
+        final String updated = changed.trim();
+        try {
+            String text = PackageHelper.loadText(file);
+
+            String existing = Strings.between(text, "// " + kind + " options: 
START", "// " + kind + " options: END");
+            if (existing != null) {
+                // remove leading line breaks etc
+                existing = existing.trim();
+                if (existing.equals(updated)) {
+                    return false;
+                }
+
+                String before = Strings.before(text, "// " + kind + " options: 
START");
+                String after = Strings.after(text, "// " + kind + " options: 
END");
+                text = before + "// " + kind + " options: START\n" + updated + 
"\n// " + kind + " options: END" + after;
+                PackageHelper.writeText(file, text);
+                return true;
+            }
+
+            getLog().warn("Cannot find markers in file " + file);
+            getLog().warn("Add the following markers");
+            getLog().warn("\t// " + kind + " options: START");
+            getLog().warn("\t// " + kind + " options: END");
+            return false;
+        } catch (IOException e) {
+            throw new MojoExecutionException("Error reading file " + file + " 
Reason: " + e, e);
+        }
+    }
+
+}
diff --git 
a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PrepareCamelMainMojo.java
 
b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PrepareCamelMainMojo.java
index 2909e51..9091913 100644
--- 
a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PrepareCamelMainMojo.java
+++ 
b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PrepareCamelMainMojo.java
@@ -46,13 +46,13 @@ import org.sonatype.plexus.build.incremental.BuildContext;
  * Prepares camel-main by generating Camel Main configuration metadata for
  * tooling support.
  */
-@Mojo(name = "prepare-main", defaultPhase = LifecyclePhase.PROCESS_CLASSES, 
threadSafe = true, requiresDependencyResolution = ResolutionScope.COMPILE)
+@Mojo(name = "prepare-main-doc", defaultPhase = 
LifecyclePhase.PROCESS_CLASSES, threadSafe = true, requiresDependencyResolution 
= ResolutionScope.COMPILE)
 public class PrepareCamelMainMojo extends AbstractGeneratorMojo {
 
     /**
      * The output directory for generated spring boot tooling file
      */
-    @Parameter(defaultValue = "${project.basedir}/src/generated/resources")
+    @Parameter(defaultValue = "${project.basedir}/src/main/doc")
     protected File outFolder;
 
     /**
diff --git 
a/tooling/maven/camel-package-maven-plugin/src/main/resources/main-options.mvel 
b/tooling/maven/camel-package-maven-plugin/src/main/resources/main-options.mvel
new file mode 100644
index 0000000..7c95c10
--- /dev/null
+++ 
b/tooling/maven/camel-package-maven-plugin/src/main/resources/main-options.mvel
@@ -0,0 +1,7 @@
+@if{!options.isEmpty()}
+[width="100%",cols="2,5,^1,2",options="header"]
+|===@comment{ Render table cells. If description contains newline, prefix cell 
with `a`, so the content is rendered with formatting. }
+| Name | Description | Default | Type
+@foreach{row : options}| *@{row.getShortName(30)}* (@{row.shortGroup}) 
@{row.description.?contains("\n") ? "a" : ""}| @{util.escape(row.description)} 
| @{row.getShortDefaultValue(20)} | @{row.getShortJavaType()}
+@end{}|===
+@end{}

Reply via email to