Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sum estimate time saved and log it #294

Merged
merged 7 commits into from
May 2, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.nio.file.Paths;
import java.time.Duration;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;
Expand Down Expand Up @@ -347,26 +348,31 @@ public void dryRun(Path reportPath, ResultsContainer results) {
}

if (results.isNotEmpty()) {
Duration estimateTimeSaved = Duration.ZERO;
for (Result result : results.generated) {
assert result.getAfter() != null;
logger.warn("These recipes would generate new file {}:", result.getAfter().getSourcePath());
logRecipesThatMadeChanges(result);
estimateTimeSaved = estimateTimeSavedSum(result, estimateTimeSaved);
}
for (Result result : results.deleted) {
assert result.getBefore() != null;
logger.warn("These recipes would delete file {}:", result.getBefore().getSourcePath());
logRecipesThatMadeChanges(result);
estimateTimeSaved = estimateTimeSavedSum(result, estimateTimeSaved);
}
for (Result result : results.moved) {
assert result.getBefore() != null;
assert result.getAfter() != null;
logger.warn("These recipes would move file from {} to {}:", result.getBefore().getSourcePath(), result.getAfter().getSourcePath());
logRecipesThatMadeChanges(result);
estimateTimeSaved = estimateTimeSavedSum(result, estimateTimeSaved);
}
for (Result result : results.refactoredInPlace) {
assert result.getBefore() != null;
logger.warn("These recipes would make changes to {}:", result.getBefore().getSourcePath());
logRecipesThatMadeChanges(result);
estimateTimeSaved = estimateTimeSavedSum(result, estimateTimeSaved);
}

//noinspection ResultOfMethodCallIgnored
Expand All @@ -391,6 +397,7 @@ public void dryRun(Path reportPath, ResultsContainer results) {
}
logger.warn("Report available:");
logger.warn(" {}", reportPath.normalize());
logger.warn("Estimate time saved: {}", formatDuration(estimateTimeSaved));
logger.warn("Run 'gradle rewriteRun' to apply the recipes.");

if (project.getExtensions().getByType(RewriteExtension.class).getFailOnDryRunResults()) {
Expand All @@ -404,6 +411,14 @@ public void dryRun(Path reportPath, ResultsContainer results) {
}
}

private static String formatDuration(Duration duration) {
return duration.toString()
.substring(2)
.replaceAll("(\\d[HMS])(?!$)", "$1 ")
.toLowerCase()
.trim();
}

@Override
public void run(Consumer<Throwable> onError) {
ExecutionContext ctx = new InMemoryExecutionContext(onError);
Expand All @@ -413,6 +428,7 @@ public void run(Consumer<Throwable> onError) {
public void run(ResultsContainer results, ExecutionContext ctx) {
try {
if (results.isNotEmpty()) {
Duration estimateTimeSaved = Duration.ZERO;
RuntimeException firstException = results.getFirstException();
if (firstException != null) {
logger.error("The recipe produced an error. Please report this to the recipe author.");
Expand All @@ -425,13 +441,15 @@ public void run(ResultsContainer results, ExecutionContext ctx) {
result.getAfter().getSourcePath() +
" by:");
logRecipesThatMadeChanges(result);
estimateTimeSaved = estimateTimeSavedSum(result, estimateTimeSaved);
}
for (Result result : results.deleted) {
assert result.getBefore() != null;
logger.lifecycle("Deleted file " +
result.getBefore().getSourcePath() +
" by:");
logRecipesThatMadeChanges(result);
estimateTimeSaved = estimateTimeSavedSum(result, estimateTimeSaved);
}
for (Result result : results.moved) {
assert result.getAfter() != null;
Expand All @@ -440,17 +458,21 @@ public void run(ResultsContainer results, ExecutionContext ctx) {
result.getBefore().getSourcePath() + " to " +
result.getAfter().getSourcePath() + " by:");
logRecipesThatMadeChanges(result);
estimateTimeSaved = estimateTimeSavedSum(result, estimateTimeSaved);
}
for (Result result : results.refactoredInPlace) {
assert result.getBefore() != null;
logger.lifecycle("Changes have been made to " +
result.getBefore().getSourcePath() +
" by:");
logRecipesThatMadeChanges(result);
estimateTimeSaved = estimateTimeSavedSum(result, estimateTimeSaved);
}

logger.lifecycle("Please review and commit the results.");

logger.lifecycle("Estimate time saved: {}", formatDuration(estimateTimeSaved));

try {
for (Result result : results.generated) {
writeAfter(results.getProjectRoot(), result, ctx);
Expand Down Expand Up @@ -515,6 +537,13 @@ public void run(ResultsContainer results, ExecutionContext ctx) {
}
}

private static Duration estimateTimeSavedSum(Result result, Duration timeSaving) {
if (null != result.getTimeSavings()) {
return timeSaving.plus(result.getTimeSavings());
}
return timeSaving;
}

private static void writeAfter(Path root, Result result, ExecutionContext ctx) {
assert result.getAfter() != null;
Path targetPath = root.resolve(result.getAfter().getSourcePath());
Expand Down
Loading