Skip to content

Commit

Permalink
Reuse GradleProject marker on freestanding scripts.
Browse files Browse the repository at this point in the history
It may be confusing to recipes that freestanding scripts have the exact same marker as the project which contains them. Maybe will add a flag to the marker in the future if this is a source of issues.
  • Loading branch information
sambsnyd committed Sep 21, 2024
1 parent 9fca527 commit 98c7cd7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
import org.openrewrite.kotlin.tree.K;
import org.openrewrite.marker.*;
import org.openrewrite.marker.ci.BuildEnvironment;
import org.openrewrite.maven.tree.MavenRepository;
import org.openrewrite.polyglot.*;
import org.openrewrite.properties.PropertiesParser;
import org.openrewrite.quark.Quark;
Expand All @@ -84,7 +83,6 @@
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;
Expand Down Expand Up @@ -629,47 +627,7 @@ public Stream<SourceFile> parse(ExecutionContext ctx) {
builder = Stream.concat(builder, parse(subProject, alreadyParsed, ctx));
}
}
builder = Stream.concat(builder, parse(project, alreadyParsed, ctx));
return applyGradleScriptMarkers(builder)
// log parse errors here at the end, so that we don't log parse errors for files that were excluded
.map(this::logParseErrors);
}

/**
* Sometimes Gradle script plugins will define dependencies.
* Recipes may want to modify dependencies declared in script plugins, which requires knowledge of which repositories dependencies should be resolved from.
* This adds a synthetic GradleProject marker listing dependency repositories to any freestanding Gradle script plugins.
*
* @param sourceFiles a stream of sources which may contain Gradle build files and scripts
* @return a stream of sources where any freestanding Gradle scripts have a GradleProject marker
*/
private Stream<SourceFile> applyGradleScriptMarkers(Stream<SourceFile> sourceFiles) {
Set<MavenRepository> allBuildscriptRepositories = new LinkedHashSet<>();
Set<MavenRepository> allRepositories = new LinkedHashSet<>();
AtomicReference<GradleProject> freestandingScriptMarker = new AtomicReference<>();
return sourceFiles
.peek(s -> s.getMarkers()
.findFirst(GradleProject.class)
.ifPresent(gp -> {
allBuildscriptRepositories.addAll(gp.getBuildscript().getMavenRepositories());
allRepositories.addAll(gp.getMavenRepositories());
}))
.map(before -> {
// Add the synthetic GradleProject marker to any freestanding Gradle scripts
String path = before.getSourcePath().toString();
if ((path.endsWith(".gradle") || path.endsWith(".gradle.kts"))
&& !before.getMarkers().findFirst(org.openrewrite.gradle.marker.GradleProject.class).isPresent()
&& !before.getMarkers().findFirst(GradleSettings.class).isPresent()) {
//noinspection ConstantValue
if (freestandingScriptMarker.get() == null) {
freestandingScriptMarker.set(new org.openrewrite.gradle.marker.GradleProject(
randomId(), "", "", "", "", emptyList(), new ArrayList<>(allRepositories),
emptyList(), emptyMap(), new GradleBuildscript(randomId(), new ArrayList<>(allBuildscriptRepositories), emptyMap())));
}
return before.withMarkers(before.getMarkers().add(freestandingScriptMarker.get()));
}
return before;
});
return Stream.concat(builder, parse(project, alreadyParsed, ctx)).map(this::logParseErrors);
}

public Stream<SourceFile> parse(Project subproject, Set<Path> alreadyParsed, ExecutionContext ctx) {
Expand Down Expand Up @@ -972,16 +930,17 @@ private Stream<SourceFile> parseJavaFiles(
view(ctx).setCharset(javaSourceCharset);

return Stream.of((Supplier<JavaParser>) () -> JavaParser.fromJavaVersion()
.classpath(dependencyPaths)
.styles(getStyles())
.typeCache(javaTypeCache)
.logCompilationWarningsAndErrors(extension.getLogCompilationWarningsAndErrors())
.build()).map(Supplier::get).flatMap(jp -> jp.parse(javaPaths, baseDir, ctx)).map(cu -> {
if (isExcluded(exclusions, cu.getSourcePath()) || cu.getSourcePath().startsWith(buildDir)) {
return null;
}
return cu;
}).filter(Objects::nonNull).map(it -> it.withMarkers(it.getMarkers().add(javaVersion)));
.classpath(dependencyPaths)
.styles(getStyles())
.typeCache(javaTypeCache)
.logCompilationWarningsAndErrors(extension.getLogCompilationWarningsAndErrors())
.build())
.map(Supplier::get).flatMap(jp -> jp.parse(javaPaths, baseDir, ctx)).map(cu -> {
if (isExcluded(exclusions, cu.getSourcePath()) || cu.getSourcePath().startsWith(buildDir)) {
return null;
}
return cu;
}).filter(Objects::nonNull).map(it -> it.withMarkers(it.getMarkers().add(javaVersion)));
}

private Stream<SourceFile> parseKotlinFiles(List<Path> kotlinPaths,
Expand All @@ -996,7 +955,7 @@ private Stream<SourceFile> parseKotlinFiles(List<Path> kotlinPaths,

return Stream.of((Supplier<KotlinParser>) () -> KotlinParser.builder()
.classpath(dependencyPaths)
.styles(styles)
.styles(getStyles())
.typeCache(javaTypeCache)
.logCompilationWarningsAndErrors(extension.getLogCompilationWarningsAndErrors())
.build()).map(Supplier::get).flatMap(kp -> kp.parse(kotlinPaths, baseDir, ctx)).map(cu -> {
Expand Down Expand Up @@ -1053,12 +1012,11 @@ private SourceFileStream parseGradleFiles(

// build.gradle
GradleParser gradleParser = null;
GradleProject gradleProject = null;
GradleProject gradleProject = GradleProjectBuilder.gradleProject(project);
File buildGradleFile = subproject.getBuildscript().getSourceFile();
if (buildGradleFile != null) {
Path buildScriptPath = baseDir.relativize(buildGradleFile.toPath());
if (!isExcluded(exclusions, buildScriptPath) && buildGradleFile.exists()) {
gradleProject = GradleProjectBuilder.gradleProject(project);
if (buildScriptPath.toString().endsWith(".gradle")) {
gradleParser = gradleParser();
sourceFiles = gradleParser.parse(singleton(buildGradleFile.toPath()), baseDir, ctx);
Expand All @@ -1067,8 +1025,7 @@ private SourceFileStream parseGradleFiles(
.parse(singleton(buildGradleFile.toPath()), baseDir, ctx);
}
gradleFileCount++;
final GradleProject finalGradleProject = gradleProject;
sourceFiles = sourceFiles.map(sourceFile -> sourceFile.withMarkers(sourceFile.getMarkers().add(finalGradleProject)));
sourceFiles = sourceFiles.map(sourceFile -> sourceFile.withMarkers(sourceFile.getMarkers().add(gradleProject)));
alreadyParsed.add(buildGradleFile.toPath());
}
}
Expand Down Expand Up @@ -1122,7 +1079,7 @@ private SourceFileStream parseGradleFiles(

// gradle.properties
File gradlePropertiesFile = subproject.file("gradle.properties");
if (gradlePropertiesFile.exists() && gradleProject != null) {
if (gradlePropertiesFile.exists()) {
Path gradlePropertiesPath = baseDir.relativize(gradlePropertiesFile.toPath());
if (!isExcluded(exclusions, gradlePropertiesPath)) {
final GradleProject finalGradleProject = gradleProject;
Expand Down Expand Up @@ -1172,7 +1129,9 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
}
sourceFiles = Stream.concat(
sourceFiles,
gradleParser.parse(freeStandingScripts, baseDir, ctx));
gradleParser.parse(freeStandingScripts, baseDir, ctx)
.map(sourceFile -> sourceFile.withMarkers(sourceFile.getMarkers().add(gradleProject)))
);
alreadyParsed.addAll(freeStandingScripts);
gradleFileCount += freeStandingScripts.size();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,12 @@ class RewriteRunTest : RewritePluginTest {
//language=java
"""
package org.openrewrite.after;
public class HelloWorld {
public static void sayHello() {
System.out.println("Hello world");
}
public static void main(String[] args) {
sayHello();
}
Expand Down

0 comments on commit 98c7cd7

Please sign in to comment.