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: treat empty catalog as jbanghub/jbang-catalog aliases #1738

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions src/main/java/dev/jbang/catalog/Alias.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

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

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -189,6 +191,8 @@ private static Alias merge(Alias a1, String name, Function<String, Alias> findUn
String[] parts = name.split("@");
if (parts.length > 2 || parts[0].isEmpty()) {
throw new RuntimeException("Invalid alias name '" + name + "'");
} else if (parts.length == 1 && (name.endsWith("@"))) {
parts = new String[] { parts[0], Catalog.JBANG_DEFAULT_CATALOG };
}
Alias a2;
if (parts.length == 1) {
Expand All @@ -197,6 +201,11 @@ private static Alias merge(Alias a1, String name, Function<String, Alias> findUn
if (parts[1].isEmpty()) {
throw new RuntimeException("Invalid alias name '" + name + "'");
}
// TODO: not happy I have to check for file existence if starts with /
// but how else can we also support dir locations?
if (parts[1].startsWith("/") && !Files.exists(Paths.get(parts[1]))) {
parts[1] = Catalog.JBANG_DEFAULT_CATALOG + parts[1];
}
a2 = fromCatalog(parts[1], parts[0]);
}
if (a2 != null) {
Expand Down
1 change: 1 addition & 0 deletions src/main/java/dev/jbang/catalog/Catalog.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
public class Catalog {
public static final String JBANG_CATALOG_JSON = "jbang-catalog.json";
public static final String JBANG_IMPLICIT_CATALOG_JSON = "implicit-catalog.json";
public static final String JBANG_DEFAULT_CATALOG = "jbanghub";

static final Map<String, Catalog> catalogCache = new HashMap<>();

Expand Down
6 changes: 4 additions & 2 deletions src/main/java/dev/jbang/catalog/CatalogRef.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,11 @@ static CatalogRef get(String catalogName) {
if (catalog != null) {
catalogRef = catalog.catalogs.get(catalogName);
}
if (catalogRef == null && Util.isValidPath(catalogName)) {
// check if local file matches first.
if (catalogRef == null && !catalogName.equals("")
&& Util.isValidPath(catalogName)) {
Path p = Util.getCwd().resolve(catalogName);
if (!p.getFileName().toString().equals(Catalog.JBANG_CATALOG_JSON)) {
if (p.getFileName() != null && !p.getFileName().toString().equals(Catalog.JBANG_CATALOG_JSON)) {
p = p.resolve(Catalog.JBANG_CATALOG_JSON);
}
if (Files.isRegularFile(p)) {
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/dev/jbang/catalog/ImplicitCatalogRef.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ public static ImplicitCatalogRef parse(String name) {
if (Util.isURL(name)) {
return null;
}
if (name.startsWith("/")) {
name = Catalog.JBANG_DEFAULT_CATALOG + name;
}
String[] parts = name.split("~", 2);
String path;
if (parts.length == 2) {
Expand Down
34 changes: 29 additions & 5 deletions src/test/java/dev/jbang/TestImplicitAlias.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package dev.jbang;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;

import java.nio.file.Files;
import java.nio.file.Path;

import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import dev.jbang.catalog.Alias;
import dev.jbang.catalog.Catalog;
import dev.jbang.catalog.ImplicitCatalogRef;

/**
Expand All @@ -19,24 +23,44 @@ public class TestImplicitAlias extends BaseTest {
@Test
public void testGitImplicitCatalog() {
assertThat(ImplicitCatalogRef.getImplicitCatalogUrl("jbangdev").get(),
Matchers.equalTo("https://github.com/jbangdev/jbang-catalog/blob/HEAD/jbang-catalog.json"));
equalTo("https://github.com/jbangdev/jbang-catalog/blob/HEAD/jbang-catalog.json"));
assertThat(ImplicitCatalogRef.getImplicitCatalogUrl("jbangdev/jbang-examples").get(),
Matchers.equalTo("https://github.com/jbangdev/jbang-examples/blob/HEAD/jbang-catalog.json"));
equalTo("https://github.com/jbangdev/jbang-examples/blob/HEAD/jbang-catalog.json"));
}

@ParameterizedTest
@ValueSource(strings = { /* not sure this should be allowed "", */ "/", "/sqlline", "jbanghub/sqlline" })
public void testGitImplicitCatalogHub(String catalog) {
String cref = ImplicitCatalogRef.getImplicitCatalogUrl(catalog).get();
assertThat(cref,
startsWith("https://github.com/jbanghub/"));

Catalog c = Catalog.getByName(catalog);
assertThat(c.catalogRef.getOriginalResource(),
startsWith("https://github.com/jbanghub/"));
}

@ParameterizedTest
@ValueSource(strings = { "sqlline@", "sqlline@/", "sqlline@/sqlline", "sqlline@jbanghub/sqlline" })
public void testImplicitHub(String alias) {
Alias a = Alias.get(alias);
assertThat(a.scriptRef, containsString("sqlline:sqlline"));
assertThat(a.catalog.baseRef, containsString("jbanghub"));
}

@Test
public void testImplictURLAlias() {

Alias url = Alias.get("[email protected]");
assertThat(url.scriptRef, Matchers.equalTo("tree/main.java"));
assertThat(url.scriptRef, equalTo("tree/main.java"));

}

@Test
public void testImplictExplicitURLAlias() {

Alias url = Alias.get("tree@https://xam.dk");
assertThat(url.scriptRef, Matchers.equalTo("tree/main.java"));
assertThat(url.scriptRef, equalTo("tree/main.java"));

}

Expand All @@ -56,7 +80,7 @@ public void testFileURLAlias() throws Exception {

Alias alias = Alias.get(url);

assertThat(alias.scriptRef, Matchers.equalTo("helloworld.java"));
assertThat(alias.scriptRef, equalTo("helloworld.java"));

}
}
Loading