Skip to content

Commit

Permalink
Improves Help command.
Browse files Browse the repository at this point in the history
  • Loading branch information
ivangsa committed Mar 28, 2023
1 parent fe11485 commit ee58a2c
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 26 deletions.
4 changes: 3 additions & 1 deletion zenwave-sdk-cli/src/main/java/io/zenwave360/sdk/Help.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@
import io.zenwave360.sdk.templating.HandlebarsEngine;
import io.zenwave360.sdk.templating.TemplateInput;
import io.zenwave360.sdk.utils.Maps;
import picocli.CommandLine;

public class Help {

enum Format {
help, detailed, json, markdown, html, list
list, help, detailed, json, markdown, html;
}

private ObjectMapper objectMapper = new ObjectMapper();
Expand Down Expand Up @@ -116,6 +117,7 @@ protected Map<String, Object> asModel(Object plugin, Field field, DocumentedOpti
}

public String help(Plugin plugin, Format format) {
format = format == null ? Format.help : format;
var model = format == Format.list ? discoverAvailablePlugins() : buildHelpModel(plugin);
model.put("version", getClass().getPackage().getImplementationVersion());
if (format == Format.json) {
Expand Down
69 changes: 47 additions & 22 deletions zenwave-sdk-cli/src/main/java/io/zenwave360/sdk/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ public class Main implements Callable<Integer> {

private Logger log = LoggerFactory.getLogger(getClass());

@Option(names = {"-h", "--help"}, usageHelp = true, description = "display this help message")
boolean help;

@Option(names = {"-f", "--help-format"}, arity = "0..1", description = "Help output format", defaultValue = "help")
Help.Format helpFormat = Help.Format.help;
@Option(names = {"-h", "--help"}, arity = "0..1", description = "Help with output format", converter = HelpFormatConverter.class)
Help.Format helpFormat;

@Option(names = {"-p", "--plugin"}, arity = "0..1", description = "Plugin Class or short-code")
String pluginConfigClass;
String pluginClass;

@Option(names = {"-c", "--chain"}, split = ",", description = "<undocumented> use --plugin instead")
@Option(names = {"-c", "--chain"}, split = ",", description = "<undocumented> use --plugin instead", hidden = true)
Class[] chain;

@Option(names = {"-f", "--force"}, description = "Force overwrite", defaultValue = "false")
boolean forceOverwrite = false;

@CommandLine.Parameters
Map<String, Object> options = new HashMap<>();

Expand All @@ -37,14 +37,21 @@ public static void main(String... args) {
CommandLine cmd = new CommandLine(main);
CommandLine.ParseResult parsed = cmd.parseArgs(args);

if (parsed.hasMatchedOption("h") && (parsed.hasMatchedOption("p") || parsed.hasMatchedOption("f"))) {
try {
main.help();
} catch (Exception e) {
e.printStackTrace();
}
boolean noOptions = !parsed.hasMatchedOption("h") && !parsed.hasMatchedOption("p");
boolean noPlugin = !parsed.hasMatchedOption("p");
boolean usage = parsed.hasMatchedOption("h") && !parsed.hasMatchedOption("p") && main.helpFormat == null;

if(usage || noOptions || noPlugin) {
cmd.usage(System.out);
main.helpFormat = Help.Format.list;
main.help();
return;
}
if (parsed.hasMatchedOption("h") && parsed.hasMatchedOption("p")) {
main.help();
return;
}


int returnCode = cmd.execute(args);
if (returnCode != 0) {
Expand All @@ -54,22 +61,40 @@ public static void main(String... args) {

@Override
public Integer call() throws Exception {
Plugin plugin = Plugin.of(this.pluginConfigClass)
if(forceOverwrite) {
options.put("forceOverwrite", true);
}
Plugin plugin = Plugin.of(this.pluginClass)
.withSpecFile((String) options.get("specFile"))
.withTargetFolder((String) options.get("targetFolder"))
.withForceOverwrite(forceOverwrite)
.withOptions(options)
.withChain(chain);
new MainGenerator().generate(plugin);
return 0;
}

public void help() throws Exception {
Plugin plugin = Plugin.of(this.pluginConfigClass)
.withSpecFile((String) options.get("specFile"))
.withTargetFolder((String) options.get("targetFolder"))
.withOptions(options)
.withChain(chain);
String help = new Help().help(plugin, helpFormat);
System.out.println(help);
public void help() {
try {
Plugin plugin = Plugin.of(this.pluginClass)
.withSpecFile((String) options.get("specFile"))
.withTargetFolder((String) options.get("targetFolder"))
.withOptions(options)
.withChain(chain);
String help = new Help().help(plugin, helpFormat);
System.out.println(help);
} catch (Exception e) {
e.printStackTrace();
}
}

private static class HelpFormatConverter implements CommandLine.ITypeConverter<Help.Format> {
@Override
public Help.Format convert(String value) throws Exception {
if(value == null || value.isEmpty()) {
return null;
}
return Help.Format.valueOf(value);
}
}
}
7 changes: 7 additions & 0 deletions zenwave-sdk-cli/src/main/java/io/zenwave360/sdk/Plugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public class Plugin {
public String targetFolder;
private List<Class> chain;

private boolean forceOverwrite = false;

private Map<String, Object> options = new HashMap<>();

private ClassLoader projectClassLoader;
Expand Down Expand Up @@ -74,6 +76,11 @@ public Plugin withProjectClassLoader(ClassLoader projectClassLoader) {
return this;
}

public Plugin withForceOverwrite(boolean forceOverwrite) {
this.forceOverwrite = forceOverwrite;
return this;
}

public Plugin withOption(String name, Object value) {
String lastPath = name;
Map<String, Object> nestedTempObject = options;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public class TemplateFileWriter implements TemplateWriter {
@DocumentedOption(description = "Target folder to generate code to. If left empty, it will print to stdout.")
private File targetFolder;

private boolean forceOverwrite = false;

public TemplateFileWriter withTargetFolder(File targetFolder) {
this.targetFolder = targetFolder;
return this;
Expand All @@ -30,11 +32,15 @@ public void setTargetFolder(File targetFolder) {
this.targetFolder = targetFolder;
}

public void setForceOverwrite(boolean forceOverwrite) {
this.forceOverwrite = forceOverwrite;
}

@Override
public void write(List<TemplateOutput> templateOutputList) {
templateOutputList.stream()
.peek(t -> log.info("Writing template with targetFile: {}", t.getTargetFile()))
.forEach(t -> writeToFile(getFile(t.getTargetFile()), t.getContent(), t.isSkipOverwrite()));
.forEach(t -> writeToFile(getFile(t.getTargetFile()), t.getContent(), !forceOverwrite && t.isSkipOverwrite()));
}

protected File getFile(String fileName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ Available plugins:
{{#each plugins as |plugin| ~}}
{{ljust plugin.plugin.shortCode size=30 pad=" "}} {{{plugin.configClassName}}}: {{{plugin.plugin.title}}} ({{plugin.version}})
{{/each}}

Use: "jbang zw -p <plugin | short-code> -h" to get help on a specific plugin
17 changes: 16 additions & 1 deletion zenwave-sdk-cli/src/test/java/io/zenwave360/sdk/HelpTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,29 @@

public class HelpTest {

@Test
public void testNoOptionsHelp() {
Main.main();
}

@Test
public void testMainHelp() {
Main.main("-h");
}

@Test
public void testPluginHelp() {
Main.main("-h", "-p", NoOpPluginConfiguration.class.getName());
}

@Test
public void testPluginMarkdownHelp() {
Main.main("-h", "markdown", "-p", NoOpPluginConfiguration.class.getName());
}

@Test
public void testDiscoverAvailablePlugins() {
Main.main("-h", "-f", Help.Format.list.toString());
Main.main("-h", Help.Format.list.toString());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ public void testGenerator() throws Exception {
Plugin plugin = new Plugin()
.withSpecFile("classpath:io/zenwave360/sdk/resources/asyncapi/asyncapi-circular-refs.yml")
.withTargetFolder("target/zenwave630/out")
.withChain(DefaultYamlParser.class, AsyncApiProcessor.class, NoOpGenerator.class, TemplateFileWriter.class);
.withChain(DefaultYamlParser.class, AsyncApiProcessor.class, NoOpGenerator.class, TemplateFileWriter.class)
.withOption("forceOverwrite", true);

new MainGenerator().generate(plugin);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public void testMain_with_array_options() {

Main.main(
"-c", StringUtils.join(processors, ","),
"--force",
"specFile=classpath:io/zenwave360/sdk/resources/asyncapi/asyncapi-circular-refs.yml",
"targetFolder=target/zenwave/out",
"inner.specFile=target/zenwave/out",
Expand Down

0 comments on commit ee58a2c

Please sign in to comment.