Skip to content

Commit

Permalink
Apply discussed changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ia3andy committed Jun 8, 2020
1 parent f66526f commit e185272
Show file tree
Hide file tree
Showing 16 changed files with 245 additions and 222 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ protected static String[] split(String str, String[] parts, int fromIndex) {
protected AppArtifactKey(String[] parts) {
this.groupId = parts[0];
this.artifactId = parts[1];
if (parts.length == 2) {
if (parts.length == 2 || parts[2] == null) {
this.classifier = "";
} else {
this.classifier = parts[2];
}
if (parts.length <= 3) {
if (parts.length <= 3 || parts[3] == null) {
this.type = "jar";
} else {
this.type = parts[3];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package io.quarkus.devtools.commands.handlers;

import static io.quarkus.devtools.commands.AddExtensions.EXTENSIONS_MANAGER;
import static io.quarkus.devtools.commands.handlers.QuarkusCommandHandlers.computeExtensionsFromQuery;
import static io.quarkus.devtools.commands.handlers.QuarkusCommandHandlers.computeCoordsFromQuery;

import io.quarkus.dependencies.Extension;
import io.quarkus.bootstrap.model.AppArtifactCoords;
import io.quarkus.devtools.commands.AddExtensions;
import io.quarkus.devtools.commands.data.QuarkusCommandException;
import io.quarkus.devtools.commands.data.QuarkusCommandInvocation;
import io.quarkus.devtools.commands.data.QuarkusCommandOutcome;
import io.quarkus.devtools.project.extensions.ExtensionsManager;
import io.quarkus.devtools.project.extensions.ExtensionsManager.InstallResult;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
Expand All @@ -27,13 +28,13 @@ public QuarkusCommandOutcome execute(QuarkusCommandInvocation invocation) throws
return QuarkusCommandOutcome.success().setValue(AddExtensions.OUTCOME_UPDATED, false);
}

final List<Extension> extensionsToAdd = computeExtensionsFromQuery(invocation, extensionsQuery);
final List<AppArtifactCoords> extensionsToAdd = computeCoordsFromQuery(invocation, extensionsQuery);
final ExtensionsManager extensionsManager = invocation.getValue(EXTENSIONS_MANAGER,
invocation.getQuarkusProject().getExtensionsManager());
try {
if (extensionsToAdd != null) {
final int added = extensionsManager.install(extensionsToAdd);
return new QuarkusCommandOutcome(true).setValue(AddExtensions.OUTCOME_UPDATED, added > 0);
final InstallResult result = extensionsManager.install(extensionsToAdd);
return new QuarkusCommandOutcome(true).setValue(AddExtensions.OUTCOME_UPDATED, result.isSourceUpdated());
}

} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package io.quarkus.devtools.commands.handlers;

import static io.quarkus.devtools.commands.handlers.QuarkusCommandHandlers.computeExtensionsFromQuery;
import static io.quarkus.devtools.commands.handlers.QuarkusCommandHandlers.computeCoordsFromQuery;
import static io.quarkus.devtools.project.codegen.ProjectGenerator.*;

import io.quarkus.dependencies.Extension;
import io.quarkus.bootstrap.model.AppArtifactCoords;
import io.quarkus.devtools.commands.data.QuarkusCommandException;
import io.quarkus.devtools.commands.data.QuarkusCommandInvocation;
import io.quarkus.devtools.commands.data.QuarkusCommandOutcome;
Expand Down Expand Up @@ -54,7 +54,7 @@ public QuarkusCommandOutcome execute(QuarkusCommandInvocation invocation) throws
invocation.setValue(CLASS_NAME, className);
}

final List<Extension> extensionsToAdd = computeExtensionsFromQuery(invocation, extensionsQuery);
final List<AppArtifactCoords> extensionsToAdd = computeCoordsFromQuery(invocation, extensionsQuery);

// extensionsToAdd is null when an error occurred while matching extensions
if (extensionsToAdd != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package io.quarkus.devtools.commands.handlers;

import static io.quarkus.devtools.project.extensions.Extensions.toKey;
import static java.util.stream.Collectors.toMap;

import io.quarkus.bootstrap.model.AppArtifactCoords;
import io.quarkus.bootstrap.model.AppArtifactKey;
import io.quarkus.dependencies.Extension;
import io.quarkus.devtools.commands.ListExtensions;
import io.quarkus.devtools.commands.data.QuarkusCommandException;
Expand Down Expand Up @@ -39,10 +42,10 @@ public QuarkusCommandOutcome execute(QuarkusCommandInvocation invocation) throws
final ExtensionsManager extensionsManager = invocation.getValue(ListExtensions.EXTENSIONS_MANAGER,
invocation.getQuarkusProject().getExtensionsManager());

Map<String, Extension> installedByGA;
Map<AppArtifactKey, AppArtifactCoords> installedByKey;
try {
installedByGA = extensionsManager.getInstalled().stream()
.collect(toMap(Extension::managementKey, Function.identity()));
installedByKey = extensionsManager.getInstalled().stream()
.collect(toMap(AppArtifactCoords::getKey, Function.identity()));
} catch (IOException e) {
throw new QuarkusCommandException("Failed to determine the list of installed extensions", e);
}
Expand Down Expand Up @@ -77,7 +80,7 @@ public QuarkusCommandOutcome execute(QuarkusCommandInvocation invocation) throws
}

platformExtensions.forEach(platformExtension -> display(invocation.log(), platformExtension,
installedByGA.get(platformExtension.managementKey()), all, currentFormatter));
installedByKey.get(toKey(platformExtension)), all, currentFormatter));
final BuildTool buildTool = invocation.getQuarkusProject().getBuildTool();
if ("concise".equalsIgnoreCase(format)) {
if (BuildTool.GRADLE.equals(buildTool)) {
Expand Down Expand Up @@ -121,7 +124,8 @@ private void nameFormatter(MessageWriter writer, String[] cols) {
writer.info(String.format(NAME_FORMAT, cols[2]));
}

private void display(MessageWriter messageWriter, final Extension platformExtension, final Extension installed, boolean all,
private void display(MessageWriter messageWriter, final Extension platformExtension, final AppArtifactCoords installed,
boolean all,
BiConsumer<MessageWriter, String[]> formatter) {
if (!all && installed != null) {
return;
Expand All @@ -130,13 +134,16 @@ private void display(MessageWriter messageWriter, final Extension platformExtens
String label = "";
String version = "";

final String installedVersion = installed != null ? installed.getVersion() : null;
if (installedVersion != null) {
if (installedVersion.equalsIgnoreCase(platformExtension.getVersion())) {
label = "current";
if (installed != null) {
final String installedVersion = installed.getVersion();
if (installedVersion == null) {
label = "managed";
version = String.format("%s", platformExtension.getVersion());
} else if (installedVersion.equalsIgnoreCase(platformExtension.getVersion())) {
label = "overridden";
version = String.format("%s", installedVersion);
} else {
label = "update";
label = "overridden";
version = String.format("%s <> %s", installedVersion, platformExtension.getVersion());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static io.quarkus.platform.tools.ConsoleMessageFormat.nok;

import com.google.common.collect.ImmutableList;
import io.quarkus.bootstrap.model.AppArtifactCoords;
import io.quarkus.dependencies.Extension;
import io.quarkus.devtools.commands.data.QuarkusCommandInvocation;
import io.quarkus.devtools.commands.data.SelectionResult;
Expand All @@ -21,15 +22,20 @@ final class QuarkusCommandHandlers {
private QuarkusCommandHandlers() {
}

static List<Extension> computeExtensionsFromQuery(final QuarkusCommandInvocation invocation,
static List<AppArtifactCoords> computeCoordsFromQuery(final QuarkusCommandInvocation invocation,
final Set<String> extensionsQuery) {
final ImmutableList.Builder<Extension> builder = ImmutableList.<Extension> builder();
final ImmutableList.Builder<AppArtifactCoords> builder = ImmutableList.builder();
for (String query : extensionsQuery) {
if (query.contains(":")) {
builder.add(Extensions.parse(query));
builder.add(AppArtifactCoords.fromString(query));
} else {
SelectionResult result = select(invocation.getPlatformDescriptor().getExtensions(), query, false);
if (!result.matches()) {
SelectionResult result = select(query, invocation.getPlatformDescriptor().getExtensions(), false);
if (result.matches()) {
final Set<AppArtifactCoords> withStrippedVersion = result.getExtensions().stream().map(Extensions::toCoords)
.map(Extensions::stripVersion).collect(Collectors.toSet());
// We strip the version because those extensions are managed
builder.addAll(withStrippedVersion);
} else {
StringBuilder sb = new StringBuilder();
// We have 3 cases, we can still have a single candidate, but the match is on label
// or we have several candidates, or none
Expand All @@ -48,11 +54,6 @@ static List<Extension> computeExtensionsFromQuery(final QuarkusCommandInvocation
invocation.log().info(sb.toString());
return null;
}
} else { // Matches.
for (Extension extension : result) {
// Don't set success to false even if the dependency is not added; as it's should be idempotent.
builder.add(extension);
}
}
}
}
Expand All @@ -62,13 +63,13 @@ static List<Extension> computeExtensionsFromQuery(final QuarkusCommandInvocation
/**
* Selection algorithm.
*
* @param allPlatformExtensions the list of all platform extensions
* @param query the query
* @param allPlatformExtensions the list of all platform extensions
* @param labelLookup whether or not the query must be tested against the labels of the extensions. Should
* be {@code false} by default.
* @return the list of matching candidates and whether or not a match has been found.
*/
static SelectionResult select(final List<Extension> allPlatformExtensions, final String query, final boolean labelLookup) {
static SelectionResult select(final String query, final List<Extension> allPlatformExtensions, final boolean labelLookup) {
String q = query.trim().toLowerCase();

// Try exact matches
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package io.quarkus.devtools.commands.handlers;

import static io.quarkus.devtools.commands.RemoveExtensions.EXTENSIONS_MANAGER;
import static io.quarkus.devtools.commands.handlers.QuarkusCommandHandlers.computeExtensionsFromQuery;
import static io.quarkus.devtools.commands.handlers.QuarkusCommandHandlers.computeCoordsFromQuery;

import com.google.common.collect.ImmutableSet;
import io.quarkus.dependencies.Extension;
import io.quarkus.bootstrap.model.AppArtifactCoords;
import io.quarkus.bootstrap.model.AppArtifactKey;
import io.quarkus.devtools.commands.RemoveExtensions;
import io.quarkus.devtools.commands.data.QuarkusCommandException;
import io.quarkus.devtools.commands.data.QuarkusCommandInvocation;
import io.quarkus.devtools.commands.data.QuarkusCommandOutcome;
import io.quarkus.devtools.project.extensions.ExtensionsManager;
import io.quarkus.devtools.project.extensions.ExtensionsManager.UninstallResult;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
* This class is thread-safe. It extracts extensions to be removed from the project from an instance of
Expand All @@ -28,13 +30,15 @@ public QuarkusCommandOutcome execute(QuarkusCommandInvocation invocation) throws
return QuarkusCommandOutcome.success().setValue(RemoveExtensions.OUTCOME_UPDATED, false);
}

final List<Extension> extensionsToRemove = computeExtensionsFromQuery(invocation, extensionsQuery);
final List<AppArtifactCoords> extensionsToRemove = computeCoordsFromQuery(invocation, extensionsQuery);
final ExtensionsManager extensionsManager = invocation.getValue(EXTENSIONS_MANAGER,
invocation.getQuarkusProject().getExtensionsManager());
try {
if (extensionsToRemove != null) {
final int removed = extensionsManager.uninstall(ImmutableSet.copyOf(extensionsToRemove));
return new QuarkusCommandOutcome(true).setValue(RemoveExtensions.OUTCOME_UPDATED, removed > 0);
final Set<AppArtifactKey> keys = extensionsToRemove.stream().map(AppArtifactCoords::getKey)
.collect(Collectors.toSet());
final UninstallResult result = extensionsManager.uninstall(keys);
return new QuarkusCommandOutcome(true).setValue(RemoveExtensions.OUTCOME_UPDATED, result.isSourceUpdated());
}
} catch (IOException e) {
throw new QuarkusCommandException("Failed to add extensions", e);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.quarkus.devtools.project.buildfile;

import io.quarkus.bootstrap.model.AppArtifactCoords;
import io.quarkus.bootstrap.model.AppArtifactKey;
import io.quarkus.devtools.project.BuildTool;
import io.quarkus.platform.descriptor.QuarkusPlatformDescriptor;
import java.io.ByteArrayInputStream;
Expand All @@ -14,7 +16,6 @@
import java.util.Scanner;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
import org.apache.maven.model.Dependency;

// We keep it here to take advantage of the abstract tests
public abstract class AbstractGradleBuildFile extends BuildFile {
Expand Down Expand Up @@ -58,35 +59,37 @@ public void writeToDisk() throws IOException {
}

@Override
protected void addDependencyInBuildFile(Dependency dependency) throws IOException {
protected void addDependencyInBuildFile(AppArtifactCoords coords) throws IOException {
addDependencyInModel(getModel(), coords);
}

static void addDependencyInModel(Model model, AppArtifactCoords coords) throws IOException {
StringBuilder newBuildContent = new StringBuilder();
readLineByLine(getModel().getBuildContent(), currentLine -> {
readLineByLine(model.getBuildContent(), currentLine -> {
newBuildContent.append(currentLine).append(System.lineSeparator());
if (currentLine.startsWith("dependencies {")) {
newBuildContent.append(" implementation '")
.append(dependency.getGroupId())
.append(coords.getGroupId())
.append(":")
.append(dependency.getArtifactId());
if (dependency.getVersion() != null && !dependency.getVersion().isEmpty()) {
.append(coords.getArtifactId());
if (coords.getVersion() != null && !coords.getVersion().isEmpty()) {
newBuildContent.append(":")
.append(dependency.getVersion());
.append(coords.getVersion());
}
newBuildContent.append("'")
.append(System.lineSeparator());
}
});
getModel().setBuildContent(newBuildContent.toString());
model.setBuildContent(newBuildContent.toString());
}

@Override
protected void removeDependencyFromBuildFile(Dependency dependency) throws IOException {
String depString = new StringBuilder("'").append(dependency.getGroupId()).append(":")
.append(dependency.getArtifactId()).toString();
protected void removeDependencyFromBuildFile(AppArtifactKey key) throws IOException {
StringBuilder newBuildContent = new StringBuilder();
Scanner scanner = new Scanner(getModel().getBuildContent());
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (!line.contains(depString)) {
if (!line.contains(key.getGroupId() + ":" + key.getArtifactId())) {
newBuildContent.append(line).append(System.lineSeparator());
}
}
Expand Down Expand Up @@ -115,16 +118,6 @@ public BuildTool getBuildTool() {
return BuildTool.GRADLE;
}

private void readLineByLine(String content, Consumer<String> lineConsumer) {
try (Scanner scanner = new Scanner(new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8)),
StandardCharsets.UTF_8.name())) {
while (scanner.hasNextLine()) {
String currentLine = scanner.nextLine();
lineConsumer.accept(currentLine);
}
}
}

private Model getModel() throws IOException {
return modelReference.updateAndGet(model -> {
if (model == null) {
Expand Down Expand Up @@ -192,7 +185,17 @@ protected String getBuildContent() throws IOException {
return getModel().getBuildContent();
}

private static class Model {
private static void readLineByLine(String content, Consumer<String> lineConsumer) {
try (Scanner scanner = new Scanner(new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8)),
StandardCharsets.UTF_8.name())) {
while (scanner.hasNextLine()) {
String currentLine = scanner.nextLine();
lineConsumer.accept(currentLine);
}
}
}

static class Model {
private String settingsContent;
private String buildContent;
private Properties propertiesContent;
Expand Down
Loading

0 comments on commit e185272

Please sign in to comment.