Implement and use helpers for economical classpath update in script configs
This commit is contained in:
+4
-8
@@ -51,6 +51,7 @@ import kotlin.script.experimental.jvm.JvmDependency
|
||||
import kotlin.script.experimental.jvm.impl.BridgeDependenciesResolver
|
||||
import kotlin.script.experimental.jvm.jdkHome
|
||||
import kotlin.script.experimental.jvm.jvm
|
||||
import kotlin.script.experimental.jvm.withUpdatedClasspath
|
||||
import kotlin.script.experimental.jvmhost.KJvmCompilerProxy
|
||||
import kotlin.script.experimental.util.getOrError
|
||||
|
||||
@@ -79,11 +80,8 @@ class KJvmCompilerImpl(val hostConfiguration: ScriptingHostConfiguration) : KJvm
|
||||
|
||||
fun updateClasspath(classpath: List<File>) {
|
||||
environment!!.updateClasspath(classpath.map(::JvmClasspathRoot))
|
||||
if (classpath.isNotEmpty()) {
|
||||
updatedConfiguration = ScriptCompilationConfiguration(updatedConfiguration) {
|
||||
dependencies.append(JvmDependency(classpath))
|
||||
}
|
||||
}
|
||||
|
||||
updatedConfiguration = updatedConfiguration.withUpdatedClasspath(classpath)
|
||||
}
|
||||
|
||||
val disposable = Disposer.newDisposable()
|
||||
@@ -122,9 +120,7 @@ class KJvmCompilerImpl(val hostConfiguration: ScriptingHostConfiguration) : KJvm
|
||||
add(CLIConfigurationKeys.CONTENT_ROOTS, JvmClasspathRoot(file))
|
||||
}
|
||||
}
|
||||
updatedConfiguration = ScriptCompilationConfiguration(updatedConfiguration) {
|
||||
dependencies.append(JvmDependency(standardLibs.map { it.second }))
|
||||
}
|
||||
updatedConfiguration = updatedConfiguration.withUpdatedClasspath(standardLibs.map { it.second })
|
||||
|
||||
put(CommonConfigurationKeys.MODULE_NAME, "kotlin-script") // TODO" take meaningful and valid name from somewhere
|
||||
languageVersionSettings = LanguageVersionSettingsImpl(
|
||||
|
||||
@@ -38,11 +38,42 @@ fun JvmScriptCompilationConfigurationBuilder.dependenciesFromClassloader(
|
||||
classLoader: ClassLoader = Thread.currentThread().contextClassLoader,
|
||||
wholeClasspath: Boolean = false
|
||||
) {
|
||||
ScriptCompilationConfiguration.dependencies.append(
|
||||
JvmDependency(scriptCompilationClasspathFromContext(*libraries, classLoader = classLoader, wholeClasspath = wholeClasspath))
|
||||
updateClasspath(
|
||||
scriptCompilationClasspathFromContext(*libraries, classLoader = classLoader, wholeClasspath = wholeClasspath)
|
||||
)
|
||||
}
|
||||
|
||||
fun ScriptCompilationConfiguration.withUpdatedClasspath(classpath: Collection<File>): ScriptCompilationConfiguration {
|
||||
|
||||
val newClasspath = classpath.filterNewClasspath(this[ScriptCompilationConfiguration.dependencies])
|
||||
?: return this
|
||||
|
||||
return ScriptCompilationConfiguration(this) {
|
||||
dependencies.append(JvmDependency(newClasspath))
|
||||
}
|
||||
}
|
||||
|
||||
fun ScriptCompilationConfiguration.Builder.updateClasspath(classpath: Collection<File>) = updateClasspathImpl(classpath)
|
||||
|
||||
fun JvmScriptCompilationConfigurationBuilder.updateClasspath(classpath: Collection<File>) = updateClasspathImpl(classpath)
|
||||
|
||||
private fun PropertiesCollection.Builder.updateClasspathImpl(classpath: Collection<File>) {
|
||||
val newClasspath = classpath.filterNewClasspath(this[ScriptCompilationConfiguration.dependencies])
|
||||
?: return
|
||||
|
||||
ScriptCompilationConfiguration.dependencies.append(JvmDependency(newClasspath))
|
||||
}
|
||||
|
||||
private fun Collection<File>.filterNewClasspath(known: Collection<ScriptDependency>?): List<File>? {
|
||||
|
||||
if (isEmpty()) return null
|
||||
|
||||
val knownClasspath = known?.flatMapTo(hashSetOf<File>()) {
|
||||
(it as? JvmDependency)?.classpath ?: emptyList()
|
||||
}
|
||||
return filterNot { knownClasspath?.contains(it) == true }.takeIf { it.isNotEmpty() }
|
||||
}
|
||||
|
||||
@Deprecated("Unused")
|
||||
val JvmScriptCompilationConfigurationKeys.javaHome by PropertiesCollection.keyCopy(ScriptingHostConfiguration.jvm.javaHome)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user