From 5c6af6559a1a84e17954c60eb0140a515bc03ed0 Mon Sep 17 00:00:00 2001 From: Pavel Punegov Date: Mon, 26 Sep 2022 17:58:04 +0200 Subject: [PATCH] [K/N][gradle] Download bundles from Maven repository Adds property to make Gradle download Native builds from maven. The URL can be specified with another property or with standard Gradle `repository { maven(...) } ` repository management blocks. --- .../gradle/plugin/PropertiesProvider.kt | 9 ++ .../native/NativeCompilerDownloader.kt | 82 +++++++++++++------ 2 files changed, 68 insertions(+), 23 deletions(-) diff --git a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/PropertiesProvider.kt b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/PropertiesProvider.kt index c3ef26187e2..20de426083a 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/PropertiesProvider.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/PropertiesProvider.kt @@ -277,6 +277,15 @@ internal class PropertiesProvider private constructor(private val project: Proje val nativeBaseDownloadUrl: String get() = this.property("kotlin.native.distribution.baseDownloadUrl") ?: NativeCompilerDownloader.BASE_DOWNLOAD_URL + /** + * Allows downloading Kotlin/Native distribution with maven. + * + * Makes downloader search for bundles in maven repositories. + * Property `kotlin.native.distribution.baseDownloadUrl` allows to specify custom maven url. + */ + val nativeDownloadFromMaven: Boolean + get() = this.booleanProperty("kotlin.native.distribution.downloadFromMaven") ?: false + /** * A property that was used to choose a restricted distribution in 1.3. */ diff --git a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/native/NativeCompilerDownloader.kt b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/native/NativeCompilerDownloader.kt index ab57bcfef1e..547a0d9a54f 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/native/NativeCompilerDownloader.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/native/NativeCompilerDownloader.kt @@ -40,10 +40,11 @@ class NativeCompilerDownloader( } internal const val BASE_DOWNLOAD_URL = "https://download.jetbrains.com/kotlin/native/builds" + internal const val KOTLIN_GROUP_ID = "org.jetbrains.kotlin" } val compilerDirectory: File - get() = DependencyDirectories.localKonanDir.resolve(dependencyNameWithVersion) + get() = DependencyDirectories.localKonanDir.resolve(dependencyNameWithOsAndVersion) private val logger: Logger get() = project.logger @@ -66,17 +67,17 @@ class NativeCompilerDownloader( get() { val dependencySuffix = distributionType.suffix return if (dependencySuffix != null) { - "kotlin-native-$dependencySuffix-$simpleOsName" + "kotlin-native-$dependencySuffix" } else { - "kotlin-native-$simpleOsName" + "kotlin-native" } } - private val dependencyNameWithVersion: String - get() = "$dependencyName-$compilerVersion" + private val dependencyNameWithOsAndVersion: String + get() = "$dependencyName-$simpleOsName-$compilerVersion" private val dependencyFileName: String - get() = "$dependencyNameWithVersion.$archiveExtension" + get() = "$dependencyNameWithOsAndVersion.$archiveExtension" private val useZip get() = HostManager.hostIsMingw @@ -107,34 +108,69 @@ class NativeCompilerDownloader( } } + private fun setupMavenRepo(repoUrl: String): ArtifactRepository { + return project.repositories.maven { repo -> + repo.setUrl(repoUrl) + repo.metadataSources { + it.artifact() + } + } + } + private fun removeRepo(repo: ArtifactRepository) { project.repositories.remove(repo) } private fun downloadAndExtract() { - val repoUrl = buildString { - append("${kotlinProperties.nativeBaseDownloadUrl}/") - append(if (compilerVersion.meta == MetaVersion.DEV) "dev/" else "releases/") - append("$compilerVersion/") - append(simpleOsName) + val repoUrl = if (kotlinProperties.nativeDownloadFromMaven) { + kotlinProperties.nativeBaseDownloadUrl.takeIf { it != BASE_DOWNLOAD_URL } + } else { + buildString { + append("${kotlinProperties.nativeBaseDownloadUrl}/") + append(if (compilerVersion.meta == MetaVersion.DEV) "dev/" else "releases/") + append("$compilerVersion/") + append(simpleOsName) + } } - val dependencyUrl = "$repoUrl/$dependencyFileName" + val dependencyUrl = if (repoUrl != null) "$repoUrl/$dependencyFileName" else "maven://$dependencyFileName" - val repo = setupRepo(repoUrl) + val repo = if (repoUrl != null) { + if (kotlinProperties.nativeDownloadFromMaven) { + setupMavenRepo(repoUrl) + } else { + setupRepo(repoUrl) + } + } else null - val compilerDependency = project.dependencies.create( - mapOf( - "name" to dependencyName, - "version" to compilerVersion.toString(), - "ext" to archiveExtension + val compilerDependency = if (kotlinProperties.nativeDownloadFromMaven) { + project.dependencies.create( + mapOf( + "group" to KOTLIN_GROUP_ID, + "name" to dependencyName, + "version" to compilerVersion.toString(), + "classifier" to simpleOsName, + "ext" to archiveExtension + ) ) - ) + } else { + project.dependencies.create( + mapOf( + "name" to "$dependencyName-$simpleOsName", + "version" to compilerVersion.toString(), + "ext" to archiveExtension + ) + ) + } val configuration = project.configurations.detachedConfiguration(compilerDependency) logger.lifecycle("\nPlease wait while Kotlin/Native compiler $compilerVersion is being installed.") - val suffix = project.probeRemoteFileLength(dependencyUrl, probingTimeoutMs = 200)?.let { " (${formatContentLength(it)})" }.orEmpty() - logger.lifecycle("Download $dependencyUrl$suffix") + if (!kotlinProperties.nativeDownloadFromMaven) { + val suffix = project.probeRemoteFileLength(dependencyUrl, probingTimeoutMs = 200) + ?.let { " (${formatContentLength(it)})" } + .orEmpty() + logger.lifecycle("Download $dependencyUrl$suffix") + } val archive = logger.lifecycleWithDuration("Download $dependencyUrl finished,") { configuration.files.single() } @@ -151,7 +187,7 @@ class NativeCompilerDownloader( it.from(archiveFileTree(archive)) it.into(tmpDir) } - val compilerTmp = tmpDir.resolve(dependencyNameWithVersion) + val compilerTmp = tmpDir.resolve(dependencyNameWithOsAndVersion) if (!compilerTmp.renameTo(compilerDirectory)) { project.copy { it.from(compilerTmp) @@ -164,7 +200,7 @@ class NativeCompilerDownloader( } } - removeRepo(repo) + if (repo != null) removeRepo(repo) } fun downloadIfNeeded() {