Skip to content

Commit

Permalink
openrewrite#936 refactor listKotlinSources()
Browse files Browse the repository at this point in the history
  • Loading branch information
reisners committed Jan 28, 2025
1 parent 15d5c58 commit 595b27a
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions src/main/java/org/openrewrite/maven/MavenMojoProjectParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.apache.maven.settings.crypto.SettingsDecryptionRequest;
import org.apache.maven.settings.crypto.SettingsDecryptionResult;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.jetbrains.annotations.NotNull;
import org.jspecify.annotations.Nullable;
import org.openrewrite.ExecutionContext;
import org.openrewrite.ParseExceptionResult;
Expand Down Expand Up @@ -388,16 +389,16 @@ private Stream<SourceFile> processMainSources(
// Some annotation processors output generated sources to the /target directory. These are added for parsing but
// should be filtered out of the final SourceFile list.
List<Path> generatedSourcePaths = listJavaSources(mavenProject.getBasedir().toPath().resolve(mavenProject.getBuild().getDirectory()));
String mavenSourceDirectory = mavenProject.getBuild().getSourceDirectory();
List<Path> mainJavaSources = Stream.concat(
generatedSourcePaths.stream(),
listJavaSources(mavenProject.getBasedir().toPath().resolve(mavenProject.getBuild().getSourceDirectory())).stream()
listJavaSources(mavenProject.getBasedir().toPath().resolve(mavenSourceDirectory)).stream()
).collect(toList());

alreadyParsed.addAll(mainJavaSources);

// scan Kotlin files
String kotlinSourceDir = getKotlinDirectory(mavenProject.getBuild().getSourceDirectory());
List<Path> mainKotlinSources = listKotlinSources(mavenProject.getBasedir().toPath().resolve(kotlinSourceDir != null ? kotlinSourceDir : mavenProject.getBuild().getSourceDirectory()));
List<Path> mainKotlinSources = listKotlinSources(mavenProject, mavenSourceDirectory);
alreadyParsed.addAll(mainKotlinSources);

logInfo(mavenProject, "Parsing source files");
Expand Down Expand Up @@ -465,8 +466,8 @@ private Stream<SourceFile> processTestSources(
alreadyParsed.addAll(testJavaSources);

// scan Kotlin files
String kotlinTestSourceDir = getKotlinDirectory(mavenProject.getBuild().getTestSourceDirectory());
List<Path> testKotlinSources = listKotlinSources(mavenProject.getBasedir().toPath().resolve(kotlinTestSourceDir != null ? kotlinTestSourceDir : mavenProject.getBuild().getTestSourceDirectory()));
String mavenTestSourceDirectory = mavenProject.getBuild().getTestSourceDirectory();
List<Path> testKotlinSources = listKotlinSources(mavenProject, mavenTestSourceDirectory);
alreadyParsed.addAll(testKotlinSources);

Stream<SourceFile> parsedJava = Stream.empty();
Expand All @@ -489,10 +490,15 @@ private Stream<SourceFile> processTestSources(
int sourcesParsedBefore = alreadyParsed.size();
Stream<SourceFile> parsedResourceFiles = resourceParser.parse(mavenProject.getBasedir().toPath().resolve("src/test/resources"), alreadyParsed);
logDebug(mavenProject, "Scanned " + (alreadyParsed.size() - sourcesParsedBefore) + " resource files in test scope.");
return Stream.concat(Stream.concat(parsedJava, kotlinParserBuilder.build().parse(testKotlinSources, baseDir, ctx)), parsedResourceFiles)
return Stream.concat(Stream.concat(parsedJava, parsedKotlin), parsedResourceFiles)
.map(addProvenance(baseDir, markers, null));
}

private @NotNull List<Path> listKotlinSources(MavenProject mavenProject, String fallbackSourceDirectory) throws MojoExecutionException {
String kotlinSourceDir = getKotlinDirectory(fallbackSourceDirectory);
return listSources(mavenProject.getBasedir().toPath().resolve(kotlinSourceDir != null ? kotlinSourceDir : fallbackSourceDirectory), ".kt");
}

private @Nullable String getKotlinDirectory(@Nullable String sourceDirectory) {
if (sourceDirectory == null) {
return null;
Expand Down Expand Up @@ -760,10 +766,6 @@ private static List<Path> listJavaSources(Path sourceDirectory) throws MojoExecu
return listSources(sourceDirectory, ".java");
}

private static List<Path> listKotlinSources(Path sourceDirectory) throws MojoExecutionException {
return listSources(sourceDirectory, ".kt");
}

private static List<Path> listSources(Path sourceDirectory, String extension) throws MojoExecutionException {
if (!Files.exists(sourceDirectory)) {
return emptyList();
Expand Down

0 comments on commit 595b27a

Please sign in to comment.