[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.
This commit is contained in:
Pavel Punegov
2022-09-26 17:58:04 +02:00
committed by Space Team
parent 25f65aff3a
commit 5c6af6559a
2 changed files with 68 additions and 23 deletions
@@ -277,6 +277,15 @@ internal class PropertiesProvider private constructor(private val project: Proje
val nativeBaseDownloadUrl: String val nativeBaseDownloadUrl: String
get() = this.property("kotlin.native.distribution.baseDownloadUrl") ?: NativeCompilerDownloader.BASE_DOWNLOAD_URL 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. * A property that was used to choose a restricted distribution in 1.3.
*/ */
@@ -40,10 +40,11 @@ class NativeCompilerDownloader(
} }
internal const val BASE_DOWNLOAD_URL = "https://download.jetbrains.com/kotlin/native/builds" 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 val compilerDirectory: File
get() = DependencyDirectories.localKonanDir.resolve(dependencyNameWithVersion) get() = DependencyDirectories.localKonanDir.resolve(dependencyNameWithOsAndVersion)
private val logger: Logger private val logger: Logger
get() = project.logger get() = project.logger
@@ -66,17 +67,17 @@ class NativeCompilerDownloader(
get() { get() {
val dependencySuffix = distributionType.suffix val dependencySuffix = distributionType.suffix
return if (dependencySuffix != null) { return if (dependencySuffix != null) {
"kotlin-native-$dependencySuffix-$simpleOsName" "kotlin-native-$dependencySuffix"
} else { } else {
"kotlin-native-$simpleOsName" "kotlin-native"
} }
} }
private val dependencyNameWithVersion: String private val dependencyNameWithOsAndVersion: String
get() = "$dependencyName-$compilerVersion" get() = "$dependencyName-$simpleOsName-$compilerVersion"
private val dependencyFileName: String private val dependencyFileName: String
get() = "$dependencyNameWithVersion.$archiveExtension" get() = "$dependencyNameWithOsAndVersion.$archiveExtension"
private val useZip private val useZip
get() = HostManager.hostIsMingw 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) { private fun removeRepo(repo: ArtifactRepository) {
project.repositories.remove(repo) project.repositories.remove(repo)
} }
private fun downloadAndExtract() { private fun downloadAndExtract() {
val repoUrl = buildString { val repoUrl = if (kotlinProperties.nativeDownloadFromMaven) {
append("${kotlinProperties.nativeBaseDownloadUrl}/") kotlinProperties.nativeBaseDownloadUrl.takeIf { it != BASE_DOWNLOAD_URL }
append(if (compilerVersion.meta == MetaVersion.DEV) "dev/" else "releases/") } else {
append("$compilerVersion/") buildString {
append(simpleOsName) 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( val compilerDependency = if (kotlinProperties.nativeDownloadFromMaven) {
mapOf( project.dependencies.create(
"name" to dependencyName, mapOf(
"version" to compilerVersion.toString(), "group" to KOTLIN_GROUP_ID,
"ext" to archiveExtension "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) val configuration = project.configurations.detachedConfiguration(compilerDependency)
logger.lifecycle("\nPlease wait while Kotlin/Native compiler $compilerVersion is being installed.") logger.lifecycle("\nPlease wait while Kotlin/Native compiler $compilerVersion is being installed.")
val suffix = project.probeRemoteFileLength(dependencyUrl, probingTimeoutMs = 200)?.let { " (${formatContentLength(it)})" }.orEmpty() if (!kotlinProperties.nativeDownloadFromMaven) {
logger.lifecycle("Download $dependencyUrl$suffix") val suffix = project.probeRemoteFileLength(dependencyUrl, probingTimeoutMs = 200)
?.let { " (${formatContentLength(it)})" }
.orEmpty()
logger.lifecycle("Download $dependencyUrl$suffix")
}
val archive = logger.lifecycleWithDuration("Download $dependencyUrl finished,") { val archive = logger.lifecycleWithDuration("Download $dependencyUrl finished,") {
configuration.files.single() configuration.files.single()
} }
@@ -151,7 +187,7 @@ class NativeCompilerDownloader(
it.from(archiveFileTree(archive)) it.from(archiveFileTree(archive))
it.into(tmpDir) it.into(tmpDir)
} }
val compilerTmp = tmpDir.resolve(dependencyNameWithVersion) val compilerTmp = tmpDir.resolve(dependencyNameWithOsAndVersion)
if (!compilerTmp.renameTo(compilerDirectory)) { if (!compilerTmp.renameTo(compilerDirectory)) {
project.copy { project.copy {
it.from(compilerTmp) it.from(compilerTmp)
@@ -164,7 +200,7 @@ class NativeCompilerDownloader(
} }
} }
removeRepo(repo) if (repo != null) removeRepo(repo)
} }
fun downloadIfNeeded() { fun downloadIfNeeded() {