From 14fac83e2b18768f63fac56d188079ba7d80f1b7 Mon Sep 17 00:00:00 2001 From: Ilya Goncharov Date: Tue, 4 Aug 2020 17:53:39 +0300 Subject: [PATCH] [Gradle, JS] Intersect of NpmRange ^KT-41054 fixed --- .../kotlin/gradle/targets/js/npm/NpmRange.kt | 78 ++++++++++++++++++- .../kotlin/gradle/targets/js/npm/semver.kt | 22 ++++++ 2 files changed, 97 insertions(+), 3 deletions(-) diff --git a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/targets/js/npm/NpmRange.kt b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/targets/js/npm/NpmRange.kt index 61ce1bb3d9d..34357fdb0f9 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/targets/js/npm/NpmRange.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/targets/js/npm/NpmRange.kt @@ -8,16 +8,88 @@ package org.jetbrains.kotlin.gradle.targets.js.npm import com.github.gundy.semver4j.model.Version data class NpmRange( - val startVersion: Version? = null, + val startVersion: SemVer? = null, val startInclusive: Boolean = false, - val endVersion: Version? = null, + val endVersion: SemVer? = null, val endInclusive: Boolean = false ) val NONE_RANGE = NpmRange( startVersion = Version.fromString(NONE_VERSION) + .toSemVer() ) -infix operator fun NpmRange.plus(other: NpmRange) { +infix fun NpmRange.or(other: NpmRange): List { + val startVersion = min(this.startVersion, other.startVersion) + val endVersion = max(this.endVersion, other.endVersion) + + return NpmRange( + startVersion = startVersion, + startInclusive = if (startVersion == other.startVersion) other.startInclusive else this.startInclusive, + endVersion = endVersion, + endInclusive = if (startVersion == other.startVersion) other.startInclusive else this.startInclusive + ) +} + +infix fun NpmRange.intersect(other: NpmRange): NpmRange? { + if (!isIntersect(other)) return null + + val startVersion = max(this.startVersion, other.startVersion) + + val endVersion = min(this.endVersion, other.endVersion) + + return NpmRange( + startVersion = startVersion, + startInclusive = if (startVersion == this.startVersion) this.startInclusive else other.startInclusive, + endVersion = endVersion, + endInclusive = if (endVersion == this.endVersion) this.endInclusive else other.endInclusive + ) +} + +infix fun NpmRange.isIntersect(other: NpmRange): Boolean { + if (this.startVersion == null) { + return other.startVersion == null || this.endVersion == null || other.startVersion < this.endVersion || + (other.startInclusive && this.endInclusive && other.startVersion == this.endVersion) + } + + if (other.startVersion == null) { + return other.endVersion == null || this.startVersion < other.endVersion || + (this.startInclusive && other.endInclusive && this.startVersion == other.endVersion) + } + + if (this.endVersion == null) { + return other.endVersion == null || other.endVersion > this.startVersion || + (this.startInclusive && other.endInclusive && other.endVersion == this.startVersion) + } + + if (other.endVersion == null) { + return this.endVersion > other.startVersion || + (other.startInclusive && this.endInclusive && this.endVersion == other.startVersion) + } + + if (other.startVersion < this.startVersion) { + return this.startVersion < other.endVersion || + (this.startInclusive && other.endInclusive && this.startVersion == other.endVersion) + } + + if (this.startVersion < other.startVersion) { + return other.startVersion < this.endVersion || + (other.startInclusive && this.endInclusive && other.startVersion == this.endVersion) + } + + if (this.endVersion < other.endVersion) { + return this.startVersion < other.endVersion || + (this.startInclusive && other.endInclusive && this.startVersion == other.endVersion) + } + + if (this.startVersion < other.startVersion) { + return other.startVersion < this.endVersion || + (other.startInclusive && this.endInclusive && other.startVersion == this.endVersion) + } + + return this.startInclusive && other.startInclusive && this.startVersion == other.startVersion || + this.startInclusive && other.endInclusive && this.startVersion == other.endVersion || + this.endInclusive && other.startInclusive && this.endVersion == other.startVersion || + this.endInclusive && other.endInclusive && this.endVersion == other.endVersion } \ No newline at end of file diff --git a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/targets/js/npm/semver.kt b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/targets/js/npm/semver.kt index f538a1fa946..f1da8f567f8 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/targets/js/npm/semver.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/targets/js/npm/semver.kt @@ -5,6 +5,7 @@ package org.jetbrains.kotlin.gradle.targets.js.npm +import com.github.gundy.semver4j.model.Version import java.math.BigInteger data class SemVer( @@ -170,4 +171,25 @@ private fun String.foldDelimiters(): String { } } return result.toString() +} + +fun Version.toSemVer(): SemVer = + SemVer( + major.toBigInteger(), + minor.toBigInteger(), + patch.toBigInteger(), + preRelease = preReleaseIdentifiers.joinToString("."), + build = buildIdentifiers.joinToString(".") + ) + +fun min(a: SemVer?, b: SemVer?): SemVer? { + if (a == null || b == null) return null + + return if (a < b) a else b +} + +fun max(a: SemVer?, b: SemVer?): SemVer? { + if (a == null || b == null) return null + + return if (a > b) a else b } \ No newline at end of file