Skip to content

Commit

Permalink
feat: extended syntax for catalog references (#1756)
Browse files Browse the repository at this point in the history
* feat: allow `catalogref@catalog` syntax in list commands

* feat: allow `name@[name@...]catalog` syntax for script refs

* fix: simplify method was too simple
  • Loading branch information
quintesse authored Feb 22, 2024
1 parent 38fa191 commit b9f8412
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/main/java/dev/jbang/catalog/Alias.java
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ private static Alias merge(Alias a1, String name, Function<String, Alias> findUn
if (names.contains(name)) {
throw new RuntimeException("Encountered alias loop on '" + name + "'");
}
String[] parts = name.split("@");
if (parts.length > 2 || parts[0].isEmpty()) {
String[] parts = name.split("@", 2);
if (parts[0].isEmpty()) {
throw new RuntimeException("Invalid alias name '" + name + "'");
}
Alias a2;
Expand Down
22 changes: 17 additions & 5 deletions src/main/java/dev/jbang/catalog/Catalog.java
Original file line number Diff line number Diff line change
Expand Up @@ -402,10 +402,14 @@ static void check(boolean ok, String message) {
}

public static String simplifyName(String catalog) {
if (catalog.endsWith("/" + JBANG_CATALOG_REPO)) {
return catalog.substring(0, catalog.length() - 14);
if (!Util.isURL(catalog) && !isValidCatalogReference(catalog)) {
if (catalog.endsWith("/" + JBANG_CATALOG_REPO)) {
return catalog.substring(0, catalog.length() - 14);
} else {
return catalog.replace("/" + JBANG_CATALOG_REPO + "~", "~");
}
} else {
return catalog.replace("/" + JBANG_CATALOG_REPO + "~", "~");
return catalog;
}
}

Expand All @@ -415,10 +419,18 @@ public static boolean isValidName(String name) {

public static boolean isValidCatalogReference(String name) {
String[] parts = name.split("@");
if (parts.length != 2 || parts[0].isEmpty() || parts[1].isEmpty()) {
if (parts.length < 2) {
return false;
}
return isValidName(parts[0]);
for (String p : parts) {
if (p.isEmpty())
return false;
}
for (int i = 0; i < parts.length - 1; i++) {
if (!isValidName(parts[i]))
return false;
}
return true;
}

}
36 changes: 35 additions & 1 deletion src/main/java/dev/jbang/catalog/CatalogRef.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.jbang.catalog;

import static dev.jbang.cli.BaseCommand.EXIT_INVALID_INPUT;
import static dev.jbang.cli.BaseCommand.EXIT_UNEXPECTED_STATE;

import java.nio.file.Files;
Expand Down Expand Up @@ -46,7 +47,24 @@ public static CatalogRef createByRefOrImplicit(String catalogRef) {
}
}

static CatalogRef get(String catalogName) {
public static CatalogRef get(String catalogRefName) {
String[] parts = catalogRefName.split("@", 2);
if (parts[0].isEmpty()) {
throw new RuntimeException("Invalid catalog ref name '" + catalogRefName + "'");
}
CatalogRef catalogRef;
if (parts.length == 1) {
catalogRef = getLocal(catalogRefName);
} else {
if (parts[1].isEmpty()) {
throw new RuntimeException("Invalid catalog ref name '" + catalogRefName + "'");
}
catalogRef = fromCatalog(parts[1], parts[0]);
}
return catalogRef;
}

private static CatalogRef getLocal(String catalogName) {
CatalogRef catalogRef = null;
Catalog catalog = findNearestCatalogWithCatalogRef(Util.getCwd(), catalogName);
if (catalog != null) {
Expand Down Expand Up @@ -74,6 +92,22 @@ static CatalogRef get(String catalogName) {
return catalogRef;
}

/**
* Returns the given CatalogRef from the given registered Catalog
*
* @param catalogName The name of a registered Catalog
* @param catalogRefName The name of a CatalogRef
* @return A Template object
*/
private static CatalogRef fromCatalog(String catalogName, String catalogRefName) {
Catalog catalog = Catalog.getByName(catalogName);
CatalogRef catalogRef = catalog.catalogs.get(catalogRefName);
if (catalogRef == null) {
throw new ExitException(EXIT_INVALID_INPUT, "No catalog ref found with name '" + catalogRefName + "'");
}
return catalogRef;
}

static Catalog findNearestCatalogWithCatalogRef(Path dir, String catalogName) {
return Catalog.findNearestCatalogWith(dir, catalog -> catalog.catalogs.containsKey(catalogName));
}
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/dev/jbang/catalog/Template.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ public Template(Map<String, String> fileRefs, String description, Map<String, Te
}

public static Template get(String templateName) {
String[] parts = templateName.split("@");
if (parts.length > 2 || parts[0].isEmpty()) {
String[] parts = templateName.split("@", 2);
if (parts[0].isEmpty()) {
throw new RuntimeException("Invalid template name '" + templateName + "'");
}
Template template = null;
Template template;
if (parts.length == 1) {
template = getLocal(templateName);
} else {
Expand All @@ -44,8 +44,8 @@ public static Template get(String templateName) {
/**
* Returns the given Template from the local file system
*
* @param templateName The name of an Template
* @return An Template object
* @param templateName The name of a Template
* @return A Template object
*/
private static Template getLocal(String templateName) {
Catalog catalog = findNearestCatalogWithTemplate(Util.getCwd(), templateName);
Expand All @@ -63,8 +63,8 @@ static Catalog findNearestCatalogWithTemplate(Path dir, String templateName) {
* Returns the given Template from the given registered Catalog
*
* @param catalogName The name of a registered Catalog
* @param templateName The name of an Template
* @return An Template object
* @param templateName The name of a Template
* @return A Template object
*/
private static Template fromCatalog(String catalogName, String templateName) {
Catalog catalog = Catalog.getByName(catalogName);
Expand Down

0 comments on commit b9f8412

Please sign in to comment.