forked from tschuchortdev/kotlin-compile-testing
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR adds caching for host classpath to avoid calling ClassGraph for every compilation. In my local tests, it saves between 50 to 80ms per compilation on my laptop, which is not necessarily much but adds up when you run hundreds of them. I've also cached common jars we find from there. It does not necessarily help with performance but rather as a cleanup to keep all of them in 1 place. Test: existing tests Issue: tschuchortdev#113
- Loading branch information
Showing
4 changed files
with
74 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
core/src/main/kotlin/com/tschuchort/compiletesting/HostEnvironment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<File> { | ||
val classGraph = ClassGraph() | ||
.enableSystemJarsAndModules() | ||
.removeTemporaryFilesAfterScan() | ||
|
||
val classpaths = classGraph.classpathFiles | ||
val modules = classGraph.modules.mapNotNull { it.locationFile } | ||
|
||
return (classpaths + modules).distinctBy(File::getAbsolutePath) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters