Skip to content

Commit

Permalink
feat: added jbanghub to builtin catalog (#1770)
Browse files Browse the repository at this point in the history
* feat: added jbanghub to builtin catalog

* chore: warn when trying to remove items from built-in catalog
  • Loading branch information
quintesse authored Mar 31, 2024
1 parent c26de98 commit 501961e
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 7 deletions.
5 changes: 5 additions & 0 deletions itests/catalog.feature
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,8 @@ Scenario: list remote catalog templates
Then match exit == 0
And match out contains "@jbangdev"
And match out !contains "@jbangdev/jbang-catalog"

Scenario: Removing built-in catalog
When command('jbang catalog remove jbanghub')
* match exit == 0
* match err contains "Cannot remove catalog ref jbanghub from built-in catalog"
6 changes: 6 additions & 0 deletions itests/template.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Feature: template

Scenario: Removing built-in template
When command('jbang template remove hello')
* match exit == 0
* match err contains "Cannot remove template hello from built-in catalog"
3 changes: 3 additions & 0 deletions src/main/java/dev/jbang/catalog/Catalog.java
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,9 @@ static Catalog findNearestCatalogWith(Path dir, boolean includeImported, boolean
}
if (catalog == null) {
catalog = accept.apply(getBuiltin());
if (catalog == null && includeImported) {
catalog = findImportedCatalogsWith(getBuiltin(), accept);
}
}
return catalog;
}
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/dev/jbang/catalog/CatalogUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ public static void removeNearestAlias(String name) {
if (catalog != null) {
if (catalog.catalogRef.isURL() && Util.isRemoteRef(catalog.catalogRef.getOriginalResource())) {
Util.warnMsg("Unable to remove alias " + name + " because it is imported from a remote catalog");
} else if (catalog.equals(Catalog.getBuiltin())) {
Util.warnMsg("Cannot remove alias " + name + " from built-in catalog");
} else {
removeAlias(catalog, name);
}
Expand Down Expand Up @@ -175,6 +177,8 @@ public static void removeNearestTemplate(String name) {
if (catalog != null) {
if (catalog.catalogRef.isURL() && Util.isRemoteRef(catalog.catalogRef.getOriginalResource())) {
Util.warnMsg("Unable to remove template " + name + " because it is imported from a remote catalog");
} else if (catalog.equals(Catalog.getBuiltin())) {
Util.warnMsg("Cannot remove template " + name + " from built-in catalog");
} else {
removeTemplate(catalog, name);
}
Expand Down Expand Up @@ -214,6 +218,8 @@ public static void removeNearestCatalogRef(String name) {
if (catalog != null) {
if (catalog.catalogRef.isURL() && Util.isRemoteRef(catalog.catalogRef.getOriginalResource())) {
Util.warnMsg("Unable to remove catalog ref " + name + " because it is imported from a remote catalog");
} else if (catalog.equals(Catalog.getBuiltin())) {
Util.warnMsg("Cannot remove catalog ref " + name + " from built-in catalog");
} else {
removeCatalogRef(catalog, name);
}
Expand Down
7 changes: 7 additions & 0 deletions src/main/resources/jbang-catalog.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
{
"catalogs": {
"jbanghub": {
"catalog-ref": "https://raw.githubusercontent.com/jbanghub/jbang-catalog/main/jbang-catalog.json",
"description": "JBangHub - Unleashing the Java community",
"import": true
}
},
"templates": {
"gpt": {
"file-refs": {"{filename}": "init-gpt.java.qute"},
Expand Down
20 changes: 13 additions & 7 deletions src/test/java/dev/jbang/cli/TestCatalogNearest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@

import static dev.jbang.util.TestUtil.clearSettingsCaches;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.io.FileMatchers.anExistingFile;

import java.io.IOException;
Expand All @@ -22,6 +18,7 @@
import dev.jbang.BaseTest;
import dev.jbang.Settings;
import dev.jbang.catalog.Catalog;
import dev.jbang.catalog.CatalogRef;
import dev.jbang.catalog.CatalogUtil;
import dev.jbang.util.Util;

Expand Down Expand Up @@ -75,13 +72,22 @@ void testList() throws IOException {
"global",
"dotparent",
"dotlocal",
"local"));
assertThat(catalog.catalogs.keySet(), equalTo(keys));
"local",
"jbanghub"));
assertThat(catalog.catalogs.keySet().containsAll(keys), is(true));

assertThat(catalog.catalogs.get("global").catalogRef, is(aliasesFile.toString()));
assertThat(catalog.catalogs.get("dotparent").catalogRef, is(aliasesFile.toString()));
assertThat(catalog.catalogs.get("dotlocal").catalogRef, is(aliasesFile.toString()));
assertThat(catalog.catalogs.get("local").catalogRef, is(aliasesFile.toString()));
assertThat(catalog.catalogs.get("jbanghub").catalog, is(Catalog.getBuiltin()));

catalog.catalogs.keySet().removeAll(keys);
// After removing the known keys, the rest must come from the jbanghub import
final String JBANGHUB_URL = "https://raw.githubusercontent.com/jbanghub/jbang-catalog/main/jbang-catalog.json";
for (CatalogRef c : catalog.catalogs.values()) {
assertThat(c.catalog.catalogRef.getOriginalResource(), is(JBANGHUB_URL));
}
}

@Test
Expand Down

0 comments on commit 501961e

Please sign in to comment.