[gradle][IDEA-346728] do not modify buildscript repositories before downloading sources

GitOrigin-RevId: 49774b242158fac327a5277601ec17570c6d36e5
This commit is contained in:
Alexander.Glukhov
2024-02-26 12:33:15 +01:00
committed by intellij-monorepo-bot
parent ea7146d0a8
commit 7ab8560477
4 changed files with 36 additions and 48 deletions

View File

@@ -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<File> {
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<File>()
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 }
)

View File

@@ -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(),
))
}

View File

@@ -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<? extends ArtifactRepository> 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)

View File

@@ -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<? extends ArtifactRepository> 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