From e2df9797601a273dd64560e2ee92bf962a01ca08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20Kr=C3=BCger?= <56278322+fabapp2@users.noreply.github.com> Date: Tue, 30 Jan 2024 17:53:11 +0100 Subject: [PATCH] Add run method for recipe instances --- .../rewrite/RewriteRecipeLauncher.java | 49 ++++++++++++++----- 1 file changed, 37 insertions(+), 12 deletions(-) diff --git a/spring-rewrite-commons-launcher/src/main/java/org/springframework/rewrite/RewriteRecipeLauncher.java b/spring-rewrite-commons-launcher/src/main/java/org/springframework/rewrite/RewriteRecipeLauncher.java index df83939..a15fe77 100644 --- a/spring-rewrite-commons-launcher/src/main/java/org/springframework/rewrite/RewriteRecipeLauncher.java +++ b/spring-rewrite-commons-launcher/src/main/java/org/springframework/rewrite/RewriteRecipeLauncher.java @@ -31,12 +31,12 @@ import java.util.Optional; import java.util.concurrent.TimeUnit; /** + * Helps to apply recipes. + * * @author Fabian Krüger */ public class RewriteRecipeLauncher { - private static final Logger logger = LoggerFactory.getLogger(RewriteRecipeLauncher.class); - private final RewriteProjectParser parser; private final RewriteRecipeDiscovery discovery; @@ -45,6 +45,9 @@ public class RewriteRecipeLauncher { private final ProjectResourceSetSerializer serializer; + /** + * Listener API for progress events/messages + */ public interface RewriteRecipeRunnerProgressListener { void progress(String progressMessage); @@ -59,21 +62,42 @@ public class RewriteRecipeLauncher { this.serializer = serializer; } + /** + * Apply the recipe with {@code recipeName} to the project under {@code path}. + * @throws IllegalStateException when the recipe couldn't be discovered + */ public void run(String recipeName, String path) { run(recipeName, path, __ -> { }); } + /** + * Apply the recipe with {@code recipeName} to the project under {@code path}. + * @throws IllegalStateException when the recipe couldn't be discovered + */ public void run(String recipeName, String path, RewriteRecipeRunnerProgressListener listener) { + Optional recipe = discoverRecipe(recipeName); + if (recipe.isEmpty()) { + throw new IllegalStateException("Could not find recipe " + recipeName + "."); + } + run(recipe.get(), path, listener); + } + + /** + * Apply the {@link Recipe} to the project under {@code path}. + */ + public void run(Recipe recipe, String path) { + run(recipe, path, __ -> { + }); + } + + /** + * Apply the {@link Recipe} to the project under {@code path}. + */ + public void run(Recipe recipe, String path, RewriteRecipeRunnerProgressListener listener) { Path baseDir = getBaseDir(path); RewriteProjectParsingResult parsingResult = parseProject(baseDir, listener); - Optional recipe = discoverRecipe(recipeName); - if (recipe.isPresent()) { - applyRecipe(recipeName, baseDir, parsingResult, recipe, listener); - } - else { - logger.error("Could not find recipe " + recipeName + "."); - } + applyRecipe(baseDir, parsingResult, recipe, listener); } @NotNull @@ -88,15 +112,16 @@ public class RewriteRecipeLauncher { return parsingResult; } - private void applyRecipe(String recipeName, Path baseDir, RewriteProjectParsingResult parsingResult, - Optional recipe, RewriteRecipeRunnerProgressListener listener) { + private void applyRecipe(Path baseDir, RewriteProjectParsingResult parsingResult, Recipe recipe, + RewriteRecipeRunnerProgressListener listener) { + Object recipeName = recipe.getName(); StopWatch stopWatch = new StopWatch("parse"); stopWatch.start(); // Use ProjectResourceSet abstraction ProjectResourceSet projectResourceSet = resourceSetFactory.create(baseDir, parsingResult.sourceFiles()); // To apply recipes listener.progress("Applying recipe %s, this may take a few minutes.".formatted(recipeName)); - projectResourceSet.apply(recipe.get()); + projectResourceSet.apply(recipe); stopWatch.stop(); double recipeRunTime = stopWatch.getTotalTime(TimeUnit.MINUTES); listener.progress("Applied recipe %s in %f min.".formatted(recipeName, recipeRunTime));