karaf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jbono...@apache.org
Subject karaf git commit: KARAF-4240 - Add asciidoc command help printer
Date Mon, 04 Jan 2016 13:14:07 GMT
Repository: karaf
Updated Branches:
  refs/heads/karaf-3.0.x 4c5ef5c76 -> 374ce6cbe


KARAF-4240 - Add asciidoc command help printer


Project: http://git-wip-us.apache.org/repos/asf/karaf/repo
Commit: http://git-wip-us.apache.org/repos/asf/karaf/commit/374ce6cb
Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/374ce6cb
Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/374ce6cb

Branch: refs/heads/karaf-3.0.x
Commit: 374ce6cbefdb84cf9cc692c984f6ae92e355becf
Parents: 4c5ef5c
Author: Jean-Baptiste Onofré <jbonofre@apache.org>
Authored: Mon Jan 4 14:13:55 2016 +0100
Committer: Jean-Baptiste Onofré <jbonofre@apache.org>
Committed: Mon Jan 4 14:13:55 2016 +0100

----------------------------------------------------------------------
 .../commands/AsciiDoctorCommandHelpPrinter.java | 146 +++++++++++++++++++
 .../tooling/commands/GenerateHelpMojo.java      |  32 +++-
 2 files changed, 171 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/karaf/blob/374ce6cb/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/commands/AsciiDoctorCommandHelpPrinter.java
----------------------------------------------------------------------
diff --git a/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/commands/AsciiDoctorCommandHelpPrinter.java
b/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/commands/AsciiDoctorCommandHelpPrinter.java
new file mode 100644
index 0000000..58207f8
--- /dev/null
+++ b/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/commands/AsciiDoctorCommandHelpPrinter.java
@@ -0,0 +1,146 @@
+/**
+ * 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.karaf.tooling.commands;
+
+import org.apache.karaf.shell.commands.*;
+import org.apache.karaf.shell.commands.meta.ActionMetaData;
+
+import java.io.PrintStream;
+import java.lang.reflect.Field;
+import java.util.*;
+
+/**
+ * Prints documentation in asciidoc syntax
+ */
+public class AsciiDoctorCommandHelpPrinter implements CommandHelpPrinter {
+
+    @Override
+    public void printHelp(Action action, ActionMetaData actionMeta, PrintStream out, boolean
includeHelpOption) {
+
+        Map<Option, Field> optionsMap = actionMeta.getOptions();
+        Map<Argument, Field> argsMap = actionMeta.getArguments();
+        Command command = action.getClass().getAnnotation(Command.class);
+        List<Argument> arguments = new ArrayList<Argument>(argsMap.keySet());
+        Collections.sort(arguments, new Comparator<Argument>() {
+            public int compare(Argument o1, Argument o2) {
+                return Integer.valueOf(o1.index()).compareTo(Integer.valueOf(o2.index()));
+            }
+        });
+        Set<Option> options = new HashSet<Option>(optionsMap.keySet());
+        if (includeHelpOption)
+            options.add(HelpOption.HELP);
+
+        out.println("= " + command.scope() + ":" + command.name());
+        out.println();
+
+        out.println("== Description");
+        out.println(command.description());
+        out.println();
+
+        StringBuffer syntax = new StringBuffer();
+        syntax.append(String.format("%s:%s", command.scope(), command.name()));
+        if (options.size() > 0) {
+            syntax.append(" [options]");
+        }
+        if (arguments.size() > 0) {
+            syntax.append(' ');
+            for (Argument argument : arguments) {
+                syntax.append(String.format(argument.required() ? "%s " : "[%s] ", argument.name()));
+            }
+        }
+        out.println("== Syntax");
+        out.println();
+        out.println(syntax.toString());
+        out.println();
+
+        if (arguments.size() > 0) {
+            out.println("== Arguments");
+            out.println();
+            out.println("|===");
+            out.println("|Name |Description");
+            for (Argument argument : arguments) {
+                out.println("    <tr>");
+                out.println("      <td>" + argument.name() + "</td>");
+                String description = argument.description();
+                if (!argument.required()) {
+                    Object o = actionMeta.getDefaultValue(action, argument);
+                    String defaultValue = actionMeta.getDefaultValueString(o);
+                    if (defaultValue != null) {
+                        description += " (defaults to " + o.toString() + ")";
+                    }
+                }
+                out.println();
+                out.println("| " + argument.name());
+                out.println("| " + description);
+            }
+            out.println("|===");
+            out.println();
+        }
+
+        if (options.size() > 0) {
+            out.println("== Options");
+            out.println();
+            out.println("|===");
+            out.println("|Name |Description");
+
+            for (Option option : options) {
+                String opt = option.name();
+                String description = option.description();
+                for (String alias : option.aliases()) {
+                    opt += ", " + alias;
+                }
+                Object o = actionMeta.getDefaultValue(action, option);
+                String defaultValue = actionMeta.getDefaultValueString(o);
+                if (defaultValue != null) {
+                    description += " (defaults to " + o.toString() + ")";
+                }
+
+                out.println();
+                out.println("|" + opt);
+                out.println("|" + description);
+            }
+            out.println("|===");
+            out.println();
+        }
+
+        String desc = actionMeta.getDetailedDescription();
+        if (desc != null) {
+            out.println("== Details");
+            out.println();
+            out.println(desc);
+            out.println();
+        }
+        out.println();
+    }
+
+    @Override
+    public void printOverview(Map<String, Set<String>> commands, PrintStream
writer) {
+        writer.println("= Commands");
+        writer.println();
+        for (String key : commands.keySet()) {
+            writer.println("== " + key);
+            writer.println();
+            for (String cmd : commands.get(key)) {
+                writer.println("* link:" + key + "-" + cmd + "[" + key + ":" + cmd + "]");
+            }
+        }
+        writer.println();
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/karaf/blob/374ce6cb/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/commands/GenerateHelpMojo.java
----------------------------------------------------------------------
diff --git a/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/commands/GenerateHelpMojo.java
b/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/commands/GenerateHelpMojo.java
index 7416640..1b1da0c 100644
--- a/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/commands/GenerateHelpMojo.java
+++ b/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/commands/GenerateHelpMojo.java
@@ -92,11 +92,12 @@ public class GenerateHelpMojo extends AbstractMojo {
 
     private static final String FORMAT_CONF = "conf";
     private static final String FORMAT_DOCBX = "docbx";
+    private static final String FORMAT_ASCIIDOC = "asciidoc";
 
     public void execute() throws MojoExecutionException, MojoFailureException {
         try {
-            if (!FORMAT_DOCBX.equals(format) && !FORMAT_CONF.equals(format)) {
-                throw new MojoFailureException("Unsupported format: " + format + ". Supported
formats are: docbx or conf.");
+            if (!FORMAT_DOCBX.equals(format) && !FORMAT_CONF.equals(format) &&
!FORMAT_ASCIIDOC.equals(format)) {
+                throw new MojoFailureException("Unsupported format: " + format + ". Supported
formats are: docbx, asciidoc, or conf.");
             }
             if (!targetFolder.exists()) {
                 targetFolder.mkdirs();
@@ -107,14 +108,31 @@ public class GenerateHelpMojo extends AbstractMojo {
             if (classes.isEmpty()) {
                 throw new MojoFailureException("No command found");
             }
-            
-            CommandHelpPrinter helpPrinter = FORMAT_DOCBX.equals(format) 
-                ? new DocBookCommandHelpPrinter()
-                : new UserConfCommandHelpPrinter();
+
+            CommandHelpPrinter helpPrinter = null;
+            if (FORMAT_DOCBX.equals(format)) {
+                helpPrinter = new DocBookCommandHelpPrinter();
+            }
+            if (FORMAT_CONF.equals(format)) {
+                helpPrinter = new UserConfCommandHelpPrinter();
+            }
+            if (FORMAT_ASCIIDOC.equals(format)) {
+                helpPrinter = new AsciiDoctorCommandHelpPrinter();
+            }
 
             Map<String, Set<String>> commands = new TreeMap<String, Set<String>>();
 
-            String commandSuffix = FORMAT_DOCBX.equals(format) ? "xml" : "conf"; 
+            String commandSuffix = null;
+            if (FORMAT_DOCBX.equals(format)) {
+                commandSuffix = "xml";
+            }
+            if (FORMAT_CONF.equals(format)) {
+                commandSuffix = "conf";
+            }
+            if (FORMAT_ASCIIDOC.equals(format)) {
+                commandSuffix = "adoc";
+            }
+             
             for (Class<?> clazz : classes) {
                 try {
                     Action action = (Action) clazz.newInstance();


Mime
View raw message