Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add jbang and gav output formats #306

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
6 changes: 3 additions & 3 deletions src/main/java/it/mulders/mcs/cli/Cli.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package it.mulders.mcs.cli;

import java.util.concurrent.Callable;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please revert the reordering of imports?


import it.mulders.mcs.search.FormatType;
import it.mulders.mcs.search.SearchCommandHandler;
import it.mulders.mcs.search.SearchQuery;
import it.mulders.mcs.search.printer.CoordinatePrinter;
import picocli.CommandLine;

import java.util.concurrent.Callable;

@CommandLine.Command(
name = "mcs",
subcommands = { Cli.SearchCommand.class, Cli.ClassSearchCommand.class },
Expand Down 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
24 changes: 19 additions & 5 deletions src/main/java/it/mulders/mcs/search/FormatType.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
package it.mulders.mcs.search;

import java.util.Arrays;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please revert the reordering of imports?

import java.util.stream.Collectors;

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;

public enum FormatType {
MAVEN("maven", new PomXmlOutput()),
GRADLE("gradle", new GradleGroovyOutput()),
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,27 @@ 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
12 changes: 12 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,12 @@
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);
}
}
12 changes: 12 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,12 @@
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
Loading