[Gradle, JS] Extract YarnEntryRegistry to upper level
- Remove copypaste with version prefixes #KT-37207 fixed
This commit is contained in:
-44
@@ -5,7 +5,6 @@
|
|||||||
|
|
||||||
package org.jetbrains.kotlin.gradle.targets.js.yarn
|
package org.jetbrains.kotlin.gradle.targets.js.yarn
|
||||||
|
|
||||||
import com.github.gundy.semver4j.SemVer
|
|
||||||
import org.gradle.api.Project
|
import org.gradle.api.Project
|
||||||
import org.jetbrains.kotlin.gradle.internal.execWithProgress
|
import org.jetbrains.kotlin.gradle.internal.execWithProgress
|
||||||
import org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootPlugin
|
import org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootPlugin
|
||||||
@@ -143,46 +142,3 @@ abstract class YarnBasics : NpmApi {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private class YarnEntryRegistry(private val lockFile: File) {
|
|
||||||
val entryMap = YarnLock.parse(lockFile)
|
|
||||||
.entries
|
|
||||||
.associateBy { it.dependencyKey }
|
|
||||||
|
|
||||||
fun find(packageKey: String, version: String): YarnLock.Entry {
|
|
||||||
val key = dependencyKey(packageKey, version)
|
|
||||||
var entry = entryMap[key]
|
|
||||||
|
|
||||||
if (entry == null && version == "*") {
|
|
||||||
val searchKey = dependencyKey(packageKey, "")
|
|
||||||
entry = entryMap.entries
|
|
||||||
.filter { it.key.startsWith(searchKey) }
|
|
||||||
.firstOrNull {
|
|
||||||
SemVer.satisfies(it.key.removePrefix(searchKey), "*")
|
|
||||||
}
|
|
||||||
?.value
|
|
||||||
}
|
|
||||||
|
|
||||||
return checkNotNull(entry) {
|
|
||||||
"Cannot find $key in yarn.lock"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
val YarnLock.Entry.dependencyKey: String
|
|
||||||
get() = key.correctDependencyKey()
|
|
||||||
|
|
||||||
private fun dependencyKey(packageKey: String, version: String) =
|
|
||||||
YarnLock.dependencyKey(packageKey, version).correctDependencyKey()
|
|
||||||
|
|
||||||
private fun String.correctDependencyKey(): String {
|
|
||||||
var correctedKey = replace("@github:", "@")
|
|
||||||
|
|
||||||
if ("@file:" in correctedKey) {
|
|
||||||
val location = correctedKey.substringAfter("@file:")
|
|
||||||
val path = lockFile.parentFile.resolve(location).canonicalPath
|
|
||||||
correctedKey = correctedKey.replaceAfter("@file:", path)
|
|
||||||
}
|
|
||||||
|
|
||||||
return correctedKey
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
+59
@@ -0,0 +1,59 @@
|
|||||||
|
/*
|
||||||
|
* 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.yarn
|
||||||
|
|
||||||
|
import com.github.gundy.semver4j.SemVer
|
||||||
|
import java.io.File
|
||||||
|
|
||||||
|
internal class YarnEntryRegistry(private val lockFile: File) {
|
||||||
|
private val entryMap = YarnLock.parse(lockFile)
|
||||||
|
.entries
|
||||||
|
.associateBy { it.dependencyKey }
|
||||||
|
|
||||||
|
fun find(packageKey: String, version: String): YarnLock.Entry {
|
||||||
|
val key = dependencyKey(packageKey, version)
|
||||||
|
var entry = entryMap[key]
|
||||||
|
|
||||||
|
if (entry == null && version == "*") {
|
||||||
|
val searchKey = dependencyKey(packageKey, "")
|
||||||
|
entry = entryMap.entries
|
||||||
|
.filter { it.key.startsWith(searchKey) }
|
||||||
|
.firstOrNull {
|
||||||
|
SemVer.satisfies(it.key.removePrefix(searchKey), "*")
|
||||||
|
}
|
||||||
|
?.value
|
||||||
|
}
|
||||||
|
|
||||||
|
return checkNotNull(entry) {
|
||||||
|
"Cannot find $key in yarn.lock"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private val YarnLock.Entry.dependencyKey: String
|
||||||
|
get() = key.correctDependencyKey()
|
||||||
|
|
||||||
|
private fun dependencyKey(packageKey: String, version: String) =
|
||||||
|
YarnLock.dependencyKey(packageKey, version).correctDependencyKey()
|
||||||
|
|
||||||
|
private fun String.correctDependencyKey(): String {
|
||||||
|
var correctedKey = replace(GITHUB_VERSION_PREFIX, "@")
|
||||||
|
|
||||||
|
if (FILE_VERSION_PREFIX in correctedKey) {
|
||||||
|
val location = correctedKey.substringAfter(FILE_VERSION_PREFIX)
|
||||||
|
val path = lockFile
|
||||||
|
.parentFile
|
||||||
|
.resolve(location)
|
||||||
|
.canonicalPath
|
||||||
|
|
||||||
|
correctedKey = correctedKey.replaceAfter(FILE_VERSION_PREFIX, path)
|
||||||
|
}
|
||||||
|
|
||||||
|
return correctedKey
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private const val FILE_VERSION_PREFIX = "@file:"
|
||||||
|
private const val GITHUB_VERSION_PREFIX = "@github:"
|
||||||
Reference in New Issue
Block a user