diff --git a/core/src/main/kotlin/com/tschuchort/compiletesting/AbstractKotlinCompilation.kt b/core/src/main/kotlin/com/tschuchort/compiletesting/AbstractKotlinCompilation.kt index 408235bb..d3f9ae8f 100644 --- a/core/src/main/kotlin/com/tschuchort/compiletesting/AbstractKotlinCompilation.kt +++ b/core/src/main/kotlin/com/tschuchort/compiletesting/AbstractKotlinCompilation.kt @@ -1,6 +1,5 @@ package com.tschuchort.compiletesting -import io.github.classgraph.ClassGraph import okio.Buffer import org.jetbrains.kotlin.base.kapt3.KaptOptions import org.jetbrains.kotlin.cli.common.CLICompiler @@ -97,8 +96,7 @@ abstract class AbstractKotlinCompilation internal c * process' classpaths */ var kotlinStdLibCommonJar: File? by default { - findInHostClasspath(hostClasspaths, "kotlin-stdlib-common.jar", - kotlinDependencyRegex("kotlin-stdlib-common")) + HostEnvironment.kotlinStdLibCommonJar } // Directory for input source files @@ -221,22 +219,7 @@ abstract class AbstractKotlinCompilation internal c } } - /** Tries to find a file matching the given [regex] in the host process' classpath */ - protected fun findInHostClasspath(hostClasspaths: List, simpleName: String, regex: Regex): File? { - val jarFile = hostClasspaths.firstOrNull { classpath -> - classpath.name.matches(regex) - //TODO("check that jar file actually contains the right classes") - } - - if (jarFile == null) - log("Searched host classpaths for $simpleName and found no match") - else - log("Searched host classpaths for $simpleName and found ${jarFile.path}") - - return jarFile - } - - protected val hostClasspaths by lazy { getHostClasspaths() } + protected val hostClasspaths by lazy { HostEnvironment.classpath } /* This internal buffer and stream is used so it can be easily converted to a string that is put into the [Result] object, in addition to printing immediately to the user's @@ -266,22 +249,6 @@ abstract class AbstractKotlinCompilation internal c internal val internalMessageStreamAccess: PrintStream get() = internalMessageStream } -internal fun kotlinDependencyRegex(prefix:String): Regex { - return Regex("$prefix(-[0-9]+\\.[0-9]+(\\.[0-9]+)?)([-0-9a-zA-Z]+)?\\.jar") -} - -/** Returns the files on the classloader's classpath and modulepath */ -internal fun getHostClasspaths(): List { - val classGraph = ClassGraph() - .enableSystemJarsAndModules() - .removeTemporaryFilesAfterScan() - - val classpaths = classGraph.classpathFiles - val modules = classGraph.modules.mapNotNull { it.locationFile } - - return (classpaths + modules).distinctBy(File::getAbsolutePath) -} - internal fun convertKotlinExitCode(code: ExitCode) = when(code) { ExitCode.OK -> KotlinCompilation.ExitCode.OK ExitCode.INTERNAL_ERROR -> KotlinCompilation.ExitCode.INTERNAL_ERROR diff --git a/core/src/main/kotlin/com/tschuchort/compiletesting/HostEnvironment.kt b/core/src/main/kotlin/com/tschuchort/compiletesting/HostEnvironment.kt new file mode 100644 index 00000000..428553f7 --- /dev/null +++ b/core/src/main/kotlin/com/tschuchort/compiletesting/HostEnvironment.kt @@ -0,0 +1,66 @@ +package com.tschuchort.compiletesting + +import io.github.classgraph.ClassGraph +import java.io.File + +/** + * Utility object to provide everything we might discover from the host environment. + */ +internal object HostEnvironment { + val classpath by lazy { + getHostClasspaths() + } + + val kotlinStdLibJar: File? by lazy { + findInClasspath(kotlinDependencyRegex("(kotlin-stdlib|kotlin-runtime)")) + } + + val kotlinStdLibCommonJar: File? by lazy { + findInClasspath(kotlinDependencyRegex("kotlin-stdlib-common")) + } + + val kotlinStdLibJdkJar: File? by lazy { + findInClasspath(kotlinDependencyRegex("kotlin-stdlib-jdk[0-9]+")) + } + + val kotlinStdLibJsJar: File? by default { + findInClasspath(kotlinDependencyRegex("kotlin-stdlib-js")) + } + + val kotlinReflectJar: File? by lazy { + findInClasspath(kotlinDependencyRegex("kotlin-reflect")) + } + + val kotlinScriptRuntimeJar: File? by lazy { + findInClasspath(kotlinDependencyRegex("kotlin-script-runtime")) + } + + val toolsJar: File? by lazy { + findInClasspath(Regex("tools.jar")) + } + + private fun kotlinDependencyRegex(prefix: String): Regex { + return Regex("$prefix(-[0-9]+\\.[0-9]+(\\.[0-9]+)?)([-0-9a-zA-Z]+)?\\.jar") + } + + /** Tries to find a file matching the given [regex] in the host process' classpath */ + private fun findInClasspath(regex: Regex): File? { + val jarFile = classpath.firstOrNull { classpath -> + classpath.name.matches(regex) + //TODO("check that jar file actually contains the right classes") + } + return jarFile + } + + /** Returns the files on the classloader's classpath and modulepath */ + private fun getHostClasspaths(): List { + val classGraph = ClassGraph() + .enableSystemJarsAndModules() + .removeTemporaryFilesAfterScan() + + val classpaths = classGraph.classpathFiles + val modules = classGraph.modules.mapNotNull { it.locationFile } + + return (classpaths + modules).distinctBy(File::getAbsolutePath) + } +} diff --git a/core/src/main/kotlin/com/tschuchort/compiletesting/KotlinCompilation.kt b/core/src/main/kotlin/com/tschuchort/compiletesting/KotlinCompilation.kt index b1a0c844..4f288b08 100644 --- a/core/src/main/kotlin/com/tschuchort/compiletesting/KotlinCompilation.kt +++ b/core/src/main/kotlin/com/tschuchort/compiletesting/KotlinCompilation.kt @@ -178,8 +178,7 @@ class KotlinCompilation : AbstractKotlinCompilation() { * process' classpaths */ var kotlinStdLibJar: File? by default { - findInHostClasspath(hostClasspaths, "kotlin-stdlib.jar", - kotlinDependencyRegex("(kotlin-stdlib|kotlin-runtime)")) + HostEnvironment.kotlinStdLibJar } /** @@ -188,8 +187,7 @@ class KotlinCompilation : AbstractKotlinCompilation() { * process' classpaths */ var kotlinStdLibJdkJar: File? by default { - findInHostClasspath(hostClasspaths, "kotlin-stdlib-jdk*.jar", - kotlinDependencyRegex("kotlin-stdlib-jdk[0-9]+")) + HostEnvironment.kotlinStdLibJdkJar } /** @@ -198,8 +196,7 @@ class KotlinCompilation : AbstractKotlinCompilation() { * process' classpaths */ var kotlinReflectJar: File? by default { - findInHostClasspath(hostClasspaths, "kotlin-reflect.jar", - kotlinDependencyRegex("kotlin-reflect")) + HostEnvironment.kotlinReflectJar } /** @@ -208,8 +205,7 @@ class KotlinCompilation : AbstractKotlinCompilation() { * process' classpaths */ var kotlinScriptRuntimeJar: File? by default { - findInHostClasspath(hostClasspaths, "kotlin-script-runtime.jar", - kotlinDependencyRegex("kotlin-script-runtime")) + HostEnvironment.kotlinScriptRuntimeJar } /** @@ -221,7 +217,7 @@ class KotlinCompilation : AbstractKotlinCompilation() { var toolsJar: File? by default { if (!isJdk9OrLater()) jdkHome?.let { findToolsJarFromJdk(it) } - ?: findInHostClasspath(hostClasspaths, "tools.jar", Regex("tools.jar")) + ?: HostEnvironment.toolsJar else null } diff --git a/core/src/main/kotlin/com/tschuchort/compiletesting/KotlinJsCompilation.kt b/core/src/main/kotlin/com/tschuchort/compiletesting/KotlinJsCompilation.kt index 6c771cd5..f46eb217 100644 --- a/core/src/main/kotlin/com/tschuchort/compiletesting/KotlinJsCompilation.kt +++ b/core/src/main/kotlin/com/tschuchort/compiletesting/KotlinJsCompilation.kt @@ -42,8 +42,7 @@ class KotlinJsCompilation : AbstractKotlinCompilation() { * process' classpaths */ var kotlinStdLibJsJar: File? by default { - findInHostClasspath(hostClasspaths, "kotlin-stdlib-js.jar", - kotlinDependencyRegex("kotlin-stdlib-js")) + HostEnvironment.kotlinStdLibJsJar } // *.class files, Jars and resources (non-temporary) that are created by the