From 4b84a0c2e3fed2f59d869d74fb4955bf9efa138f Mon Sep 17 00:00:00 2001 From: Maarten Mulders Date: Sun, 28 Apr 2024 21:44:44 +0200 Subject: [PATCH] feat: add jbang and gav output formats Closes #306 --- README.md | 2 +- src/main/java/it/mulders/mcs/cli/Cli.java | 2 +- .../it/mulders/mcs/search/FormatType.java | 19 ++++++++++++++++--- .../mcs/search/printer/CoordinatePrinter.java | 2 +- .../mulders/mcs/search/printer/GavOutput.java | 11 +++++++++++ .../mcs/search/printer/JBangOutput.java | 11 +++++++++++ .../search/printer/CoordinatePrinterTest.java | 6 +++++- 7 files changed, 46 insertions(+), 7 deletions(-) create mode 100644 src/main/java/it/mulders/mcs/search/printer/GavOutput.java create mode 100644 src/main/java/it/mulders/mcs/search/printer/JBangOutput.java diff --git a/README.md b/README.md index 2b80512c..c93eba39 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ This tool supports the following modes of searching: But if there's only one hit, this will give you by default a pom.xml snippet for the artifact you searched for. Ready for copy & paste in your favourite IDE! If you require snippet in different format, use `-f ` or `--format=`. - Supported types are: `maven`, `gradle`, `gradle-short`, `gradle-kotlin`, `sbt`, `ivy`, `grape`, `leiningen`, `buildr`. + Supported types are: `maven`, `gradle`, `gradle-short`, `gradle-kotlin`, `sbt`, `ivy`, `grape`, `leiningen`, `buildr`, `jbang`, `gav`. 3. **Class-name search** ```console mcs class-search CommandLine diff --git a/src/main/java/it/mulders/mcs/cli/Cli.java b/src/main/java/it/mulders/mcs/cli/Cli.java index b8f8e9a8..8d5fb377 100644 --- a/src/main/java/it/mulders/mcs/cli/Cli.java +++ b/src/main/java/it/mulders/mcs/cli/Cli.java @@ -67,7 +67,7 @@ public class SearchCommand implements Callable { description = """ Show result in format Supported types are: - maven, gradle, gradle-short, gradle-kotlin, sbt, ivy, grape, leiningen, buildr + maven, gradle, gradle-short, gradle-kotlin, sbt, ivy, grape, leiningen, buildr, jbang, gav """, paramLabel = "" ) diff --git a/src/main/java/it/mulders/mcs/search/FormatType.java b/src/main/java/it/mulders/mcs/search/FormatType.java index 33b7cfdc..9a0f8d60 100644 --- a/src/main/java/it/mulders/mcs/search/FormatType.java +++ b/src/main/java/it/mulders/mcs/search/FormatType.java @@ -2,16 +2,19 @@ import it.mulders.mcs.search.printer.BuildrOutput; import it.mulders.mcs.search.printer.CoordinatePrinter; +import it.mulders.mcs.search.printer.GavOutput; import it.mulders.mcs.search.printer.GradleGroovyOutput; import it.mulders.mcs.search.printer.GradleGroovyShortOutput; import it.mulders.mcs.search.printer.GradleKotlinOutput; import it.mulders.mcs.search.printer.GrapeOutput; import it.mulders.mcs.search.printer.IvyXmlOutput; +import it.mulders.mcs.search.printer.JBangOutput; import it.mulders.mcs.search.printer.LeiningenOutput; import it.mulders.mcs.search.printer.PomXmlOutput; import it.mulders.mcs.search.printer.SbtOutput; import java.util.Arrays; +import java.util.stream.Collectors; public enum FormatType { MAVEN("maven", new PomXmlOutput()), @@ -22,7 +25,9 @@ public enum FormatType { IVY("ivy", new IvyXmlOutput()), GRAPE("grape", new GrapeOutput()), LEININGEN("leiningen", new LeiningenOutput()), - BUILDR("buildr", new BuildrOutput()); + BUILDR("buildr", new BuildrOutput()), + JBANG("jbang", new JBangOutput()), + GAV("gav", new GavOutput()); private final String label; private final CoordinatePrinter printer; @@ -36,18 +41,26 @@ public CoordinatePrinter getPrinter() { return printer; } + static String commaSeparatedLabels() { + return Arrays.stream(values()) + .map(type -> type.label) + .collect(Collectors.joining(", ")); + } + public static CoordinatePrinter providePrinter(final String text) { if (text == null) { return Constants.DEFAULT_PRINTER; } if (text.isBlank()) { - throw new UnsupportedFormatException("Empty format type is not allowed."); + throw new UnsupportedFormatException("Empty format type is not allowed. Use on of %s" + .formatted(commaSeparatedLabels())); } return Arrays.stream(values()) .filter(type -> type.label.equals(text.trim())) .map(FormatType::getPrinter) .findFirst() - .orElseThrow(() -> new UnsupportedFormatException("Format type '%s' is not supported.".formatted(text))); + .orElseThrow(() -> new UnsupportedFormatException("Format type '%s' is not supported. Use one of %s" + .formatted(text, commaSeparatedLabels()))); } } diff --git a/src/main/java/it/mulders/mcs/search/printer/CoordinatePrinter.java b/src/main/java/it/mulders/mcs/search/printer/CoordinatePrinter.java index a404228b..855c0bf3 100644 --- a/src/main/java/it/mulders/mcs/search/printer/CoordinatePrinter.java +++ b/src/main/java/it/mulders/mcs/search/printer/CoordinatePrinter.java @@ -11,7 +11,7 @@ public sealed interface CoordinatePrinter extends OutputPrinter permits BuildrOutput, GradleGroovyOutput, GradleGroovyShortOutput, GradleKotlinOutput, GrapeOutput, - IvyXmlOutput, LeiningenOutput, PomXmlOutput, SbtOutput { + IvyXmlOutput, LeiningenOutput, PomXmlOutput, SbtOutput, JBangOutput, GavOutput { String provideCoordinates(final String group, final String artifact, final String version, final String packaging); diff --git a/src/main/java/it/mulders/mcs/search/printer/GavOutput.java b/src/main/java/it/mulders/mcs/search/printer/GavOutput.java new file mode 100644 index 00000000..ed01bbbc --- /dev/null +++ b/src/main/java/it/mulders/mcs/search/printer/GavOutput.java @@ -0,0 +1,11 @@ +package it.mulders.mcs.search.printer; + +public final class GavOutput implements CoordinatePrinter { + @Override + public String provideCoordinates(final String group, final String artifact, final String version, String packaging) { + if("jar".equals(packaging)) + return "%s:%s:%s".formatted(group, artifact, version); + else + return "%s:%s:%s@%s".formatted(group, artifact, version, packaging); + } +} diff --git a/src/main/java/it/mulders/mcs/search/printer/JBangOutput.java b/src/main/java/it/mulders/mcs/search/printer/JBangOutput.java new file mode 100644 index 00000000..5d675273 --- /dev/null +++ b/src/main/java/it/mulders/mcs/search/printer/JBangOutput.java @@ -0,0 +1,11 @@ +package it.mulders.mcs.search.printer; + +public final class JBangOutput implements CoordinatePrinter { + @Override + public String provideCoordinates(final String group, final String artifact, final String version, String packaging) { + if("jar".equals(packaging)) + return "//DEPS %s:%s:%s".formatted(group, artifact, version); + else + return "//DEPS %s:%s:%s@%s".formatted(group, artifact, version, packaging); + } +} diff --git a/src/test/java/it/mulders/mcs/search/printer/CoordinatePrinterTest.java b/src/test/java/it/mulders/mcs/search/printer/CoordinatePrinterTest.java index bdbd2af7..2945d651 100644 --- a/src/test/java/it/mulders/mcs/search/printer/CoordinatePrinterTest.java +++ b/src/test/java/it/mulders/mcs/search/printer/CoordinatePrinterTest.java @@ -72,6 +72,8 @@ class CoordinatePrinterTest implements WithAssertions { """; private static final String LEININGEN_OUTPUT = "[org.codehaus.plexus/plexus-utils \"3.4.1\"]"; private static final String BUILDR_OUTPUT = "'org.codehaus.plexus:plexus-utils:jar:3.4.1'"; + private static final String JBANG_OUTPUT = "//DEPS org.codehaus.plexus:plexus-utils:3.4.1"; + private static final String GAV_OUTPUT = "org.codehaus.plexus:plexus-utils:3.4.1"; private final ByteArrayOutputStream buffer = new ByteArrayOutputStream(); @@ -86,7 +88,9 @@ private static Stream coordinatePrinters() { Arguments.of(new IvyXmlOutput(), IVY_XML_OUTPUT, RESPONSE), Arguments.of(new GrapeOutput(), GRAPE_OUTPUT, RESPONSE), Arguments.of(new LeiningenOutput(), LEININGEN_OUTPUT, RESPONSE), - Arguments.of(new BuildrOutput(), BUILDR_OUTPUT, RESPONSE) + Arguments.of(new BuildrOutput(), BUILDR_OUTPUT, RESPONSE), + Arguments.of(new JBangOutput(), JBANG_OUTPUT, RESPONSE), + Arguments.of(new GavOutput(), GAV_OUTPUT, RESPONSE) ); }