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:38 UTC

[2/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/1001f1ae
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/1001f1ae
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/1001f1ae

Branch: refs/heads/master
Commit: 1001f1aeec52702e16c6a957480a2c06748bde03
Parents: 07bdbad
Author: Claus Ibsen <da...@apache.org>
Authored: Tue Dec 29 12:37:20 2015 +0100
Committer: Claus Ibsen <da...@apache.org>
Committed: Tue Jan 26 19:45:16 2016 +0100

----------------------------------------------------------------------
 .../camel/maven/packaging/JSonSchemaHelper.java |  10 ++
 .../maven/packaging/ReadmeComponentMojo.java    | 113 +++++++++++++++++--
 2 files changed, 113 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/1001f1ae/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 9b6732e..9b1c1a2 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
@@ -112,4 +112,14 @@ public final class JSonSchemaHelper {
         return value;
     }
 
+    public static String getValue(String key, List<Map<String, String>> rows) {
+        for (Map<String, String> row : rows) {
+            String value = row.get(key);
+            if (value != null) {
+                return value;
+            }
+        }
+        return null;
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/1001f1ae/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 8ecfe7d..e1c2f23 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
@@ -17,16 +17,26 @@
 package org.apache.camel.maven.packaging;
 
 import java.io.File;
+import java.io.FileInputStream;
 import java.io.FilenameFilter;
 import java.io.IOException;
 import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.TreeSet;
 
+import org.apache.maven.model.Resource;
 import org.apache.maven.plugin.AbstractMojo;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugin.MojoFailureException;
 import org.apache.maven.project.MavenProject;
 import org.sonatype.plexus.build.incremental.BuildContext;
 
+import static org.apache.camel.maven.packaging.JSonSchemaHelper.getValue;
+import static org.apache.camel.maven.packaging.PackageHelper.loadText;
+
 /**
  * Generate or updates the component readme.md file in the project root directort.
  *
@@ -61,6 +71,98 @@ public class ReadmeComponentMojo extends AbstractMojo {
 
     @Override
     public void execute() throws MojoExecutionException, MojoFailureException {
+        // find the component names
+        List<String> componentNames = findComponentNames();
+
+        final Set<File> jsonFiles = new TreeSet<File>();
+        PackageHelper.findJsonFiles(buildDir, jsonFiles, new PackageHelper.CamelComponentsModelFilter());
+
+        // only if there is components we should create/update the readme file
+        if (!componentNames.isEmpty()) {
+            getLog().info("Found " + componentNames.size() + " components");
+            File readmeFile = initReadMeFile();
+
+            for (String componentName : componentNames) {
+                String json = loadComponentJson(jsonFiles, componentName);
+                if (json != null) {
+                    updateReadMeFile(readmeFile, componentName, json);
+                }
+            }
+        }
+    }
+
+    private String loadComponentJson(Set<File> jsonFiles, String componentName) {
+        try {
+            for (File file : jsonFiles) {
+                if (file.getName().equals(componentName + ".json")) {
+                    String json = loadText(new FileInputStream(file));
+                    boolean isComponent = json.contains("\"kind\": \"component\"");
+                    if (isComponent) {
+                        return json;
+                    }
+                }
+            }
+        } catch (IOException e) {
+            // ignore
+        }
+        return null;
+    }
+
+    private void updateReadMeFile(File readmeFile, String componentName, String json) throws MojoExecutionException {
+        // TODO: use some template like velocity or freemarker
+
+        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);
+
+        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);
+        }
+    }
+
+    private List<String> findComponentNames() {
+        List<String> componentNames = new ArrayList<String>();
+        for (Resource r : project.getBuild().getResources()) {
+            File f = new File(r.getDirectory());
+            if (!f.exists()) {
+                f = new File(project.getBasedir(), r.getDirectory());
+            }
+            f = new File(f, "META-INF/services/org/apache/camel/component");
+
+            if (f.exists() && f.isDirectory()) {
+                File[] files = f.listFiles();
+                if (files != null) {
+                    for (File file : files) {
+                        // skip directories as there may be a sub .resolver directory
+                        if (file.isDirectory()) {
+                            continue;
+                        }
+                        String name = file.getName();
+                        if (name.charAt(0) != '.') {
+                            componentNames.add(name);
+                        }
+                    }
+                }
+            }
+        }
+        return componentNames;
+    }
+
+    private File initReadMeFile() throws MojoExecutionException {
         File readmeDir = new File(buildDir, "..");
         File readmeFile = new File(readmeDir, "readme.md");
 
@@ -82,16 +184,7 @@ public class ReadmeComponentMojo extends AbstractMojo {
             getLog().info("Creating new readme.md file");
         }
 
-        try {
-            OutputStream os = buildContext.newFileOutputStream(readmeFile);
-            os.write("Hello World".getBytes());
-            os.close();
-
-        } catch (IOException e) {
-            throw new MojoExecutionException("Failed to write to " + readmeFile + ". Reason: " + e, e);
-        }
-
+        return readmeFile;
     }
 
-
 }