You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2016/01/26 19:46:39 UTC

[3/5] camel git commit: First cut of mvn goal to generate/update component readme.md file

First cut of mvn goal to generate/update component readme.md file


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/973af27e
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/973af27e
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/973af27e

Branch: refs/heads/master
Commit: 973af27e3d4d366c57f15202f6f5f5199f3babd9
Parents: 1001f1a
Author: Claus Ibsen <da...@apache.org>
Authored: Wed Dec 30 09:03:23 2015 +0100
Committer: Claus Ibsen <da...@apache.org>
Committed: Tue Jan 26 19:45:24 2016 +0100

----------------------------------------------------------------------
 .../maven/camel-package-maven-plugin/pom.xml    |   6 +
 .../camel/maven/packaging/JSonSchemaHelper.java |   8 +
 .../maven/packaging/ReadmeComponentMojo.java    |  76 +++++++---
 .../maven/packaging/model/ComponentModel.java   | 149 +++++++++++++++++++
 .../packaging/model/ComponentOptionModel.java   |  75 ++++++++++
 .../src/main/resources/component-header.ftl     |  22 +++
 .../src/main/resources/component-options.ftl    |   4 +
 7 files changed, 318 insertions(+), 22 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/973af27e/tooling/maven/camel-package-maven-plugin/pom.xml
----------------------------------------------------------------------
diff --git a/tooling/maven/camel-package-maven-plugin/pom.xml b/tooling/maven/camel-package-maven-plugin/pom.xml
index 1129663..68920e9 100644
--- a/tooling/maven/camel-package-maven-plugin/pom.xml
+++ b/tooling/maven/camel-package-maven-plugin/pom.xml
@@ -44,6 +44,12 @@
   <dependencies>
 
     <dependency>
+      <groupId>org.freemarker</groupId>
+      <artifactId>freemarker</artifactId>
+      <version>${freemarker-version}</version>
+    </dependency>
+
+    <dependency>
       <groupId>org.apache.maven</groupId>
       <artifactId>maven-core</artifactId>
     </dependency>

http://git-wip-us.apache.org/repos/asf/camel/blob/973af27e/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/JSonSchemaHelper.java
----------------------------------------------------------------------
diff --git a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/JSonSchemaHelper.java b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/JSonSchemaHelper.java
index 9b1c1a2..ea17772 100644
--- a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/JSonSchemaHelper.java
+++ b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/JSonSchemaHelper.java
@@ -122,4 +122,12 @@ public final class JSonSchemaHelper {
         return null;
     }
 
+    public static String getValue(String key, Map<String, String> rows) {
+        String value = rows.get(key);
+        if (value != null) {
+            return value;
+        }
+        return null;
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/973af27e/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/ReadmeComponentMojo.java
----------------------------------------------------------------------
diff --git a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/ReadmeComponentMojo.java b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/ReadmeComponentMojo.java
index e1c2f23..59c5f6d 100644
--- a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/ReadmeComponentMojo.java
+++ b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/ReadmeComponentMojo.java
@@ -20,13 +20,17 @@ import java.io.File;
 import java.io.FileInputStream;
 import java.io.FilenameFilter;
 import java.io.IOException;
-import java.io.OutputStream;
+import java.io.StringWriter;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import java.util.TreeSet;
 
+import freemarker.template.Configuration;
+import freemarker.template.Template;
+import org.apache.camel.maven.packaging.model.ComponentModel;
+import org.apache.camel.maven.packaging.model.ComponentOptionModel;
 import org.apache.maven.model.Resource;
 import org.apache.maven.plugin.AbstractMojo;
 import org.apache.maven.plugin.MojoExecutionException;
@@ -85,7 +89,9 @@ public class ReadmeComponentMojo extends AbstractMojo {
             for (String componentName : componentNames) {
                 String json = loadComponentJson(jsonFiles, componentName);
                 if (json != null) {
-                    updateReadMeFile(readmeFile, componentName, json);
+                    ComponentModel model = generateComponentModel(componentName, json);
+                    String component = templateComponent(model);
+                    getLog().info(component);
                 }
             }
         }
@@ -108,29 +114,55 @@ public class ReadmeComponentMojo extends AbstractMojo {
         return null;
     }
 
-    private void updateReadMeFile(File readmeFile, String componentName, String json) throws MojoExecutionException {
-        // TODO: use some template like velocity or freemarker
-
+    private ComponentModel generateComponentModel(String componentName, String json) {
         List<Map<String, String>> rows = JSonSchemaHelper.parseJsonSchema("component", json, false);
-        String scheme = getValue("scheme", rows);
-        String syntax = getValue("syntax", rows);
-        String title = getValue("title", rows);
-        String description = getValue("description", rows);
-        String label = getValue("label", rows);
-        String groupId = getValue("groupId", rows);
-        String artifactId = getValue("artifactId", rows);
-        String version = getValue("version", rows);
+
+        ComponentModel component = new ComponentModel();
+        component.setScheme(getValue("scheme", rows));
+        component.setSyntax(getValue("syntax", rows));
+        component.setTitle(getValue("title", rows));
+        component.setDescription(getValue("description", rows));
+        component.setLabel(getValue("label", rows));
+        component.setDeprecated(getValue("deprecated", rows));
+        component.setConsumerOnly(getValue("consumerOnly", rows));
+        component.setProducerOnly(getValue("producerOnly", rows));
+        component.setJavaType(getValue("javaType", rows));
+        component.setGroupId(getValue("groupId", rows));
+        component.setArtifactId(getValue("artifactId", rows));
+        component.setVersion(getValue("version", rows));
+
+        rows = JSonSchemaHelper.parseJsonSchema("componentProperties", json, true);
+
+        List<ComponentOptionModel> options = new ArrayList<ComponentOptionModel>();
+        ComponentOptionModel option = new ComponentOptionModel();
+        for (Map<String, String> row : rows) {
+            option.setKey(getValue("key", row));
+            option.setKind(getValue("kind", row));
+            option.setType(getValue("type", row));
+            option.setJavaType(getValue("javaType", row));
+            option.setDeprecated(getValue("javaType", row));
+            option.setDescription(getValue("description", row));
+            options.add(option);
+        }
+
+        component.setOptions(options);
+
+        return component;
+    }
+
+    private String templateComponent(ComponentModel model) throws MojoExecutionException {
 
         try {
-            OutputStream os = buildContext.newFileOutputStream(readmeFile);
-            os.write("##".getBytes());
-            os.write(title.getBytes());
-            os.write("\n\n".getBytes());
-            os.write(description.getBytes());
-            os.write("\n\n".getBytes());
-            os.close();
-        } catch (IOException e) {
-            throw new MojoExecutionException("Failed to update " + readmeFile + " file. Reason: " + e, e);
+            String ftl = loadText(ReadmeComponentMojo.class.getClassLoader().getResourceAsStream("component-header.ftl"));
+            Template template = new Template("header", ftl, new Configuration());
+
+            StringWriter buffer = new StringWriter();
+            template.process(model, buffer);
+            buffer.flush();
+
+            return buffer.toString();
+        } catch (Exception e) {
+            throw new MojoExecutionException("Error processing freemarker template. Readon: " + e, e);
         }
     }
 

http://git-wip-us.apache.org/repos/asf/camel/blob/973af27e/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/model/ComponentModel.java
----------------------------------------------------------------------
diff --git a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/model/ComponentModel.java b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/model/ComponentModel.java
new file mode 100644
index 0000000..7e689dd
--- /dev/null
+++ b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/model/ComponentModel.java
@@ -0,0 +1,149 @@
+/**
+ * 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
+ * <p/>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p/>
+ * 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.model;
+
+import java.util.List;
+
+public class ComponentModel {
+
+    private String kind;
+    private String scheme;
+    private String syntax;
+    private String title;
+    private String description;
+    private String label;
+    private String deprecated;
+    private String consumerOnly;
+    private String producerOnly;
+    private String javaType;
+    private String groupId;
+    private String artifactId;
+    private String version;
+    private List<ComponentOptionModel> options;
+
+    public String getKind() {
+        return kind;
+    }
+
+    public void setKind(String kind) {
+        this.kind = kind;
+    }
+
+    public String getScheme() {
+        return scheme;
+    }
+
+    public void setScheme(String scheme) {
+        this.scheme = scheme;
+    }
+
+    public String getSyntax() {
+        return syntax;
+    }
+
+    public void setSyntax(String syntax) {
+        this.syntax = syntax;
+    }
+
+    public String getTitle() {
+        return title;
+    }
+
+    public void setTitle(String title) {
+        this.title = title;
+    }
+
+    public String getDescription() {
+        return description;
+    }
+
+    public void setDescription(String description) {
+        this.description = description;
+    }
+
+    public String getLabel() {
+        return label;
+    }
+
+    public void setLabel(String label) {
+        this.label = label;
+    }
+
+    public String getDeprecated() {
+        return deprecated;
+    }
+
+    public void setDeprecated(String deprecated) {
+        this.deprecated = deprecated;
+    }
+
+    public String getConsumerOnly() {
+        return consumerOnly;
+    }
+
+    public void setConsumerOnly(String consumerOnly) {
+        this.consumerOnly = consumerOnly;
+    }
+
+    public String getProducerOnly() {
+        return producerOnly;
+    }
+
+    public void setProducerOnly(String producerOnly) {
+        this.producerOnly = producerOnly;
+    }
+
+    public String getJavaType() {
+        return javaType;
+    }
+
+    public void setJavaType(String javaType) {
+        this.javaType = javaType;
+    }
+
+    public String getGroupId() {
+        return groupId;
+    }
+
+    public void setGroupId(String groupId) {
+        this.groupId = groupId;
+    }
+
+    public String getArtifactId() {
+        return artifactId;
+    }
+
+    public void setArtifactId(String artifactId) {
+        this.artifactId = artifactId;
+    }
+
+    public String getVersion() {
+        return version;
+    }
+
+    public void setVersion(String version) {
+        this.version = version;
+    }
+
+    public List<ComponentOptionModel> getOptions() {
+        return options;
+    }
+
+    public void setOptions(List<ComponentOptionModel> options) {
+        this.options = options;
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/973af27e/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/model/ComponentOptionModel.java
----------------------------------------------------------------------
diff --git a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/model/ComponentOptionModel.java b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/model/ComponentOptionModel.java
new file mode 100644
index 0000000..31df0dd
--- /dev/null
+++ b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/model/ComponentOptionModel.java
@@ -0,0 +1,75 @@
+/**
+ * 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
+ * <p/>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p/>
+ * 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.model;
+
+public class ComponentOptionModel {
+
+    private String key;
+    private String kind;
+    private String type;
+    private String javaType;
+    private String deprecated;
+    private String description;
+
+    public String getKey() {
+        return key;
+    }
+
+    public void setKey(String key) {
+        this.key = key;
+    }
+
+    public String getKind() {
+        return kind;
+    }
+
+    public void setKind(String kind) {
+        this.kind = kind;
+    }
+
+    public String getType() {
+        return type;
+    }
+
+    public void setType(String type) {
+        this.type = type;
+    }
+
+    public String getJavaType() {
+        return javaType;
+    }
+
+    public void setJavaType(String javaType) {
+        this.javaType = javaType;
+    }
+
+    public String getDeprecated() {
+        return deprecated;
+    }
+
+    public void setDeprecated(String deprecated) {
+        this.deprecated = deprecated;
+    }
+
+    public String getDescription() {
+        return description;
+    }
+
+    public void setDescription(String description) {
+        this.description = description;
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/973af27e/tooling/maven/camel-package-maven-plugin/src/main/resources/component-header.ftl
----------------------------------------------------------------------
diff --git a/tooling/maven/camel-package-maven-plugin/src/main/resources/component-header.ftl b/tooling/maven/camel-package-maven-plugin/src/main/resources/component-header.ftl
new file mode 100644
index 0000000..4d7d47c
--- /dev/null
+++ b/tooling/maven/camel-package-maven-plugin/src/main/resources/component-header.ftl
@@ -0,0 +1,22 @@
+Welcome to ${title}
+
+${description}
+
+The syntax:
+
+    ${syntax}
+
+Maven users would need to add dependency:
+
+```xml
+    <dependency>
+      <groupId>${groupId}</groupId>
+      <artifactId>${artifactId}</artifactId>
+      <version>${version}</version>
+    </dependency>
+```
+
+Some more bla bla
+
+There are ${this.options} options which are listed below
+

http://git-wip-us.apache.org/repos/asf/camel/blob/973af27e/tooling/maven/camel-package-maven-plugin/src/main/resources/component-options.ftl
----------------------------------------------------------------------
diff --git a/tooling/maven/camel-package-maven-plugin/src/main/resources/component-options.ftl b/tooling/maven/camel-package-maven-plugin/src/main/resources/component-options.ftl
new file mode 100644
index 0000000..615c2ad
--- /dev/null
+++ b/tooling/maven/camel-package-maven-plugin/src/main/resources/component-options.ftl
@@ -0,0 +1,4 @@
+Component Options
+
+Generate a table of options
+