diff --git a/shared/src/main/kotlin/org/javacs/kt/classpath/BackupClassPathResolver.kt b/shared/src/main/kotlin/org/javacs/kt/classpath/BackupClassPathResolver.kt index b3b0eb0db..b8c91c8e9 100644 --- a/shared/src/main/kotlin/org/javacs/kt/classpath/BackupClassPathResolver.kt +++ b/shared/src/main/kotlin/org/javacs/kt/classpath/BackupClassPathResolver.kt @@ -3,11 +3,12 @@ package org.javacs.kt.classpath import java.io.File import java.nio.file.Files import java.nio.file.Path +import java.nio.file.Paths import java.nio.file.attribute.BasicFileAttributes import java.util.function.BiPredicate -import org.javacs.kt.util.tryResolving +import kotlin.math.min import org.javacs.kt.util.findCommandOnPath -import java.nio.file.Paths +import org.javacs.kt.util.tryResolving /** Backup classpath that find Kotlin in the user's Maven/Gradle home or kotlinc's libraries folder. */ object BackupClassPathResolver : ClassPathResolver { @@ -27,24 +28,13 @@ private fun findLocalArtifact(group: String, artifact: String) = private fun tryFindingLocalArtifactUsing(@Suppress("UNUSED_PARAMETER") group: String, artifact: String, artifactDirResolution: LocalArtifactDirectoryResolution): Path? { val isCorrectArtifact = BiPredicate { file, _ -> val name = file.fileName.toString() - when (artifactDirResolution.buildTool) { - "Maven" -> { - val version = file.parent.fileName.toString() - val expected = "${artifact}-${version}.jar" - name == expected - } - else -> name.startsWith(artifact) && name.endsWith(".jar") - } + name == "$artifact-${KotlinVersion.CURRENT}.jar" } return Files.list(artifactDirResolution.artifactDir) .sorted(::compareVersions) .findFirst() .orElse(null) - ?.let { - Files.find(artifactDirResolution.artifactDir, 3, isCorrectArtifact) - .findFirst() - .orElse(null) - } + ?.let { Files.find(artifactDirResolution.artifactDir, 3, isCorrectArtifact).findFirst().orElse(null) } } private data class LocalArtifactDirectoryResolution(val artifactDir: Path?, val buildTool: String) @@ -105,7 +95,7 @@ private fun compareVersions(left: Path, right: Path): Int { val leftVersion = extractVersion(left) val rightVersion = extractVersion(right) - for (i in 0 until Math.min(leftVersion.size, rightVersion.size)) { + for (i in 0 until min(leftVersion.size, rightVersion.size)) { val leftRev = leftVersion[i].reversed() val rightRev = rightVersion[i].reversed() val compare = leftRev.compareTo(rightRev) @@ -116,6 +106,6 @@ private fun compareVersions(left: Path, right: Path): Int { return -leftVersion.size.compareTo(rightVersion.size) } private fun extractVersion(artifactVersionDir: Path): List { - return artifactVersionDir.toString().split(".") + return artifactVersionDir.toString().split("/").last().split(".") }