Skip to content

Fix MavenProjectParser to parse all resources #191

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/mvn-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ jobs:
with:
java-version: '11'
distribution: 'adopt'
cache: 'maven'
- name: Build with Maven
run: mvn --update-snapshots -DtrimStackTrace=false -Dsurefire.useFile=false -DskipITs verify
4 changes: 4 additions & 0 deletions components/sbm-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.sbm</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@

import lombok.extern.slf4j.Slf4j;
import lombok.extern.slf4j.XSlf4j;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.ipc.http.HttpSender;
import org.openrewrite.ipc.http.OkHttpSender;
import org.openrewrite.maven.MavenSettings;
import org.openrewrite.maven.cache.LocalMavenArtifactCache;
import org.openrewrite.maven.cache.MavenArtifactCache;
Expand All @@ -27,6 +31,7 @@
import org.springframework.stereotype.Component;

import java.nio.file.Paths;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;

@Slf4j
Expand All @@ -40,6 +45,13 @@ public RewriteMavenArtifactDownloader() {
new LocalMavenArtifactCache(Paths.get(System.getProperty("user.home"), ".rewrite", "cache", "artifacts"))
),
null,
new OkHttpSender(
new OkHttpClient.Builder()
.retryOnConnectionFailure(true)
.connectTimeout(1, TimeUnit.SECONDS)
.readTimeout(2, TimeUnit.SECONDS)
.build()
),
(t) -> log.error("Error while downloading dependencies", t)
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,20 @@ public List<SourceFile> parse(Path projectDirectory, List<Resource> resources) {
Path.of("src/main/mule")
);

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

List<SourceFile> mainResources = resourceParser.parse(projectDirectory, mainResourcePaths, resources);
List<Marker> resourceMarker = new ArrayList(javaProvenanceMarkers);
resourceMarker.add(mainSourceSet);
resourceMarker.add(gitProvenance);
List<SourceFile> mainResources = resourceParser.parse(projectDirectory, resourceList, resourceMarker);
sourceFiles.addAll(mainResources);

// -------
// Test Java sources
List<J.CompilationUnit> testJavaSources = parseTestJavaSources(projectDirectory, resources, ctx, javaParser, pomXml, mavenWithMarkers, mavenProjectDirectory, javaProvenanceMarkers);
ArrayList<Marker> markers = new ArrayList<>(javaProvenanceMarkers);
markers.add(mainSourceSet);
List<J.CompilationUnit> testJavaSources = parseTestJavaSources(projectDirectory, resources, ctx, javaParser, pomXml, mavenWithMarkers, mavenProjectDirectory, markers);
JavaSourceSet testSourceSet = javaParser.getSourceSet(ctx);
sourceFiles.addAll(testJavaSources);

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

// FIXME: mainSourceSetMarker and provenance marker needs to be a dde to all resources

List<SourceFile> testResources = resourceParser.parse(projectDirectory, testResourcePaths, resources);
List<Resource> filteredResources = resourceParser.filter(projectDirectory, testResourcePaths, resources, relativeModuleDir);
List<Marker> testResourceMarker = new ArrayList(javaProvenanceMarkers);
testResourceMarker.add(testSourceSet);
testResourceMarker.add(gitProvenance);
List<SourceFile> testResources = resourceParser.parse(projectDirectory, filteredResources, testResourceMarker);
sourceFiles.addAll(testResources);

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@
import org.openrewrite.SourceFile;
import org.openrewrite.hcl.HclParser;
import org.openrewrite.json.JsonParser;
import org.openrewrite.marker.Marker;
import org.openrewrite.marker.Markers;
import org.openrewrite.properties.PropertiesParser;
import org.openrewrite.protobuf.ProtoParser;
import org.openrewrite.text.PlainTextParser;
import org.openrewrite.tree.ParsingExecutionContextView;
import org.openrewrite.xml.XmlParser;
import org.openrewrite.xml.tree.Xml;
import org.openrewrite.yaml.YamlParser;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.core.io.Resource;
Expand All @@ -52,37 +55,10 @@ public class ResourceParser {
private final ResourceFilter resourceFilter;
private final ApplicationEventPublisher eventPublisher;

public List<SourceFile> parse(Path baseDir, Set<Path> resourcePaths, List<Resource> resources) {
ParsingExecutionContextView ctx = ParsingExecutionContextView.view(new RewriteExecutionContext(eventPublisher));
ctx.setParsingListener((input, sourceFile) -> eventPublisher.publishEvent(new StartedScanningProjectResourceEvent(sourceFile.getSourcePath())));

List<Resource> relevantResources = resourceFilter.filter(resources, baseDir, resourcePaths);

HashMap<Parser<? extends SourceFile>, List<Parser.Input>> parserAndParserInputMappings = new LinkedHashMap();
parserAndParserInputMappings.put(jsonParser, new ArrayList<>());
parserAndParserInputMappings.put(xmlParser, new ArrayList<>());
parserAndParserInputMappings.put(yamlParser, new ArrayList<>());
parserAndParserInputMappings.put(propertiesParser, new ArrayList<>());
parserAndParserInputMappings.put(new ProtoParser(), new ArrayList<>());
parserAndParserInputMappings.put(HclParser.builder().build(), new ArrayList<>());
parserAndParserInputMappings.put(plainTextParser, new ArrayList<>());

List<Parser.Input> parserInputs = createParserInputs(relevantResources);

parserInputs.forEach(r -> {
Parser parser = parserAndParserInputMappings.keySet().stream()
.filter(p -> p.accept(r))
.findFirst()
.orElseThrow(() -> new RuntimeException("Could not find matching parser for " + r.getPath()));

parserAndParserInputMappings.get(parser).add(r);
});

return parserAndParserInputMappings.entrySet().stream()
.map(e -> e.getKey().parseInputs(e.getValue(), baseDir, ctx))
.flatMap(List::stream)
.collect(Collectors.toList());

List<Resource> filter(Path projectDirectory, Set<Path> resourcePaths, List<Resource> resources, Path relativeModuleDir) {
Path comparingPath = relativeModuleDir != null ? projectDirectory.resolve(relativeModuleDir) : projectDirectory;
List<Resource> relevantResources = resourceFilter.filter(resources, comparingPath, resourcePaths);
return relevantResources;
}

private List<Parser.Input> createParserInputs(List<Resource> relevantResources) {
Expand Down Expand Up @@ -114,6 +90,42 @@ private InputStream getInputStream(Resource r) {
}
}

public List<SourceFile> parse(Path baseDir, List<Resource> relevantResources, List<Marker> markers) {
List<Parser.Input> parserInputs = createParserInputs(relevantResources);

HashMap<Parser<? extends SourceFile>, List<Parser.Input>> parserAndParserInputMappings = new LinkedHashMap();
parserAndParserInputMappings.put(jsonParser, new ArrayList<>());
parserAndParserInputMappings.put(xmlParser, new ArrayList<>());
parserAndParserInputMappings.put(yamlParser, new ArrayList<>());
parserAndParserInputMappings.put(propertiesParser, new ArrayList<>());
parserAndParserInputMappings.put(new ProtoParser(), new ArrayList<>());
parserAndParserInputMappings.put(HclParser.builder().build(), new ArrayList<>());
parserAndParserInputMappings.put(plainTextParser, new ArrayList<>());

parserInputs.forEach(r -> {
Parser parser = parserAndParserInputMappings.keySet().stream()
.filter(p -> p.accept(r))
.findFirst()
.orElseThrow(() -> new RuntimeException("Could not find matching parser for " + r.getPath()));

parserAndParserInputMappings.get(parser).add(r);
});

ParsingExecutionContextView ctx = ParsingExecutionContextView.view(new RewriteExecutionContext(eventPublisher));
ctx.setParsingListener((input, sourceFile) -> eventPublisher.publishEvent(new StartedScanningProjectResourceEvent(sourceFile.getSourcePath())));

return parserAndParserInputMappings.entrySet().stream()
.map(e -> e.getKey().parseInputs(e.getValue(), baseDir, ctx))
.flatMap(List::stream)
.map(e -> addMarkers(e, markers))
.collect(Collectors.toList());

}

private SourceFile addMarkers(SourceFile e, List<Marker> markers) {
return e.withMarkers(Markers.build(markers));
}

@Component
public static class ResourceFilter {
private List<Resource> filter(List<Resource> resources, Path moduleDir, Set<Path> searchDirs) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,19 +144,19 @@ void testRemoveAnnotation() {
void removeMethodAnnotationsFromDependency() {
String given =
"import javax.ejb.*;\n" +
"@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)\n" +
"public class TransactionalService {\n" +
" @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)\n" +
" public void notSupported() {}\n" +
"}";
"@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)\n" +
"public class TransactionalService {\n" +
" @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)\n" +
" public void notSupported() {}\n" +
"}";

String expected =
"import javax.ejb.*;\n" +
"@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)\n" +
"public class TransactionalService {\n" +
" \n" +
" public void notSupported() {}\n" +
"}";
"@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)\n" +
"public class TransactionalService {\n" +
" \n" +
" public void notSupported() {}\n" +
"}";

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