Skip to content

Commit

Permalink
Revamp/949 move projectresourceset related classes to sbm support rew…
Browse files Browse the repository at this point in the history
…rite (#950)

* Add method to sync recipe run
* Moved ProjectResourceSet related classes to sbm-support-rewrite
* Add temp integration test
* Use new ProjectResourceSetFactory
* Use RewriteMigrationResultMerger
  • Loading branch information
fabapp2 authored Sep 29, 2023
1 parent 4a3e42e commit 2b7ffa7
Show file tree
Hide file tree
Showing 102 changed files with 638 additions and 257 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ private RecipeTestSupport() {
DefaultActionDeserializer.class,
RewriteJavaSearchActionDeserializer.class,
RewriteRecipeLoader.class,
RewriteMigrationResultMerger.class,
MigrationResultProjectContextMerger.class,
RewriteSourceFileWrapper.class,
SbmRecipeLoader.class,
BasePackageCalculator.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,17 @@
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.filter.ProjectResourceFinder;
import org.springframework.sbm.project.resource.finder.ProjectResourceFinder;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.openrewrite.SourceFile;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -142,7 +145,8 @@ public List<Module> getModules() {
basePackageCalculator,
javaParserBuilder,
executionContext,
rewriteMigrationResultMerger)
rewriteMigrationResultMerger,
projectResourceSetFactory)
)
.collect(Collectors.toList());
} else {
Expand All @@ -156,8 +160,14 @@ public List<String> getDeclaredModules() {

public <T> T search(ProjectResourceFinder<T> finder) {
List<RewriteSourceFileHolder<? extends SourceFile>> resources = getModuleResources();
ProjectResourceSet filteredProjectResourceSet = new ProjectResourceSet(resources, executionContext);
return finder.apply(filteredProjectResourceSet);
if(!resources.isEmpty()) {
Path baseDir = resources.get(0).getAbsoluteProjectDir();
List<SourceFile> 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<RewriteSourceFileHolder<? extends SourceFile>> getModuleResources() {
Expand All @@ -184,25 +194,25 @@ private boolean isResourceOnPath(RewriteSourceFileHolder<? extends SourceFile> r
}

public <T> T searchMainResources(ProjectResourceFinder<T> finder) {
ProjectResourceSet resourceSet = new ImmutableFilteringProjectResourceSet(projectResourceSet, (RewriteSourceFileHolder<? extends SourceFile> r) -> r.getAbsolutePath().normalize().startsWith(getMainResourceSet().getAbsolutePath().toAbsolutePath().normalize()));
ProjectResourceSet resourceSet = new ImmutableFilteringProjectResourceSet(projectResourceSet, (RewriteSourceFileHolder<? extends SourceFile> r) -> r.getAbsolutePath().normalize().startsWith(getMainResourceSet().getAbsolutePath().toAbsolutePath().normalize()), rewriteMigrationResultMerger);
return finder.apply(resourceSet);
}

public <T> T searchMainJava(ProjectResourceFinder<T> finder) {
ProjectResourceSet resourceSet = new ImmutableFilteringProjectResourceSet(projectResourceSet, (RewriteSourceFileHolder<? extends SourceFile> r) -> r.getAbsolutePath().normalize().startsWith(getMainJavaSourceSet().getAbsolutePath().toAbsolutePath().normalize()));
ProjectResourceSet resourceSet = new ImmutableFilteringProjectResourceSet(projectResourceSet, (RewriteSourceFileHolder<? extends SourceFile> r) -> r.getAbsolutePath().normalize().startsWith(getMainJavaSourceSet().getAbsolutePath().toAbsolutePath().normalize()), rewriteMigrationResultMerger);
return finder.apply(resourceSet);
}

public <T> T searchTestResources(ProjectResourceFinder<T> finder) {
Predicate<RewriteSourceFileHolder<? extends SourceFile>> predicate = (RewriteSourceFileHolder<? extends SourceFile> 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> T searchTestJava(ProjectResourceFinder<T> finder) {
ProjectResourceSet resourceSet = new ImmutableFilteringProjectResourceSet(projectResourceSet, (RewriteSourceFileHolder<? extends SourceFile> r) -> r.getAbsolutePath().normalize().startsWith(getTestJavaSourceSet().getAbsolutePath().toAbsolutePath().normalize()));
ProjectResourceSet resourceSet = new ImmutableFilteringProjectResourceSet(projectResourceSet, (RewriteSourceFileHolder<? extends SourceFile> r) -> r.getAbsolutePath().normalize().startsWith(getTestJavaSourceSet().getAbsolutePath().toAbsolutePath().normalize()), rewriteMigrationResultMerger);
return finder.apply(resourceSet);
}

Expand Down Expand Up @@ -230,8 +240,8 @@ private class ImmutableFilteringProjectResourceSet extends ProjectResourceSet{
private final ProjectResourceSet projectResourceSet;
private final Predicate<RewriteSourceFileHolder<? extends SourceFile>> predicate;

public ImmutableFilteringProjectResourceSet(ProjectResourceSet projectResourceSet, Predicate<RewriteSourceFileHolder<? extends SourceFile>> predicate) {
super(projectResourceSet.list(), executionContext);
public ImmutableFilteringProjectResourceSet(ProjectResourceSet projectResourceSet, Predicate<RewriteSourceFileHolder<? extends SourceFile>> predicate, RewriteMigrationResultMerger migrationResultMerger) {
super(projectResourceSet.list(), executionContext, migrationResultMerger);
this.projectResourceSet = projectResourceSet;
this.predicate = predicate;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package org.springframework.sbm.build.api;

import org.springframework.sbm.project.resource.ProjectResourceSet;
import org.springframework.sbm.project.resource.filter.ProjectResourceFinder;
import org.springframework.sbm.project.resource.finder.ProjectResourceFinder;

public class RootBuildFileFilter implements ProjectResourceFinder<BuildFile> {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
package org.springframework.sbm.build.filter;

import org.springframework.sbm.build.api.BuildFile;
import org.springframework.sbm.project.resource.filter.GenericTypeListFilter;
import org.springframework.sbm.project.resource.finder.GenericTypeListFinder;

public class BuildFileProjectResourceFilter extends GenericTypeListFilter<BuildFile> {
public BuildFileProjectResourceFilter() {
public class BuildFileProjectResourceFinder extends GenericTypeListFinder<BuildFile> {
public BuildFileProjectResourceFinder() {
super(BuildFile.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package org.springframework.sbm.common.migration.actions;

import lombok.Setter;
import org.springframework.sbm.common.filter.PathPatternMatchingProjectResourceFinder;
import org.springframework.sbm.project.resource.finder.PathPatternMatchingProjectResourceFinder;
import org.springframework.sbm.engine.context.ProjectContext;
import org.springframework.sbm.engine.recipe.AbstractAction;
import org.springframework.sbm.project.resource.ProjectResource;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import org.springframework.sbm.engine.recipe.AbstractAction;
import org.springframework.sbm.engine.context.ProjectContext;
import org.springframework.sbm.project.resource.ProjectResource;
import org.springframework.sbm.common.filter.PathPatternMatchingProjectResourceFinder;
import org.springframework.sbm.project.resource.finder.PathPatternMatchingProjectResourceFinder;
import lombok.Getter;
import lombok.Setter;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package org.springframework.sbm.common.migration.conditions;

import lombok.*;
import org.springframework.sbm.common.util.OsAgnosticPathMatcher;
import org.springframework.sbm.utils.OsAgnosticPathMatcher;
import org.springframework.sbm.engine.context.ProjectContext;
import org.springframework.sbm.engine.recipe.Condition;
import org.springframework.util.PathMatcher;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
package org.springframework.sbm.engine.commands;

import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.sbm.common.filter.DeletedResourcePathStringFilter;
import org.springframework.sbm.common.filter.ModifiedResourcePathStringFilter;
import org.springframework.sbm.project.resource.finder.DeletedResourcePathStringFilter;
import org.springframework.sbm.project.resource.finder.ModifiedResourcePathStringFilter;
import org.springframework.sbm.engine.context.ProjectContext;
import org.springframework.sbm.engine.context.ProjectContextSerializer;
import org.springframework.sbm.engine.git.GitSupport;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
import org.springframework.sbm.engine.recipe.RecipesBuilder;
import org.springframework.sbm.engine.context.ProjectContext;
import org.springframework.sbm.engine.context.ProjectContextSerializer;
import org.springframework.sbm.common.filter.DeletedResourcePathStringFilter;
import org.springframework.sbm.common.filter.ModifiedResourcePathStringFilter;
import org.springframework.sbm.project.resource.finder.DeletedResourcePathStringFilter;
import org.springframework.sbm.project.resource.finder.ModifiedResourcePathStringFilter;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,17 @@
import org.springframework.sbm.build.api.Module;
import org.springframework.sbm.build.api.BuildFile;
import org.springframework.sbm.build.api.RootBuildFileFilter;
import org.springframework.sbm.build.filter.BuildFileProjectResourceFilter;
import org.springframework.sbm.engine.recipe.RewriteMigrationResultMerger;
import org.springframework.sbm.build.filter.BuildFileProjectResourceFinder;
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.filter.ProjectResourceFinder;
import org.springframework.sbm.project.resource.finder.ProjectResourceFinder;
import lombok.Getter;
import lombok.Setter;

Expand All @@ -53,15 +54,17 @@ 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;
this.basePackageCalculator = basePackageCalculator;
this.javaParserBuilder = javaParserBuilder;
this.executionContext = executionContext;
this.rewriteMigrationResultMerger = rewriteMigrationResultMerger;
this.projectResourceSetFactory = projectResourceSetFactory;
}

public ProjectResourceSet getProjectResources() {
Expand All @@ -74,7 +77,7 @@ public ProjectResourceSet getProjectResources() {
*/
@Deprecated(forRemoval = false)
public List<Module> getModules() {
return search(new BuildFileProjectResourceFilter()).stream()
return search(new BuildFileProjectResourceFinder()).stream()
.map(this::mapToModule)
.collect(Collectors.toList());
}
Expand All @@ -92,7 +95,8 @@ private Module mapToModule(BuildFile buildFile) {
basePackageCalculator,
javaParserBuilder,
executionContext,
rewriteMigrationResultMerger
rewriteMigrationResultMerger,
projectResourceSetFactory
);
}

Expand Down Expand Up @@ -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 -> {
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@

import org.openrewrite.ExecutionContext;
import org.springframework.sbm.build.api.BuildFile;
import org.springframework.sbm.build.filter.BuildFileProjectResourceFilter;
import org.springframework.sbm.engine.recipe.RewriteMigrationResultMerger;
import org.springframework.sbm.build.filter.BuildFileProjectResourceFinder;
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;
import org.springframework.sbm.parsers.JavaParserBuilder;
import org.springframework.sbm.parsers.RewriteMavenArtifactDownloader;
import org.springframework.sbm.project.parser.DependencyHelper;
import org.springframework.sbm.project.resource.*;
import lombok.RequiredArgsConstructor;
import org.jetbrains.annotations.NotNull;
Expand All @@ -45,14 +43,15 @@ 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) {
projectResourceSetHolder.setProjectResourceSet(projectResourceSet);
applyProjectResourceWrappers(projectResourceSet);
List<BuildFile> buildFiles = new BuildFileProjectResourceFilter().apply(projectResourceSet);
List<BuildFile> 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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package org.springframework.sbm.engine.precondition;

import org.springframework.core.io.Resource;
import org.springframework.sbm.common.util.OsAgnosticPathMatcher;
import org.springframework.sbm.utils.OsAgnosticPathMatcher;
import org.springframework.sbm.utils.LinuxWindowsPathUnifier;
import org.springframework.stereotype.Component;
import org.springframework.util.PathMatcher;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Result> results) {
rewriteMigrationResultMerger.mergeResults(context.getProjectResources(), results);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class OpenRewriteRecipeAdapterAction extends AbstractAction {

@JsonIgnore
@Autowired
private RewriteMigrationResultMerger resultMerger;
private MigrationResultProjectContextMerger resultMerger;
@JsonIgnore
@Autowired
private ExecutionContext executionContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import org.openrewrite.SourceFile;
import org.springframework.sbm.project.resource.ProjectResourceSet;
import org.springframework.sbm.project.resource.RewriteSourceFileHolder;
import org.springframework.sbm.project.resource.filter.ProjectResourceFinder;
import org.springframework.sbm.project.resource.finder.ProjectResourceFinder;

import java.util.List;
import java.util.stream.Collectors;
Expand Down
Loading

0 comments on commit 2b7ffa7

Please sign in to comment.