Skip to content

Commit

Permalink
Merge pull request #33315 from iocanel/cli-plugin-catalogs
Browse files Browse the repository at this point in the history
CLI uses locally enabled jbang catalogs
  • Loading branch information
iocanel authored May 17, 2023
2 parents 62e9725 + 9e5e684 commit f6cf227
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 16 deletions.
2 changes: 0 additions & 2 deletions devtools/cli/src/main/java/io/quarkus/cli/QuarkusCli.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.function.Supplier;

Expand Down Expand Up @@ -264,7 +263,6 @@ private Supplier<QuarkusProject> quarkusProject(Optional<String> testDir) {

private PluginManager pluginManager(OutputOptionMixin output, Optional<String> testDir, boolean interactiveMode) {
PluginManagerSettings settings = PluginManagerSettings.defaultSettings()
.withCatalogs(Set.<String> of("quarkusio"))
.withInteractivetMode(interactiveMode); // Why not just getting it from output.isClieTest ? Cause args have not been parsed yet.
return PluginManager.create(settings, output, Optional.ofNullable(Paths.get(System.getProperty("user.home"))),
getProjectRoot(testDir), quarkusProject(testDir));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,23 @@ public class JBangCatalogService extends CatalogService<JBangCatalog> {
private static final Pattern PATH = Pattern.compile(PATH_REGEX);

private final String pluginPrefix;
private final String fallbackCatalog;
private final String[] remoteCatalogs;
private final JBangSupport jbang;

public JBangCatalogService(MessageWriter output) {
this(output, "quarkus", "quarkusio");
}

public JBangCatalogService(MessageWriter output, String pluginPrefix, String... remoteCatalogs) {
this(false, output, pluginPrefix, remoteCatalogs);
public JBangCatalogService(MessageWriter output, String pluginPrefix, String fallbackCatalog, String... remoteCatalogs) {
this(false, output, pluginPrefix, fallbackCatalog, remoteCatalogs);
}

public JBangCatalogService(boolean interactiveMode, MessageWriter output, String pluginPrefix, String... remoteCatalogs) {
public JBangCatalogService(boolean interactiveMode, MessageWriter output, String pluginPrefix, String fallbackCatalog,
String... remoteCatalogs) {
super(JBangCatalog.class, GIT_ROOT, RELATIVE_PLUGIN_CATALOG);
this.pluginPrefix = pluginPrefix;
this.fallbackCatalog = fallbackCatalog;
this.remoteCatalogs = remoteCatalogs;
this.jbang = new JBangSupport(interactiveMode, output);
}
Expand Down Expand Up @@ -90,16 +93,49 @@ public JBangCatalog readCombinedCatalog(Optional<Path> projectDir, Optional<Path
});
});

for (String remoteCatalog : remoteCatalogs) {
List<String> lines = jbang.execute("alias", "list", "--verbose", remoteCatalog);
aliases.putAll(readAliases(lines).entrySet()
//If not catalog have been specified use all available.
if (remoteCatalogs.length == 0) {
aliases.putAll(listAliasesOrFallback(jbang, fallbackCatalog).entrySet()
.stream()
.filter(e -> !aliases.containsKey(e.getKey()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)));
} else {
for (String remoteCatalog : remoteCatalogs) {
aliases.putAll(listAliases(jbang, remoteCatalog).entrySet()
.stream()
.filter(e -> !aliases.containsKey(e.getKey()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)));
}
}
return new JBangCatalog(catalogs, aliases, Optional.empty(), Optional.empty());
}

private Map<String, JBangAlias> listAliases(JBangSupport jbang, String remoteCatalog) {
List<String> lines = jbang.execute("alias", "list", "--verbose", remoteCatalog);
return readAliases(lines);
}

private Map<String, JBangAlias> listAliasesOrFallback(JBangSupport jbang, String fallbackCatalog) {
List<String> localCatalogs = jbang.execute("catalog", "list").stream()
.map(l -> l.substring(0, l.indexOf(" ")))
.collect(Collectors.toList());

//If there are locally installed catalogs, then go through every single one of them
//and collect the aliases.
//Unfortunaltely jbang can't return all alias in one go.
//This is because it currently omits `@catalog` suffix in some cases.
if (!localCatalogs.isEmpty()) {
Map<String, JBangAlias> aliases = new HashMap<>();
for (String catalog : localCatalogs) {
aliases.putAll(listAliases(jbang, catalog));
}
return aliases;
}
//If no aliases found then there is not remote jbang catalog available
//In this case we need to fallback to the default.
return listAliases(jbang, fallbackCatalog);
}

private Map<String, JBangAlias> readAliases(List<String> lines) {
Map<String, JBangAlias> aliases = new HashMap<>();
for (int i = 0; i < lines.size(); i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,43 +13,51 @@
public class PluginManagerSettings {

public static String DEFAULT_PLUGIN_PREFIX = "quarkus";
public static String[] DEFAULT_REMOTE_JBANG_CATALOGS = new String[] { "quarkusio" };
public static String FALLBACK_REMOTE_JBANG_CATALOG = "quarkusio";
public static String[] DEFAULT_REMOTE_JBANG_CATALOGS = new String[0];
public static Function<Path, Path> DEFAULT_RELATIVE_PATH_FUNC = p -> p.resolve(".quarkus").resolve("cli").resolve("plugins")
.resolve("quarkus-cli-catalog.json");

private final boolean interactiveMode;
private final String pluginPrefix;
private final String fallbackJBangCatalog;
private final String[] remoteJBangCatalogs;
private final Function<Path, Path> toRelativePath;

public PluginManagerSettings(boolean interactiveMode, String pluginPrefix, String[] remoteJBangCatalogs,
public PluginManagerSettings(boolean interactiveMode, String pluginPrefix, String fallbackJBangCatalog,
String[] remoteJBangCatalogs,
Function<Path, Path> toRelativePath) {
this.interactiveMode = interactiveMode;
this.pluginPrefix = pluginPrefix;
this.fallbackJBangCatalog = fallbackJBangCatalog;
this.remoteJBangCatalogs = remoteJBangCatalogs;
this.toRelativePath = toRelativePath;
}

public static PluginManagerSettings defaultSettings() {
return new PluginManagerSettings(false, DEFAULT_PLUGIN_PREFIX, DEFAULT_REMOTE_JBANG_CATALOGS,
return new PluginManagerSettings(false, DEFAULT_PLUGIN_PREFIX, FALLBACK_REMOTE_JBANG_CATALOG,
DEFAULT_REMOTE_JBANG_CATALOGS,
DEFAULT_RELATIVE_PATH_FUNC);
}

public PluginManagerSettings withPluignPrefix(String pluginPrefix) {
return new PluginManagerSettings(interactiveMode, pluginPrefix, remoteJBangCatalogs, toRelativePath);
return new PluginManagerSettings(interactiveMode, pluginPrefix, fallbackJBangCatalog, remoteJBangCatalogs,
toRelativePath);
}

public PluginManagerSettings withCatalogs(Set<String> remoteJBangCatalogs) {
return new PluginManagerSettings(interactiveMode, pluginPrefix,
remoteJBangCatalogs.toArray(new String[remoteJBangCatalogs.size()]), toRelativePath);
return new PluginManagerSettings(interactiveMode, pluginPrefix, fallbackJBangCatalog,
remoteJBangCatalogs.toArray(new String[0]), toRelativePath);
}

public PluginManagerSettings withCatalogs(String... remoteJBangCatalogs) {
return new PluginManagerSettings(interactiveMode, pluginPrefix, remoteJBangCatalogs, toRelativePath);
return new PluginManagerSettings(interactiveMode, pluginPrefix, fallbackJBangCatalog, remoteJBangCatalogs,
toRelativePath);
}

public PluginManagerSettings withInteractivetMode(boolean interactiveMode) {
return new PluginManagerSettings(interactiveMode, pluginPrefix, remoteJBangCatalogs, toRelativePath);
return new PluginManagerSettings(interactiveMode, pluginPrefix, fallbackJBangCatalog, remoteJBangCatalogs,
toRelativePath);
}

/**
Expand All @@ -63,6 +71,15 @@ public String getPluginPrefix() {
return pluginPrefix;
}

/**
* The name of the fallback JBang catalogs to get plugins from.
*
* @return the name of the catalog.
*/
public String getFallbackJBangCatalog() {
return fallbackJBangCatalog;
}

/**
* The names of the JBang catalogs to get plugins from.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class PluginMangerState {
//Inferred
this.projectRoot = projectRoot.filter(p -> !p.equals(userHome.orElse(null)));
this.jbangCatalogService = new JBangCatalogService(settings.isInteractiveMode(), output, settings.getPluginPrefix(),
settings.getFallbackJBangCatalog(),
settings.getRemoteJBangCatalogs());
this.pluginCatalogService = new PluginCatalogService(settings.getToRelativePath());
this.util = PluginManagerUtil.getUtil(settings);
Expand Down

0 comments on commit f6cf227

Please sign in to comment.