Skip to content

Commit

Permalink
Implemented all items from issue quarkusio#854 (quarkusio#858)
Browse files Browse the repository at this point in the history
  • Loading branch information
quintesse authored May 7, 2021
1 parent e219f9a commit 0311125
Show file tree
Hide file tree
Showing 12 changed files with 112 additions and 82 deletions.
17 changes: 9 additions & 8 deletions itests/catalog.feature
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
Feature: catalog testing

Scenario: java catalog list
When command('jbang catalog add --global tc jbang-catalog.json')
When command('jbang catalog add --global --name averylongcatalogname jbang-catalog.json')
And command('jbang catalog list')
Then match out contains "tc = JBang test scripts"
Then match out contains "averylongcatalogname"
Then match out contains "JBang test scripts"

Scenario: add catalog and run catalog named reference
When command('jbang catalog add --global tc jbang-catalog.json')
When command('jbang catalog add --global --name tc jbang-catalog.json')
Then command('jbang echo@tc tako')
Then match out == "0:tako\n"

Scenario: add catalog and remove
When command('jbang catalog add --global tc jbang-catalog.json')
When command('jbang catalog add --global --name tc jbang-catalog.json')
Then command('jbang catalog remove tc')
Then command('jbang echo@tc tako')
Then match err contains "Unknown catalog 'tc'"

Scenario: add catalog twice with same name
When command('jbang catalog add --global tc jbang-catalog.json')
Then command('jbang catalog add --global tc jbang-catalog.json')
When command('jbang catalog add --global --name tc jbang-catalog.json')
Then command('jbang catalog add --global --name tc jbang-catalog.json')
Then match exit == 0

Scenario: add catalog twice with different name
When command('jbang catalog add --global tc jbang-catalog.json')
Then command('jbang catalog add --global ct jbang-catalog.json')
When command('jbang catalog add --global --name tc jbang-catalog.json')
Then command('jbang catalog add --global --name ct jbang-catalog.json')
Then command('jbang echo@tc tako')
And match exit == 0
Then command('jbang echo@ct tako')
Expand Down
2 changes: 1 addition & 1 deletion itests/run.feature
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Scenario: java run multiple files using alias
Then match out contains "hello properties"

Scenario: java run multiple files using remote alias
When command('jbang catalog add test https://raw.githubusercontent.com/jbangdev/jbang/master/itests/jbang-catalog.json')
When command('jbang catalog add --name test https://raw.githubusercontent.com/jbangdev/jbang/master/itests/jbang-catalog.json')
Then command('jbang trust add https://raw.githubusercontent.com')
Then command('jbang resource@test')
Then match out contains "hello properties"
Expand Down
6 changes: 3 additions & 3 deletions readme.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1286,11 +1286,11 @@ Catalogs are lists of Aliases as defined in the previous section, but while the
within a catalog, the `catalog` command is for managing references to catalogs. This is mostly useful when dealing with
remote catalogs. You can for example add a catalog like this:

jbang catalog add demo https://github.com/jbangdev/jbang-catalog/blob/master/jbang-catalog.json
jbang catalog add --name demo https://github.com/jbangdev/jbang-catalog/blob/master/jbang-catalog.json

or simply by using the same "implicit" catalog system described in <<Implicit Alias Catalogs>>:

jbang catalog add demo jbangdev
jbang catalog add --name demo jbangdev

The aliases in that catalog are now available by adding `@demo` to their names. For example:

Expand All @@ -1303,7 +1303,7 @@ The aliases in that catalog are now available by adding `@demo` to their names.
[jbang] Building jar...
Hello World!

In fact it's possible to run the alias just by using `jbang run hello`, the `@demo` part is only necessary when trying to
In fact, it's possible to run the alias just by using `jbang run hello`, the `@demo` part is only necessary when trying to
disambiguate between aliases with the same name from different catalogs.

You can list the available catalogs by running:
Expand Down
25 changes: 12 additions & 13 deletions src/main/java/dev/jbang/catalog/Catalog.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,23 @@ public class Catalog {
public final String description;
public transient ResourceRef catalogRef;

public Catalog(String baseRef, String description, ResourceRef catalogRef) {
this.baseRef = baseRef;
this.description = description;
this.catalogRef = catalogRef;
}

public Catalog(String baseRef, String description, ResourceRef catalogRef, Map<String, CatalogRef> catalogs,
Map<String, Alias> aliases, Map<String, Template> templates) {
this.baseRef = baseRef;
this.description = description;
this.catalogRef = catalogRef;
catalogs.forEach((key, c) -> this.catalogs.put(key,
new CatalogRef(c.catalogRef, c.description)));
new CatalogRef(c.catalogRef, c.description, this)));
aliases.forEach((key, a) -> this.aliases.put(key,
new Alias(a.scriptRef, a.description, a.arguments, a.properties, this)));
templates.forEach((key, t) -> this.templates.put(key,
new Template(t.fileRefs, t.description, this)));
}

public static Catalog empty() {
return new Catalog(null, null, null, Collections.emptyMap(), Collections.emptyMap(), Collections.emptyMap());
}

/**
* Returns in all cases the absolute base reference that can be used to resolve
* an Alias' script location. The result will either be a URL or an absolute
Expand Down Expand Up @@ -136,7 +134,7 @@ void write() throws IOException {
}

/**
* Load a Catalog's aliases given the name of a previously registered Catalog
* Load a Catalog given the name of a previously registered Catalog
*
* @param catalogName The name of a registered
* @return An Aliases object
Expand Down Expand Up @@ -232,7 +230,7 @@ public static Catalog getMerged(boolean includeImplicits) {
return false;
});

Catalog result = new Catalog(null, null, null);
Catalog result = Catalog.empty();
Collections.reverse(catalogs);
for (Catalog catalog : catalogs) {
if (!includeImplicits
Expand Down Expand Up @@ -274,7 +272,7 @@ static Catalog findNearestCatalogWith(Path dir, Function<Catalog, Boolean> accep
} else if (accept.apply(getBuiltin())) {
return getBuiltin();
}
return new Catalog(null, null, null);
return Catalog.empty();
}

public static Catalog get(Path catalogPath) {
Expand Down Expand Up @@ -316,7 +314,7 @@ public static String findImplicitName(Catalog catalog) {

// This returns the built-in Catalog that can be found in the resources
public static Catalog getBuiltin() {
Catalog catalog = new Catalog(null, null, null);
Catalog catalog = Catalog.empty();
if (Util.isFresh() || !catalogCache.containsKey(CACHE_BUILTIN)) {
String res = "classpath:/" + JBANG_CATALOG_JSON;
ResourceRef catRef = ResourceRef.forResource(res);
Expand All @@ -338,7 +336,7 @@ public static void clearCache() {

static Catalog read(Path catalogPath) {
Util.verboseMsg(String.format("Reading catalog from %s", catalogPath));
Catalog catalog = new Catalog(null, null, null);
Catalog catalog = Catalog.empty();
if (Files.isRegularFile(catalogPath)) {
try (Reader in = Files.newBufferedReader(catalogPath)) {
catalog = read(in);
Expand All @@ -365,6 +363,7 @@ private static Catalog read(Reader in) {
}
for (String catName : catalog.catalogs.keySet()) {
CatalogRef cat = catalog.catalogs.get(catName);
cat.catalog = catalog;
check(cat.catalogRef != null, "Missing required attribute 'catalogs.catalogRef'");
}
for (String aliasName : catalog.aliases.keySet()) {
Expand All @@ -379,7 +378,7 @@ private static Catalog read(Reader in) {
check(!tpl.fileRefs.isEmpty(), "Attribute 'templates.file-refs' has no elements");
}
} else {
catalog = new Catalog(null, null, null);
catalog = Catalog.empty();
}
return catalog;
}
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/dev/jbang/catalog/CatalogRef.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@
import dev.jbang.cli.ExitException;
import dev.jbang.util.Util;

public class CatalogRef {
public class CatalogRef extends CatalogItem {
@SerializedName(value = "catalog-ref", alternate = { "catalogRef" })
public final String catalogRef;
public final String description;

CatalogRef(String catalogRef, String description) {
CatalogRef(String catalogRef, String description, Catalog catalog) {
super(catalog);
this.catalogRef = catalogRef;
this.description = description;
}
Expand All @@ -33,15 +34,15 @@ public class CatalogRef {
public static CatalogRef createByRefOrImplicit(String catalogRef) {
if (Util.isAbsoluteRef(catalogRef) || Files.isRegularFile(Paths.get(catalogRef))) {
Catalog cat = Catalog.getByRef(catalogRef);
return new CatalogRef(catalogRef, cat.description);
return new CatalogRef(catalogRef, cat.description, cat);
} else {
Optional<String> url = ImplicitCatalogRef.getImplicitCatalogUrl(catalogRef);
if (!url.isPresent()) {
throw new ExitException(EXIT_UNEXPECTED_STATE,
"Unable to locate catalog: " + catalogRef);
}
Catalog cat = Catalog.getByRef(url.get());
return new CatalogRef(url.get(), cat.description);
return new CatalogRef(url.get(), cat.description, cat);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/dev/jbang/catalog/CatalogUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ public static CatalogRef addCatalogRef(Path catalogFile, String name, String cat
} catch (InvalidPathException ex) {
// Ignore
}
CatalogRef ref = new CatalogRef(catalogRef, description);
CatalogRef ref = new CatalogRef(catalogRef, description, catalog);
catalog.catalogs.put(name, ref);
try {
catalog.write();
Expand Down
31 changes: 9 additions & 22 deletions src/main/java/dev/jbang/cli/Alias.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import dev.jbang.source.ResourceRef;
import dev.jbang.source.RunContext;
import dev.jbang.source.Source;
import dev.jbang.util.ConsoleOutput;
import dev.jbang.util.Util;

import picocli.CommandLine;
Expand Down Expand Up @@ -144,7 +145,7 @@ static void printAliasesWithOrigin(PrintStream out, String catalogName, Catalog
Collectors.groupingBy(
e -> e.getValue().catalog.catalogRef));
groups.forEach((ref, entries) -> {
out.println(ref.getOriginalResource());
out.println(ConsoleOutput.bold(ref.getOriginalResource()));
entries.stream().map(Map.Entry::getKey).sorted().forEach(k -> printAlias(out, catalogName, catalog, k, 3));
});
}
Expand All @@ -161,38 +162,24 @@ private static void printAlias(PrintStream out, String catalogName, Catalog cata
}
out.print(Util.repeat(" ", indent));
if (alias.description != null) {
out.println(yellow(fullName) + " = " + alias.description);
out.println(ConsoleOutput.yellow(fullName) + " = " + alias.description);
if (Util.isVerbose())
out.println(Util.repeat(" ", fullName.length() + indent) + faint(" (" + scriptRef + ")"));
out.println(
Util.repeat(" ", fullName.length() + indent) + ConsoleOutput.faint(" (" + scriptRef + ")"));
} else {
out.println(yellow(fullName) + " = " + scriptRef);
out.println(ConsoleOutput.yellow(fullName) + " = " + scriptRef);
}
if (alias.arguments != null) {
out.println(
Util.repeat(" ", fullName.length() + indent) + cyan(" Arguments: ")
Util.repeat(" ", fullName.length() + indent) + ConsoleOutput.cyan(" Arguments: ")
+ String.join(" ", alias.arguments));
}
if (alias.properties != null) {
out.println(
Util.repeat(" ", fullName.length() + indent) + magenta(" Properties: ") + alias.properties);
Util.repeat(" ", fullName.length() + indent) + ConsoleOutput.magenta(" Properties: ")
+ alias.properties);
}
}

private static String yellow(String text) {
return CommandLine.Help.Ansi.AUTO.new Text("@|fg(yellow) " + text + "|@").toString();
}

private static String cyan(String text) {
return CommandLine.Help.Ansi.AUTO.new Text("@|fg(cyan) " + text + "|@").toString();
}

private static String magenta(String text) {
return CommandLine.Help.Ansi.AUTO.new Text("@|fg(magenta) " + text + "|@").toString();
}

private static String faint(String text) {
return CommandLine.Help.Ansi.AUTO.new Text("@|faint " + text + "|@").toString();
}
}

@CommandLine.Command(name = "remove", description = "Remove existing alias.")
Expand Down
52 changes: 42 additions & 10 deletions src/main/java/dev/jbang/cli/Catalog.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import dev.jbang.Settings;
import dev.jbang.catalog.CatalogRef;
import dev.jbang.catalog.CatalogUtil;
import dev.jbang.source.ResourceRef;
import dev.jbang.util.ConsoleOutput;
import dev.jbang.util.Util;

import picocli.CommandLine;
Expand Down Expand Up @@ -57,26 +62,29 @@ class CatalogAdd extends BaseCatalogCommand {
"-d" }, description = "A description for the catalog")
String description;

@CommandLine.Parameters(paramLabel = "name", index = "0", description = "A name for the catalog", arity = "1")
@CommandLine.Option(names = { "--name" }, description = "A name for the alias")
String name;

@CommandLine.Parameters(paramLabel = "urlOrFile", index = "1", description = "A file or URL to a catalog file", arity = "1")
@CommandLine.Parameters(paramLabel = "urlOrFile", index = "0", description = "A file or URL to a catalog file", arity = "1")
String urlOrFile;

@Override
public Integer doCall() {
if (!name.matches("^[a-zA-Z][-.\\w]*$")) {
if (name != null && !dev.jbang.catalog.Catalog.isValidName(name)) {
throw new IllegalArgumentException(
"Invalid catalog name, it should start with a letter followed by 0 or more letters, digits, underscores, hyphens or dots");
}
if (name == null) {
name = CatalogUtil.nameFromRef(urlOrFile);
}
CatalogRef ref = CatalogRef.createByRefOrImplicit(urlOrFile);
Path catFile = getCatalog(false);
if (catFile != null) {
CatalogUtil.addCatalogRef(catFile, name, ref.catalogRef, ref.description);
} else {
catFile = CatalogUtil.addNearestCatalogRef(name, ref.catalogRef, ref.description);
}
info(String.format("Catalog added to %s", catFile));
info(String.format("Catalog '%s' added to '%s'", name, catFile));
return EXIT_OK;
}
}
Expand Down Expand Up @@ -105,6 +113,9 @@ public Integer doCall() {
@CommandLine.Command(name = "list", description = "Show currently defined catalogs.")
class CatalogList extends BaseCatalogCommand {

@CommandLine.Option(names = { "--show-origin" }, description = "Show the origin of the catalog")
boolean showOrigin;

@CommandLine.Parameters(paramLabel = "name", index = "0", description = "The name of a catalog", arity = "0..1")
String name;

Expand All @@ -119,7 +130,11 @@ public Integer doCall() {
} else {
catalog = dev.jbang.catalog.Catalog.getMerged(true);
}
printCatalogs(out, name, catalog);
if (showOrigin) {
printCatalogsWithOrigin(out, name, catalog);
} else {
printCatalogs(out, name, catalog);
}
} else {
dev.jbang.catalog.Catalog catalog = dev.jbang.catalog.Catalog.getByName(name);
if (!catalog.aliases.isEmpty()) {
Expand Down Expand Up @@ -147,21 +162,38 @@ static void printCatalogs(PrintStream out, String catalogName, dev.jbang.catalog
.stream()
.sorted()
.forEach(nm -> {
printCatalog(out, catalogName, catalog, nm);
printCatalog(out, catalogName, catalog, nm, 0);
});
}

static void printCatalogsWithOrigin(PrintStream out, String catalogName, dev.jbang.catalog.Catalog catalog) {
Map<ResourceRef, List<Map.Entry<String, dev.jbang.catalog.CatalogRef>>> groups = catalog.catalogs
.entrySet()
.stream()
.collect(
Collectors.groupingBy(
e -> e.getValue().catalog.catalogRef));
groups.forEach((ref, entries) -> {
out.println(ConsoleOutput.bold(ref.getOriginalResource()));
entries .stream()
.map(Map.Entry::getKey)
.sorted()
.forEach(k -> printCatalog(out, catalogName, catalog, k, 3));
});
}

private static void printCatalog(PrintStream out, String catalogName, dev.jbang.catalog.Catalog catalog,
String name) {
String name, int indent) {
out.print(Util.repeat(" ", indent));
String fullName = catalogName != null ? name + "@" + catalogName : name;
CatalogRef ref = catalog.catalogs.get(name);
if (ref.description != null) {
out.println(fullName + " = " + ref.description);
out.println(Util.repeat(" ", fullName.length()) + " ("
out.println(ConsoleOutput.yellow(fullName) + " = " + ref.description);
out.println(Util.repeat(" ", fullName.length() + indent) + " ("
+ ref.catalogRef
+ ")");
} else {
out.println(fullName + " = " + ref.catalogRef);
out.println(ConsoleOutput.yellow(fullName) + " = " + ref.catalogRef);
}
}
}
Expand Down
Loading

0 comments on commit 0311125

Please sign in to comment.