Skip to content

Commit

Permalink
Significantly improve the output of the update command
Browse files Browse the repository at this point in the history
When having an error, it was extremely confusing, you had confusing
error messages, a lot of nested exceptions...

This will improve the output, make sure we have proper colors for the
log levels and avoid having two levels logged.
Also avoid a couple of nested exceptions and make the error messages
more actionable.

(cherry picked from commit 8150138)
  • Loading branch information
gsmet committed Aug 26, 2023
1 parent e0aad4e commit fe1bba7
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 16 deletions.
7 changes: 5 additions & 2 deletions devtools/maven/src/main/java/io/quarkus/maven/UpdateMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import io.quarkus.devtools.commands.data.QuarkusCommandOutcome;
import io.quarkus.devtools.project.QuarkusProject;
import io.quarkus.devtools.project.QuarkusProjectHelper;
import io.quarkus.devtools.project.update.rewrite.QuarkusUpdateExitErrorException;
import io.quarkus.maven.dependency.ArtifactCoords;
import io.quarkus.registry.RegistryResolutionException;
import io.quarkus.registry.catalog.ExtensionCatalog;
Expand Down Expand Up @@ -129,10 +130,12 @@ protected void processProjectState(QuarkusProject quarkusProject) throws MojoExe
final QuarkusCommandOutcome result = invoker.execute();
if (!result.isSuccess()) {
throw new MojoExecutionException(
"The command did not succeed.");
"Failed to apply the updates.");
}
} catch (QuarkusUpdateExitErrorException e) {
throw new MojoExecutionException(e.getMessage());
} catch (QuarkusCommandException e) {
throw new MojoExecutionException("Failed to resolve the available updates", e);
throw new MojoExecutionException("Failed to apply the updates", e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import io.quarkus.devtools.project.update.ProjectPlatformUpdateInfo;
import io.quarkus.devtools.project.update.ProjectUpdateInfos;
import io.quarkus.devtools.project.update.rewrite.QuarkusUpdateCommand;
import io.quarkus.devtools.project.update.rewrite.QuarkusUpdateException;
import io.quarkus.devtools.project.update.rewrite.QuarkusUpdates;
import io.quarkus.devtools.project.update.rewrite.QuarkusUpdatesRepository;
import io.quarkus.maven.dependency.ArtifactCoords;
Expand Down Expand Up @@ -117,8 +116,6 @@ public QuarkusCommandOutcome execute(QuarkusCommandInvocation invocation) throws
rewriteDryRun);
} catch (IOException e) {
throw new QuarkusCommandException("Error while generating the project update script", e);
} catch (QuarkusUpdateException e) {
throw new QuarkusCommandException("Error while running the project update script", e);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand All @@ -39,7 +41,7 @@ public static String goal(boolean dryRun) {
}

public static void handle(MessageWriter log, BuildTool buildTool, Path baseDir,
String rewritePluginVersion, String recipesGAV, Path recipe, boolean dryRun) throws QuarkusUpdateException {
String rewritePluginVersion, String recipesGAV, Path recipe, boolean dryRun) {
switch (buildTool) {
case MAVEN:
runMavenUpdate(log, baseDir, rewritePluginVersion, recipesGAV, recipe, dryRun);
Expand All @@ -53,7 +55,7 @@ public static void handle(MessageWriter log, BuildTool buildTool, Path baseDir,
}

private static void runGradleUpdate(MessageWriter log, Path baseDir, String rewritePluginVersion, String recipesGAV,
Path recipe, boolean dryRun) throws QuarkusUpdateException {
Path recipe, boolean dryRun) {
Path tempInit = null;
try {
tempInit = Files.createTempFile("openrewrite-init", "gradle");
Expand All @@ -77,8 +79,11 @@ private static void runGradleUpdate(MessageWriter log, Path baseDir, String rewr
"--init-script",
tempInit.toAbsolutePath().toString(), dryRun ? "rewriteDryRun" : "rewriteRun" };
executeCommand(baseDir, command, log);
} catch (QuarkusUpdateException e) {
throw e;
} catch (Exception e) {
throw new QuarkusUpdateException("Error while running Gradle rewrite command", e);
throw new QuarkusUpdateException(
"Error while running Gradle rewrite command, see the execution logs above for more details", e);
} finally {
if (tempInit != null) {
try {
Expand All @@ -93,7 +98,7 @@ private static void runGradleUpdate(MessageWriter log, Path baseDir, String rewr

private static void runMavenUpdate(MessageWriter log, Path baseDir, String rewritePluginVersion, String recipesGAV,
Path recipe,
boolean dryRun) throws QuarkusUpdateException {
boolean dryRun) {
final String mvnBinary = findMvnBinary(baseDir);
executeCommand(baseDir, getMavenUpdateCommand(mvnBinary, rewritePluginVersion, recipesGAV, recipe, dryRun), log);

Expand All @@ -118,7 +123,7 @@ private static String[] getMavenUpdateCommand(String mvnBinary, String rewritePl
"-Drewrite.pomCacheEnabled=false" };
}

private static void executeCommand(Path baseDir, String[] command, MessageWriter log) throws QuarkusUpdateException {
private static void executeCommand(Path baseDir, String[] command, MessageWriter log) {
ProcessBuilder processBuilder = new ProcessBuilder();
log.info("");
log.info("");
Expand All @@ -136,8 +141,28 @@ private static void executeCommand(Path baseDir, String[] command, MessageWriter
BufferedReader errorReader = new BufferedReader(new java.io.InputStreamReader(process.getErrorStream()));

String line;
LogLevel currentLogLevel = LogLevel.UNKNOWN;

while ((line = inputReader.readLine()) != null) {
log.info(line);
Optional<LogLevel> detectedLogLevel = LogLevel.of(line);
if (detectedLogLevel.isPresent()) {
currentLogLevel = detectedLogLevel.get();
}
switch (currentLogLevel) {
case ERROR:
log.error(currentLogLevel.clean(line));
break;
case WARNING:
log.warn(currentLogLevel.clean(line));
break;
case INFO:
log.info(currentLogLevel.clean(line));
break;
case UNKNOWN:
default:
log.info(line);
break;
}
}
while ((line = errorReader.readLine()) != null) {
log.error(line);
Expand All @@ -149,14 +174,19 @@ private static void executeCommand(Path baseDir, String[] command, MessageWriter

int exitCode = process.waitFor();
if (exitCode != 0) {
throw new QuarkusUpdateException("The command to update the project exited with an error: " + exitCode);
throw new QuarkusUpdateExitErrorException(
"The command to update the project exited with an error, see the execution logs above for more details");
}
} catch (QuarkusUpdateException e) {
throw e;
} catch (Exception e) {
throw new QuarkusUpdateException("Error while executing command to udpate the project", e);
throw new QuarkusUpdateException(
"Error while executing the command to update the project, see the execution logs above for more details",
e);
}
}

static String findMvnBinary(Path baseDir) throws QuarkusUpdateException {
static String findMvnBinary(Path baseDir) {
Path mavenCmd = findWrapperOrBinary(baseDir, "mvnw", "mvn");
if (mavenCmd == null) {
throw new QuarkusUpdateException("Cannot locate mvnw or mvn"
Expand All @@ -165,7 +195,7 @@ static String findMvnBinary(Path baseDir) throws QuarkusUpdateException {
return mavenCmd.toString();
}

static String findGradleBinary(Path baseDir) throws QuarkusUpdateException {
static String findGradleBinary(Path baseDir) {
Path gradleCmd = findWrapperOrBinary(baseDir, "gradlew", "gradle");
if (gradleCmd == null) {
throw new QuarkusUpdateException("Cannot gradlew mvnw or gradle"
Expand Down Expand Up @@ -247,4 +277,49 @@ private static boolean hasMaven(Path dir) {
return Files.exists(dir.resolve("pom.xml"));
}

private enum LogLevel {

ERROR,
WARNING,
INFO,
UNKNOWN;

private static final Pattern LEVEL_PATTERN = Pattern.compile("^\\[[A-Z]+\\].*");

private static Optional<LogLevel> of(String line) {
if (line == null || line.isBlank()) {
return Optional.empty();
}

for (LogLevel level : LogLevel.values()) {
if (level.matches(line)) {
return Optional.of(level);
}
}

if (LEVEL_PATTERN.matcher(line).matches()) {
return Optional.of(UNKNOWN);
}

return Optional.empty();
}

private String clean(String line) {
if (line == null || line.isBlank()) {
return line;
}

String pattern = "[" + name() + "]";

if (line.length() < pattern.length()) {
return line;
}

return line.substring(pattern.length()).trim();
}

private boolean matches(String line) {
return line != null && line.startsWith("[" + name() + "]");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.quarkus.devtools.project.update.rewrite;

public class QuarkusUpdateException extends Exception {
public class QuarkusUpdateException extends RuntimeException {

private static final long serialVersionUID = 1L;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.quarkus.devtools.project.update.rewrite;

public class QuarkusUpdateExitErrorException extends QuarkusUpdateException {

private static final long serialVersionUID = 1L;

public QuarkusUpdateExitErrorException(String message, Throwable cause) {
super(message, cause);
}

public QuarkusUpdateExitErrorException(String message) {
super(message);
}
}

0 comments on commit fe1bba7

Please sign in to comment.