Skip to content

Commit 6d59af6

Browse files
committed
Gate the settings.gradle path on Gradle 9+, keep -b for Gradle 8
Determine the Gradle version from the explicit useGradleVersion call or from gradle-wrapper.properties when useBuildDistribution is in play. Existing tests on Gradle 8 keep using -b. The settings.gradle path now writes with CREATE_NEW so it can never overwrite a user-supplied file, and the cleanup deletes only files we actually wrote (settingsWritten flag).
1 parent de39f3b commit 6d59af6

1 file changed

Lines changed: 59 additions & 14 deletions

File tree

rewrite-gradle-tooling-model/model/src/main/java/org/openrewrite/gradle/toolingapi/OpenRewriteModelBuilder.java

Lines changed: 59 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.nio.file.Files;
3030
import java.nio.file.Path;
3131
import java.nio.file.Paths;
32+
import java.nio.file.StandardOpenOption;
3233
import java.util.ArrayList;
3334
import java.util.List;
3435

@@ -77,12 +78,16 @@ public static OpenRewriteModel forProjectDirectory(File projectDir, @Nullable Fi
7778
*/
7879
public static OpenRewriteModel forProjectDirectory(File projectDir, @Nullable File buildFile, @Nullable String initScript) throws IOException {
7980
DefaultGradleConnector connector = (DefaultGradleConnector) GradleConnector.newConnector();
81+
String gradleVersion;
8082
if (System.getProperty("org.openrewrite.test.gradleVersion") != null) {
81-
connector.useGradleVersion(System.getProperty("org.openrewrite.test.gradleVersion"));
83+
gradleVersion = System.getProperty("org.openrewrite.test.gradleVersion");
84+
connector.useGradleVersion(gradleVersion);
8285
} else if (Files.exists(projectDir.toPath().resolve("gradle/wrapper/gradle-wrapper.properties"))) {
86+
gradleVersion = wrapperGradleVersion(projectDir.toPath().resolve("gradle/wrapper/gradle-wrapper.properties"));
8387
connector.useBuildDistribution();
8488
} else {
85-
connector.useGradleVersion("8.14.4");
89+
gradleVersion = "8.14.4";
90+
connector.useGradleVersion(gradleVersion);
8691
}
8792
connector
8893
// Uncomment to hit breakpoints inside OpenRewriteModelBuilder in unit tests
@@ -93,25 +98,32 @@ public static OpenRewriteModel forProjectDirectory(File projectDir, @Nullable Fi
9398
arguments.add("--init-script");
9499
Path init = projectDir.toPath().resolve("openrewrite-tooling.gradle").toAbsolutePath();
95100
arguments.add(init.toString());
96-
// Gradle 9 dropped -b; point at a non-conventional build file via a temporary settings.gradle.
97101
Path settings = null;
98102
if (buildFile != null && buildFile.exists()) {
99-
File abs = buildFile.getAbsoluteFile();
100-
boolean atConventionalLocation = abs.equals(new File(projectDir, "build.gradle").getAbsoluteFile()) ||
101-
abs.equals(new File(projectDir, "build.gradle.kts").getAbsoluteFile());
102-
if (!atConventionalLocation) {
103-
Path projectPath = projectDir.toPath();
104-
if (!Files.exists(projectPath.resolve("settings.gradle")) &&
105-
!Files.exists(projectPath.resolve("settings.gradle.kts"))) {
106-
settings = projectPath.resolve("settings.gradle");
103+
if (isGradle9OrLater(gradleVersion)) {
104+
// Gradle 9 dropped -b; for non-conventional build files write a temporary settings.gradle with rootProject.buildFileName.
105+
File abs = buildFile.getAbsoluteFile();
106+
boolean atConventionalLocation = abs.equals(new File(projectDir, "build.gradle").getAbsoluteFile()) ||
107+
abs.equals(new File(projectDir, "build.gradle.kts").getAbsoluteFile());
108+
if (!atConventionalLocation) {
109+
Path projectPath = projectDir.toPath();
110+
if (!Files.exists(projectPath.resolve("settings.gradle")) &&
111+
!Files.exists(projectPath.resolve("settings.gradle.kts"))) {
112+
settings = projectPath.resolve("settings.gradle");
113+
}
107114
}
115+
} else {
116+
arguments.add("-b");
117+
arguments.add(buildFile.getAbsolutePath());
108118
}
109119
}
120+
boolean settingsWritten = false;
110121
try (ProjectConnection connection = connector.connect()) {
111122
ModelBuilder<OpenRewriteModelProxy> customModelBuilder = connection.model(OpenRewriteModelProxy.class);
112123
try {
113124
if (settings != null) {
114-
Files.write(settings, ("rootProject.buildFileName = '" + buildFile.getName() + "'\n").getBytes(StandardCharsets.UTF_8));
125+
Files.write(settings, ("rootProject.buildFileName = '" + buildFile.getName() + "'\n").getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE_NEW);
126+
settingsWritten = true;
115127
}
116128
if (initScript == null) {
117129
if (System.getProperty("org.openrewrite.gradle.local.use-embedded-classpath") != null) {
@@ -137,8 +149,8 @@ public static OpenRewriteModel forProjectDirectory(File projectDir, @Nullable Fi
137149
if (Files.exists(init)) {
138150
Files.delete(init);
139151
}
140-
if (settings != null && Files.exists(settings)) {
141-
Files.delete(settings);
152+
if (settingsWritten) {
153+
Files.deleteIfExists(settings);
142154
}
143155
} catch (IOException e) {
144156
//noinspection ThrowFromFinallyBlock
@@ -148,6 +160,39 @@ public static OpenRewriteModel forProjectDirectory(File projectDir, @Nullable Fi
148160
}
149161
}
150162

163+
private static boolean isGradle9OrLater(@Nullable String version) {
164+
if (version == null) {
165+
return false;
166+
}
167+
int dot = version.indexOf('.');
168+
try {
169+
return Integer.parseInt(dot < 0 ? version : version.substring(0, dot)) >= 9;
170+
} catch (NumberFormatException e) {
171+
return false;
172+
}
173+
}
174+
175+
private static @Nullable String wrapperGradleVersion(Path wrapperProperties) {
176+
try {
177+
for (String line : Files.readAllLines(wrapperProperties, StandardCharsets.UTF_8)) {
178+
if (line.startsWith("distributionUrl=")) {
179+
int idx = line.lastIndexOf("gradle-");
180+
if (idx < 0) {
181+
return null;
182+
}
183+
int start = idx + "gradle-".length();
184+
int end = start;
185+
while (end < line.length() && (Character.isDigit(line.charAt(end)) || line.charAt(end) == '.')) {
186+
end++;
187+
}
188+
return end > start ? line.substring(start, end) : null;
189+
}
190+
}
191+
} catch (IOException ignored) {
192+
}
193+
return null;
194+
}
195+
151196
/**
152197
* Everywhere except within openrewrite/rewrite itself the plugin will be resolved from some artifact repository
153198
* But within openrewrite/rewrite it comes from a build directory.

0 commit comments

Comments
 (0)