From 83418404c5c828f097e71fff01f8c216126d8eab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Mart=C3=ADn=20Sotuyo=20Dodero?= Date: Tue, 9 Aug 2022 19:06:18 -0300 Subject: [PATCH] Draft DesignerCommand to identify gaps --- .../java/net/sourceforge/pmd/cli/PmdCli.java | 2 +- .../internal/AbstractPmdSubcommand.java | 2 +- .../commands/internal/DesignerCommand.java | 71 +++++++++++++++++++ .../cli/commands/internal/PmdRootCommand.java | 3 +- 4 files changed, 74 insertions(+), 4 deletions(-) create mode 100644 pmd-cli/src/main/java/net/sourceforge/pmd/cli/commands/internal/DesignerCommand.java diff --git a/pmd-cli/src/main/java/net/sourceforge/pmd/cli/PmdCli.java b/pmd-cli/src/main/java/net/sourceforge/pmd/cli/PmdCli.java index b19b854702..fec55153fa 100644 --- a/pmd-cli/src/main/java/net/sourceforge/pmd/cli/PmdCli.java +++ b/pmd-cli/src/main/java/net/sourceforge/pmd/cli/PmdCli.java @@ -7,7 +7,7 @@ public class PmdCli { public static void main(String[] args) { new CommandLine(new PmdRootCommand()).setCaseInsensitiveEnumValuesAllowed(true) - .execute("run", "-h"); + .execute("designer", "--version"); // .execute("run", "--use-version", "scala-2.11", "--use-version", "apex", "--use-version", // "ecmascript-latest", "-P", "foo=bar", "-R", "foo,bar", "-R", "baz", "-d", // "src/main/java", "-f", "xml"); diff --git a/pmd-cli/src/main/java/net/sourceforge/pmd/cli/commands/internal/AbstractPmdSubcommand.java b/pmd-cli/src/main/java/net/sourceforge/pmd/cli/commands/internal/AbstractPmdSubcommand.java index af938de078..6e33803a03 100644 --- a/pmd-cli/src/main/java/net/sourceforge/pmd/cli/commands/internal/AbstractPmdSubcommand.java +++ b/pmd-cli/src/main/java/net/sourceforge/pmd/cli/commands/internal/AbstractPmdSubcommand.java @@ -19,7 +19,7 @@ public abstract class AbstractPmdSubcommand implements Callable { @Option(names = { "-h", "--help" }, usageHelp = true, description = "Show this help message and exit.") protected boolean helpRequested; - @Option(names = { "--debug", "--verbose", "-D", "-V" }, description = "Debug mode.") + @Option(names = { "--debug", "--verbose", "-D", "-v" }, description = "Debug mode.") protected boolean debug; @Override diff --git a/pmd-cli/src/main/java/net/sourceforge/pmd/cli/commands/internal/DesignerCommand.java b/pmd-cli/src/main/java/net/sourceforge/pmd/cli/commands/internal/DesignerCommand.java new file mode 100644 index 0000000000..8d45c9c495 --- /dev/null +++ b/pmd-cli/src/main/java/net/sourceforge/pmd/cli/commands/internal/DesignerCommand.java @@ -0,0 +1,71 @@ +package net.sourceforge.pmd.cli.commands.internal; + +import javax.swing.JOptionPane; + +import net.sourceforge.pmd.cli.internal.ExecutionResult; +import net.sourceforge.pmd.util.fxdesigner.Designer; +import net.sourceforge.pmd.util.fxdesigner.DesignerStarter; +import picocli.CommandLine.Command; +import picocli.CommandLine.IVersionProvider; +import picocli.CommandLine.Option; + +@Command(name = "designer", showDefaultValues = true, + versionProvider = DesignerVersionProvider.class, + description = "The PMD visual rule designer") +public class DesignerCommand extends AbstractPmdSubcommand { + + @Option(names = {"-V", "--version"}, versionHelp = true, description = "Print version information and exit.") + private boolean versionRequested; + + // TODO : Until a Designer version is released making DesignerStarter.launchGui() public we need to copy these… + // launchGui should probably be changed to take no arguments, and return an int with the exit status code + private static final String MISSING_JAVAFX = + "You seem to be missing the JavaFX runtime." + System.lineSeparator() + + " Please install JavaFX on your system and try again." + System.lineSeparator() + + " See https://gluonhq.com/products/javafx/"; + + private static final int ERROR_EXIT = 1; + private static final int OK = 0; + + private static boolean isJavaFxAvailable() { + try { + DesignerStarter.class.getClassLoader().loadClass("javafx.application.Application"); + return true; + } catch (ClassNotFoundException | LinkageError e) { + return false; + } + } + + private static int launchGui() { + String message = null; + if (!isJavaFxAvailable()) { + message = MISSING_JAVAFX; + } + + if (message != null) { + System.err.println(message); + JOptionPane.showMessageDialog(null, message); + return ERROR_EXIT; + } + + // TODO : add JavaFX to manually launch app… + //Application.launch(Designer.class, args); + return OK; + } + + @Override + protected ExecutionResult execute() { + final int status = launchGui(); + return status == OK ? ExecutionResult.OK : ExecutionResult.ERROR; + } +} + +class DesignerVersionProvider implements IVersionProvider { + + // TODO : Since getCurrentVersion is within Designer, we can't ask for the version without JavaFX or we will face a NoClassDefFoundError + @Override + public String[] getVersion() throws Exception { + return new String[] { "PMD Rule Designer " + Designer.getCurrentVersion() }; + } + +} \ No newline at end of file diff --git a/pmd-cli/src/main/java/net/sourceforge/pmd/cli/commands/internal/PmdRootCommand.java b/pmd-cli/src/main/java/net/sourceforge/pmd/cli/commands/internal/PmdRootCommand.java index 6c5f03c346..aabf9f16a6 100644 --- a/pmd-cli/src/main/java/net/sourceforge/pmd/cli/commands/internal/PmdRootCommand.java +++ b/pmd-cli/src/main/java/net/sourceforge/pmd/cli/commands/internal/PmdRootCommand.java @@ -4,13 +4,12 @@ import net.sourceforge.pmd.PMDVersion; import picocli.CommandLine.Command; import picocli.CommandLine.IVersionProvider; -// TODO : Status code 4 is actually contingent on using --fail-on-violation… we need to raise that to a common flag @Command(name = "pmd", mixinStandardHelpOptions = true, versionProvider = PMDVersionProvider.class, exitCodeListHeading = "Exit Codes:%n", exitCodeList = { "0:Succesful analysis, no violations found", "1:An unexpected error occurred during execution", "2:Usage error, please refer to the command help", "4:Successful analysis, at least 1 violation found" }, - subcommands = { PmdCommand.class, CpdCommand.class }) + subcommands = { PmdCommand.class, CpdCommand.class, DesignerCommand.class }) public class PmdRootCommand { }