From c2a215d7b517aac60fd006051008b05324f52dfe Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Thu, 29 Feb 2024 18:47:29 +0100 Subject: [PATCH] [cli] Display a PMD banner and more detailed version info Generated via https://www.asciiart.eu/image-to-ascii from docs/images/logo/pmd-logo-300px.png --- .../internal/AbstractPmdSubcommand.java | 5 +++ .../commands/internal/PMDVersionProvider.java | 23 +++++++++++ .../cli/commands/internal/PmdRootCommand.java | 11 ------ .../pmd/cli/internal/PmdBanner.java | 39 +++++++++++++++++++ .../sourceforge/pmd/cli/internal/banner.txt | 8 ++++ 5 files changed, 75 insertions(+), 11 deletions(-) create mode 100644 pmd-cli/src/main/java/net/sourceforge/pmd/cli/commands/internal/PMDVersionProvider.java create mode 100644 pmd-cli/src/main/java/net/sourceforge/pmd/cli/internal/PmdBanner.java create mode 100644 pmd-cli/src/main/resources/net/sourceforge/pmd/cli/internal/banner.txt 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 bd55b1cc14..9dc1b3db34 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 @@ -5,8 +5,11 @@ package net.sourceforge.pmd.cli.commands.internal; import java.util.concurrent.Callable; +import java.util.stream.Collectors; +import net.sourceforge.pmd.PMDVersion; import net.sourceforge.pmd.cli.internal.CliExitCode; +import net.sourceforge.pmd.cli.internal.PmdBanner; import picocli.CommandLine.Model.CommandSpec; import picocli.CommandLine.Option; @@ -26,6 +29,8 @@ public abstract class AbstractPmdSubcommand implements Callable { @Override public final Integer call() throws Exception { + System.err.println(PmdBanner.loadBanner().stream().collect(Collectors.joining(System.lineSeparator()))); + System.err.println(PMDVersion.getFullVersionName()); validate(); return execute().getExitCode(); } diff --git a/pmd-cli/src/main/java/net/sourceforge/pmd/cli/commands/internal/PMDVersionProvider.java b/pmd-cli/src/main/java/net/sourceforge/pmd/cli/commands/internal/PMDVersionProvider.java new file mode 100644 index 0000000000..d35e80b497 --- /dev/null +++ b/pmd-cli/src/main/java/net/sourceforge/pmd/cli/commands/internal/PMDVersionProvider.java @@ -0,0 +1,23 @@ +/* + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html + */ + +package net.sourceforge.pmd.cli.commands.internal; + +import java.util.ArrayList; +import java.util.List; + +import net.sourceforge.pmd.PMDVersion; +import net.sourceforge.pmd.cli.internal.PmdBanner; + +import picocli.CommandLine; + +class PMDVersionProvider implements CommandLine.IVersionProvider { + @Override + public String[] getVersion() throws Exception { + List lines = new ArrayList<>(PmdBanner.loadBanner()); + lines.add(PMDVersion.getFullVersionName()); + lines.add("Java version: " + System.getProperty("java.version") + ", vendor: " + System.getProperty("java.vendor") + ", runtime: " + System.getProperty("java.home")); + return lines.toArray(new String[0]); + } +} 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 169690f98a..1ef0bcc74a 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,11 +4,8 @@ package net.sourceforge.pmd.cli.commands.internal; -import net.sourceforge.pmd.PMDVersion; - import picocli.AutoComplete.GenerateCompletion; import picocli.CommandLine.Command; -import picocli.CommandLine.IVersionProvider; @Command(name = "pmd", mixinStandardHelpOptions = true, versionProvider = PMDVersionProvider.class, @@ -20,11 +17,3 @@ import picocli.CommandLine.IVersionProvider; public class PmdRootCommand { } - -class PMDVersionProvider implements IVersionProvider { - - @Override - public String[] getVersion() throws Exception { - return new String[] { "PMD " + PMDVersion.VERSION }; - } -} diff --git a/pmd-cli/src/main/java/net/sourceforge/pmd/cli/internal/PmdBanner.java b/pmd-cli/src/main/java/net/sourceforge/pmd/cli/internal/PmdBanner.java new file mode 100644 index 0000000000..9807d274d2 --- /dev/null +++ b/pmd-cli/src/main/java/net/sourceforge/pmd/cli/internal/PmdBanner.java @@ -0,0 +1,39 @@ +/* + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html + */ + +package net.sourceforge.pmd.cli.internal; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.List; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public final class PmdBanner { + private static final Logger LOGGER = LoggerFactory.getLogger(PmdBanner.class); + + private static final String BANNER_RESOURCE = "/net/sourceforge/pmd/cli/internal/banner.txt"; + + private PmdBanner() {} + + public static List loadBanner() { + List lines = new ArrayList<>(); + + try (BufferedReader bannerReader = new BufferedReader( + new InputStreamReader(PmdBanner.class.getResourceAsStream(BANNER_RESOURCE), StandardCharsets.UTF_8))) { + String line = bannerReader.readLine(); + while (line != null) { + lines.add(line); + line = bannerReader.readLine(); + } + } catch (IOException e) { + LOGGER.debug("Couldn't load banner", e); + } + return lines; + } +} diff --git a/pmd-cli/src/main/resources/net/sourceforge/pmd/cli/internal/banner.txt b/pmd-cli/src/main/resources/net/sourceforge/pmd/cli/internal/banner.txt new file mode 100644 index 0000000000..d1cad9dba1 --- /dev/null +++ b/pmd-cli/src/main/resources/net/sourceforge/pmd/cli/internal/banner.txt @@ -0,0 +1,8 @@ + ████ ████ + ██ ██ + ██ █████ █ ███ ███ ███████ ██ + ███ ██ ███ ████ ████ ██ ██ ███ + ███ ███████ ██ ████ ██ ██ ██ ███ + ██ ██ ██ ██ ██ ███████ ██ + ██ ██ + ████ ████