|
| 1 | +/* |
| 2 | + * Copyright 2010-2025 JetBrains s.r.o. and Kotlin Programming Language contributors. |
| 3 | + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. |
| 4 | + */ |
| 5 | + |
| 6 | +package org.jetbrains.kotlin.buildtools.api.internal |
| 7 | + |
| 8 | +import java.io.Serializable |
| 9 | + |
| 10 | +// Copied from KotlinToolingVersion |
| 11 | +@OptIn(ExperimentalStdlibApi::class) |
| 12 | +internal class KotlinCompilerVersion( |
| 13 | + val major: Int, |
| 14 | + val minor: Int, |
| 15 | + val patch: Int, |
| 16 | + val classifier: String? |
| 17 | +) : Comparable<KotlinCompilerVersion>, Serializable { |
| 18 | + |
| 19 | + enum class Maturity { |
| 20 | + SNAPSHOT, DEV, MILESTONE, ALPHA, BETA, RC, STABLE |
| 21 | + } |
| 22 | + |
| 23 | + val maturity: Maturity = run { |
| 24 | + val classifier = this.classifier?.lowercase() |
| 25 | + when { |
| 26 | + classifier == null || classifier.matches(Regex("""(release-)?\d+""")) -> Maturity.STABLE |
| 27 | + classifier == "snapshot" -> Maturity.SNAPSHOT |
| 28 | + classifier.matches(Regex("""(rc)(\d*)?(-release)?(-?\d+)?""")) -> Maturity.RC |
| 29 | + classifier.matches(Regex("""beta(\d*)?(-release)?(-?\d+)?""")) -> Maturity.BETA |
| 30 | + classifier.matches(Regex("""alpha(\d*)?(-release)?(-?\d+)?""")) -> Maturity.ALPHA |
| 31 | + classifier.matches(Regex("""m\d+(-release)?(-\d+)?""")) -> Maturity.MILESTONE |
| 32 | + else -> Maturity.DEV |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + override fun compareTo(other: KotlinCompilerVersion): Int { |
| 37 | + if (this == other) return 0 |
| 38 | + (this.major - other.major).takeIf { it != 0 }?.let { return it } |
| 39 | + (this.minor - other.minor).takeIf { it != 0 }?.let { return it } |
| 40 | + (this.patch - other.patch).takeIf { it != 0 }?.let { return it } |
| 41 | + (this.maturity.ordinal - other.maturity.ordinal).takeIf { it != 0 }?.let { return it } |
| 42 | + |
| 43 | + if (this.classifier == null && other.classifier != null) { |
| 44 | + /* eg. 1.6.20 > 1.6.20-200 */ |
| 45 | + return 1 |
| 46 | + } |
| 47 | + |
| 48 | + if (this.classifier != null && other.classifier == null) { |
| 49 | + /* e.g. 1.6.20-200 < 1.6.20 */ |
| 50 | + return -1 |
| 51 | + } |
| 52 | + |
| 53 | + val thisClassifierNumber = this.classifierNumber |
| 54 | + val otherClassifierNumber = other.classifierNumber |
| 55 | + if (thisClassifierNumber != null && otherClassifierNumber != null) { |
| 56 | + (thisClassifierNumber - otherClassifierNumber).takeIf { it != 0 }?.let { return it } |
| 57 | + } |
| 58 | + |
| 59 | + if (thisClassifierNumber != null && otherClassifierNumber == null) { |
| 60 | + /* e.g. 1.6.20-rc1 > 1.6.20-rc */ |
| 61 | + return 1 |
| 62 | + } |
| 63 | + |
| 64 | + if (thisClassifierNumber == null && otherClassifierNumber != null) { |
| 65 | + /* e.g. 1.6.20-rc < 1.6.20-rc1 */ |
| 66 | + return -1 |
| 67 | + } |
| 68 | + |
| 69 | + val thisBuildNumber = this.buildNumber |
| 70 | + val otherBuildNumber = other.buildNumber |
| 71 | + if (thisBuildNumber != null && otherBuildNumber != null) { |
| 72 | + (thisBuildNumber - otherBuildNumber).takeIf { it != 0 }?.let { return it } |
| 73 | + } |
| 74 | + |
| 75 | + if (thisBuildNumber == null && otherBuildNumber != null) { |
| 76 | + /* e.g. 1.6.20-M1 > 1.6.20-M1-200 */ |
| 77 | + return 1 |
| 78 | + } |
| 79 | + |
| 80 | + if (thisBuildNumber != null && otherBuildNumber == null) { |
| 81 | + /* e.g. 1.6.20-M1-200 < 1.6.20-M1 */ |
| 82 | + return -1 |
| 83 | + } |
| 84 | + |
| 85 | + return 0 |
| 86 | + } |
| 87 | + |
| 88 | + override fun equals(other: Any?): Boolean { |
| 89 | + if (this === other) return true |
| 90 | + if (other !is KotlinCompilerVersion) return false |
| 91 | + if (this.major != other.major) return false |
| 92 | + if (this.minor != other.minor) return false |
| 93 | + if (this.patch != other.patch) return false |
| 94 | + if (this.classifier?.lowercase() != other.classifier?.lowercase()) return false |
| 95 | + return true |
| 96 | + } |
| 97 | + |
| 98 | + override fun hashCode(): Int { |
| 99 | + var result = major |
| 100 | + result = 31 * result + minor |
| 101 | + result = 31 * result + patch |
| 102 | + result = 31 * result + (classifier?.hashCode() ?: 0) |
| 103 | + return result |
| 104 | + } |
| 105 | + |
| 106 | + override fun toString(): String { |
| 107 | + return "$major.$minor.$patch" + if (classifier != null) "-$classifier" else "" |
| 108 | + } |
| 109 | + |
| 110 | + private val classifierNumber: Int? |
| 111 | + get() { |
| 112 | + if (classifier == null) return null |
| 113 | + |
| 114 | + /* |
| 115 | + dev builds allow additional wildcards in the version (like 1.6.20-dev-myWildcard21-510) |
| 116 | + In this case, 510 will be the buildNumber, but there is still no associated classifierNumber. |
| 117 | + In order to keep the regex below simple, we fast path out here, since we know that |
| 118 | + dev builds never carry classifier numbers |
| 119 | + */ |
| 120 | + if (maturity == KotlinCompilerVersion.Maturity.DEV) return null |
| 121 | + |
| 122 | + /* |
| 123 | + Classifiers with only a buildNumber assigned |
| 124 | + */ |
| 125 | + val buildNumberOnlyClassifierRegex = Regex("\\d+") |
| 126 | + if (buildNumberOnlyClassifierRegex.matches(classifier)) { |
| 127 | + return null |
| 128 | + } |
| 129 | + |
| 130 | + |
| 131 | + val classifierRegex = Regex("""(.+?)(\d*)?(-release)?-?(\d*)?""") |
| 132 | + val classifierMatch = classifierRegex.matchEntire(classifier) ?: return null |
| 133 | + return classifierMatch.groupValues.getOrNull(2)?.toIntOrNull() |
| 134 | + } |
| 135 | + |
| 136 | + private val buildNumber: Int? |
| 137 | + get() { |
| 138 | + if (classifier == null) return null |
| 139 | + |
| 140 | + /* |
| 141 | + Handle classifiers that only consist of version + build number. This is used for stable releases |
| 142 | + like: |
| 143 | + 1.6.20-1 |
| 144 | + 1.6.20-22 |
| 145 | + 1.6. |
| 146 | + */ |
| 147 | + val buildNumberOnlyClassifierRegex = Regex("\\d+") |
| 148 | + if (buildNumberOnlyClassifierRegex.matches(classifier)) { |
| 149 | + return classifier.toIntOrNull() |
| 150 | + } |
| 151 | + |
| 152 | + val classifierRegex = Regex("""(.+?)(\d*)?(-release)?-?(\d*)?""") |
| 153 | + val classifierMatch = classifierRegex.matchEntire(classifier) ?: return null |
| 154 | + return classifierMatch.groupValues.getOrNull(4)?.toIntOrNull() |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +internal fun KotlinCompilerVersion(kotlinVersionString: String): KotlinCompilerVersion { |
| 159 | + val baseVersion = kotlinVersionString.split("-", limit = 2)[0] |
| 160 | + val classifier = kotlinVersionString.split("-", limit = 2).getOrNull(1) |
| 161 | + |
| 162 | + val baseVersionSplit = baseVersion.split(".") |
| 163 | + |
| 164 | + val majorVersion = baseVersionSplit[0].toIntOrNull() |
| 165 | + val minorVersion = baseVersionSplit.getOrNull(1)?.toIntOrNull() |
| 166 | + |
| 167 | + if (majorVersion == null || minorVersion == null) { |
| 168 | + throw IllegalArgumentException("Invalid Kotlin version: $kotlinVersionString (Failed parsing major/minor version)") |
| 169 | + } |
| 170 | + |
| 171 | + return KotlinCompilerVersion( |
| 172 | + major = majorVersion, |
| 173 | + minor = minorVersion, |
| 174 | + patch = baseVersionSplit.getOrNull(2)?.toIntOrNull() ?: 0, |
| 175 | + classifier = classifier |
| 176 | + ) |
| 177 | +} |
0 commit comments