[Gradle, JS] Use NpmVersionConstraint

^KT-41054 fixed
This commit is contained in:
Ilya Goncharov
2020-07-31 19:06:01 +03:00
parent bb569f36b1
commit 768b9a0340
2 changed files with 38 additions and 3 deletions
@@ -23,7 +23,7 @@ class NpmDependencyConstraint(
private var reason: String? = null
private val versionConstraint = DefaultMutableVersionConstraint(version)
private val versionConstraint = NpmVersionConstraint(version)
override fun getGroup(): String? = null
@@ -62,7 +62,7 @@ class NpmDependencyConstraint(
override fun version(configureAction: Action<in MutableVersionConstraint>) {
configureAction.execute(versionConstraint)
val nodeJs = NodeJsRootPlugin.apply(project.rootProject)
nodeJs.npmResolutionManager.putNpmResolution(path, version)
nodeJs.npmResolutionManager.putNpmResolution(path, versionConstraint.toSemVer())
}
override fun getReason(): String? {
@@ -75,7 +75,7 @@ class NpmDependencyConstraint(
override fun getVersionConstraint(): VersionConstraint {
val nodeJs = NodeJsRootPlugin.apply(project.rootProject)
nodeJs.npmResolutionManager.putNpmResolution(path, version)
nodeJs.npmResolutionManager.putNpmResolution(path, versionConstraint.toSemVer())
return versionConstraint
}
@@ -0,0 +1,35 @@
/*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.gradle.targets.js.npm
import com.github.gundy.semver4j.SemVer
import org.gradle.api.internal.artifacts.dependencies.DefaultMutableVersionConstraint
class NpmVersionConstraint(
version: String
) : DefaultMutableVersionConstraint(version) {
override fun rejectAll() {
super.rejectAll()
reject("0.0.0")
}
fun toSemVer(): String {
if (requiredVersion.isEmpty()) {
return preferredVersion
}
if (strictVersion.isNotEmpty()) {
return strictVersion
}
val requiredRange = "^$requiredVersion"
return if (SemVer.valid(requiredRange) && SemVer.satisfies(requiredVersion, requiredRange)) {
requiredRange
} else {
requiredVersion
}
}
}