Search in sources :

Example 1 with Action

use of org.apache.karaf.shell.api.action.Action in project karaf by apache.

the class GenerateHelpMojo method execute.

public void execute() throws MojoExecutionException, MojoFailureException {
    try {
        if (!FORMAT_DOCBX.equals(format) && !FORMAT_CONF.equals(format) && !FORMAT_ASCIIDOC.equals(format)) {
            throw new MojoFailureException("Unsupported format: " + format + ". Supported formats are: asciidoc, docbx, or conf.");
        }
        if (!targetFolder.exists()) {
            targetFolder.mkdirs();
        }
        ClassFinder finder = createFinder(classLoader);
        List<Class<?>> classes = finder.findAnnotatedClasses(Command.class);
        if (classes.isEmpty()) {
            throw new MojoFailureException("No command found");
        }
        CommandHelpPrinter helpPrinter = null;
        if (FORMAT_ASCIIDOC.equals(format)) {
            helpPrinter = new AsciiDoctorCommandHelpPrinter();
        }
        if (FORMAT_CONF.equals(format)) {
            helpPrinter = new UserConfCommandHelpPrinter();
        }
        if (FORMAT_DOCBX.equals(format)) {
            helpPrinter = new DocBookCommandHelpPrinter();
        }
        Map<String, Set<String>> commands = new TreeMap<>();
        String commandSuffix = null;
        if (FORMAT_ASCIIDOC.equals(format)) {
            commandSuffix = "adoc";
        }
        if (FORMAT_CONF.equals(format)) {
            commandSuffix = "conf";
        }
        if (FORMAT_DOCBX.equals(format)) {
            commandSuffix = "xml";
        }
        for (Class<?> clazz : classes) {
            try {
                Action action = (Action) clazz.newInstance();
                Command cmd = clazz.getAnnotation(Command.class);
                // skip the *-help command
                if (cmd.scope().equals("*"))
                    continue;
                File output = new File(targetFolder, cmd.scope() + "-" + cmd.name() + "." + commandSuffix);
                FileOutputStream outStream = new FileOutputStream(output);
                PrintStream out = new PrintStream(outStream);
                helpPrinter.printHelp(action, out, includeHelpOption);
                out.close();
                outStream.close();
                commands.computeIfAbsent(cmd.scope(), k -> new TreeSet<>()).add(cmd.name());
                getLog().info("Found command: " + cmd.scope() + ":" + cmd.name());
            } catch (Exception e) {
                getLog().warn("Unable to write help for " + clazz.getName(), e);
            }
        }
        String overViewSuffix = null;
        if (FORMAT_ASCIIDOC.equals(format)) {
            overViewSuffix = "adoc";
        }
        if (FORMAT_CONF.equals(format)) {
            overViewSuffix = "conf";
        }
        if (FORMAT_DOCBX.equals(format)) {
            overViewSuffix = "xml";
        }
        PrintStream writer = new PrintStream(new FileOutputStream(new File(targetFolder, "commands." + overViewSuffix)));
        helpPrinter.printOverview(commands, writer);
        writer.close();
    } catch (Exception e) {
        throw new MojoExecutionException("Error building commands help", e);
    }
}
Also used : PrintStream(java.io.PrintStream) URL(java.net.URL) FileOutputStream(java.io.FileOutputStream) Set(java.util.Set) Parameter(org.apache.maven.plugins.annotations.Parameter) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ClassFinder(org.apache.xbean.finder.ClassFinder) File(java.io.File) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) Command(org.apache.karaf.shell.api.action.Command) MojoFailureException(org.apache.maven.plugin.MojoFailureException) Action(org.apache.karaf.shell.api.action.Action) Mojo(org.apache.maven.plugins.annotations.Mojo) URLClassLoader(java.net.URLClassLoader) List(java.util.List) TreeMap(java.util.TreeMap) MavenProject(org.apache.maven.project.MavenProject) Map(java.util.Map) LifecyclePhase(org.apache.maven.plugins.annotations.LifecyclePhase) ResolutionScope(org.apache.maven.plugins.annotations.ResolutionScope) AbstractMojo(org.apache.maven.plugin.AbstractMojo) PrintStream(java.io.PrintStream) Action(org.apache.karaf.shell.api.action.Action) Set(java.util.Set) TreeSet(java.util.TreeSet) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) TreeMap(java.util.TreeMap) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) Command(org.apache.karaf.shell.api.action.Command) TreeSet(java.util.TreeSet) FileOutputStream(java.io.FileOutputStream) ClassFinder(org.apache.xbean.finder.ClassFinder) File(java.io.File)

Aggregations

File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 PrintStream (java.io.PrintStream)1 URL (java.net.URL)1 URLClassLoader (java.net.URLClassLoader)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 TreeMap (java.util.TreeMap)1 TreeSet (java.util.TreeSet)1 Action (org.apache.karaf.shell.api.action.Action)1 Command (org.apache.karaf.shell.api.action.Command)1 AbstractMojo (org.apache.maven.plugin.AbstractMojo)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1 MojoFailureException (org.apache.maven.plugin.MojoFailureException)1 LifecyclePhase (org.apache.maven.plugins.annotations.LifecyclePhase)1 Mojo (org.apache.maven.plugins.annotations.Mojo)1 Parameter (org.apache.maven.plugins.annotations.Parameter)1 ResolutionScope (org.apache.maven.plugins.annotations.ResolutionScope)1