mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
[Gradle|Exec] cleanup: move Gradle build root resolution to the GradlePropertiesFile utility class
GitOrigin-RevId: fcba69f5d542a59788d68738fd7821e160a50ae6
This commit is contained in:
committed by
intellij-monorepo-bot
parent
079d78d040
commit
6048fc2e21
@@ -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? {
|
||||
|
||||
+1
-20
@@ -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
|
||||
|
||||
+30
-10
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
+5
-5
@@ -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()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user