From 6048fc2e216a91354f270010bf3cc616a67cae8b Mon Sep 17 00:00:00 2001 From: Sergei Vorobyov Date: Thu, 28 Nov 2024 13:21:52 +0100 Subject: [PATCH] [Gradle|Exec] cleanup: move Gradle build root resolution to the GradlePropertiesFile utility class GitOrigin-RevId: fcba69f5d542a59788d68738fd7821e160a50ae6 --- .../gradle/properties/GradlePropertiesFile.kt | 18 ++++++++- .../execution/GradleExecutionHelper.java | 21 +--------- .../properties/GradlePropertiesFileTest.kt | 40 ++++++++++++++----- .../GradlePropertiesFileTestCase.kt | 10 ++--- 4 files changed, 53 insertions(+), 36 deletions(-) diff --git a/plugins/gradle/src/org/jetbrains/plugins/gradle/properties/GradlePropertiesFile.kt b/plugins/gradle/src/org/jetbrains/plugins/gradle/properties/GradlePropertiesFile.kt index e604ca135d41..0890965d85fe 100644 --- a/plugins/gradle/src/org/jetbrains/plugins/gradle/properties/GradlePropertiesFile.kt +++ b/plugins/gradle/src/org/jetbrains/plugins/gradle/properties/GradlePropertiesFile.kt @@ -10,6 +10,7 @@ import org.jetbrains.plugins.gradle.util.GradleConstants import org.jetbrains.plugins.gradle.util.GradleUtil import java.nio.file.Path import java.nio.file.Paths +import kotlin.io.path.exists const val USER_HOME = "user.home" @@ -71,7 +72,22 @@ object GradlePropertiesFile { } private fun getGradlePropertiesPathInProject(projectPath: Path): Path { - return projectPath.resolve(GRADLE_PROPERTIES_FILE_NAME) + return resolveGradleProjectRoot(projectPath) + .resolve(GRADLE_PROPERTIES_FILE_NAME) + } + + private fun resolveGradleProjectRoot(projectPath: Path): Path { + var buildRoot: Path? = projectPath + while (buildRoot != null) { + for (settingsFileName in GradleConstants.KNOWN_GRADLE_SETTINGS_FILES) { + val settingsFile = buildRoot.resolve(settingsFileName) + if (settingsFile.exists()) { + return buildRoot + } + } + buildRoot = buildRoot.parent + } + return projectPath } private fun loadGradleProperties(propertiesPath: Path): GradleProperties? { diff --git a/plugins/gradle/src/org/jetbrains/plugins/gradle/service/execution/GradleExecutionHelper.java b/plugins/gradle/src/org/jetbrains/plugins/gradle/service/execution/GradleExecutionHelper.java index f3a88a2f8db8..dc09b1816072 100644 --- a/plugins/gradle/src/org/jetbrains/plugins/gradle/service/execution/GradleExecutionHelper.java +++ b/plugins/gradle/src/org/jetbrains/plugins/gradle/service/execution/GradleExecutionHelper.java @@ -42,7 +42,6 @@ import org.jetbrains.plugins.gradle.util.cmd.node.GradleCommandLineTask; import java.io.File; import java.io.InputStream; -import java.nio.file.Files; import java.nio.file.Path; import java.util.*; @@ -343,31 +342,13 @@ public final class GradleExecutionHelper { } } - private static @Nullable Path getWorkingRoot(@Nullable BuildEnvironment buildEnvironment) { + private static @Nullable Path getBuildRoot(@Nullable BuildEnvironment buildEnvironment) { if (buildEnvironment == null) { return null; } return buildEnvironment.getBuildIdentifier().getRootDir().toPath(); } - private static @Nullable Path getBuildRoot(@Nullable BuildEnvironment buildEnvironment) { - return ObjectUtils.doIfNotNull(getWorkingRoot(buildEnvironment), it -> resolveBuildRoot(it)); - } - - private static @NotNull Path resolveBuildRoot(@NotNull Path workingDirectory) { - var buildRoot = workingDirectory; - while (buildRoot != null) { - for (var settingsFileName : GradleConstants.KNOWN_GRADLE_SETTINGS_FILES) { - var settingsFile = buildRoot.resolve(settingsFileName); - if (Files.exists(settingsFile)) { - return buildRoot; - } - } - buildRoot = buildRoot.getParent(); - } - return workingDirectory; - } - private static void setupEnvironment( @NotNull LongRunningOperation operation, @NotNull GradleExecutionSettings settings diff --git a/plugins/gradle/testSources/org/jetbrains/plugins/gradle/properties/GradlePropertiesFileTest.kt b/plugins/gradle/testSources/org/jetbrains/plugins/gradle/properties/GradlePropertiesFileTest.kt index b102e04ae14b..a546d2a77f6c 100644 --- a/plugins/gradle/testSources/org/jetbrains/plugins/gradle/properties/GradlePropertiesFileTest.kt +++ b/plugins/gradle/testSources/org/jetbrains/plugins/gradle/properties/GradlePropertiesFileTest.kt @@ -1,6 +1,9 @@ // Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package org.jetbrains.plugins.gradle.properties +import com.intellij.testFramework.utils.io.createFile +import com.intellij.util.io.createDirectories +import com.intellij.util.io.createParentDirectories import org.gradle.api.logging.LogLevel import org.junit.jupiter.api.Assertions import org.junit.jupiter.api.Test @@ -9,8 +12,8 @@ class GradlePropertiesFileTest : GradlePropertiesFileTestCase() { @Test fun testEmptyProjectGradlePropertiesFile() { - createGradlePropertiesFile {} - assertGradlePropertiesFile { + createGradlePropertiesFile(projectPath) {} + assertGradlePropertiesFile(projectPath) { Assertions.assertNull(javaHomeProperty) Assertions.assertNull(logLevel) Assertions.assertNull(parallel) @@ -21,11 +24,11 @@ class GradlePropertiesFileTest : GradlePropertiesFileTestCase() { @Test fun testUnexpectedPropertiesInProjectGradlePropertiesFile() { - createGradlePropertiesFile { + createGradlePropertiesFile(projectPath) { setProperty("another.property.1", "value1") setProperty("another.property.2", "value2") } - assertGradlePropertiesFile { + assertGradlePropertiesFile(projectPath) { Assertions.assertNull(javaHomeProperty) Assertions.assertNull(logLevel) Assertions.assertNull(parallel) @@ -36,14 +39,14 @@ class GradlePropertiesFileTest : GradlePropertiesFileTestCase() { @Test fun testExpectedPropertiesInProjectGradlePropertiesFile() { - createGradlePropertiesFile { + createGradlePropertiesFile(projectPath) { setProperty(GRADLE_JAVA_HOME_PROPERTY, "javaHome") setProperty(GRADLE_LOGGING_LEVEL_PROPERTY, "info") setProperty(GRADLE_PARALLEL_PROPERTY, "true") setProperty(GRADLE_ISOLATED_PROJECTS_PROPERTY, "true") setProperty(GRADLE_JVM_OPTIONS_PROPERTY, "-Xmx20G") } - assertGradlePropertiesFile { + assertGradlePropertiesFile(projectPath) { Assertions.assertEquals("javaHome", javaHomeProperty?.value) Assertions.assertEquals(projectPropertiesPath, javaHomeProperty?.location) Assertions.assertEquals("info", logLevel?.value) @@ -60,7 +63,7 @@ class GradlePropertiesFileTest : GradlePropertiesFileTestCase() { @Test fun testMultiplePropertiesInProjectGradlePropertiesFile() { - createGradlePropertiesFile { + createGradlePropertiesFile(projectPath) { setProperty("another.property.1", "value1") setProperty(GRADLE_JAVA_HOME_PROPERTY, "value2") setProperty(GRADLE_LOGGING_LEVEL_PROPERTY, "debug") @@ -69,7 +72,7 @@ class GradlePropertiesFileTest : GradlePropertiesFileTestCase() { setProperty(GRADLE_JVM_OPTIONS_PROPERTY, "-Xmx20G") setProperty("another.property.2", "value4") } - assertGradlePropertiesFile { + assertGradlePropertiesFile(projectPath) { Assertions.assertEquals("value2", javaHomeProperty?.value) Assertions.assertEquals(projectPropertiesPath, javaHomeProperty?.location) Assertions.assertEquals("debug", logLevel?.value) @@ -86,14 +89,31 @@ class GradlePropertiesFileTest : GradlePropertiesFileTestCase() { @Test fun testInvalidLogLevelInProjectGradlePropertiesFile() { - createGradlePropertiesFile { + createGradlePropertiesFile(projectPath) { setProperty(GRADLE_LOGGING_LEVEL_PROPERTY, "invalid") } - assertGradlePropertiesFile { + assertGradlePropertiesFile(projectPath) { Assertions.assertEquals("invalid", logLevel?.value) Assertions.assertEquals(projectPropertiesPath, logLevel?.location) Assertions.assertNull(getGradleLogLevel()) } } + + @Test + fun `test Gradle properties file resolution from module root`() { + projectPath.resolve("settings.gradle") + .createParentDirectories() + .createFile() + projectPath.resolve("module") + .createDirectories() + + createGradlePropertiesFile(projectPath) { + setProperty(GRADLE_JVM_OPTIONS_PROPERTY, "-Xmx20G") + } + assertGradlePropertiesFile(projectPath.resolve("module")) { + Assertions.assertEquals("-Xmx20G", jvmOptions?.value) + Assertions.assertEquals(projectPropertiesPath, jvmOptions?.location) + } + } } \ No newline at end of file diff --git a/plugins/gradle/testSources/org/jetbrains/plugins/gradle/properties/GradlePropertiesFileTestCase.kt b/plugins/gradle/testSources/org/jetbrains/plugins/gradle/properties/GradlePropertiesFileTestCase.kt index cd767ea0f8ad..df79da67fc3e 100644 --- a/plugins/gradle/testSources/org/jetbrains/plugins/gradle/properties/GradlePropertiesFileTestCase.kt +++ b/plugins/gradle/testSources/org/jetbrains/plugins/gradle/properties/GradlePropertiesFileTestCase.kt @@ -2,7 +2,7 @@ package org.jetbrains.plugins.gradle.properties import com.intellij.testFramework.junit5.TestApplication -import com.intellij.util.io.createParentDirectories +import com.intellij.util.io.createDirectories import org.junit.jupiter.api.io.TempDir import java.nio.file.Path import java.util.* @@ -20,14 +20,14 @@ abstract class GradlePropertiesFileTestCase { val projectPropertiesPath: Path get() = projectPath.resolve(GRADLE_PROPERTIES_FILE_NAME) - fun createGradlePropertiesFile(configure: Properties.() -> Unit) { + fun createGradlePropertiesFile(projectPath: Path, configure: Properties.() -> Unit) { val properties = Properties() properties.configure() - projectPropertiesPath.createParentDirectories() - properties.store(projectPropertiesPath.outputStream(), null) + projectPath.createDirectories() + properties.store(projectPath.resolve(GRADLE_PROPERTIES_FILE_NAME).outputStream(), null) } - fun assertGradlePropertiesFile(assertion: GradleProperties. () -> Unit) { + fun assertGradlePropertiesFile(projectPath: Path, assertion: GradleProperties. () -> Unit) { val properties = GradlePropertiesFile.getProperties(null, projectPath) properties.assertion() }