Skip to content

Commit

Permalink
Builds
Browse files Browse the repository at this point in the history
* Use new ProjectResourceSetFactory
* Use RewriteMigrationResultMerger
  • Loading branch information
fabapp2 committed Sep 28, 2023
1 parent 86cc72a commit 1b8d134
Show file tree
Hide file tree
Showing 17 changed files with 169 additions and 143 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,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;
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, migrationResultMerger);
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,7 +240,7 @@ 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) {
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 @@ -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;
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 Down Expand Up @@ -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 @@ -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;
Expand All @@ -43,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 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
@@ -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

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit 1b8d134

Please sign in to comment.