Skip to content

Commit 1750829

Browse files
committed
update from main
1 parent 561f7d7 commit 1750829

File tree

10 files changed

+156
-87
lines changed

10 files changed

+156
-87
lines changed

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/project/parser/ProjectContextInitializerTest.java

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,12 @@
4242
import org.springframework.sbm.engine.context.ProjectRootPathResolver;
4343
import org.springframework.sbm.engine.git.GitSupport;
4444
import org.springframework.sbm.engine.precondition.PreconditionVerifier;
45+
import org.springframework.sbm.java.impl.RewriteJavaParser;
4546
import org.springframework.sbm.java.refactoring.JavaRefactoringFactoryImpl;
4647
import org.springframework.sbm.java.util.BasePackageCalculator;
4748
import org.springframework.sbm.openrewrite.RewriteExecutionContext;
4849
import org.springframework.sbm.project.resource.*;
50+
import org.springframework.sbm.properties.parser.RewritePropertiesParser;
4951
import org.springframework.sbm.xml.parser.RewriteXmlParser;
5052
import org.springframework.util.FileSystemUtils;
5153

@@ -73,8 +75,16 @@
7375
ProjectContextFactory.class,
7476
RewriteMavenParserFactory.class, // FIXME: #7 remove class
7577
MavenPomCacheProvider.class,
76-
PathScanner.class,
7778
SbmApplicationProperties.class,
79+
PathScanner.class,
80+
RewriteJavaParser.class,
81+
RewritePlainTextParser.class,
82+
RewriteYamlParser.class,
83+
RewriteJsonParser.class,
84+
ResourceParser.class,
85+
RewritePropertiesParser.class,
86+
MavenProjectParser.class,
87+
RewriteMavenParser.class,
7888
RewriteXmlParser.class,
7989
ResourceHelper.class,
8090
ResourceLoader.class,
@@ -118,13 +128,13 @@ void test() {
118128

119129
assertThat(projectDirectory.toAbsolutePath().resolve(".git")).exists();
120130

121-
assertThat(projectResources).hasSize(18);
131+
assertThat(projectResources).hasSize(19);
122132

123133
verifyResource("testcode/pom.xml").wrapsInstanceOf(Xml.Document.class);
124134
verifyIgnored(projectResources, "testcode/path-scanner/.git");
125135

126136
verifyResource("testcode/path-scanner/pom.xml")
127-
.wrapsInstanceOf(Maven.class)
137+
.wrapsInstanceOf(Xml.Document.class)
128138
.havingMarkers(
129139
mavenResolutionResult(null, "com.example:example-project-parent:1.0.0-SNAPSHOT",
130140
List.of(
@@ -140,7 +150,7 @@ void test() {
140150
.isContainedIn(projectResources);
141151

142152
verifyResource("testcode/path-scanner/module1/pom.xml")
143-
.wrapsInstanceOf(Maven.class)
153+
.wrapsInstanceOf(Xml.Document.class)
144154
.havingMarkers(
145155
mavenResolutionResult(
146156
"com.example:example-project-parent:1.0.0-SNAPSHOT",
@@ -166,7 +176,6 @@ void test() {
166176
buildToolMarker("Maven", "3.6"),
167177
javaVersionMarker(11, "11", "11"),
168178
javaProjectMarker(null, "com.example:module1:1.0.0-SNAPSHOT"),
169-
javaSourceSetMarker("main", ""),
170179
gitProvenanceMarker("master")
171180
)
172181
.isContainedIn(projectResources);
@@ -177,7 +186,7 @@ void test() {
177186
buildToolMarker("Maven", "3.6"),
178187
javaVersionMarker(11, "11", "11"),
179188
javaProjectMarker(null, "com.example:module1:1.0.0-SNAPSHOT"),
180-
javaSourceSetMarker("main", ""),
189+
javaSourceSetMarker("main", 1903),
181190
gitProvenanceMarker("master")
182191
)
183192
.isContainedIn(projectResources);
@@ -188,7 +197,7 @@ void test() {
188197
buildToolMarker("Maven", "3.6"),
189198
javaVersionMarker(11, "11", "11"),
190199
javaProjectMarker(null, "com.example:module1:1.0.0-SNAPSHOT"),
191-
javaSourceSetMarker("main", ""),
200+
javaSourceSetMarker("main", 1903),
192201
gitProvenanceMarker("master")
193202
)
194203
.isContainedIn(projectResources);
@@ -199,7 +208,7 @@ void test() {
199208
buildToolMarker("Maven", "3.6"),
200209
javaVersionMarker(11, "11", "11"),
201210
javaProjectMarker(null, "com.example:module1:1.0.0-SNAPSHOT"),
202-
javaSourceSetMarker("main", ""),
211+
javaSourceSetMarker("main", 1903),
203212
gitProvenanceMarker("master")
204213
)
205214
.isContainedIn(projectResources);
@@ -210,7 +219,7 @@ void test() {
210219
buildToolMarker("Maven", "3.6"),
211220
javaVersionMarker(11, "11", "11"),
212221
javaProjectMarker(null, "com.example:module1:1.0.0-SNAPSHOT"),
213-
javaSourceSetMarker("main", ""),
222+
javaSourceSetMarker("main", 1903),
214223
gitProvenanceMarker("master")
215224
)
216225
.isContainedIn(projectResources);
@@ -221,7 +230,7 @@ void test() {
221230
buildToolMarker("Maven", "3.6"),
222231
javaVersionMarker(11, "11", "11"),
223232
javaProjectMarker(null, "com.example:module1:1.0.0-SNAPSHOT"),
224-
javaSourceSetMarker("main", ""),
233+
javaSourceSetMarker("main", 1903),
225234
gitProvenanceMarker("master")
226235
)
227236
.isContainedIn(projectResources);
@@ -232,7 +241,7 @@ void test() {
232241
buildToolMarker("Maven", "3.6"),
233242
javaVersionMarker(11, "11", "11"),
234243
javaProjectMarker(null, "com.example:module1:1.0.0-SNAPSHOT"),
235-
javaSourceSetMarker("main", ""),
244+
javaSourceSetMarker("main", 1903),
236245
gitProvenanceMarker("master")
237246
)
238247
.isContainedIn(projectResources);
@@ -242,7 +251,7 @@ void test() {
242251
.havingMarkers(buildToolMarker("Maven", "3.6"),
243252
javaVersionMarker(11, "11", "11"),
244253
javaProjectMarker(null, "com.example:module1:1.0.0-SNAPSHOT"),
245-
javaSourceSetMarker("main", ""),
254+
javaSourceSetMarker("main", 1903),
246255
gitProvenanceMarker("master")
247256
)
248257
.isContainedIn(projectResources);
@@ -253,7 +262,7 @@ void test() {
253262
buildToolMarker("Maven", "3.6"),
254263
javaVersionMarker(11, "11", "11"),
255264
javaProjectMarker(null, "com.example:module1:1.0.0-SNAPSHOT"),
256-
javaSourceSetMarker("main", ""),
265+
javaSourceSetMarker("main", 1903),
257266
gitProvenanceMarker("master")
258267
)
259268
.isContainedIn(projectResources);
@@ -264,7 +273,7 @@ void test() {
264273
buildToolMarker("Maven", "3.6"),
265274
javaVersionMarker(11, "11", "11"),
266275
javaProjectMarker(null, "com.example:module1:1.0.0-SNAPSHOT"),
267-
javaSourceSetMarker("main", ""),
276+
javaSourceSetMarker("main", 1903),
268277
gitProvenanceMarker("master")
269278
)
270279
.isContainedIn(projectResources);
@@ -275,7 +284,7 @@ void test() {
275284
buildToolMarker("Maven", "3.6"),
276285
javaVersionMarker(11, "11", "11"),
277286
javaProjectMarker(null, "com.example:module1:1.0.0-SNAPSHOT"),
278-
javaSourceSetMarker("main", ""),
287+
javaSourceSetMarker("main", 1903),
279288
gitProvenanceMarker("master")
280289
)
281290
.isContainedIn(projectResources);
@@ -285,7 +294,7 @@ void test() {
285294
.havingMarkers(buildToolMarker("Maven", "3.6"),
286295
javaVersionMarker(11, "11", "11"),
287296
javaProjectMarker(null, "com.example:module1:1.0.0-SNAPSHOT"),
288-
javaSourceSetMarker("main", ""),
297+
javaSourceSetMarker("main", 1903),
289298
gitProvenanceMarker("master")
290299
)
291300
.isContainedIn(projectResources);
@@ -296,14 +305,14 @@ void test() {
296305
buildToolMarker("Maven", "3.6"),
297306
javaVersionMarker(11, "11", "11"),
298307
javaProjectMarker(null, "com.example:module1:1.0.0-SNAPSHOT"),
299-
javaSourceSetMarker("main", ""),
308+
javaSourceSetMarker("main", 1903),
300309
gitProvenanceMarker("master")
301310
)
302311
.isContainedIn(projectResources);
303312

304313
// module2
305314
verifyResource("testcode/path-scanner/module2/pom.xml")
306-
.wrapsInstanceOf(Maven.class)
315+
.wrapsInstanceOf(Xml.Document.class)
307316
.havingMarkers(
308317
mavenResolutionResult(
309318
"com.example:example-project-parent:1.0.0-SNAPSHOT",
@@ -355,10 +364,10 @@ private void verifyIgnored(List<RewriteSourceFileHolder<? extends SourceFile>> p
355364
@NotNull
356365
private Map<? extends Scope, ? extends List<String>> noDependencies() {
357366
return Map.of(
358-
Scope.Compile, List.of(),
359-
Scope.Provided, List.of(),
360-
Scope.Test, List.of(),
361-
Scope.Runtime, List.of()
367+
Scope.Compile, List.of(),
368+
Scope.Provided, List.of(),
369+
Scope.Test, List.of(),
370+
Scope.Runtime, List.of()
362371
);
363372
}
364373
}

0 commit comments

Comments
 (0)