[JS IR] Replace canonicalPath with absolutePath

Since canonicalPath is slow, replace it with
absolutePath in SourceMaps and CompilationOutputs.

^KT-58187 Fixed
This commit is contained in:
Alexander Korepanov
2023-05-02 12:27:31 +02:00
committed by Space Team
parent 1d9db1b9f8
commit 97024edf8a
2 changed files with 14 additions and 9 deletions
@@ -35,7 +35,7 @@ abstract class CompilationOutputs {
fun File.writeAsJsFile(out: CompilationOutputs) {
parentFile.mkdirs()
val jsMapFile = mapForJsFile
val jsFile = canonicalFile
val jsFile = normalizedAbsoluteFile
out.writeJsCode(jsFile, jsMapFile)
writtenFiles += jsFile
@@ -65,11 +65,14 @@ abstract class CompilationOutputs {
return allTsDefinitions.toTypeScript(moduleName, moduleKind)
}
private val File.normalizedAbsoluteFile
get() = absoluteFile.normalize()
private val File.mapForJsFile
get() = resolveSibling("$name.map").canonicalFile
get() = resolveSibling("$name.map").normalizedAbsoluteFile
private val File.dtsForJsFile
get() = resolveSibling("$nameWithoutExtension.d.ts").canonicalFile
get() = resolveSibling("$nameWithoutExtension.d.ts").normalizedAbsoluteFile
}
private fun File.copyModificationTimeFrom(from: File) {
@@ -8,20 +8,22 @@ package org.jetbrains.kotlin.js.sourceMap
import java.io.File
class RelativePathCalculator(baseDir: File) {
private val baseDirPath = generateSequence(baseDir.canonicalFile) { it.parentFile }.toList().asReversed()
private fun File.getAllParents() = generateSequence(absoluteFile.normalize()) { it.parentFile }.toList().asReversed()
private val baseDirPath = baseDir.getAllParents()
fun calculateRelativePathTo(file: File): String? {
val path = generateSequence(file.canonicalFile) { it.parentFile }.toList().asReversed()
if (baseDirPath[0] != path[0]) return null
val parents = file.getAllParents()
if (baseDirPath[0] != parents[0]) return null
val commonLength = baseDirPath.zip(path).takeWhile { (first, second) -> first == second }.size
val commonLength = baseDirPath.zip(parents).count { (first, second) -> first == second }
val sb = StringBuilder()
for (i in commonLength until baseDirPath.size) {
sb.append("../")
}
for (i in commonLength until path.size) {
sb.append(path[i].name).append('/')
for (i in commonLength until parents.size) {
sb.append(parents[i].name).append('/')
}
sb.setLength(sb.lastIndex)