From 14af869a8719e7398508b282dee13e7f709d2bb8 Mon Sep 17 00:00:00 2001 From: Ilya Goncharov Date: Wed, 25 Mar 2020 13:16:47 +0300 Subject: [PATCH] [Gradle, JS] Extract YarnEntryRegistry to upper level - Remove copypaste with version prefixes #KT-37207 fixed --- .../gradle/targets/js/yarn/YarnBasics.kt | 44 -------------- .../targets/js/yarn/YarnEntryRegistry.kt | 59 +++++++++++++++++++ 2 files changed, 59 insertions(+), 44 deletions(-) create mode 100644 libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/targets/js/yarn/YarnEntryRegistry.kt diff --git a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/targets/js/yarn/YarnBasics.kt b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/targets/js/yarn/YarnBasics.kt index 732c2171f72..b8da179c3f4 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/targets/js/yarn/YarnBasics.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/targets/js/yarn/YarnBasics.kt @@ -5,7 +5,6 @@ package org.jetbrains.kotlin.gradle.targets.js.yarn -import com.github.gundy.semver4j.SemVer import org.gradle.api.Project import org.jetbrains.kotlin.gradle.internal.execWithProgress 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 - } -} diff --git a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/targets/js/yarn/YarnEntryRegistry.kt b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/targets/js/yarn/YarnEntryRegistry.kt new file mode 100644 index 00000000000..d585486a84d --- /dev/null +++ b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/targets/js/yarn/YarnEntryRegistry.kt @@ -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:" \ No newline at end of file