diff --git a/rewrite-core/src/main/java/org/openrewrite/ExecutionContext.java b/rewrite-core/src/main/java/org/openrewrite/ExecutionContext.java index 808dc79059f..d7cb109d9b9 100644 --- a/rewrite-core/src/main/java/org/openrewrite/ExecutionContext.java +++ b/rewrite-core/src/main/java/org/openrewrite/ExecutionContext.java @@ -32,7 +32,6 @@ public interface ExecutionContext { String CURRENT_CYCLE = "org.openrewrite.currentCycle"; String CURRENT_RECIPE = "org.openrewrite.currentRecipe"; - String WORKING_DIRECTORY = "org.openrewrite.workingDirectory"; String DATA_TABLES = "org.openrewrite.dataTables"; String RUN_TIMEOUT = "org.openrewrite.runTimeout"; String REQUIRE_PRINT_EQUALS_INPUT = "org.openrewrite.requirePrintEqualsInput"; diff --git a/rewrite-core/src/main/java/org/openrewrite/WorkingDirectoryExecutionContextView.java b/rewrite-core/src/main/java/org/openrewrite/WorkingDirectoryExecutionContextView.java deleted file mode 100644 index 4469aa2a3e4..00000000000 --- a/rewrite-core/src/main/java/org/openrewrite/WorkingDirectoryExecutionContextView.java +++ /dev/null @@ -1,103 +0,0 @@ -/* - * Copyright 2023 the original author or authors. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * https://www.apache.org/licenses/LICENSE-2.0 - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.openrewrite; - -import org.openrewrite.internal.lang.Nullable; - -import java.io.IOException; -import java.io.UncheckedIOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.Objects; -import java.util.Optional; -import java.util.stream.Stream; - -public class WorkingDirectoryExecutionContextView extends DelegatingExecutionContext implements AutoCloseable { - @Nullable - private final Path existing; - - private WorkingDirectoryExecutionContextView(ExecutionContext delegate, @Nullable Path existing) { - super(delegate); - this.existing = existing; - try { - if (existing != null) { - if (!Files.isDirectory(existing)) { - Files.createDirectories(existing); - } - putMessage(WORKING_DIRECTORY, existing); - } else { - putMessage(WORKING_DIRECTORY, Files.createTempDirectory("recipe-wd")); - } - } catch (IOException e) { - throw new UncheckedIOException(e); - } - } - - /** - * Create a new {@link WorkingDirectoryExecutionContextView} which will create a new temporary directory for the - * working directory. This temporary working directory will automatically get deleted once this object is closed. - *

- * For recipes which only want to access the provided working directory, use {@link #getWorkingDirectory(ExecutionContext)}. - */ - public static WorkingDirectoryExecutionContextView createNew(ExecutionContext ctx) { - if (ctx instanceof WorkingDirectoryExecutionContextView) { - return (WorkingDirectoryExecutionContextView) ctx; - } - return new WorkingDirectoryExecutionContextView(ctx, null); - } - - /** - * Create a new {@link WorkingDirectoryExecutionContextView} using a provided working directory. While the directory - * will get created if it doesn't already exist, it is the responsibility of the caller to delete it. Thus, the - * {@link #close()} method is a no-op. - *

- * For recipes which only want to access the provided working directory, use {@link #getWorkingDirectory(ExecutionContext)}. - */ - public static WorkingDirectoryExecutionContextView useProvided(ExecutionContext ctx, Path existing) { - if (ctx instanceof WorkingDirectoryExecutionContextView) { - return (WorkingDirectoryExecutionContextView) ctx; - } - return new WorkingDirectoryExecutionContextView(ctx, existing); - } - - public static Path getWorkingDirectory(ExecutionContext ctx) { - return Optional.ofNullable(ctx.getMessage(WORKING_DIRECTORY)) - .orElseThrow(() -> new IllegalStateException("Working directory not set")); - } - - public static void deleteWorkingDirectory(ExecutionContext ctx) { - delete(getWorkingDirectory(ctx)); - } - - @Override - public void close() { - if (existing == null) { - delete(Objects.requireNonNull(getMessage(WORKING_DIRECTORY))); - } - } - - private static void delete(Path path) { - try { - if (Files.isDirectory(path)) { - try (Stream files = Files.list(path)) { - files.forEach(WorkingDirectoryExecutionContextView::delete); - } - } - Files.delete(path); - } catch (IOException ignore) { - } - } -} diff --git a/rewrite-test/src/main/java/org/openrewrite/test/RewriteTest.java b/rewrite-test/src/main/java/org/openrewrite/test/RewriteTest.java index c7c3270ca24..7c88bb7530e 100644 --- a/rewrite-test/src/main/java/org/openrewrite/test/RewriteTest.java +++ b/rewrite-test/src/main/java/org/openrewrite/test/RewriteTest.java @@ -341,15 +341,12 @@ default void rewriteRun(Consumer spec, SourceSpec... sourceSpecs) lss = new LargeSourceSetCheckingExpectedCycles(expectedCyclesThatMakeChanges, runnableSourceFiles); } - RecipeRun recipeRun; - try (WorkingDirectoryExecutionContextView ctx = WorkingDirectoryExecutionContextView.createNew(recipeExecutionContext)) { - recipeRun = recipe.run( - lss, - ctx, - cycles, - expectedCyclesThatMakeChanges + 1 - ); - } + RecipeRun recipeRun = recipe.run( + lss, + recipeExecutionContext, + cycles, + expectedCyclesThatMakeChanges + 1 + ); for (Consumer afterRecipe : testClassSpec.afterRecipes) { afterRecipe.accept(recipeRun);