Skip to content

Commit

Permalink
feat: add jbang and gav output formats
Browse files Browse the repository at this point in the history
Closes #306
  • Loading branch information
mthmulders committed May 7, 2024
1 parent 980371e commit 4b84a0c
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <type>` or `--format=<type>`.
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
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/it/mulders/mcs/cli/Cli.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 67,7 @@ public class SearchCommand implements Callable<Integer> {
description = """
Show result in <type> 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 = "<type>"
)
Expand Down
19 changes: 16 additions & 3 deletions src/main/java/it/mulders/mcs/search/FormatType.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand All @@ -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;
Expand All @@ -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())));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
11 changes: 11 additions & 0 deletions src/main/java/it/mulders/mcs/search/printer/GavOutput.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
11 changes: 11 additions & 0 deletions src/main/java/it/mulders/mcs/search/printer/JBangOutput.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -86,7 88,9 @@ private static Stream<Arguments> 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)
);
}

Expand Down

0 comments on commit 4b84a0c

Please sign in to comment.