Skip to content

Commit 49f44cc

Browse files
committed
WIP #304
1 parent a83e788 commit 49f44cc

13 files changed

+1221
-8
lines changed

components/sbm-core/src/main/java/org/springframework/sbm/build/api/ApplicationModule.java

+51
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,13 @@
3232
import org.openrewrite.SourceFile;
3333

3434
import java.nio.file.Path;
35+
import java.nio.file.PathMatcher;
3536
import java.util.ArrayList;
3637
import java.util.List;
3738
import java.util.Optional;
39+
import java.util.function.Predicate;
3840
import java.util.stream.Collectors;
41+
import java.util.stream.Stream;
3942

4043
@RequiredArgsConstructor
4144
public class ApplicationModule {
@@ -112,6 +115,11 @@ public ResourceSet getMainResourceSet() {
112115
return new ResourceSet(projectResourceSet, projectRootDir, modulePath, mainResourceSet);
113116
}
114117

118+
public ResourceSet getTestResourceSet() {
119+
Path testResourceSet = buildFile.getTestResourceFolder();
120+
return new ResourceSet(projectResourceSet, projectRootDir, modulePath, testResourceSet);
121+
}
122+
115123
public List<ApplicationModule> getModules() {
116124
Optional<MavenResolutionResult> mavenResolution = MavenBuildFileUtil.findMavenResolution(((OpenRewriteMavenBuildFile) buildFile).getSourceFile());
117125
List<MavenResolutionResult> modulesMarker = mavenResolution.get().getModules();
@@ -145,4 +153,47 @@ private List<RewriteSourceFileHolder<? extends SourceFile>> getModuleResources()
145153
.filter(r -> r.getAbsolutePath().toString().startsWith(projectRootDir.resolve(modulePath).toString()))
146154
.collect(Collectors.toList());
147155
}
156+
157+
public <T> T searchMainResources(ProjectResourceFinder<T> finder) {
158+
ProjectResourceSet resourceSet = new ImmutableFilteringProjectResourceSet(projectResourceSet, (RewriteSourceFileHolder<? extends SourceFile> r) -> r.getAbsolutePath().normalize().startsWith(getMainResourceSet().getAbsolutePath().toAbsolutePath().normalize()));
159+
return finder.apply(resourceSet);
160+
}
161+
162+
public <T> T searchMainJava(ProjectResourceFinder<T> finder) {
163+
ProjectResourceSet resourceSet = new ImmutableFilteringProjectResourceSet(projectResourceSet, (RewriteSourceFileHolder<? extends SourceFile> r) -> r.getAbsolutePath().normalize().startsWith(getMainJavaSourceSet().getAbsolutePath().toAbsolutePath().normalize()));
164+
return finder.apply(resourceSet);
165+
}
166+
167+
public <T> T searchTestResources(ProjectResourceFinder<T> finder) {
168+
ProjectResourceSet resourceSet = new ImmutableFilteringProjectResourceSet(projectResourceSet, (RewriteSourceFileHolder<? extends SourceFile> r) -> r.getAbsolutePath().normalize().startsWith(getTestResourceSet().getAbsolutePath().toAbsolutePath().normalize()));
169+
return finder.apply(resourceSet);
170+
}
171+
172+
public <T> T searchTestJava(ProjectResourceFinder<T> finder) {
173+
ProjectResourceSet resourceSet = new ImmutableFilteringProjectResourceSet(projectResourceSet, (RewriteSourceFileHolder<? extends SourceFile> r) -> r.getAbsolutePath().normalize().startsWith(getTestJavaSourceSet().getAbsolutePath().toAbsolutePath().normalize()));
174+
return finder.apply(resourceSet);
175+
}
176+
177+
/**
178+
* Class provides filtering on the list of resources in a {@code ProjectResourceSet}.
179+
* As all read methods rely on {@code stream()}, only this stream has to be filtered. :fingers_crossed:
180+
*
181+
* It's a private inner class as it is currently only used here and quite hacky overwriting only parts of
182+
* {@code ProjectResourceSet} with a lot of assumptions of the inner workings of other classes.
183+
*/
184+
private class ImmutableFilteringProjectResourceSet extends ProjectResourceSet{
185+
private final ProjectResourceSet projectResourceSet;
186+
private final Predicate<RewriteSourceFileHolder<? extends SourceFile>> predicate;
187+
188+
public ImmutableFilteringProjectResourceSet(ProjectResourceSet projectResourceSet, Predicate<RewriteSourceFileHolder<? extends SourceFile>> predicate) {
189+
this.projectResourceSet = projectResourceSet;
190+
this.predicate = predicate;
191+
}
192+
193+
@Override
194+
public Stream<RewriteSourceFileHolder<? extends SourceFile>> stream() {
195+
return projectResourceSet.stream()
196+
.filter(this.predicate);
197+
}
198+
}
148199
}

components/sbm-core/src/main/java/org/springframework/sbm/build/api/ApplicationModules.java

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.openrewrite.maven.tree.MavenResolutionResult;
2020
import org.springframework.sbm.build.impl.MavenBuildFileUtil;
2121
import org.springframework.sbm.build.impl.OpenRewriteMavenBuildFile;
22+
import org.springframework.sbm.project.resource.filter.ProjectResourceFinder;
2223

2324
import java.nio.file.Path;
2425
import java.util.ArrayList;
@@ -130,4 +131,5 @@ private boolean noOtherPomDependsOn(BuildFile buildFile) {
130131
public boolean isSingleModuleApplication() {
131132
return modules.size() == 1;
132133
}
134+
133135
}

components/sbm-core/src/main/java/org/springframework/sbm/build/api/BuildFile.java

+7-5
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,20 @@ public interface BuildFile extends ProjectResource {
8585

8686
void addPlugin(Plugin plugin);
8787

88+
List<Path> getClasspath();
89+
8890
List<Path> getSourceFolders();
8991

9092
List<Path> getTestSourceFolders();
9193

9294
List<Path> getResourceFolders();
9395

94-
List<Path> getClasspath();
95-
9696
List<Path> getTestResourceFolders();
9797

98+
Path getTestResourceFolder();
99+
100+
Path getMainResourceFolder();
101+
98102
void setProperty(String key, String value);
99103

100104
String getProperty(String key);
@@ -131,10 +135,8 @@ public interface BuildFile extends ProjectResource {
131135
Optional<ParentDeclaration> getParentPomDeclaration();
132136

133137
Optional<String> getName();
134-
135-
Path getMainResourceFolder();
136-
137138
// TODO: add same method to ApplicationModules to add excludes to all relevant dependencies in all BuildFiles
139+
138140
/**
139141
* Adds an exclusion for each of the dependencies in {@code excludedDependencies} to all declared dependencies in this {@code BuildFile} which transitively depend on it.
140142
*/

components/sbm-core/src/main/java/org/springframework/sbm/build/api/JavaSourceSet.java

+1
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,5 @@ public interface JavaSourceSet {
4949

5050
JavaSourceLocation getJavaSourceLocation();
5151

52+
Path getAbsolutePath();
5253
}

components/sbm-core/src/main/java/org/springframework/sbm/build/api/ResourceSet.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class ResourceSet {
3232
private final Path resourceSetPath;
3333

3434
public void addStringResource(String filePath, String content) {
35-
Path absFilePath = this.projectRoot.resolve(resourceSetPath).resolve(filePath);
35+
Path absFilePath = getAbsolutePath().resolve(filePath);
3636
StringProjectResource resource = new StringProjectResource(projectRoot, absFilePath, content);
3737
resource.markAsChanged();
3838
projectResourceSet.add(resource);
@@ -42,4 +42,8 @@ public void addResource(RewriteSourceFileHolder<? extends SourceFile> resource)
4242
resource.markChanged();
4343
projectResourceSet.add(resource);
4444
}
45+
46+
public Path getAbsolutePath() {
47+
return projectRoot.resolve(resourceSetPath);
48+
}
4549
}

components/sbm-core/src/main/java/org/springframework/sbm/build/impl/JavaSourceSetImpl.java

+5
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ public JavaSourceLocation getJavaSourceLocation() {
124124
return new JavaSourceLocation(sourceSetRoot, basePackage);
125125
}
126126

127+
@Override
128+
public Path getAbsolutePath() {
129+
return sourceSetRoot;
130+
}
131+
127132
@Override
128133
public boolean hasImportStartingWith(String... value) {
129134
return false;

components/sbm-core/src/main/java/org/springframework/sbm/build/impl/OpenRewriteMavenBuildFile.java

+5
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,11 @@ public Path getMainResourceFolder() {
699699
return getResourceFolders().get(0);
700700
}
701701

702+
@Override
703+
public Path getTestResourceFolder() {
704+
return getTestResourceFolders().get(0);
705+
}
706+
702707
@Override
703708
public void excludeDependencies(List<Dependency> excludedDependencies) {
704709
excludeDependenciesInner(excludedDependencies);

components/sbm-core/src/main/java/org/springframework/sbm/build/migration/conditions/NoDependencyExistMatchingRegex.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.sbm.build.migration.conditions;
1717

18+
import org.springframework.sbm.build.api.ApplicationModule;
1819
import org.springframework.sbm.engine.recipe.Condition;
1920
import org.springframework.sbm.engine.context.ProjectContext;
2021
import lombok.*;
@@ -40,7 +41,9 @@ public String getDescription() {
4041
@Override
4142
public boolean evaluate(ProjectContext context) {
4243
return dependencies.stream().noneMatch(d ->
43-
context.getBuildFile().hasDeclaredDependencyMatchingRegex(d)
44+
context.getModules().stream()
45+
.map(ApplicationModule::getBuildFile)
46+
.noneMatch(b -> b.hasDeclaredDependencyMatchingRegex(d))
4447
);
4548
}
4649
}

components/sbm-core/src/test/java/org/springframework/sbm/build/api/ApplicationModuleTest.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,16 @@
2525
import static org.assertj.core.api.Assertions.assertThat;
2626

2727

28-
@Disabled("TODO: Maven Reactor")
2928
class ApplicationModuleTest {
3029

30+
/**
31+
* Searches for files in {@code /src/main/resources} of this module.
32+
*/
33+
@Test
34+
void searchMainResources() {
35+
36+
}
37+
3138
@Test
3239
void testGetModuleResources() {
3340
String parentPom =

0 commit comments

Comments
 (0)