diff --git a/devtools/common/src/main/java/io/quarkus/cli/commands/AddExtensions.java b/devtools/common/src/main/java/io/quarkus/cli/commands/AddExtensions.java index fd04c23a0c224..536ac80b6d753 100644 --- a/devtools/common/src/main/java/io/quarkus/cli/commands/AddExtensions.java +++ b/devtools/common/src/main/java/io/quarkus/cli/commands/AddExtensions.java @@ -3,10 +3,12 @@ import static io.quarkus.maven.utilities.MojoUtils.readPom; import java.io.IOException; -import java.util.ArrayList; +import java.util.Collections; import java.util.LinkedHashSet; import java.util.List; import java.util.Set; +import java.util.regex.Pattern; +import java.util.regex.PatternSyntaxException; import java.util.stream.Collectors; import org.apache.maven.model.Dependency; @@ -74,7 +76,22 @@ static SelectionResult select(String query, List extensions, boolean matchesLabels = extensions.stream() .filter(extension -> extension.labels().contains(q)).collect(Collectors.toList()); } else { - matchesLabels = new ArrayList<>(); + matchesLabels = Collections.emptyList(); + } + + // find by pattern + Set matchesPatterns; + Pattern pattern = toRegex(q); + if (pattern != null) { + matchesPatterns = extensions.stream() + .filter(extension -> pattern.matcher(extension.getName().toLowerCase()).matches() + || pattern.matcher(extension.getArtifactId().toLowerCase()).matches() + || pattern.matcher(extension.getShortName().toLowerCase()).matches() + || matchLabels(pattern, extension.getLabels())) + .collect(Collectors.toSet()); + return new SelectionResult(matchesPatterns, true); + } else { + matchesPatterns = Collections.emptySet(); } Set candidates = new LinkedHashSet<>(); @@ -82,9 +99,76 @@ static SelectionResult select(String query, List extensions, boolean candidates.addAll(matchesShortName); candidates.addAll(partialMatches); candidates.addAll(matchesLabels); + candidates.addAll(matchesPatterns); return new SelectionResult(candidates, false); } + private static boolean matchLabels(Pattern pattern, String[] labels) { + boolean matches = false; + // if any label match it's ok + for (int i = 0; i < labels.length; i++) { + matches = matches | pattern.matcher(labels[i].toLowerCase()).matches(); + } + return matches; + } + + private static Pattern toRegex(final String str) { + try { + String wildcardToRegex = wildcardToRegex(str); + if (wildcardToRegex != null && !wildcardToRegex.isEmpty()) { + return Pattern.compile(wildcardToRegex); + } + } catch (PatternSyntaxException e) { + //ignore it + } + return null; + } + + public static String wildcardToRegex(String wildcard) { + if (wildcard == null || wildcard.isEmpty()) { + return null; + } + // don't try with file match char in pattern + if (!(wildcard.contains("*") || wildcard.contains("?"))) { + return null; + } + StringBuffer s = new StringBuffer(wildcard.length()); + s.append("^.*"); + for (int i = 0, is = wildcard.length(); i < is; i++) { + char c = wildcard.charAt(i); + switch (c) { + case '*': + s.append(".*"); + break; + case '?': + s.append("."); + break; + case '^': // escape character in cmd.exe + s.append("\\"); + break; + // escape special regexp-characters + case '(': + case ')': + case '[': + case ']': + case '$': + case '.': + case '{': + case '}': + case '|': + case '\\': + s.append("\\"); + s.append(c); + break; + default: + s.append(c); + break; + } + } + s.append(".*$"); + return (s.toString()); + } + private static boolean matchesShortName(Extension extension, String q) { return q.equalsIgnoreCase(extension.getShortName()); } @@ -131,9 +215,10 @@ public AddExtensionResult addExtensions(final Set extensions) throws IOE success = false; } } else { // Matches. - final Extension extension = result.getMatch(); - // Don't set success to false even if the dependency is not added; as it's should be idempotent. - updated = buildFile.addDependency(dependenciesFromBom, extension) || updated; + for (Extension extension : result) { + // Don't set success to false even if the dependency is not added; as it's should be idempotent. + updated = buildFile.addDependency(dependenciesFromBom, extension) || updated; + } } } } diff --git a/devtools/common/src/main/java/io/quarkus/cli/commands/SelectionResult.java b/devtools/common/src/main/java/io/quarkus/cli/commands/SelectionResult.java index f46e5740684b4..f7acb878a5e7b 100644 --- a/devtools/common/src/main/java/io/quarkus/cli/commands/SelectionResult.java +++ b/devtools/common/src/main/java/io/quarkus/cli/commands/SelectionResult.java @@ -1,10 +1,12 @@ package io.quarkus.cli.commands; +import java.util.Collections; +import java.util.Iterator; import java.util.Set; import io.quarkus.dependencies.Extension; -public class SelectionResult { +public class SelectionResult implements Iterable { private final Set extensions; private final boolean matches; @@ -22,13 +24,11 @@ public boolean matches() { return matches; } - public Extension getMatch() { + @Override + public Iterator iterator() { if (matches) { - if (extensions.isEmpty() || extensions.size() > 1) { - throw new IllegalStateException("Invalid selection result"); - } - return extensions.iterator().next(); + return extensions.iterator(); } - return null; + return Collections.emptyIterator(); } } diff --git a/devtools/common/src/test/java/io/quarkus/cli/commands/AddExtensionsTest.java b/devtools/common/src/test/java/io/quarkus/cli/commands/AddExtensionsTest.java index dbdec663158f8..c3a2c5d0b1227 100644 --- a/devtools/common/src/test/java/io/quarkus/cli/commands/AddExtensionsTest.java +++ b/devtools/common/src/test/java/io/quarkus/cli/commands/AddExtensionsTest.java @@ -64,6 +64,38 @@ void testPartialMatches() throws IOException { hasDependency(model, "quarkus-jdbc-postgresql"); } + @Test + void testRegexpMatches() throws IOException { + File pom = new File("target/extensions-test", "pom.xml"); + + CreateProjectTest.delete(pom.getParentFile()); + new CreateProject(new FileProjectWriter(pom.getParentFile())) + .groupId("org.acme") + .artifactId("add-extension-test") + .version("0.0.1-SNAPSHOT") + .doCreateProject(new HashMap<>()); + + File pomFile = new File(pom.getAbsolutePath()); + AddExtensionResult result = new AddExtensions(new FileProjectWriter(pomFile.getParentFile())) + .addExtensions(new HashSet<>(asList("SmallRye*"))); + + result.isUpdated(); + + Model model = MojoUtils.readPom(pom); + hasDependency(model, "quarkus-smallrye-reactive-messaging"); + hasDependency(model, "quarkus-smallrye-reactive-streams-operators"); + hasDependency(model, "quarkus-smallrye-opentracing"); + hasDependency(model, "quarkus-smallrye-metrics"); + hasDependency(model, "quarkus-smallrye-reactive-messaging-kafka"); + hasDependency(model, "quarkus-smallrye-health"); + hasDependency(model, "quarkus-smallrye-openapi"); + hasDependency(model, "quarkus-smallrye-jwt"); + hasDependency(model, "quarkus-smallrye-context-propagation"); + hasDependency(model, "quarkus-smallrye-reactive-type-converters"); + hasDependency(model, "quarkus-smallrye-reactive-messaging-amqp"); + hasDependency(model, "quarkus-smallrye-fault-tolerance"); + } + @Test void addMissingExtension() throws IOException { final File pom = new File("target/extensions-test", "pom.xml"); @@ -276,8 +308,9 @@ void testShortNameSelection() { SelectionResult matches = AddExtensions.select("foo", extensions, false); Assertions.assertTrue(matches.matches()); Assertions.assertEquals(1, matches.getExtensions().size()); - Assertions.assertNotNull(matches.getMatch()); - Assertions.assertTrue(matches.getMatch().getArtifactId().equalsIgnoreCase("some-complex-seo-unaware-artifactid")); + Assertions.assertTrue(matches.iterator().hasNext()); + Assertions + .assertTrue(matches.iterator().next().getArtifactId().equalsIgnoreCase("some-complex-seo-unaware-artifactid")); } @Test @@ -297,8 +330,8 @@ void testArtifactIdSelectionWithQuarkusPrefix() { Collections.shuffle(extensions); SelectionResult matches = AddExtensions.select("foo", extensions, false); Assertions.assertEquals(1, matches.getExtensions().size()); - Assertions.assertNotNull(matches.getMatch()); - Assertions.assertTrue(matches.getMatch().getArtifactId().equalsIgnoreCase("quarkus-foo")); + Assertions.assertTrue(matches.iterator().hasNext()); + Assertions.assertTrue(matches.iterator().next().getArtifactId().equalsIgnoreCase("quarkus-foo")); } @Test