Skip to content

Commit c4f6209

Browse files
authored
Fix MavenProjectParser to parse all resources (#191)
* Fix MavenProjectParser to parse all resources * Provide HTTPClient with retry enabled and timeouts to RewriteMavenArtifactDownloader * Add maven caching to GitHub build action * Fix formatting * Disabled test until #195 is fixed
1 parent a993ac5 commit c4f6209

File tree

13 files changed

+181
-103
lines changed

13 files changed

+181
-103
lines changed

.github/workflows/mvn-build.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@ jobs:
1616
with:
1717
java-version: '11'
1818
distribution: 'adopt'
19+
cache: 'maven'
1920
- name: Build with Maven
2021
run: mvn --update-snapshots -DtrimStackTrace=false -Dsurefire.useFile=false -DskipITs verify

components/sbm-core/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@
138138
<groupId>com.fasterxml.jackson.dataformat</groupId>
139139
<artifactId>jackson-dataformat-yaml</artifactId>
140140
</dependency>
141+
<dependency>
142+
<groupId>com.squareup.okhttp3</groupId>
143+
<artifactId>okhttp</artifactId>
144+
</dependency>
141145

142146
<dependency>
143147
<groupId>org.springframework.sbm</groupId>

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,12 @@
1717

1818
import lombok.extern.slf4j.Slf4j;
1919
import lombok.extern.slf4j.XSlf4j;
20+
import okhttp3.OkHttpClient;
21+
import okhttp3.Request;
22+
import okhttp3.Response;
2023
import org.openrewrite.internal.lang.Nullable;
2124
import org.openrewrite.ipc.http.HttpSender;
25+
import org.openrewrite.ipc.http.OkHttpSender;
2226
import org.openrewrite.maven.MavenSettings;
2327
import org.openrewrite.maven.cache.LocalMavenArtifactCache;
2428
import org.openrewrite.maven.cache.MavenArtifactCache;
@@ -27,6 +31,7 @@
2731
import org.springframework.stereotype.Component;
2832

2933
import java.nio.file.Paths;
34+
import java.util.concurrent.TimeUnit;
3035
import java.util.function.Consumer;
3136

3237
@Slf4j
@@ -40,6 +45,13 @@ public RewriteMavenArtifactDownloader() {
4045
new LocalMavenArtifactCache(Paths.get(System.getProperty("user.home"), ".rewrite", "cache", "artifacts"))
4146
),
4247
null,
48+
new OkHttpSender(
49+
new OkHttpClient.Builder()
50+
.retryOnConnectionFailure(true)
51+
.connectTimeout(1, TimeUnit.SECONDS)
52+
.readTimeout(2, TimeUnit.SECONDS)
53+
.build()
54+
),
4355
(t) -> log.error("Error while downloading dependencies", t)
4456
);
4557

components/sbm-core/src/main/java/org/springframework/sbm/project/parser/MavenProjectParser.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -147,14 +147,20 @@ public List<SourceFile> parse(Path projectDirectory, List<Resource> resources) {
147147
Path.of("src/main/mule")
148148
);
149149

150-
// FIXME: mainSourceSetMarker and provenance marker needs to be a dde to all resources
150+
// FIXME: mainSourceSetMarker and provenance marker must be added to all resources
151+
List<Resource> resourceList = resourceParser.filter(projectDirectory, mainResourcePaths, resources, relativeModuleDir);
151152

152-
List<SourceFile> mainResources = resourceParser.parse(projectDirectory, mainResourcePaths, resources);
153+
List<Marker> resourceMarker = new ArrayList(javaProvenanceMarkers);
154+
resourceMarker.add(mainSourceSet);
155+
resourceMarker.add(gitProvenance);
156+
List<SourceFile> mainResources = resourceParser.parse(projectDirectory, resourceList, resourceMarker);
153157
sourceFiles.addAll(mainResources);
154158

155159
// -------
156160
// Test Java sources
157-
List<J.CompilationUnit> testJavaSources = parseTestJavaSources(projectDirectory, resources, ctx, javaParser, pomXml, mavenWithMarkers, mavenProjectDirectory, javaProvenanceMarkers);
161+
ArrayList<Marker> markers = new ArrayList<>(javaProvenanceMarkers);
162+
markers.add(mainSourceSet);
163+
List<J.CompilationUnit> testJavaSources = parseTestJavaSources(projectDirectory, resources, ctx, javaParser, pomXml, mavenWithMarkers, mavenProjectDirectory, markers);
158164
JavaSourceSet testSourceSet = javaParser.getSourceSet(ctx);
159165
sourceFiles.addAll(testJavaSources);
160166

@@ -166,9 +172,11 @@ public List<SourceFile> parse(Path projectDirectory, List<Resource> resources) {
166172
Path.of("src/test/mule")
167173
);
168174

169-
// FIXME: mainSourceSetMarker and provenance marker needs to be a dde to all resources
170-
171-
List<SourceFile> testResources = resourceParser.parse(projectDirectory, testResourcePaths, resources);
175+
List<Resource> filteredResources = resourceParser.filter(projectDirectory, testResourcePaths, resources, relativeModuleDir);
176+
List<Marker> testResourceMarker = new ArrayList(javaProvenanceMarkers);
177+
testResourceMarker.add(testSourceSet);
178+
testResourceMarker.add(gitProvenance);
179+
List<SourceFile> testResources = resourceParser.parse(projectDirectory, filteredResources, testResourceMarker);
172180
sourceFiles.addAll(testResources);
173181

174182
//
@@ -270,8 +278,7 @@ private List<J.CompilationUnit> parseTestJavaSources(Path projectDirectory, List
270278
}).collect(Collectors.toList());
271279
List<J.CompilationUnit> testCompilationUnits = javaParser.parseInputs(testJavaSourcesInput, projectDirectory, ctx);
272280
// FIXME: #7 JavaParser and adding markers is required when adding java sources and should go into dedicated component
273-
testCompilationUnits.stream()
274-
.forEach(cu -> cu.getMarkers().getMarkers().addAll(javaProvenanceMarkers));
281+
testCompilationUnits.forEach(cu -> cu.getMarkers().getMarkers().addAll(javaProvenanceMarkers));
275282
return testCompilationUnits;
276283
}
277284

components/sbm-core/src/main/java/org/springframework/sbm/project/parser/ResourceParser.java

Lines changed: 43 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,14 @@
2121
import org.openrewrite.SourceFile;
2222
import org.openrewrite.hcl.HclParser;
2323
import org.openrewrite.json.JsonParser;
24+
import org.openrewrite.marker.Marker;
25+
import org.openrewrite.marker.Markers;
2426
import org.openrewrite.properties.PropertiesParser;
2527
import org.openrewrite.protobuf.ProtoParser;
2628
import org.openrewrite.text.PlainTextParser;
2729
import org.openrewrite.tree.ParsingExecutionContextView;
2830
import org.openrewrite.xml.XmlParser;
31+
import org.openrewrite.xml.tree.Xml;
2932
import org.openrewrite.yaml.YamlParser;
3033
import org.springframework.context.ApplicationEventPublisher;
3134
import org.springframework.core.io.Resource;
@@ -52,37 +55,10 @@ public class ResourceParser {
5255
private final ResourceFilter resourceFilter;
5356
private final ApplicationEventPublisher eventPublisher;
5457

55-
public List<SourceFile> parse(Path baseDir, Set<Path> resourcePaths, List<Resource> resources) {
56-
ParsingExecutionContextView ctx = ParsingExecutionContextView.view(new RewriteExecutionContext(eventPublisher));
57-
ctx.setParsingListener((input, sourceFile) -> eventPublisher.publishEvent(new StartedScanningProjectResourceEvent(sourceFile.getSourcePath())));
58-
59-
List<Resource> relevantResources = resourceFilter.filter(resources, baseDir, resourcePaths);
60-
61-
HashMap<Parser<? extends SourceFile>, List<Parser.Input>> parserAndParserInputMappings = new LinkedHashMap();
62-
parserAndParserInputMappings.put(jsonParser, new ArrayList<>());
63-
parserAndParserInputMappings.put(xmlParser, new ArrayList<>());
64-
parserAndParserInputMappings.put(yamlParser, new ArrayList<>());
65-
parserAndParserInputMappings.put(propertiesParser, new ArrayList<>());
66-
parserAndParserInputMappings.put(new ProtoParser(), new ArrayList<>());
67-
parserAndParserInputMappings.put(HclParser.builder().build(), new ArrayList<>());
68-
parserAndParserInputMappings.put(plainTextParser, new ArrayList<>());
69-
70-
List<Parser.Input> parserInputs = createParserInputs(relevantResources);
71-
72-
parserInputs.forEach(r -> {
73-
Parser parser = parserAndParserInputMappings.keySet().stream()
74-
.filter(p -> p.accept(r))
75-
.findFirst()
76-
.orElseThrow(() -> new RuntimeException("Could not find matching parser for " + r.getPath()));
77-
78-
parserAndParserInputMappings.get(parser).add(r);
79-
});
80-
81-
return parserAndParserInputMappings.entrySet().stream()
82-
.map(e -> e.getKey().parseInputs(e.getValue(), baseDir, ctx))
83-
.flatMap(List::stream)
84-
.collect(Collectors.toList());
85-
58+
List<Resource> filter(Path projectDirectory, Set<Path> resourcePaths, List<Resource> resources, Path relativeModuleDir) {
59+
Path comparingPath = relativeModuleDir != null ? projectDirectory.resolve(relativeModuleDir) : projectDirectory;
60+
List<Resource> relevantResources = resourceFilter.filter(resources, comparingPath, resourcePaths);
61+
return relevantResources;
8662
}
8763

8864
private List<Parser.Input> createParserInputs(List<Resource> relevantResources) {
@@ -114,6 +90,42 @@ private InputStream getInputStream(Resource r) {
11490
}
11591
}
11692

93+
public List<SourceFile> parse(Path baseDir, List<Resource> relevantResources, List<Marker> markers) {
94+
List<Parser.Input> parserInputs = createParserInputs(relevantResources);
95+
96+
HashMap<Parser<? extends SourceFile>, List<Parser.Input>> parserAndParserInputMappings = new LinkedHashMap();
97+
parserAndParserInputMappings.put(jsonParser, new ArrayList<>());
98+
parserAndParserInputMappings.put(xmlParser, new ArrayList<>());
99+
parserAndParserInputMappings.put(yamlParser, new ArrayList<>());
100+
parserAndParserInputMappings.put(propertiesParser, new ArrayList<>());
101+
parserAndParserInputMappings.put(new ProtoParser(), new ArrayList<>());
102+
parserAndParserInputMappings.put(HclParser.builder().build(), new ArrayList<>());
103+
parserAndParserInputMappings.put(plainTextParser, new ArrayList<>());
104+
105+
parserInputs.forEach(r -> {
106+
Parser parser = parserAndParserInputMappings.keySet().stream()
107+
.filter(p -> p.accept(r))
108+
.findFirst()
109+
.orElseThrow(() -> new RuntimeException("Could not find matching parser for " + r.getPath()));
110+
111+
parserAndParserInputMappings.get(parser).add(r);
112+
});
113+
114+
ParsingExecutionContextView ctx = ParsingExecutionContextView.view(new RewriteExecutionContext(eventPublisher));
115+
ctx.setParsingListener((input, sourceFile) -> eventPublisher.publishEvent(new StartedScanningProjectResourceEvent(sourceFile.getSourcePath())));
116+
117+
return parserAndParserInputMappings.entrySet().stream()
118+
.map(e -> e.getKey().parseInputs(e.getValue(), baseDir, ctx))
119+
.flatMap(List::stream)
120+
.map(e -> addMarkers(e, markers))
121+
.collect(Collectors.toList());
122+
123+
}
124+
125+
private SourceFile addMarkers(SourceFile e, List<Marker> markers) {
126+
return e.withMarkers(Markers.build(markers));
127+
}
128+
117129
@Component
118130
public static class ResourceFilter {
119131
private List<Resource> filter(List<Resource> resources, Path moduleDir, Set<Path> searchDirs) {

components/sbm-core/src/test/java/org/springframework/sbm/java/impl/OpenRewriteMethodTest.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -144,19 +144,19 @@ void testRemoveAnnotation() {
144144
void removeMethodAnnotationsFromDependency() {
145145
String given =
146146
"import javax.ejb.*;\n" +
147-
"@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)\n" +
148-
"public class TransactionalService {\n" +
149-
" @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)\n" +
150-
" public void notSupported() {}\n" +
151-
"}";
147+
"@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)\n" +
148+
"public class TransactionalService {\n" +
149+
" @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)\n" +
150+
" public void notSupported() {}\n" +
151+
"}";
152152

153153
String expected =
154154
"import javax.ejb.*;\n" +
155-
"@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)\n" +
156-
"public class TransactionalService {\n" +
157-
" \n" +
158-
" public void notSupported() {}\n" +
159-
"}";
155+
"@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)\n" +
156+
"public class TransactionalService {\n" +
157+
" \n" +
158+
" public void notSupported() {}\n" +
159+
"}";
160160

161161
JavaSource javaSource = TestProjectContext.buildProjectContext()
162162
.withBuildFileHavingDependencies("javax.ejb:javax.ejb-api:3.2", "org.springframework.data:spring-data-jpa:2.6.1")

0 commit comments

Comments
 (0)