Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
Working on a module parser
JavaParserBuilder keeps state (classpath) and is passed through
  • Loading branch information
fabapp2 committed Oct 16, 2023
1 parent aff35d3 commit 7910103
Show file tree
Hide file tree
Showing 12 changed files with 468 additions and 56 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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.parsers;

import lombok.RequiredArgsConstructor;
import org.openrewrite.maven.tree.MavenResolutionResult;
import org.openrewrite.maven.tree.ResolvedDependency;
import org.openrewrite.maven.tree.Scope;
import org.openrewrite.maven.utilities.MavenArtifactDownloader;

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


/**
* @author Fabian Krüger
*/
@RequiredArgsConstructor
public class ClasspathExtractor {
private final MavenArtifactDownloader rewriteMavenArtifactDownloader;

public List<Path> extractClasspath(MavenResolutionResult pom, Scope scope) {
List<ResolvedDependency> resolvedDependencies = pom.getDependencies().get(scope);
if (resolvedDependencies != null) {
return resolvedDependencies
// FIXME: 945 - deal with dependencies to projects in reactor
//
.stream()
.filter(rd -> rd.getRepository() != null)
.map(rd -> rewriteMavenArtifactDownloader.downloadArtifact(rd))
.filter(Objects::nonNull)
.distinct()
.toList();
} else {
return new ArrayList<>();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@
import org.apache.maven.model.Model;
import org.apache.maven.model.Plugin;
import org.jetbrains.annotations.NotNull;
import org.openrewrite.SourceFile;
import org.openrewrite.maven.tree.*;
import org.openrewrite.maven.utilities.MavenArtifactDownloader;
import org.openrewrite.xml.tree.Xml;
import org.springframework.core.io.Resource;
import org.springframework.sbm.parsers.maven.MavenRuntimeInformation;
import org.springframework.sbm.project.resource.ProjectResourceSet;
import org.springframework.sbm.utils.ResourceUtil;

import java.io.File;
Expand All @@ -43,19 +42,17 @@ public class MavenProject {

private final Path projectRoot;
private final Resource pomFile;
// FIXME: 945 temporary method, model should nopt come from Maven
private final Model pomModel;
private final List<Resource> resources;
private List<MavenProject> collectedProjects = new ArrayList<>();
private Xml.Document sourceFile;
private final MavenArtifactDownloader rewriteMavenArtifactDownloader;
private final List<Resource> resources;
private ProjectId projectId;
private Map<Scope, Set<Path>> classpath;

public MavenProject(Path projectRoot, Resource pomFile, Model pomModel, MavenArtifactDownloader rewriteMavenArtifactDownloader, List<Resource> resources) {
public MavenProject(Path projectRoot, Resource pomFile, Model pomModel, List<Resource> resources) {
this.projectRoot = projectRoot;
this.pomFile = pomFile;
this.pomModel = pomModel;
this.rewriteMavenArtifactDownloader = rewriteMavenArtifactDownloader;
this.resources = resources;
projectId = new ProjectId(getGroupId(), getArtifactId());
}
Expand Down Expand Up @@ -145,32 +142,18 @@ public String getSourceDirectory() {
return s == null ? ResourceUtil.getPath(pomFile).getParent().resolve("src/main/java").toAbsolutePath().normalize().toString() : s;
}

public List<Path> getCompileClasspathElements() {
public Set<Path> getCompileClasspathElements() {
Scope scope = Scope.Compile;
return getClasspathElements(scope);
}

public List<Path> getTestClasspathElements() {
public Set<Path> getTestClasspathElements() {
return getClasspathElements(Scope.Test);
}

@NotNull
private List<Path> getClasspathElements(Scope scope) {
MavenResolutionResult pom = getSourceFile().getMarkers().findFirst(MavenResolutionResult.class).get();
List<ResolvedDependency> resolvedDependencies = pom.getDependencies().get(scope);
if(resolvedDependencies != null) {
return resolvedDependencies
// FIXME: 945 - deal with dependencies to projects in reactor
//
.stream()
.filter(rd -> rd.getRepository() != null)
.map(rd -> rewriteMavenArtifactDownloader.downloadArtifact(rd))
.filter(Objects::nonNull)
.distinct()
.toList();
} else {
return new ArrayList<>();
}
private Set<Path> getClasspathElements(Scope scope) {
return this.classpath.get(scope);
}

public String getTestSourceDirectory() {
Expand Down Expand Up @@ -199,7 +182,6 @@ private static Predicate<Resource> whenIn(Path sourceDirectory) {
return r -> ResourceUtil.getPath(r).toString().startsWith(sourceDirectory.toString());
}


public List<Resource> getJavaSourcesInTarget() {
return listJavaSources(getResources(), getBasedir().resolve(getBuildDirectory()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public List<SourceFile> processMainSources(
// Or, the classpath must be created from the sources of the project.


List<Path> dependencies = currentProject.getCompileClasspathElements();
Set<Path> dependencies = currentProject.getCompileClasspathElements();

javaParserBuilder.classpath(dependencies);

Expand Down Expand Up @@ -148,7 +148,7 @@ public List<SourceFile> processMainSources(
}

@NotNull
private static JavaSourceSet sourceSet(String name, List<Path> dependencies, JavaTypeCache typeCache) {
private static JavaSourceSet sourceSet(String name, Set<Path> dependencies, JavaTypeCache typeCache) {
return JavaSourceSet.build(name, dependencies, typeCache, false);
}

Expand All @@ -168,7 +168,7 @@ public List<SourceFile> processTestSources(
) {
log.info("Processing test sources in module '%s'".formatted(currentProject.getProjectId()));

List<Path> testDependencies = currentProject.getTestClasspathElements();
Set<Path> testDependencies = currentProject.getTestClasspathElements();

javaParserBuilder.classpath(testDependencies);
JavaTypeCache typeCache = new JavaTypeCache();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* 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.parsers;

import org.openrewrite.SourceFile;

import java.util.List;

/**
* @author Fabian Krüger
*/
public record ModuleParsingResult(List<SourceFile> sourceFiles) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ Consumer<Throwable> artifactDownloaderErrorConsumer() {
return (t) -> {throw new RuntimeException(t);};
}

@Bean
ClasspathExtractor classpathExtractor(MavenArtifactDownloader rewriteMavenArtifactDownloader) {
return new ClasspathExtractor(rewriteMavenArtifactDownloader);
}

@Bean
RewriteMavenArtifactDownloader artifactDownloader(MavenArtifactCache mavenArtifactCache, ProjectMetadata projectMetadata, Consumer<Throwable> artifactDownloaderErrorConsumer) {
return new RewriteMavenArtifactDownloader(mavenArtifactCache, projectMetadata.getMavenSettings(), artifactDownloaderErrorConsumer);
Expand Down Expand Up @@ -147,8 +152,8 @@ ParsingEventListener parsingEventListener(ApplicationEventPublisher eventPublish
// }

@Bean
MavenProjectAnalyzer mavenProjectAnalyzer(MavenArtifactDownloader artifactDownloader) {
return new MavenProjectAnalyzer(artifactDownloader);
MavenProjectAnalyzer mavenProjectAnalyzer(MavenArtifactDownloader artifactDownloader, ClasspathExtractor classpathExtractor) {
return new MavenProjectAnalyzer(artifactDownloader, classpathExtractor);
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,21 @@

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.openrewrite.ExecutionContext;
import org.openrewrite.SourceFile;
import org.openrewrite.java.JavaParser;
import org.openrewrite.marker.Marker;
import org.openrewrite.style.NamedStyles;
import org.openrewrite.xml.tree.Xml;
import org.springframework.core.io.Resource;
import org.springframework.sbm.parsers.MavenProject;
import org.springframework.sbm.parsers.ModuleParser;
import org.springframework.sbm.parsers.ParserProperties;
import org.springframework.sbm.parsers.RewriteResourceParser;
import org.springframework.sbm.parsers.*;
import org.springframework.sbm.utils.ResourceUtil;

import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -148,4 +145,54 @@ private Set<Path> pathsToOtherMavenProjects(MavenProject mavenProject, Path modu
.collect(Collectors.toSet());
}

//-------------

public ModuleParsingResult parseModule(Path baseDir, String modulePathSegment, Collection<Path> classpath, Collection<byte[]> classBytesClasspath, List<Resource> resources) {
resources = filterResources(baseDir, modulePathSegment, resources);
List<SourceFile> parsedSources = new ArrayList<>();
List<SourceFile> mainSources = parseMainSources(baseDir, classpath, classBytesClasspath, resources);
parsedSources.addAll(mainSources);
List<SourceFile> testSources = parseTestSources(resources, classpath, classBytesClasspath, resources);
parsedSources.addAll(testSources);
List<SourceFile> otherResources = parseOtherResources(resources);
parsedSources.addAll(otherResources);
ModuleParsingResult moduleParsingResult = new ModuleParsingResult(parsedSources);
return moduleParsingResult;
}

private List<SourceFile> parseOtherResources(List<Resource> resources) {
return new ArrayList<>();
}

private List<SourceFile> parseTestSources(List<Resource> resources, Collection<Path> classpath, Collection<byte[]> classBytesClasspath, List<Resource> resources1) {
return new ArrayList<>();
}

private List<SourceFile> parseMainSources(Path baseDir, Collection<Path> classpath, Collection<byte[]> classBytesClasspath, List<Resource> resources) {
List<byte[]> array = classBytesClasspath
.stream()
.map(b -> {
return (byte[]) b;
})
.toList();

List<byte[]> classBytesClasspathList = new ArrayList<>();
classBytesClasspathList.addAll(classBytesClasspath);

byte[][] classBytesClasspathArray = new byte[classBytesClasspath.size()][];
for(int i=0; i<classBytesClasspathList.size(); i++) {
byte[] bytes = classBytesClasspathList.get(i);
classBytesClasspathArray[i] = bytes;
}
JavaParser.fromJavaVersion().classpath(classBytesClasspathArray);
return new ArrayList<>();
}

private List<Resource> filterResources(Path baseDir, String modulePathSegment, List<Resource> resources) {
String pattern = "glob:" + baseDir.resolve(modulePathSegment);
PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher(pattern);
return resources.stream()
.filter(r -> pathMatcher.matches(ResourceUtil.getPath(r)))
.toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import org.openrewrite.maven.utilities.MavenArtifactDownloader;
import org.springframework.core.io.Resource;
import org.springframework.sbm.parsers.ClasspathExtractor;
import org.springframework.sbm.parsers.MavenProject;
import org.springframework.sbm.parsers.ParserContext;
import org.springframework.sbm.utils.ResourceUtil;
Expand All @@ -42,9 +43,11 @@ public class MavenProjectAnalyzer {
private static final String POM_XML = "pom.xml";
private static final MavenXpp3Reader XPP_3_READER = new MavenXpp3Reader();
private final MavenArtifactDownloader rewriteMavenArtifactDownloader;
private final ClasspathExtractor classpathExtractor;

public MavenProjectAnalyzer(MavenArtifactDownloader rewriteMavenArtifactDownloader) {
public MavenProjectAnalyzer(MavenArtifactDownloader rewriteMavenArtifactDownloader, ClasspathExtractor classpathExtractor) {
this.rewriteMavenArtifactDownloader = rewriteMavenArtifactDownloader;
this.classpathExtractor = classpathExtractor;
}

public List<MavenProject> getSortedProjects(Path baseDir, List<Resource> resources) {
Expand All @@ -59,7 +62,7 @@ public List<MavenProject> getSortedProjects(Path baseDir, List<Resource> resourc
Model rootPomModel = new Model(rootPom);

if (isSingleModuleProject(rootPomModel)) {
return List.of(new MavenProject(baseDir, rootPom, rootPomModel, rewriteMavenArtifactDownloader, resources));
return List.of(new MavenProject(baseDir, rootPom, rootPomModel, resources));
}
List<Model> reactorModels = new ArrayList<>();
recursivelyFindReactorModules(baseDir, null, reactorModels, allPomFiles, rootPomModel);
Expand All @@ -75,7 +78,7 @@ private List<MavenProject> map(Path baseDir, List<Resource> resources, List<Mode
.forEach(m -> {
String projectDir = baseDir.resolve(m.getProjectDirectory().toString()).normalize().toString();
List<Resource> filteredResources = resources.stream().filter(r -> ResourceUtil.getPath(r).toString().startsWith(projectDir)).toList();
mavenProjects.add(new MavenProject(baseDir, m.getResource(), m, rewriteMavenArtifactDownloader, filteredResources));
mavenProjects.add(new MavenProject(baseDir, m.getResource(), m, filteredResources));
});
// set all non parent poms as collected projects for root parent pom
List<MavenProject> collected = new ArrayList<>(mavenProjects);
Expand Down
Loading

0 comments on commit 7910103

Please sign in to comment.