Skip to content

Commit

Permalink
feat: allow plug alias in extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
iocanel committed May 12, 2024
1 parent 637f04f commit 463a391
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ public Optional<Path> getCatalogLocation() {
return catalogLocation;
}

public Plugin withName(String name) {
return new Plugin(name, type, location, description, catalogLocation, inUserCatalog);
}

public Plugin withDescription(Optional<String> description) {
return new Plugin(name, type, location, description, catalogLocation, inUserCatalog);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

public class PluginManagerUtil {

static final String ALIAS_SEPARATOR = ": ";
private static final Pattern CLI_SUFFIX = Pattern.compile("(\\-cli)(@\\w+)?$");

private final PluginManagerSettings settings;
Expand All @@ -30,9 +31,9 @@ public PluginManagerUtil(PluginManagerSettings settings) {
* Create a {@link Plugin} from the specified location.
*
* @param the location
* @return the {@link Plugin} that corresponds to the location.
* @return the {@link Plugin} that corresponds to the spec.
*/
public Plugin from(String location) {
public Plugin fromLocation(String location) {
Optional<URL> url = PluginUtil.checkUrl(location);
Optional<Path> path = PluginUtil.checkPath(location);
Optional<GACTV> gactv = PluginUtil.checkGACTV(location);
Expand All @@ -41,6 +42,22 @@ public Plugin from(String location) {
return new Plugin(name, type, Optional.of(location), Optional.empty());
}

/**
* Create a {@link Plugin} from the specified alias.
*
* @param alias (e.g. name: location)
* @return the {@link Plugin} that corresponds to the alias.
*/
public Plugin fromAlias(String alias) {
String name = null;
String location = alias;
if (alias.contains(": ")) {
name = alias.substring(0, alias.indexOf(": "));
location = alias.substring(alias.indexOf(": ") + 2);
}
return fromLocation(location).withName(name);
}

/**
* Get the name that corresponds the the specified location.
* The name is the filename (without the jar extension) of any of the specified gactv, url or path.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package io.quarkus.cli.plugin;

import static io.quarkus.cli.plugin.PluginManagerUtil.ALIAS_SEPARATOR;

import java.nio.file.Path;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
Expand All @@ -16,6 +17,7 @@
import io.quarkus.maven.dependency.ArtifactCoords;
import io.quarkus.maven.dependency.ArtifactKey;
import io.quarkus.platform.catalog.processor.ExtensionProcessor;
import io.quarkus.registry.catalog.Extension;

class PluginMangerState {

Expand Down Expand Up @@ -173,11 +175,15 @@ public Map<String, Plugin> extensionPlugins() {
try {
Set<ArtifactKey> installed = project.getExtensionManager().getInstalled().stream()
.map(ArtifactCoords::getKey).collect(Collectors.toSet());

extensionPlugins.putAll(project.getExtensionsCatalog().getExtensions().stream()
.filter(e -> installed.contains(e.getArtifact().getKey()))
.map(ExtensionProcessor::getCliPlugins).flatMap(Collection::stream).map(util::from)
.collect(Collectors.toMap(p -> p.getName(), p -> p.inProjectCatalog())));
for (Extension e : project.getExtensionsCatalog().getExtensions()) {
if (installed.contains(e.getArtifact().getKey())) {
for (String cliPlugin : ExtensionProcessor.getCliPlugins(e)) {
Plugin plugin = cliPlugin.contains(ALIAS_SEPARATOR) ? util.fromAlias(cliPlugin)
: util.fromLocation(cliPlugin);
extensionPlugins.put(plugin.getName(), plugin);
}
}
}
} catch (Exception ignore) {
output.warn("Failed to read the extension catalog. Ignoring extension plugins.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,41 @@ public void shouldGetNameFromLocation() {
public void shouldGetPluginFromLocation() {
PluginManagerUtil util = PluginManagerUtil.getUtil();

Plugin p = util.from("http://shomehost/some/path/my.jar");
Plugin p = util.fromLocation("http://shomehost/some/path/my.jar");
assertEquals("my", p.getName());
assertEquals(PluginType.jar, p.getType());

p = util.from("/some/path/my.jar");
p = util.fromLocation("/some/path/my.jar");
assertEquals("my", p.getName());
assertEquals(PluginType.jar, p.getType());

p = util.from("my.group:my-artifact-cli:my.version");
p = util.fromLocation("my.group:my-artifact-cli:my.version");
assertEquals("my-artifact", p.getName());
assertEquals(PluginType.maven, p.getType());

p = util.from("quarkus-alias");
p = util.fromLocation("quarkus-alias");
assertEquals("alias", p.getName());
assertEquals(PluginType.jbang, p.getType());
}

@Test
public void shouldGetPluginFromAliasedLocation() {
PluginManagerUtil util = PluginManagerUtil.getUtil();

Plugin p = util.fromAlias("my-alias: http://shomehost/some/path/my.jar");
assertEquals("my-alias", p.getName());
assertEquals(PluginType.jar, p.getType());

p = util.fromAlias("my-alias: /some/path/my.jar");
assertEquals("my-alias", p.getName());
assertEquals(PluginType.jar, p.getType());

p = util.fromAlias("my-alias: my.group:my-artifact-cli:my.version");
assertEquals("my-alias", p.getName());
assertEquals(PluginType.maven, p.getType());

p = util.fromAlias("my-alias: quarkus-alias");
assertEquals("my-alias", p.getName());
assertEquals(PluginType.jbang, p.getType());
}
}

0 comments on commit 463a391

Please sign in to comment.