Refactor Kotlin/Native path constants

Use java.io.File instead of java.nio.Path to have more uniform API
This commit is contained in:
Dmitriy Dolovov
2019-04-29 13:42:04 +07:00
parent 2ef7cde34b
commit 20d77afcce
3 changed files with 23 additions and 31 deletions
@@ -11,7 +11,6 @@ import com.intellij.openapi.roots.ModuleRootManager
import com.intellij.openapi.roots.OrderRootType import com.intellij.openapi.roots.OrderRootType
import com.intellij.openapi.roots.libraries.Library import com.intellij.openapi.roots.libraries.Library
import com.intellij.openapi.vfs.VfsUtilCore.urlToPath import com.intellij.openapi.vfs.VfsUtilCore.urlToPath
import com.intellij.util.io.readText
import org.jetbrains.kotlin.ide.konan.NativeLibraryKind import org.jetbrains.kotlin.ide.konan.NativeLibraryKind
import org.jetbrains.kotlin.idea.codeInsight.gradle.GradleImportingTestCase import org.jetbrains.kotlin.idea.codeInsight.gradle.GradleImportingTestCase
import org.jetbrains.kotlin.idea.configuration.externalCompilerVersion import org.jetbrains.kotlin.idea.configuration.externalCompilerVersion
@@ -25,9 +24,7 @@ import org.jetbrains.plugins.gradle.util.GradleConstants
import org.junit.Assert.* import org.junit.Assert.*
import org.junit.Test import org.junit.Test
import org.junit.runners.Parameterized import org.junit.runners.Parameterized
import java.nio.file.Files import java.io.File
import java.nio.file.Path
import java.nio.file.Paths
class GradleNativeLibrariesInIDENamingTest : GradleImportingTestCase() { class GradleNativeLibrariesInIDENamingTest : GradleImportingTestCase() {
@@ -37,8 +34,7 @@ class GradleNativeLibrariesInIDENamingTest : GradleImportingTestCase() {
configureProject() configureProject()
importProject() importProject()
val projectRoot = Paths.get(projectPath) myProject.allModules().forEach { assertValidModule(it, projectPath) }
myProject.allModules().forEach { assertValidModule(it, projectRoot) }
} }
// Test naming of Kotlin/Native libraries in projects with Gradle plugin 1.3.20 or earlier // Test naming of Kotlin/Native libraries in projects with Gradle plugin 1.3.20 or earlier
@@ -47,8 +43,7 @@ class GradleNativeLibrariesInIDENamingTest : GradleImportingTestCase() {
configureProject() configureProject()
importProject() importProject()
val projectRoot = Paths.get(projectPath) myProject.allModules().forEach { assertValidModule(it, projectPath) }
myProject.allModules().forEach { assertValidModule(it, projectRoot) }
} }
override fun getExternalSystemConfigFileName() = GradleConstants.KOTLIN_DSL_SCRIPT_NAME override fun getExternalSystemConfigFileName() = GradleConstants.KOTLIN_DSL_SCRIPT_NAME
@@ -59,15 +54,15 @@ class GradleNativeLibrariesInIDENamingTest : GradleImportingTestCase() {
configureByFiles() configureByFiles()
// include data dir with fake Kotlin/Native libraries // include data dir with fake Kotlin/Native libraries
val testSuiteDataDir = testDataDirectory().toPath().parent val testSuiteDataDir = testDataDirectory().parentFile
val kotlinNativeHome = testSuiteDataDir.resolve(FAKE_KOTLIN_NATIVE_HOME_RELATIVE_PATH) val kotlinNativeHome = testSuiteDataDir.resolve(FAKE_KOTLIN_NATIVE_HOME_RELATIVE_PATH)
Files.walk(kotlinNativeHome) kotlinNativeHome.walkTopDown()
.filter { Files.isRegularFile(it) } .filter { it.isFile }
.forEach { pathInTestSuite -> .forEach { pathInTestSuite ->
// need to put copied file one directory upper than the project root, so adding ".." to the beginning of relative path // need to put copied file one directory upper than the project root, so adding ".." to the beginning of relative path
// reason: distribution KLIBs should not be appear in IDEA indexes, so they should be located outside of the project root // reason: distribution KLIBs should not be appear in IDEA indexes, so they should be located outside of the project root
val relativePathInProject = DOUBLE_DOT_PATH.resolve(pathInTestSuite.toRelativePath(testSuiteDataDir)) val relativePathInProject = DOUBLE_DOT_PATH.resolve(pathInTestSuite.relativeTo(testSuiteDataDir))
createProjectSubFile(relativePathInProject.toString(), pathInTestSuite.readText()) createProjectSubFile(relativePathInProject.toString(), pathInTestSuite.readText())
} }
} }
@@ -80,7 +75,7 @@ class GradleNativeLibrariesInIDENamingTest : GradleImportingTestCase() {
} }
} }
private val FAKE_KOTLIN_NATIVE_HOME_RELATIVE_PATH = Paths.get("kotlin-native-data-dir", "kotlin-native-PLATFORM-VERSION") private val FAKE_KOTLIN_NATIVE_HOME_RELATIVE_PATH = File("kotlin-native-data-dir", "kotlin-native-PLATFORM-VERSION")
private val NATIVE_LIBRARY_NAME_REGEX = Regex("^Kotlin/Native ([\\d\\w\\.-]+) - ([\\w\\d]+)( \\[([\\w\\d_]+)\\])?$") private val NATIVE_LIBRARY_NAME_REGEX = Regex("^Kotlin/Native ([\\d\\w\\.-]+) - ([\\w\\d]+)( \\[([\\w\\d_]+)\\])?$")
private val NATIVE_LINUX_LIBRARIES = listOf( private val NATIVE_LINUX_LIBRARIES = listOf(
@@ -103,9 +98,7 @@ private val NATIVE_MINGW_LIBRARIES = listOf(
"Kotlin/Native {version} - opengl32 [mingw_x64]", "Kotlin/Native {version} - opengl32 [mingw_x64]",
"Kotlin/Native {version} - windows [mingw_x64]" "Kotlin/Native {version} - windows [mingw_x64]"
) )
private val DOUBLE_DOT_PATH = Paths.get("..") private val DOUBLE_DOT_PATH = File("..")
private fun Path.toRelativePath(basePath: Path) = basePath.relativize(this)
private val Module.libraries private val Module.libraries
get() = ModuleRootManager.getInstance(this).orderEntries get() = ModuleRootManager.getInstance(this).orderEntries
@@ -113,7 +106,7 @@ private val Module.libraries
.filterIsInstance<LibraryOrderEntry>() .filterIsInstance<LibraryOrderEntry>()
.mapNotNull { it.library } .mapNotNull { it.library }
private fun assertValidModule(module: Module, projectRoot: Path) { private fun assertValidModule(module: Module, projectRoot: String) {
val (nativeLibraries, otherLibraries) = module.libraries.partition { library -> val (nativeLibraries, otherLibraries) = module.libraries.partition { library ->
detectLibraryKind(library.getFiles(OrderRootType.CLASSES)) == NativeLibraryKind detectLibraryKind(library.getFiles(OrderRootType.CLASSES)) == NativeLibraryKind
} }
@@ -144,7 +137,7 @@ private fun assertValidModule(module: Module, projectRoot: Path) {
otherLibraries.forEach(::assertValidNonNativeLibrary) otherLibraries.forEach(::assertValidNonNativeLibrary)
} }
private fun assertValidNativeLibrary(library: Library, projectRoot: Path) { private fun assertValidNativeLibrary(library: Library, projectRoot: String) {
val fullName = library.name.orEmpty() val fullName = library.name.orEmpty()
val result = NATIVE_LIBRARY_NAME_REGEX.matchEntire(fullName) val result = NATIVE_LIBRARY_NAME_REGEX.matchEntire(fullName)
@@ -155,9 +148,9 @@ private fun assertValidNativeLibrary(library: Library, projectRoot: Path) {
val libraryUrls = library.getUrls(OrderRootType.CLASSES).toList() val libraryUrls = library.getUrls(OrderRootType.CLASSES).toList()
assertTrue("Kotlin/Native library $fullName has multiple roots (${libraryUrls.size}): $libraryUrls", libraryUrls.size == 1) assertTrue("Kotlin/Native library $fullName has multiple roots (${libraryUrls.size}): $libraryUrls", libraryUrls.size == 1)
val actualShortPath = Paths.get(urlToPath(libraryUrls.single())) val actualShortPath = File(urlToPath(libraryUrls.single()))
.toRelativePath(projectRoot.resolve(DOUBLE_DOT_PATH).normalize()) .relativeTo(File(projectRoot).resolve(DOUBLE_DOT_PATH).normalize())
.toRelativePath(FAKE_KOTLIN_NATIVE_HOME_RELATIVE_PATH) .relativeTo(FAKE_KOTLIN_NATIVE_HOME_RELATIVE_PATH)
val expectedShortPath = if (platform.isEmpty()) konanCommonLibraryPath(name) else konanPlatformLibraryPath(name, platform) val expectedShortPath = if (platform.isEmpty()) konanCommonLibraryPath(name) else konanPlatformLibraryPath(name, platform)
assertEquals("The short path of $fullName does not match its location", expectedShortPath, actualShortPath) assertEquals("The short path of $fullName does not match its location", expectedShortPath, actualShortPath)
@@ -5,8 +5,7 @@
package org.jetbrains.kotlin.konan.library package org.jetbrains.kotlin.konan.library
import java.nio.file.Path import java.io.File
import java.nio.file.Paths
const val KLIB_FILE_EXTENSION = "klib" const val KLIB_FILE_EXTENSION = "klib"
const val KLIB_FILE_EXTENSION_WITH_DOT = ".$KLIB_FILE_EXTENSION" const val KLIB_FILE_EXTENSION_WITH_DOT = ".$KLIB_FILE_EXTENSION"
@@ -28,14 +27,14 @@ const val KONAN_PLATFORM_LIBS_DIR_NAME = "platform"
const val KONAN_SOURCES_DIR_NAME = "sources" const val KONAN_SOURCES_DIR_NAME = "sources"
val KONAN_COMMON_LIBS_PATH: Path val KONAN_COMMON_LIBS_PATH: File
get() = Paths.get(KLIB_DIR_NAME, KONAN_COMMON_LIBS_DIR_NAME) get() = File(KLIB_DIR_NAME, KONAN_COMMON_LIBS_DIR_NAME)
val KONAN_ALL_PLATFORM_LIBS_PATH: Path val KONAN_ALL_PLATFORM_LIBS_PATH: File
get() = Paths.get(KLIB_DIR_NAME, KONAN_PLATFORM_LIBS_DIR_NAME) get() = File(KLIB_DIR_NAME, KONAN_PLATFORM_LIBS_DIR_NAME)
fun konanCommonLibraryPath(libraryName: String): Path = KONAN_COMMON_LIBS_PATH.resolve(libraryName) fun konanCommonLibraryPath(libraryName: String): File = KONAN_COMMON_LIBS_PATH.resolve(libraryName)
fun konanSpecificPlatformLibrariesPath(platform: String): Path = KONAN_ALL_PLATFORM_LIBS_PATH.resolve(platform) fun konanSpecificPlatformLibrariesPath(platform: String): File = KONAN_ALL_PLATFORM_LIBS_PATH.resolve(platform)
fun konanPlatformLibraryPath(libraryName: String, platform: String): Path = konanSpecificPlatformLibrariesPath(platform).resolve(libraryName) fun konanPlatformLibraryPath(libraryName: String, platform: String): File = konanSpecificPlatformLibrariesPath(platform).resolve(libraryName)
@@ -13,7 +13,7 @@ import java.io.File
object LiteKonanDistributionProvider { object LiteKonanDistributionProvider {
fun getDistribution(konanHomeDir: File): LiteKonanDistribution? { fun getDistribution(konanHomeDir: File): LiteKonanDistribution? {
val stdlib = LiteKonanLibraryFacade.getDistributionLibraryProvider(konanHomeDir) val stdlib = LiteKonanLibraryFacade.getDistributionLibraryProvider(konanHomeDir)
.getLibrary(konanHomeDir.resolve(konanCommonLibraryPath(KONAN_STDLIB_NAME).toFile())) as? LiteKonanLibraryImpl ?: return null .getLibrary(konanHomeDir.resolve(konanCommonLibraryPath(KONAN_STDLIB_NAME))) as? LiteKonanLibraryImpl ?: return null
return LiteKonanDistribution( return LiteKonanDistribution(
konanHomeDir, konanHomeDir,