From e2ed07fd75491709c6a5586f854e9d210d6f5055 Mon Sep 17 00:00:00 2001 From: Joffrey Bion Date: Sun, 22 Sep 2024 13:59:52 +0200 Subject: [PATCH] Support downloading prebuilt distributions >= 1.9.20 from Maven Central Resolves: https://github.com/LouisCAD/CompleteKotlin/issues/15 --- .../internal/KotlinNativeCompilerDirs.kt | 2 +- .../internal/KotlinNativeCompilerInfo.kt | 61 +++++++++++++++---- .../internal/PlatformKlibsInstaller.kt | 16 +++-- 3 files changed, 62 insertions(+), 17 deletions(-) diff --git a/plugin/src/main/kotlin/com/louiscad/complete_kotlin/internal/KotlinNativeCompilerDirs.kt b/plugin/src/main/kotlin/com/louiscad/complete_kotlin/internal/KotlinNativeCompilerDirs.kt index d68f257..523977f 100644 --- a/plugin/src/main/kotlin/com/louiscad/complete_kotlin/internal/KotlinNativeCompilerDirs.kt +++ b/plugin/src/main/kotlin/com/louiscad/complete_kotlin/internal/KotlinNativeCompilerDirs.kt @@ -12,7 +12,7 @@ internal class KotlinNativeCompilerDirs(compilerVersion: CompilerVersion) { hostPlatform = HostPlatform.current ) - val compilerDir = KotlinNativeCompilerInfo.konanDir.resolve(compilerInfo.dependencyNameWithVersion) + val compilerDir = KotlinNativeCompilerInfo.konanDir.resolve(compilerInfo.rootDirName) val platformKlibsDir = compilerDir.resolve("klib").resolve("platform") val completeKotlinExtractionDir = platformKlibsDir.resolve("tmp-complete-kotlin") diff --git a/plugin/src/main/kotlin/com/louiscad/complete_kotlin/internal/KotlinNativeCompilerInfo.kt b/plugin/src/main/kotlin/com/louiscad/complete_kotlin/internal/KotlinNativeCompilerInfo.kt index adab3da..f8684f7 100644 --- a/plugin/src/main/kotlin/com/louiscad/complete_kotlin/internal/KotlinNativeCompilerInfo.kt +++ b/plugin/src/main/kotlin/com/louiscad/complete_kotlin/internal/KotlinNativeCompilerInfo.kt @@ -16,6 +16,8 @@ internal class KotlinNativeCompilerInfo( val konanDir = File(System.getProperty("user.home")).resolve(".konan") } + private val isPublishedToMavenCentral = compilerVersion.isAtLeast(CompilerVersion.fromString("1.9.20")) + private val kindaSimpleOsName: String = when { compilerVersion.supportsPlatformName() -> when (hostPlatform) { HostPlatform.current -> HostPlatform.platformName @@ -26,23 +28,60 @@ internal class KotlinNativeCompilerInfo( else -> hostPlatform.simpleOsName } - val dependencyName: String = "kotlin-native-prebuilt-$kindaSimpleOsName" - val dependencyNameWithVersion: String = "$dependencyName-$compilerVersion" + private val artifactId: String = "kotlin-native-prebuilt" + + /** + * The name of the root directory contained inside the Native compiler archive, or as seen in the .konan directory. + * This follows the old style pattern `kotlin-native-prebuilt--` for all versions. + * + * Example: `kotlin-native-prebuilt-windows-x86_64-2.0.20` + */ + val rootDirName: String = "$artifactId-$kindaSimpleOsName-$compilerVersion" + + /** + * The name of the Kotlin/Native prebuilt archive, without the extension. + * + * * On JetBrains CDN (for versions < 1.9.20), the pattern is `kotlin-native-prebuilt--`. + * * On Maven Central (for versions >= 1.9.20), the pattern is `kotlin-native-prebuilt--`. + * + * Examples: + * * `kotlin-native-prebuilt-1.9.10-windows-x86_64` + * * `kotlin-native-prebuilt-windows-x86_64-2.0.20` + */ + val archiveNameWithoutExtension: String = if (isPublishedToMavenCentral) { + "$artifactId-$compilerVersion-$kindaSimpleOsName" + } else { + "$artifactId-$kindaSimpleOsName-$compilerVersion" + } val useZip = hostPlatform == HostPlatform.Windows val archiveExtension: String = if (useZip) "zip" else "tar.gz" + /** + * The URL of the repository, including the path to the directory that directly contains the artifact. + * + * Examples: + * * `https://repo1.maven.org/maven2/org/jetbrains/kotlin/kotlin-native-prebuilt/2.0.20` + * * `https://complete-kotlin-prebuilt.louiscad.com/kotlin/native/builds/releases/1.9.10/macos-x86_64` + */ val repoUrl: String - get() = "https://complete-kotlin-prebuilt.louiscad.com/kotlin/native/builds".let { baseDownloadUrl -> - val releasePath = when (compilerVersion.meta) { - MetaVersion.DEV -> "dev" - else -> "releases" + get() = if (isPublishedToMavenCentral) { + "https://repo1.maven.org/maven2/org/jetbrains/kotlin/kotlin-native-prebuilt/$compilerVersion" + } else { + "https://complete-kotlin-prebuilt.louiscad.com/kotlin/native/builds".let { baseDownloadUrl -> + val releasePath = when (compilerVersion.meta) { + MetaVersion.DEV -> "dev" + else -> "releases" + } + "$baseDownloadUrl/$releasePath/$compilerVersion/$kindaSimpleOsName" } - "$baseDownloadUrl/$releasePath/$compilerVersion/$kindaSimpleOsName" } + + /** + * The direct URL to the prebuilt Kotlin/Native distribution archive. + * + * Example: `https://repo1.maven.org/maven2/org/jetbrains/kotlin/kotlin-native-prebuilt/2.0.20/kotlin-native-prebuilt-2.0.20-macos-x86_64.tar.gz` + */ val dependencyUrl: String - get() { - val dependencyFileName = "$dependencyNameWithVersion.$archiveExtension" - return "$repoUrl/$dependencyFileName" - } + get() = "$repoUrl/$archiveNameWithoutExtension.$archiveExtension" } diff --git a/plugin/src/main/kotlin/com/louiscad/complete_kotlin/internal/PlatformKlibsInstaller.kt b/plugin/src/main/kotlin/com/louiscad/complete_kotlin/internal/PlatformKlibsInstaller.kt index ed84e3e..d6679bd 100644 --- a/plugin/src/main/kotlin/com/louiscad/complete_kotlin/internal/PlatformKlibsInstaller.kt +++ b/plugin/src/main/kotlin/com/louiscad/complete_kotlin/internal/PlatformKlibsInstaller.kt @@ -31,7 +31,14 @@ internal class PlatformKlibsInstaller( return project.repositories.ivy { setUrl(repoUrl) patternLayout { - artifact("[artifact]-[revision].[ext]") + // We shove everything into 'artifact' for 2 reasons: + // 1) We want to support both the old and new archive names and they don't have the same pattern. + // 2) Ivy doesn't seem to have a concept of classifier, so we can't support the new format + // (kotlin-native-prebuilt--.) by keeping the version separate. + // Examples: + // - kotlin-native-prebuilt-1.9.10-windows-x86_64.zip + // - kotlin-native-prebuilt-windows-x86_64-2.0.20.zip + artifact("[artifact].[ext]") } metadataSources { artifact() @@ -49,8 +56,7 @@ internal class PlatformKlibsInstaller( val compilerDependency = project.dependencies.create( mapOf( - "name" to platformCompilerInfo.dependencyName, - "version" to platformCompilerInfo.compilerVersion.toString(), + "name" to platformCompilerInfo.archiveNameWithoutExtension, // corresponds to Ivy's `artifact` "ext" to platformCompilerInfo.archiveExtension ) ) @@ -76,7 +82,7 @@ internal class PlatformKlibsInstaller( logger.lifecycleWithDuration("Unpack of the magic to $compilerDirectory finished,") { project.copy { from(archiveFileTree(archive)) { - include("${platformCompilerInfo.dependencyNameWithVersion}/klib/platform/**") + include("${platformCompilerInfo.rootDirName}/klib/platform/**") } into(hostCompilerDirs.completeKotlinExtractionDir) } @@ -85,7 +91,7 @@ internal class PlatformKlibsInstaller( file.isDirectory }?.asSequence()?.map { it.name }?.toList() ?: emptyList() hostCompilerDirs.completeKotlinExtractionDir - .resolve(platformCompilerInfo.dependencyNameWithVersion) + .resolve(platformCompilerInfo.rootDirName) .resolve("klib") .resolve("platform") .listFiles { file ->