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);
}
}
Aggregations