Skip to content

Commit

Permalink
Merge pull request quarkusio#11098 from gastaldi/add_extension_fix
Browse files Browse the repository at this point in the history
Avoid installing multiple extensions with multiple matches
  • Loading branch information
gastaldi authored Jul 31, 2020
2 parents 7a90d9a + afa209e commit 858851b
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.quarkus.devtools.commands.handlers;

import static io.quarkus.devtools.commands.AddExtensions.EXTENSION_MANAGER;
import static io.quarkus.platform.tools.ConsoleMessageFormats.nok;

import io.quarkus.devtools.commands.AddExtensions;
import io.quarkus.devtools.commands.data.QuarkusCommandException;
Expand All @@ -12,6 +13,7 @@
import io.quarkus.platform.tools.ConsoleMessageFormats;
import io.quarkus.registry.DefaultExtensionRegistry;
import io.quarkus.registry.ExtensionRegistry;
import io.quarkus.registry.MultipleExtensionsFoundException;
import java.io.IOException;
import java.util.Collections;
import java.util.Set;
Expand All @@ -34,11 +36,11 @@ public QuarkusCommandOutcome execute(QuarkusCommandInvocation invocation) throws
extensionRegistry = DefaultExtensionRegistry.fromPlatform(invocation.getPlatformDescriptor());
}
String quarkusVersion = invocation.getPlatformDescriptor().getQuarkusVersion();
ExtensionInstallPlan extensionInstallPlan = extensionRegistry.planInstallation(quarkusVersion, extensionsQuery);

final ExtensionManager extensionManager = invocation.getValue(EXTENSION_MANAGER,
invocation.getQuarkusProject().getExtensionManager());
try {
ExtensionInstallPlan extensionInstallPlan = extensionRegistry.planInstallation(quarkusVersion, extensionsQuery);
if (extensionInstallPlan.isNotEmpty()) {
final InstallResult result = extensionManager.install(extensionInstallPlan);
result.getInstalled()
Expand All @@ -47,7 +49,15 @@ public QuarkusCommandOutcome execute(QuarkusCommandInvocation invocation) throws
+ " has been installed"));
return new QuarkusCommandOutcome(true).setValue(AddExtensions.OUTCOME_UPDATED, result.isSourceUpdated());
}

} catch (MultipleExtensionsFoundException m) {
StringBuilder sb = new StringBuilder();
sb.append(nok("Multiple extensions matching '")).append(m.getKeyword()).append("'");
m.getExtensions()
.forEach(extension -> sb.append(System.lineSeparator()).append(" * ")
.append(extension.managementKey()));
sb.append(System.lineSeparator())
.append(" Be more specific e.g using the exact name or the full GAV.");
invocation.log().info(sb.toString());
} catch (IOException e) {
throw new QuarkusCommandException("Failed to add extensions", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ public ExtensionInstallPlan planInstallation(String quarkusCore, Collection<Stri
// No extension found for this keyword. Return empty immediately
return ExtensionInstallPlan.EMPTY;
}
// If it's a pattern allow multiple results
// See https://github.com/quarkusio/quarkus/issues/11086#issuecomment-666360783
else if (tuples.size() > 1 && !ExtensionPredicate.isPattern(keyword)) {
throw new MultipleExtensionsFoundException(keyword,
tuples.stream().map(this::toQuarkusExtension).collect(Collectors.toList()));
}
for (ExtensionReleaseTuple tuple : tuples) {
ArtifactKey id = tuple.getExtension().getId();
String groupId = id.getGroupId();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.quarkus.registry;

import io.quarkus.dependencies.Extension;
import java.util.Collection;
import java.util.Objects;

/**
* Thrown when multiple extensions are found for a given extension when
* {@link ExtensionRegistry#planInstallation(String, Collection)} is called
*
* @see {@link ExtensionRegistry#planInstallation(String, Collection)}
*/
public class MultipleExtensionsFoundException extends RuntimeException {

private final String keyword;
private final Collection<Extension> extensions;

public MultipleExtensionsFoundException(String keyword, Collection<Extension> extensions) {
this.keyword = Objects.requireNonNull(keyword, "Keyword must not be null");
this.extensions = Objects.requireNonNull(extensions, "Extensions should not be null");
}

public String getKeyword() {
return keyword;
}

public Collection<Extension> getExtensions() {
return extensions;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,28 @@ public static Predicate<Extension> create(String keyword) {
return new ExtensionPredicate(keyword);
}

public static boolean isPattern(String keyword) {
for (char c : keyword.toCharArray()) {
switch (c) {
case '*':
case '?':
case '^': // escape character in cmd.exe
case '(':
case ')':
case '[':
case ']':
case '$':
case '.':
case '{':
case '}':
case '|':
case '\\':
return true;
}
}
return false;
}

@Override
public boolean test(Extension extension) {
if (extension.isUnlisted()) {
Expand Down

0 comments on commit 858851b

Please sign in to comment.