From 1b8d134b122d704121cac172a5214fbe919b3494 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20Kr=C3=BCger?= Date: Fri, 29 Sep 2023 01:31:07 +0200 Subject: [PATCH] Builds * Use new ProjectResourceSetFactory * Use RewriteMigrationResultMerger --- .../sbm/test/RecipeTestSupport.java | 2 +- .../springframework/sbm/build/api/Module.java | 28 +++-- .../sbm/engine/context/ProjectContext.java | 12 +- .../engine/context/ProjectContextFactory.java | 5 +- .../MigrationResultProjectContextMerger.java | 42 +++++++ .../OpenRewriteRecipeAdapterAction.java | 2 +- .../recipe/RewriteMigrationResultMerger.java | 104 ------------------ .../engine/recipe/RewriteRecipeRunner.java | 2 +- .../parser/ProjectContextInitializer.java | 4 +- .../archfitfun/ExecutionScopeArchFitTest.java | 8 +- ...clarativeRecipeAdapterIntegrationTest.java | 2 +- ...riteNamedRecipeAdapterIntegrationTest.java | 2 +- .../parser/ProjectContextInitializerTest.java | 4 +- .../com/acme/example/SpringBoot3Upgrade.java | 27 +++-- .../ProjectResourceSetConfiguration.java | 19 ++++ .../project/resource/ProjectResourceSet.java | 5 + .../resource/ProjectResourceSetFactory.java | 44 ++++++++ 17 files changed, 169 insertions(+), 143 deletions(-) create mode 100644 components/sbm-core/src/main/java/org/springframework/sbm/engine/recipe/MigrationResultProjectContextMerger.java delete mode 100644 components/sbm-core/src/main/java/org/springframework/sbm/engine/recipe/RewriteMigrationResultMerger.java create mode 100644 sbm-support-rewrite/src/main/java/org/springframework/sbm/project/resource/ProjectResourceSetFactory.java diff --git a/components/recipe-test-support/src/main/java/org/springframework/sbm/test/RecipeTestSupport.java b/components/recipe-test-support/src/main/java/org/springframework/sbm/test/RecipeTestSupport.java index 1007c623a..fab62c8c7 100644 --- a/components/recipe-test-support/src/main/java/org/springframework/sbm/test/RecipeTestSupport.java +++ b/components/recipe-test-support/src/main/java/org/springframework/sbm/test/RecipeTestSupport.java @@ -69,7 +69,7 @@ private RecipeTestSupport() { DefaultActionDeserializer.class, RewriteJavaSearchActionDeserializer.class, RewriteRecipeLoader.class, - RewriteMigrationResultMerger.class, + MigrationResultProjectContextMerger.class, RewriteSourceFileWrapper.class, SbmRecipeLoader.class, BasePackageCalculator.class, diff --git a/components/sbm-core/src/main/java/org/springframework/sbm/build/api/Module.java b/components/sbm-core/src/main/java/org/springframework/sbm/build/api/Module.java index e5cddb8c0..51b76f752 100644 --- a/components/sbm-core/src/main/java/org/springframework/sbm/build/api/Module.java +++ b/components/sbm-core/src/main/java/org/springframework/sbm/build/api/Module.java @@ -21,13 +21,15 @@ import org.springframework.sbm.build.impl.MavenBuildFileUtil; import org.springframework.sbm.build.impl.OpenRewriteMavenBuildFile; import org.springframework.sbm.common.util.Verify; -import org.springframework.sbm.engine.recipe.RewriteMigrationResultMerger; +import org.springframework.sbm.engine.recipe.MigrationResultProjectContextMerger; import org.springframework.sbm.java.api.JavaSource; import org.springframework.sbm.java.api.JavaSourceLocation; import org.springframework.sbm.java.refactoring.JavaRefactoringFactory; import org.springframework.sbm.java.util.BasePackageCalculator; import org.springframework.sbm.parsers.JavaParserBuilder; import org.springframework.sbm.project.resource.ProjectResourceSet; +import org.springframework.sbm.project.resource.ProjectResourceSetFactory; +import org.springframework.sbm.project.resource.RewriteMigrationResultMerger; import org.springframework.sbm.project.resource.RewriteSourceFileHolder; import org.springframework.sbm.project.resource.finder.ProjectResourceFinder; import lombok.Getter; @@ -59,6 +61,7 @@ public class Module { private final JavaParserBuilder javaParserBuilder; private final ExecutionContext executionContext; private final RewriteMigrationResultMerger rewriteMigrationResultMerger; + private final ProjectResourceSetFactory projectResourceSetFactory; public JavaSourceLocation getBaseJavaSourceLocation() { return getMainJavaSourceSet().getJavaSourceLocation(); @@ -142,7 +145,8 @@ public List getModules() { basePackageCalculator, javaParserBuilder, executionContext, - rewriteMigrationResultMerger) + rewriteMigrationResultMerger, + projectResourceSetFactory) ) .collect(Collectors.toList()); } else { @@ -156,8 +160,14 @@ public List getDeclaredModules() { public T search(ProjectResourceFinder finder) { List> resources = getModuleResources(); - ProjectResourceSet filteredProjectResourceSet = new ProjectResourceSet(resources, executionContext, migrationResultMerger); - return finder.apply(filteredProjectResourceSet); + if(!resources.isEmpty()) { + Path baseDir = resources.get(0).getAbsoluteProjectDir(); + List sourceFiles = getModuleResources().stream().map(RewriteSourceFileHolder::getSourceFile).map(SourceFile.class::cast).toList(); + ProjectResourceSet filteredProjectResourceSet = projectResourceSetFactory.create(baseDir, sourceFiles); + return finder.apply(filteredProjectResourceSet); + } else { + return null; + } } private List> getModuleResources() { @@ -184,12 +194,12 @@ private boolean isResourceOnPath(RewriteSourceFileHolder r } public T searchMainResources(ProjectResourceFinder finder) { - ProjectResourceSet resourceSet = new ImmutableFilteringProjectResourceSet(projectResourceSet, (RewriteSourceFileHolder r) -> r.getAbsolutePath().normalize().startsWith(getMainResourceSet().getAbsolutePath().toAbsolutePath().normalize())); + ProjectResourceSet resourceSet = new ImmutableFilteringProjectResourceSet(projectResourceSet, (RewriteSourceFileHolder r) -> r.getAbsolutePath().normalize().startsWith(getMainResourceSet().getAbsolutePath().toAbsolutePath().normalize()), rewriteMigrationResultMerger); return finder.apply(resourceSet); } public T searchMainJava(ProjectResourceFinder finder) { - ProjectResourceSet resourceSet = new ImmutableFilteringProjectResourceSet(projectResourceSet, (RewriteSourceFileHolder r) -> r.getAbsolutePath().normalize().startsWith(getMainJavaSourceSet().getAbsolutePath().toAbsolutePath().normalize())); + ProjectResourceSet resourceSet = new ImmutableFilteringProjectResourceSet(projectResourceSet, (RewriteSourceFileHolder r) -> r.getAbsolutePath().normalize().startsWith(getMainJavaSourceSet().getAbsolutePath().toAbsolutePath().normalize()), rewriteMigrationResultMerger); return finder.apply(resourceSet); } @@ -197,12 +207,12 @@ public T searchTestResources(ProjectResourceFinder finder) { Predicate> predicate = (RewriteSourceFileHolder r) -> { return r.getAbsolutePath().normalize().startsWith(getTestResourceSet().getAbsolutePath().toAbsolutePath().normalize()); }; - ProjectResourceSet resourceSet = new ImmutableFilteringProjectResourceSet(projectResourceSet, predicate); + ProjectResourceSet resourceSet = new ImmutableFilteringProjectResourceSet(projectResourceSet, predicate, rewriteMigrationResultMerger); return finder.apply(resourceSet); } public T searchTestJava(ProjectResourceFinder finder) { - ProjectResourceSet resourceSet = new ImmutableFilteringProjectResourceSet(projectResourceSet, (RewriteSourceFileHolder r) -> r.getAbsolutePath().normalize().startsWith(getTestJavaSourceSet().getAbsolutePath().toAbsolutePath().normalize())); + ProjectResourceSet resourceSet = new ImmutableFilteringProjectResourceSet(projectResourceSet, (RewriteSourceFileHolder r) -> r.getAbsolutePath().normalize().startsWith(getTestJavaSourceSet().getAbsolutePath().toAbsolutePath().normalize()), rewriteMigrationResultMerger); return finder.apply(resourceSet); } @@ -230,7 +240,7 @@ private class ImmutableFilteringProjectResourceSet extends ProjectResourceSet{ private final ProjectResourceSet projectResourceSet; private final Predicate> predicate; - public ImmutableFilteringProjectResourceSet(ProjectResourceSet projectResourceSet, Predicate> predicate) { + public ImmutableFilteringProjectResourceSet(ProjectResourceSet projectResourceSet, Predicate> predicate, RewriteMigrationResultMerger migrationResultMerger) { super(projectResourceSet.list(), executionContext, migrationResultMerger); this.projectResourceSet = projectResourceSet; this.predicate = predicate; diff --git a/components/sbm-core/src/main/java/org/springframework/sbm/engine/context/ProjectContext.java b/components/sbm-core/src/main/java/org/springframework/sbm/engine/context/ProjectContext.java index 23d80f94c..e3b0b433a 100644 --- a/components/sbm-core/src/main/java/org/springframework/sbm/engine/context/ProjectContext.java +++ b/components/sbm-core/src/main/java/org/springframework/sbm/engine/context/ProjectContext.java @@ -25,13 +25,14 @@ import org.springframework.sbm.build.api.BuildFile; import org.springframework.sbm.build.api.RootBuildFileFilter; import org.springframework.sbm.build.filter.BuildFileProjectResourceFinder; -import org.springframework.sbm.engine.recipe.RewriteMigrationResultMerger; import org.springframework.sbm.java.api.ProjectJavaSources; import org.springframework.sbm.java.impl.ProjectJavaSourcesImpl; import org.springframework.sbm.java.refactoring.JavaRefactoringFactory; import org.springframework.sbm.java.util.BasePackageCalculator; import org.springframework.sbm.parsers.JavaParserBuilder; import org.springframework.sbm.project.resource.ProjectResourceSet; +import org.springframework.sbm.project.resource.ProjectResourceSetFactory; +import org.springframework.sbm.project.resource.RewriteMigrationResultMerger; import org.springframework.sbm.project.resource.RewriteSourceFileHolder; import org.springframework.sbm.project.resource.finder.ProjectResourceFinder; import lombok.Getter; @@ -53,8 +54,9 @@ public class ProjectContext { private final JavaParserBuilder javaParserBuilder; private final ExecutionContext executionContext; private final RewriteMigrationResultMerger rewriteMigrationResultMerger; + private final ProjectResourceSetFactory projectResourceSetFactory; - public ProjectContext(JavaRefactoringFactory javaRefactoringFactory, Path projectRootDirectory, ProjectResourceSet projectResources, BasePackageCalculator basePackageCalculator, JavaParserBuilder javaParserBuilder, ExecutionContext executionContext, RewriteMigrationResultMerger rewriteMigrationResultMerger) { + public ProjectContext(JavaRefactoringFactory javaRefactoringFactory, Path projectRootDirectory, ProjectResourceSet projectResources, BasePackageCalculator basePackageCalculator, JavaParserBuilder javaParserBuilder, ExecutionContext executionContext, RewriteMigrationResultMerger rewriteMigrationResultMerger, ProjectResourceSetFactory projectResourceSetFactory) { this.projectRootDirectory = projectRootDirectory.toAbsolutePath(); this.projectResources = projectResources; this.javaRefactoringFactory = javaRefactoringFactory; @@ -62,6 +64,7 @@ public ProjectContext(JavaRefactoringFactory javaRefactoringFactory, Path projec this.javaParserBuilder = javaParserBuilder; this.executionContext = executionContext; this.rewriteMigrationResultMerger = rewriteMigrationResultMerger; + this.projectResourceSetFactory = projectResourceSetFactory; } public ProjectResourceSet getProjectResources() { @@ -92,7 +95,8 @@ private Module mapToModule(BuildFile buildFile) { basePackageCalculator, javaParserBuilder, executionContext, - rewriteMigrationResultMerger + rewriteMigrationResultMerger, + projectResourceSetFactory ); } @@ -128,7 +132,7 @@ public void apply(Recipe upgradeBootRecipe) { .toList(); RecipeRun recipeRun = upgradeBootRecipe.run(new InMemoryLargeSourceSet(ast), executionContext); - rewriteMigrationResultMerger.mergeResults(this, recipeRun.getChangeset().getAllResults()); + rewriteMigrationResultMerger.mergeResults(getProjectResources(), recipeRun.getChangeset().getAllResults()); // recipeRun.getChangeset().getAllResults().stream() // .forEach(r -> { // diff --git a/components/sbm-core/src/main/java/org/springframework/sbm/engine/context/ProjectContextFactory.java b/components/sbm-core/src/main/java/org/springframework/sbm/engine/context/ProjectContextFactory.java index 991695b55..348fe9c8d 100644 --- a/components/sbm-core/src/main/java/org/springframework/sbm/engine/context/ProjectContextFactory.java +++ b/components/sbm-core/src/main/java/org/springframework/sbm/engine/context/ProjectContextFactory.java @@ -18,7 +18,7 @@ import org.openrewrite.ExecutionContext; import org.springframework.sbm.build.api.BuildFile; import org.springframework.sbm.build.filter.BuildFileProjectResourceFinder; -import org.springframework.sbm.engine.recipe.RewriteMigrationResultMerger; +import org.springframework.sbm.engine.recipe.MigrationResultProjectContextMerger; import org.springframework.sbm.java.refactoring.JavaRefactoringFactory; import org.springframework.sbm.java.impl.ClasspathRegistry; import org.springframework.sbm.java.util.BasePackageCalculator; @@ -43,6 +43,7 @@ public class ProjectContextFactory { private final JavaParserBuilder javaParserBuilder; private final ExecutionContext executionContext; private final RewriteMigrationResultMerger rewriteMigrationResultMerger; + private final ProjectResourceSetFactory projectResourceSetFactory; @NotNull public ProjectContext createProjectContext(Path projectDir, ProjectResourceSet projectResourceSet) { @@ -50,7 +51,7 @@ public ProjectContext createProjectContext(Path projectDir, ProjectResourceSet p applyProjectResourceWrappers(projectResourceSet); List buildFiles = new BuildFileProjectResourceFinder().apply(projectResourceSet); ClasspathRegistry.initializeFromBuildFiles(buildFiles); - ProjectContext projectContext = new ProjectContext(javaRefactoringFactory, projectDir, projectResourceSet, basePackageCalculator, javaParserBuilder, executionContext, rewriteMigrationResultMerger); + ProjectContext projectContext = new ProjectContext(javaRefactoringFactory, projectDir, projectResourceSet, basePackageCalculator, javaParserBuilder, executionContext, rewriteMigrationResultMerger, projectResourceSetFactory); return projectContext; } diff --git a/components/sbm-core/src/main/java/org/springframework/sbm/engine/recipe/MigrationResultProjectContextMerger.java b/components/sbm-core/src/main/java/org/springframework/sbm/engine/recipe/MigrationResultProjectContextMerger.java new file mode 100644 index 000000000..346e9192c --- /dev/null +++ b/components/sbm-core/src/main/java/org/springframework/sbm/engine/recipe/MigrationResultProjectContextMerger.java @@ -0,0 +1,42 @@ +/* + * Copyright 2021 - 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.springframework.sbm.engine.recipe; + +import lombok.RequiredArgsConstructor; +import org.openrewrite.Result; +import org.openrewrite.SourceFile; +import org.springframework.sbm.project.resource.RewriteMigrationResultMerger; +import org.springframework.sbm.project.resource.finder.AbsolutePathResourceFinder; +import org.springframework.sbm.engine.context.ProjectContext; +import org.springframework.sbm.project.RewriteSourceFileWrapper; +import org.springframework.sbm.project.resource.ProjectResourceSet; +import org.springframework.sbm.project.resource.RewriteSourceFileHolder; +import org.springframework.stereotype.Component; + +import java.nio.file.Path; +import java.util.List; +import java.util.Optional; + +@Component +@RequiredArgsConstructor +public class MigrationResultProjectContextMerger { + + private final RewriteMigrationResultMerger rewriteMigrationResultMerger; + + public void mergeResults(ProjectContext context, List results) { + rewriteMigrationResultMerger.mergeResults(context.getProjectResources(), results); + } +} diff --git a/components/sbm-core/src/main/java/org/springframework/sbm/engine/recipe/OpenRewriteRecipeAdapterAction.java b/components/sbm-core/src/main/java/org/springframework/sbm/engine/recipe/OpenRewriteRecipeAdapterAction.java index 22a017098..fe49f2784 100644 --- a/components/sbm-core/src/main/java/org/springframework/sbm/engine/recipe/OpenRewriteRecipeAdapterAction.java +++ b/components/sbm-core/src/main/java/org/springframework/sbm/engine/recipe/OpenRewriteRecipeAdapterAction.java @@ -34,7 +34,7 @@ public class OpenRewriteRecipeAdapterAction extends AbstractAction { @JsonIgnore @Autowired - private RewriteMigrationResultMerger resultMerger; + private MigrationResultProjectContextMerger resultMerger; @JsonIgnore @Autowired private ExecutionContext executionContext; diff --git a/components/sbm-core/src/main/java/org/springframework/sbm/engine/recipe/RewriteMigrationResultMerger.java b/components/sbm-core/src/main/java/org/springframework/sbm/engine/recipe/RewriteMigrationResultMerger.java deleted file mode 100644 index 5323e2099..000000000 --- a/components/sbm-core/src/main/java/org/springframework/sbm/engine/recipe/RewriteMigrationResultMerger.java +++ /dev/null @@ -1,104 +0,0 @@ -/* - * Copyright 2021 - 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.springframework.sbm.engine.recipe; - -import lombok.RequiredArgsConstructor; -import org.openrewrite.Result; -import org.openrewrite.SourceFile; -import org.springframework.sbm.project.resource.finder.AbsolutePathResourceFinder; -import org.springframework.sbm.engine.context.ProjectContext; -import org.springframework.sbm.project.RewriteSourceFileWrapper; -import org.springframework.sbm.project.resource.ProjectResourceSet; -import org.springframework.sbm.project.resource.RewriteSourceFileHolder; -import org.springframework.stereotype.Component; - -import java.nio.file.Path; -import java.util.List; -import java.util.Optional; - -@Component -@RequiredArgsConstructor -public class RewriteMigrationResultMerger { - - private final RewriteSourceFileWrapper surceFileWrapper; - public void mergeResults(ProjectContext context, List results) { - // TODO: handle added - results.forEach(result -> { - SourceFile after = result.getAfter(); - SourceFile before = result.getBefore(); - if (after == null) { - handleDeleted(context, before); - } else if (before == null) { - handleAdded(context, after); - } else { - handleModified(context, after); - } - }); - } - - public void mergeResults(ProjectResourceSet resourceSet, List results) { - results.forEach(result -> { - SourceFile after = result.getAfter(); - SourceFile before = result.getBefore(); - if (after == null) { - handleDeleted(resourceSet, before); - } else if (before == null) { - handleAdded(resourceSet, after); - } else { - handleModified(resourceSet, after); - } - }); - } - - private void handleDeleted(ProjectContext context, SourceFile before) { - handleDeleted(context.getProjectResources(), before); - } - - private void handleDeleted(ProjectResourceSet resourceSet, SourceFile before) { - Path path = resourceSet.list().get(0).getAbsoluteProjectDir().resolve(before.getSourcePath()); - Optional> match = new AbsolutePathResourceFinder(path).apply(resourceSet); - match.get().delete(); - } - - private void handleAdded(ProjectContext context, SourceFile after) { - handleAdded(context.getProjectResources(), after); - } - - private void handleAdded(ProjectResourceSet resourceSet, SourceFile after) { - RewriteSourceFileHolder modifiableProjectResource = surceFileWrapper.wrapRewriteSourceFiles(resourceSet.list().get(0).getAbsoluteProjectDir(), List.of(after)).get(0); - resourceSet.add(modifiableProjectResource); - } - - private void handleModified(ProjectResourceSet resourceSet, SourceFile after) { - Path absoluteProjectDir = resourceSet.list().get(0).getAbsoluteProjectDir(); - Path resolve = absoluteProjectDir.resolve(after.getSourcePath()); - Optional> modifiedResource = new AbsolutePathResourceFinder(resolve).apply(resourceSet); - if(modifiedResource.isEmpty()) { - throw new IllegalStateException("Could not find resource matching path '%s'".formatted(resolve)); - } - // TODO: handle situations where resource is not rewriteSourceFileHolder -> use predicates for known types to reuse, alternatively using the ProjectContextBuiltEvent might help - replaceWrappedResource(modifiedResource.get(), after); - } - - private void handleModified(ProjectContext context, SourceFile after) { - handleModified(context.getProjectResources(), after); - } - - private void replaceWrappedResource(RewriteSourceFileHolder resource, SourceFile r) { - Class type = resource.getType(); - resource.replaceWith((T) type.cast(r)); - } -} diff --git a/components/sbm-core/src/main/java/org/springframework/sbm/engine/recipe/RewriteRecipeRunner.java b/components/sbm-core/src/main/java/org/springframework/sbm/engine/recipe/RewriteRecipeRunner.java index abc1a3d0b..4997c876c 100644 --- a/components/sbm-core/src/main/java/org/springframework/sbm/engine/recipe/RewriteRecipeRunner.java +++ b/components/sbm-core/src/main/java/org/springframework/sbm/engine/recipe/RewriteRecipeRunner.java @@ -29,7 +29,7 @@ @Component @RequiredArgsConstructor public class RewriteRecipeRunner { - private final RewriteMigrationResultMerger resultMerger; + private final MigrationResultProjectContextMerger resultMerger; private final ExecutionContext executionContext; // FIXME: Make this a method 'apply(org.openrewrite.Recipe)' on ProjectContext, see https://github.com/spring-projects-experimental/spring-boot-migrator/issues/803 diff --git a/components/sbm-core/src/main/java/org/springframework/sbm/project/parser/ProjectContextInitializer.java b/components/sbm-core/src/main/java/org/springframework/sbm/project/parser/ProjectContextInitializer.java index e4e01e9ab..d688219b3 100644 --- a/components/sbm-core/src/main/java/org/springframework/sbm/project/parser/ProjectContextInitializer.java +++ b/components/sbm-core/src/main/java/org/springframework/sbm/project/parser/ProjectContextInitializer.java @@ -27,6 +27,7 @@ import org.springframework.sbm.parsers.RewriteProjectParser; import org.springframework.sbm.project.RewriteSourceFileWrapper; import org.springframework.sbm.project.resource.ProjectResourceSet; +import org.springframework.sbm.project.resource.ProjectResourceSetFactory; import org.springframework.sbm.project.resource.RewriteSourceFileHolder; import org.springframework.stereotype.Component; @@ -44,6 +45,7 @@ public class ProjectContextInitializer { private final RewriteSourceFileWrapper rewriteSourceFileWrapper; private final ExecutionContext executionContext; private final ProjectContextHolder projectContextHolder; + private final ProjectResourceSetFactory projectResourceSetFactory; public ProjectContext initProjectContext(Path projectDir, List resources) { final Path absoluteProjectDir = projectDir.toAbsolutePath().normalize(); @@ -53,7 +55,7 @@ public ProjectContext initProjectContext(Path projectDir, List resourc List parsedResources = mavenProjectParser.parse(absoluteProjectDir, resources, executionContext).sourceFiles(); List> rewriteSourceFileHolders = rewriteSourceFileWrapper.wrapRewriteSourceFiles(absoluteProjectDir, parsedResources); - ProjectResourceSet projectResourceSet = new ProjectResourceSet(rewriteSourceFileHolders, executionContext, migrationResultMerger); + ProjectResourceSet projectResourceSet = projectResourceSetFactory.createFromSourceFileHolders(rewriteSourceFileHolders); ProjectContext projectContext = projectContextFactory.createProjectContext(projectDir, projectResourceSet); storeGitCommitHash(projectDir, projectContext); diff --git a/components/sbm-core/src/test/java/org/springframework/sbm/archfitfun/ExecutionScopeArchFitTest.java b/components/sbm-core/src/test/java/org/springframework/sbm/archfitfun/ExecutionScopeArchFitTest.java index 571b1ccab..984fe6631 100644 --- a/components/sbm-core/src/test/java/org/springframework/sbm/archfitfun/ExecutionScopeArchFitTest.java +++ b/components/sbm-core/src/test/java/org/springframework/sbm/archfitfun/ExecutionScopeArchFitTest.java @@ -29,7 +29,6 @@ import org.springframework.sbm.boot.autoconfigure.SbmSupportRewriteConfiguration; import org.springframework.sbm.boot.autoconfigure.ScopeConfiguration; import org.springframework.sbm.build.impl.MavenSettingsInitializer; -import org.springframework.sbm.build.impl.RewriteMavenParser; import org.springframework.sbm.engine.commands.ApplicableRecipeListCommand; import org.springframework.sbm.engine.commands.ApplyCommand; import org.springframework.sbm.engine.commands.ScanCommand; @@ -41,21 +40,16 @@ import org.springframework.sbm.java.refactoring.JavaRefactoringFactoryImpl; import org.springframework.sbm.java.util.BasePackageCalculator; import org.springframework.sbm.parsers.JavaParserBuilder; -import org.springframework.sbm.parsers.RewriteMavenArtifactDownloader; -import org.springframework.sbm.parsers.RewriteParserConfiguration; -import org.springframework.sbm.parsers.RewriteProjectParser; import org.springframework.sbm.project.RewriteSourceFileWrapper; import org.springframework.sbm.project.parser.*; import org.springframework.sbm.project.resource.ProjectResourceSetHolder; import org.springframework.sbm.project.resource.ProjectResourceWrapperRegistry; import org.springframework.sbm.project.resource.ResourceHelper; import org.springframework.sbm.project.resource.SbmApplicationProperties; -import org.springframework.sbm.properties.parser.RewritePropertiesParser; import org.springframework.sbm.scopes.AbstractBaseScope; import org.springframework.sbm.scopes.ExecutionScope; import org.springframework.sbm.scopes.ProjectMetadata; import org.springframework.sbm.scopes.ScanScope; -import org.springframework.sbm.xml.parser.RewriteXmlParser; import org.springframework.test.util.ReflectionTestUtils; import org.springframework.validation.beanvalidation.CustomValidatorBean; @@ -128,7 +122,7 @@ // RewritePlainTextParser.class, // RewriteMavenParser.class, MavenSettingsInitializer.class, - RewriteMigrationResultMerger.class, + MigrationResultProjectContextMerger.class, JavaProvenanceMarkerFactory.class, MavenConfigHandler.class, RewriteSourceFileWrapper.class, diff --git a/components/sbm-core/src/test/java/org/springframework/sbm/engine/recipe/OpenRewriteDeclarativeRecipeAdapterIntegrationTest.java b/components/sbm-core/src/test/java/org/springframework/sbm/engine/recipe/OpenRewriteDeclarativeRecipeAdapterIntegrationTest.java index 13164711f..67d337c37 100644 --- a/components/sbm-core/src/test/java/org/springframework/sbm/engine/recipe/OpenRewriteDeclarativeRecipeAdapterIntegrationTest.java +++ b/components/sbm-core/src/test/java/org/springframework/sbm/engine/recipe/OpenRewriteDeclarativeRecipeAdapterIntegrationTest.java @@ -40,7 +40,7 @@ ResourceHelper.class, ActionDeserializerRegistry.class, DefaultActionDeserializer.class, - RewriteMigrationResultMerger.class, + MigrationResultProjectContextMerger.class, RewriteSourceFileWrapper.class, CustomValidatorBean.class, RewriteExecutionContext.class, diff --git a/components/sbm-core/src/test/java/org/springframework/sbm/engine/recipe/OpenRewriteNamedRecipeAdapterIntegrationTest.java b/components/sbm-core/src/test/java/org/springframework/sbm/engine/recipe/OpenRewriteNamedRecipeAdapterIntegrationTest.java index c6bb48f40..b8ac480a0 100644 --- a/components/sbm-core/src/test/java/org/springframework/sbm/engine/recipe/OpenRewriteNamedRecipeAdapterIntegrationTest.java +++ b/components/sbm-core/src/test/java/org/springframework/sbm/engine/recipe/OpenRewriteNamedRecipeAdapterIntegrationTest.java @@ -41,7 +41,7 @@ ResourceHelper.class, ActionDeserializerRegistry.class, DefaultActionDeserializer.class, - RewriteMigrationResultMerger.class, + MigrationResultProjectContextMerger.class, RewriteSourceFileWrapper.class, RewriteRecipeLoader.class, CustomValidatorBean.class, diff --git a/components/sbm-core/src/test/java/org/springframework/sbm/project/parser/ProjectContextInitializerTest.java b/components/sbm-core/src/test/java/org/springframework/sbm/project/parser/ProjectContextInitializerTest.java index 5882ace31..61c6c253f 100644 --- a/components/sbm-core/src/test/java/org/springframework/sbm/project/parser/ProjectContextInitializerTest.java +++ b/components/sbm-core/src/test/java/org/springframework/sbm/project/parser/ProjectContextInitializerTest.java @@ -43,7 +43,7 @@ import org.springframework.sbm.engine.context.ProjectRootPathResolver; import org.springframework.sbm.engine.git.GitSupport; import org.springframework.sbm.engine.precondition.PreconditionVerifier; -import org.springframework.sbm.engine.recipe.RewriteMigrationResultMerger; +import org.springframework.sbm.engine.recipe.MigrationResultProjectContextMerger; import org.springframework.sbm.java.impl.RewriteJavaParser; import org.springframework.sbm.java.refactoring.JavaRefactoringFactoryImpl; import org.springframework.sbm.java.util.BasePackageCalculator; @@ -77,7 +77,7 @@ ProjectContextFactory.class, MavenPomCacheProvider.class, SbmApplicationProperties.class, - RewriteMigrationResultMerger.class, + MigrationResultProjectContextMerger.class, PathScanner.class, RewriteJavaParser.class, RewritePlainTextParser.class, diff --git a/sbm-support-rewrite-integration-test/src/main/java/com/acme/example/SpringBoot3Upgrade.java b/sbm-support-rewrite-integration-test/src/main/java/com/acme/example/SpringBoot3Upgrade.java index 3ef02fdea..9ea047f09 100644 --- a/sbm-support-rewrite-integration-test/src/main/java/com/acme/example/SpringBoot3Upgrade.java +++ b/sbm-support-rewrite-integration-test/src/main/java/com/acme/example/SpringBoot3Upgrade.java @@ -27,9 +27,7 @@ import org.springframework.sbm.parsers.RewriteProjectParsingResult; import org.springframework.sbm.parsers.events.StartedParsingResourceEvent; import org.springframework.sbm.project.RewriteSourceFileWrapper; -import org.springframework.sbm.project.resource.ProjectResourceSet; -import org.springframework.sbm.project.resource.RewriteMigrationResultMerger; -import org.springframework.sbm.project.resource.RewriteSourceFileHolder; +import org.springframework.sbm.project.resource.*; import org.springframework.sbm.recipes.RewriteRecipeDiscovery; import java.nio.file.Path; @@ -54,9 +52,9 @@ public static void main(String[] args) { @Autowired ExecutionContext executionContext; @Autowired - RewriteSourceFileWrapper sourceFileWrapper; + ProjectResourceSetSerializer serializer; @Autowired - RewriteMigrationResultMerger resultMerger; + ProjectResourceSetFactory projectResourceSetFactory; @EventListener(StartedParsingResourceEvent.class) public void onStartedParsingResourceEvent(StartedParsingResourceEvent event) { @@ -65,20 +63,31 @@ public void onStartedParsingResourceEvent(StartedParsingResourceEvent event) { @Override public void run(String... args) throws Exception { - Path baseDir = Path.of("/Users/fkrueger/projects/sbm-projects/spring-restbucks/server"); + Path baseDir = null; + if(args.length == 0) { + throw new IllegalArgumentException("A path must be provided"); + } else { + String pathStr = args[0]; + baseDir = Path.of(pathStr); + } + // scan List resources = scanner.scan(baseDir); + // parse RewriteProjectParsingResult parsingResult = parser.parse(baseDir, resources, executionContext); List sourceFiles = parsingResult.sourceFiles(); - List> rewriteSourceFileHolders = sourceFileWrapper.wrapRewriteSourceFiles(baseDir, sourceFiles); - ProjectResourceSet projectResourceSet = new ProjectResourceSet(rewriteSourceFileHolders, executionContext, resultMerger); - // apply recipe + ProjectResourceSet projectResourceSet = projectResourceSetFactory.create(baseDir, sourceFiles); + + // discover String recipeName = "org.openrewrite.java.spring.boot3.UpgradeSpringBoot_3_1"; List recipes = discovery.discoverRecipes(); Optional recipe = recipes.stream().filter(r -> recipeName.equals(r.getName())).findFirst(); + + // apply recipe.ifPresent((Recipe r) -> { projectResourceSet.apply(r); + serializer.writeChanges(projectResourceSet); }); } } diff --git a/sbm-support-rewrite/src/main/java/org/springframework/sbm/boot/autoconfigure/ProjectResourceSetConfiguration.java b/sbm-support-rewrite/src/main/java/org/springframework/sbm/boot/autoconfigure/ProjectResourceSetConfiguration.java index d166ff14c..924347d8a 100644 --- a/sbm-support-rewrite/src/main/java/org/springframework/sbm/boot/autoconfigure/ProjectResourceSetConfiguration.java +++ b/sbm-support-rewrite/src/main/java/org/springframework/sbm/boot/autoconfigure/ProjectResourceSetConfiguration.java @@ -15,9 +15,13 @@ */ package org.springframework.sbm.boot.autoconfigure; +import org.openrewrite.ExecutionContext; import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.context.annotation.Bean; import org.springframework.sbm.project.RewriteSourceFileWrapper; +import org.springframework.sbm.project.resource.ProjectResourceSerializer; +import org.springframework.sbm.project.resource.ProjectResourceSetFactory; +import org.springframework.sbm.project.resource.ProjectResourceSetSerializer; import org.springframework.sbm.project.resource.RewriteMigrationResultMerger; /** @@ -34,4 +38,19 @@ RewriteSourceFileWrapper rewriteSourceFileWrapper() { RewriteMigrationResultMerger rewriteMigrationResultMerger(RewriteSourceFileWrapper rewriteSourceFileWrapper) { return new RewriteMigrationResultMerger(rewriteSourceFileWrapper); } + + @Bean + ProjectResourceSerializer projectResourceSerializer() { + return new ProjectResourceSerializer(); + } + + @Bean + ProjectResourceSetSerializer projectResourceSetSerializer(ProjectResourceSerializer resourceSerializer) { + return new ProjectResourceSetSerializer(resourceSerializer); + } + + @Bean + ProjectResourceSetFactory projectResourceSetFactory(RewriteMigrationResultMerger rewriteMigrationResultMerger, RewriteSourceFileWrapper sourceFileWrapper, ExecutionContext executionContext) { + return new ProjectResourceSetFactory(rewriteMigrationResultMerger, sourceFileWrapper, executionContext); + } } diff --git a/sbm-support-rewrite/src/main/java/org/springframework/sbm/project/resource/ProjectResourceSet.java b/sbm-support-rewrite/src/main/java/org/springframework/sbm/project/resource/ProjectResourceSet.java index 1a722663b..f44767b6d 100644 --- a/sbm-support-rewrite/src/main/java/org/springframework/sbm/project/resource/ProjectResourceSet.java +++ b/sbm-support-rewrite/src/main/java/org/springframework/sbm/project/resource/ProjectResourceSet.java @@ -34,6 +34,11 @@ public class ProjectResourceSet { private final RewriteMigrationResultMerger migrationResultMerger; + /** + * @deprecated + * Use {@link ProjectResourceSetFactory#create(Path, List) >)} instead. + */ + @Deprecated public ProjectResourceSet(List> projectResources, ExecutionContext executionContext, RewriteMigrationResultMerger migrationResultMerger) { this.executionContext = executionContext; this.migrationResultMerger = migrationResultMerger; diff --git a/sbm-support-rewrite/src/main/java/org/springframework/sbm/project/resource/ProjectResourceSetFactory.java b/sbm-support-rewrite/src/main/java/org/springframework/sbm/project/resource/ProjectResourceSetFactory.java new file mode 100644 index 000000000..27c65cabb --- /dev/null +++ b/sbm-support-rewrite/src/main/java/org/springframework/sbm/project/resource/ProjectResourceSetFactory.java @@ -0,0 +1,44 @@ +/* + * Copyright 2021 - 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.springframework.sbm.project.resource; + +import lombok.RequiredArgsConstructor; +import org.openrewrite.ExecutionContext; +import org.openrewrite.SourceFile; +import org.springframework.sbm.project.RewriteSourceFileWrapper; + +import java.nio.file.Path; +import java.util.List; + +/** + * @author Fabian Krüger + */ +@RequiredArgsConstructor +public class ProjectResourceSetFactory { + private final RewriteMigrationResultMerger rewriteMigrationResultMerger; + private final RewriteSourceFileWrapper sourceFileWrapper; + private final ExecutionContext executionContext; + + + public ProjectResourceSet create(Path baseDir, List sourceFiles) { + List> rewriteSourceFileHolders = sourceFileWrapper.wrapRewriteSourceFiles(baseDir, sourceFiles); + return createFromSourceFileHolders(rewriteSourceFileHolders); + } + + public ProjectResourceSet createFromSourceFileHolders(List> rewriteSourceFileHolders) { + return new ProjectResourceSet(rewriteSourceFileHolders, executionContext, rewriteMigrationResultMerger); + } +}