Skip to content

Commit

Permalink
Merge pull request #40580 from iocanel/cli-plug-ext-alias
Browse files Browse the repository at this point in the history
Allow extension to define CLI plugins with aliases
  • Loading branch information
gsmet authored Jun 6, 2024
2 parents b20c79c + c8f164d commit 8b28b65
Show file tree
Hide file tree
Showing 5 changed files with 61 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 @@ -32,7 +33,7 @@ public PluginManagerUtil(PluginManagerSettings settings) {
* @param the location
* @return the {@link Plugin} that corresponds to the location.
*/
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(ALIAS_SEPARATOR)) {
name = alias.substring(0, alias.indexOf(ALIAS_SEPARATOR)).trim();
location = alias.substring(alias.indexOf(ALIAS_SEPARATOR) + 1).trim();
}
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 @@ -5,6 +5,7 @@
import java.net.URL;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
Expand Down Expand Up @@ -136,7 +137,7 @@ public static Optional<GACTV> checkGACTV(Optional<String> location) {
*/
public static Optional<Path> checkPath(String location) {
try {
return Optional.of(Path.of(location));
return Optional.of(Paths.get(location));
} catch (InvalidPathException | NullPointerException e) {
return Optional.empty();
}
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 8b28b65

Please sign in to comment.