From 7ab85604772915ac62e7ddac7a2714b845b30be9 Mon Sep 17 00:00:00 2001 From: "Alexander.Glukhov" Date: Mon, 26 Feb 2024 12:33:15 +0100 Subject: [PATCH] [gradle][IDEA-346728] do not modify buildscript repositories before downloading sources GitOrigin-RevId: 49774b242158fac327a5277601ec17570c6d36e5 --- .../util/GradleDependencySourceDownloader.kt | 13 ++++---- .../service/execution/GradleInitScriptUtil.kt | 22 ++++++------- .../internal/init/downloadSources.gradle | 18 ++--------- .../init/legacyDownloadSources.gradle | 31 +++++++++---------- 4 files changed, 36 insertions(+), 48 deletions(-) diff --git a/plugins/gradle/java/src/util/GradleDependencySourceDownloader.kt b/plugins/gradle/java/src/util/GradleDependencySourceDownloader.kt index 23e37406172f..8fba524b3a6e 100644 --- a/plugins/gradle/java/src/util/GradleDependencySourceDownloader.kt +++ b/plugins/gradle/java/src/util/GradleDependencySourceDownloader.kt @@ -35,7 +35,7 @@ object GradleDependencySourceDownloader { private const val INIT_SCRIPT_FILE_PREFIX = "ijDownloadSources" @JvmStatic - fun downloadSources(project: Project, executionName: @Nls String, sourceArtifactNotation: String, externalProjectPath: String) + fun downloadSources(project: Project, executionName: @Nls String, sourceArtifactNotation: String, projectPath: String) : CompletableFuture { var sourcesLocationFilePath: String var sourcesLocationFile: File @@ -51,12 +51,12 @@ object GradleDependencySourceDownloader { val taskName = "ijDownloadSources" + UUID.randomUUID().toString().substring(0, 12) val settings = ExternalSystemTaskExecutionSettings().also { it.executionName = executionName - it.externalProjectPath = externalProjectPath + it.externalProjectPath = projectPath it.taskNames = listOf(taskName) it.vmOptions = GradleSettings.getInstance(project).getGradleVmOptions() it.externalSystemIdString = GradleConstants.SYSTEM_ID.id } - val userData = prepareUserData(sourceArtifactNotation, taskName, sourcesLocationFilePath) + val userData = prepareUserData(sourceArtifactNotation, taskName, sourcesLocationFilePath, projectPath) val resultWrapper = CompletableFuture() val callback = object : TaskCallback { override fun onSuccess() { @@ -94,14 +94,15 @@ object GradleDependencySourceDownloader { return resultWrapper } - private fun prepareUserData(sourceArtifactNotation: String, taskName: String, sourcesLocationFilePath: String): UserDataHolderBase { + private fun prepareUserData(sourceArtifactNotation: String, taskName: String, sourcesLocationFilePath: String, projectPath: String) + : UserDataHolderBase { val legacyInitScript = LazyVersionSpecificInitScript( - scriptSupplier = { loadLegacyDownloadSourcesInitScript(sourceArtifactNotation, taskName, sourcesLocationFilePath) }, + scriptSupplier = { loadLegacyDownloadSourcesInitScript(sourceArtifactNotation, taskName, sourcesLocationFilePath, projectPath) }, filePrefix = INIT_SCRIPT_FILE_PREFIX, isApplicable = { GRADLE_5_6 > it } ) val initScript = LazyVersionSpecificInitScript( - scriptSupplier = { loadDownloadSourcesInitScript(sourceArtifactNotation, taskName, sourcesLocationFilePath) }, + scriptSupplier = { loadDownloadSourcesInitScript(sourceArtifactNotation, taskName, sourcesLocationFilePath, projectPath) }, filePrefix = INIT_SCRIPT_FILE_PREFIX, isApplicable = { GRADLE_5_6 <= it } ) diff --git a/plugins/gradle/src/org/jetbrains/plugins/gradle/service/execution/GradleInitScriptUtil.kt b/plugins/gradle/src/org/jetbrains/plugins/gradle/service/execution/GradleInitScriptUtil.kt index b0b7897a55d7..10cf9b1872a3 100644 --- a/plugins/gradle/src/org/jetbrains/plugins/gradle/service/execution/GradleInitScriptUtil.kt +++ b/plugins/gradle/src/org/jetbrains/plugins/gradle/service/execution/GradleInitScriptUtil.kt @@ -38,16 +38,13 @@ fun createIdeaPluginConfiguratorInitScript() : Path { return createInitScript(IDEA_PLUGIN_CONFIGURATOR_SCRIPT_NAME, initScript) } -fun loadDownloadSourcesInitScript(dependencyNotation: String, - taskName: String, - downloadTarget: String): String = loadDownloadSourcesInitScript( - "/org/jetbrains/plugins/gradle/tooling/internal/init/downloadSources.gradle", dependencyNotation, taskName, downloadTarget) +fun loadDownloadSourcesInitScript(dependencyNotation: String, taskName: String, downloadTarget: String, projectPath: String): String = + loadDownloadSourcesInitScript("/org/jetbrains/plugins/gradle/tooling/internal/init/downloadSources.gradle", dependencyNotation, + taskName, downloadTarget, projectPath) -fun loadLegacyDownloadSourcesInitScript(dependencyNotation: String, - taskName: String, - downloadTarget: String): String = loadDownloadSourcesInitScript( - "/org/jetbrains/plugins/gradle/tooling/internal/init/legacyDownloadSources.gradle", dependencyNotation, - taskName, downloadTarget) +fun loadLegacyDownloadSourcesInitScript(dependencyNotation: String, taskName: String, downloadTarget: String, projectPath: String): String = + loadDownloadSourcesInitScript("/org/jetbrains/plugins/gradle/tooling/internal/init/legacyDownloadSources.gradle", dependencyNotation, + taskName, downloadTarget, projectPath) fun loadTaskInitScript( projectPath: String, @@ -204,10 +201,13 @@ private fun isContentEquals(path: Path, content: ByteArray): Boolean { content.contentEquals(path.readBytes()) } -private fun loadDownloadSourcesInitScript(path: String, dependencyNotation: String, taskName: String, downloadTarget: String): String { +private fun loadDownloadSourcesInitScript( + path: String, dependencyNotation: String, taskName: String, downloadTarget: String, projectPath: String +): String { return loadInitScript(path, mapOf( "DEPENDENCY_NOTATION" to dependencyNotation.toGroovyStringLiteral(), "TARGET_PATH" to downloadTarget.toGroovyStringLiteral(), - "GRADLE_TASK_NAME" to taskName.toGroovyStringLiteral() + "GRADLE_TASK_NAME" to taskName.toGroovyStringLiteral(), + "GRADLE_PROJECT_PATH" to projectPath.toGroovyStringLiteral(), )) } diff --git a/plugins/gradle/tooling-extension-impl/src/org/jetbrains/plugins/gradle/tooling/internal/init/downloadSources.gradle b/plugins/gradle/tooling-extension-impl/src/org/jetbrains/plugins/gradle/tooling/internal/init/downloadSources.gradle index 7bbb3df7bb9a..37cedd80e5c2 100644 --- a/plugins/gradle/tooling-extension-impl/src/org/jetbrains/plugins/gradle/tooling/internal/init/downloadSources.gradle +++ b/plugins/gradle/tooling-extension-impl/src/org/jetbrains/plugins/gradle/tooling/internal/init/downloadSources.gradle @@ -7,6 +7,8 @@ interface Properties { public static final String targetPath = TARGET_PATH //noinspection GrUnresolvedAccess public static final String gradleTaskName = GRADLE_TASK_NAME + //noinspection GrUnresolvedAccess + public static final String projectPath = GRADLE_PROJECT_PATH } // Requires Gradle 5.6+ @@ -40,22 +42,8 @@ abstract class IjDownloadTask extends DefaultTask { } } -Set projectRepositories() { - def target = new HashSet() - allprojects { - target.addAll(it.repositories) - } - return target -} - -gradle.projectsEvaluated { - rootProject - .repositories - .addAll(projectRepositories()) -} - afterProject { - if (it.name == rootProject.name) { + if (it.projectDir.path == Properties.projectPath) { Configuration configuration = it.configurations.create('downloadSources_' + UUID.randomUUID()) configuration.transitive = false it.dependencies.add(configuration.name, Properties.dependencyNotation) diff --git a/plugins/gradle/tooling-extension-impl/src/org/jetbrains/plugins/gradle/tooling/internal/init/legacyDownloadSources.gradle b/plugins/gradle/tooling-extension-impl/src/org/jetbrains/plugins/gradle/tooling/internal/init/legacyDownloadSources.gradle index cb88fb48d542..4b81374dffa8 100644 --- a/plugins/gradle/tooling-extension-impl/src/org/jetbrains/plugins/gradle/tooling/internal/init/legacyDownloadSources.gradle +++ b/plugins/gradle/tooling-extension-impl/src/org/jetbrains/plugins/gradle/tooling/internal/init/legacyDownloadSources.gradle @@ -7,32 +7,31 @@ interface Properties { public static final String targetPath = TARGET_PATH //noinspection GrUnresolvedAccess public static final String gradleTaskName = GRADLE_TASK_NAME + //noinspection GrUnresolvedAccess + public static final String projectPath = GRADLE_PROJECT_PATH } -// Solution for Gradle 3.0...5.5 -Set projectRepositories() { - def target = new HashSet() - allprojects { - target.addAll(it.repositories) +allprojects { + afterEvaluate { + if (it.projectDir.path == Properties.projectPath) { + configureProject(it) + } } - return target } -gradle.projectsEvaluated { - rootProject - .repositories - .addAll(projectRepositories()) - rootProject.tasks +private static void configureProject(Project project) { + project + .tasks .create(Properties.gradleTaskName) { doLast { def configuration - def repository = rootProject.repositories.toList().find { + def repository = project.repositories.toList().find { logger.lifecycle('Attempt to download sources from ' + it.name) project.repositories.clear() project.repositories.add(it) - configuration = rootProject.configurations.create('downloadSourcesFrom_' + UUID.randomUUID()) + configuration = project.configurations.create('downloadSourcesFrom_' + UUID.randomUUID()) configuration.transitive = false - rootProject.dependencies.add(configuration.name, Properties.dependencyNotation) + project.dependencies.add(configuration.name, Properties.dependencyNotation) def files = null try { files = configuration.resolvedConfiguration.lenientConfiguration.getFiles() @@ -42,9 +41,9 @@ gradle.projectsEvaluated { return files && !files.isEmpty() } if (!repository) { - configuration = rootProject.configurations.create('downloadSources_' + UUID.randomUUID()) + configuration = project.configurations.create('downloadSources_' + UUID.randomUUID()) configuration.transitive = false - rootProject.dependencies.add(configuration.name, Properties.dependencyNotation) + project.dependencies.add(configuration.name, Properties.dependencyNotation) configuration.resolve() } def sourcesPath = configuration?.singleFile?.path