Scripting: refactor plugin command line processing
- process command line properly when plugin is autoloaded - add and fix options to disable definitions autoloading and discovery - cleanup unused code (partial test is added to "avoid definitions discovery" commit)
This commit is contained in:
@@ -28,6 +28,8 @@ import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity.ERROR
|
||||
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity.INFO
|
||||
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity.LOGGING
|
||||
import org.jetbrains.kotlin.cli.jvm.plugins.PluginCliParser
|
||||
import org.jetbrains.kotlin.cli.jvm.plugins.processCompilerPluginsOptions
|
||||
import org.jetbrains.kotlin.compiler.plugin.CommandLineProcessor
|
||||
import org.jetbrains.kotlin.compiler.plugin.ComponentRegistrar
|
||||
import org.jetbrains.kotlin.config.CompilerConfiguration
|
||||
import org.jetbrains.kotlin.config.Services
|
||||
@@ -45,6 +47,7 @@ import java.io.PrintStream
|
||||
abstract class CLICompiler<A : CommonCompilerArguments> : CLITool<A>() {
|
||||
companion object {
|
||||
const val SCRIPT_PLUGIN_REGISTRAR_NAME = "org.jetbrains.kotlin.scripting.compiler.plugin.ScriptingCompilerConfigurationComponentRegistrar"
|
||||
const val SCRIPT_PLUGIN_COMMANDLINE_PROCESSOR_NAME = "org.jetbrains.kotlin.scripting.compiler.plugin.ScriptingCommandLineProcessor"
|
||||
}
|
||||
|
||||
abstract val defaultPerformanceManager: CommonCompilerPerformanceManager
|
||||
@@ -174,9 +177,10 @@ abstract class CLICompiler<A : CommonCompilerArguments> : CLITool<A>() {
|
||||
}
|
||||
|
||||
if (!arguments.disableDefaultScriptingPlugin) {
|
||||
pluginOptions.addPlatformOptions(arguments)
|
||||
val explicitOrLoadedScriptingPlugin =
|
||||
pluginClasspaths.any { File(it).name.startsWith(PathUtil.KOTLIN_SCRIPTING_COMPILER_PLUGIN_NAME) } ||
|
||||
tryLoadScriptingPluginFromCurrentClassLoader(configuration)
|
||||
tryLoadScriptingPluginFromCurrentClassLoader(configuration, pluginOptions)
|
||||
if (!explicitOrLoadedScriptingPlugin) {
|
||||
val kotlinPaths = paths ?: PathUtil.kotlinPathsForCompiler
|
||||
val libPath = kotlinPaths.libPath.takeIf { it.exists() && it.isDirectory } ?: File(".")
|
||||
@@ -191,23 +195,31 @@ abstract class CLICompiler<A : CommonCompilerArguments> : CLITool<A>() {
|
||||
)
|
||||
}
|
||||
}
|
||||
pluginOptions.addPlatformOptions(arguments)
|
||||
} else {
|
||||
pluginOptions.add("plugin:kotlin.scripting:disable=true")
|
||||
}
|
||||
return PluginCliParser.loadPluginsSafe(pluginClasspaths, pluginOptions, configuration)
|
||||
}
|
||||
|
||||
private fun tryLoadScriptingPluginFromCurrentClassLoader(configuration: CompilerConfiguration): Boolean = try {
|
||||
val pluginRegistrarClass = PluginCliParser::class.java.classLoader.loadClass(SCRIPT_PLUGIN_REGISTRAR_NAME)
|
||||
val pluginRegistrar = pluginRegistrarClass.newInstance() as? ComponentRegistrar
|
||||
if (pluginRegistrar != null) {
|
||||
configuration.add(ComponentRegistrar.PLUGIN_COMPONENT_REGISTRARS, pluginRegistrar)
|
||||
true
|
||||
} else false
|
||||
} catch (_: Throwable) {
|
||||
// TODO: add finer error processing and logging
|
||||
false
|
||||
}
|
||||
private fun tryLoadScriptingPluginFromCurrentClassLoader(configuration: CompilerConfiguration, pluginOptions: List<String>): Boolean =
|
||||
try {
|
||||
val pluginRegistrarClass = PluginCliParser::class.java.classLoader.loadClass(SCRIPT_PLUGIN_REGISTRAR_NAME)
|
||||
val pluginRegistrar = pluginRegistrarClass.getDeclaredConstructor().newInstance() as? ComponentRegistrar
|
||||
if (pluginRegistrar != null) {
|
||||
val cmdlineProcessorClass =
|
||||
if (pluginOptions.isEmpty()) null
|
||||
else PluginCliParser::class.java.classLoader.loadClass(SCRIPT_PLUGIN_COMMANDLINE_PROCESSOR_NAME)!!
|
||||
val cmdlineProcessor = cmdlineProcessorClass?.getDeclaredConstructor()?.newInstance() as? CommandLineProcessor
|
||||
configuration.add(ComponentRegistrar.PLUGIN_COMPONENT_REGISTRARS, pluginRegistrar)
|
||||
if (cmdlineProcessor != null) {
|
||||
processCompilerPluginsOptions(configuration, pluginOptions, listOf(cmdlineProcessor))
|
||||
}
|
||||
true
|
||||
} else false
|
||||
} catch (e: Throwable) {
|
||||
val messageCollector = configuration.getNotNull(MESSAGE_COLLECTOR_KEY)
|
||||
messageCollector.report(LOGGING, "Exception on loading scripting plugin: $e")
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ Buildfile: [TestData]/build.xml
|
||||
build:
|
||||
[kotlin2js] Compiling [[TestData]/root1] => [[Temp]/out.js]
|
||||
[kotlin2js] logging: using Kotlin home directory [KotlinProjectHome]/dist/kotlinc
|
||||
[kotlin2js] logging: exception on loading scripting plugin: java.lang.ClassNotFoundException: org.jetbrains.kotlin.scripting.compiler.plugin.ScriptingCompilerConfigurationComponentRegistrar
|
||||
[kotlin2js] logging: configure scripting: Added template org.jetbrains.kotlin.mainKts.MainKtsScript from [[CompilerLib]/kotlin-main-kts.jar, [CompilerLib]/kotlin-reflect.jar, [CompilerLib]/kotlin-script-runtime.jar, [CompilerLib]/kotlin-stdlib.jar]
|
||||
[kotlin2js] logging: compiling source files: [TestData]/root1/foo.kt
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ Buildfile: [TestData]/build.xml
|
||||
build:
|
||||
[kotlinc] Compiling [[TestData]/hello.kt] => [[Temp]/hello.jar]
|
||||
[kotlinc] logging: using Kotlin home directory [KotlinProjectHome]/dist/kotlinc
|
||||
[kotlinc] logging: exception on loading scripting plugin: java.lang.ClassNotFoundException: org.jetbrains.kotlin.scripting.compiler.plugin.ScriptingCompilerConfigurationComponentRegistrar
|
||||
[kotlinc] logging: using JVM IR backend
|
||||
[kotlinc] logging: configuring the compilation environment
|
||||
[kotlinc] logging: configure scripting: Added template org.jetbrains.kotlin.mainKts.MainKtsScript from [[CompilerLib]/kotlin-main-kts.jar, [CompilerLib]/kotlin-reflect.jar, [CompilerLib]/kotlin-script-runtime.jar, [CompilerLib]/kotlin-stdlib.jar]
|
||||
|
||||
+3
@@ -29,6 +29,9 @@ object ScriptingConfigurationKeys {
|
||||
val DISABLE_SCRIPT_DEFINITIONS_FROM_CLASSPATH_OPTION: CompilerConfigurationKey<Boolean> =
|
||||
CompilerConfigurationKey.create("Do not extract script definitions from the compilation classpath")
|
||||
|
||||
val DISABLE_SCRIPT_DEFINITIONS_AUTOLOADING_OPTION: CompilerConfigurationKey<Boolean> =
|
||||
CompilerConfigurationKey.create("Do not automatically load compiler-supplied script definitions, like main-kts")
|
||||
|
||||
val LEGACY_SCRIPT_RESOLVER_ENVIRONMENT_OPTION: CompilerConfigurationKey<MutableMap<String, Any?>> =
|
||||
CompilerConfigurationKey.create("Script resolver environment")
|
||||
}
|
||||
+11
-17
@@ -15,23 +15,6 @@ import org.jetbrains.kotlin.config.JVMConfigurationKeys
|
||||
import org.jetbrains.kotlin.scripting.configuration.ScriptingConfigurationKeys
|
||||
import java.io.File
|
||||
|
||||
object ScriptingConfigurationKeys {
|
||||
val DISABLE_SCRIPTING_PLUGIN_OPTION: CompilerConfigurationKey<Boolean> =
|
||||
CompilerConfigurationKey.create("Disable scripting plugin")
|
||||
|
||||
val SCRIPT_DEFINITIONS: CompilerConfigurationKey<List<String>> =
|
||||
CompilerConfigurationKey.create("Script definition classes")
|
||||
|
||||
val SCRIPT_DEFINITIONS_CLASSPATH: CompilerConfigurationKey<List<File>> =
|
||||
CompilerConfigurationKey.create("Additional classpath for the script definitions")
|
||||
|
||||
val DISABLE_SCRIPT_DEFINITIONS_FROM_CLASSPATH_OPTION: CompilerConfigurationKey<Boolean> =
|
||||
CompilerConfigurationKey.create("Do not extract script definitions from the compilation classpath")
|
||||
|
||||
val LEGACY_SCRIPT_RESOLVER_ENVIRONMENT_OPTION: CompilerConfigurationKey<MutableMap<String, Any?>> =
|
||||
CompilerConfigurationKey.create("Script resolver environment")
|
||||
}
|
||||
|
||||
class ScriptingCommandLineProcessor : CommandLineProcessor {
|
||||
companion object {
|
||||
val DISABLE_SCRIPTING_PLUGIN_OPTION = CliOption(
|
||||
@@ -54,6 +37,10 @@ class ScriptingCommandLineProcessor : CommandLineProcessor {
|
||||
"disable-script-definitions-from-classpath", "true/false", "Do not extract script definitions from the compilation classpath",
|
||||
required = false, allowMultipleOccurrences = false
|
||||
)
|
||||
val DISABLE_SCRIPT_DEFINITIONS_AUTOLOADING_OPTION = CliOption(
|
||||
"disable-script-definitions-autoloading", "true/false", "Do not automatically load compiler-supplied script definitions, like main-kts",
|
||||
required = false, allowMultipleOccurrences = false
|
||||
)
|
||||
val LEGACY_SCRIPT_TEMPLATES_OPTION = CliOption(
|
||||
"script-templates", "<fully qualified class name[,]>", "Script definition template classes",
|
||||
required = false, allowMultipleOccurrences = true
|
||||
@@ -73,6 +60,7 @@ class ScriptingCommandLineProcessor : CommandLineProcessor {
|
||||
SCRIPT_DEFINITIONS_CLASSPATH_OPTION,
|
||||
DISABLE_STANDARD_SCRIPT_DEFINITION_OPTION,
|
||||
DISABLE_SCRIPT_DEFINITIONS_FROM_CLSSPATH_OPTION,
|
||||
DISABLE_SCRIPT_DEFINITIONS_AUTOLOADING_OPTION,
|
||||
LEGACY_SCRIPT_TEMPLATES_OPTION,
|
||||
LEGACY_SCRIPT_RESOLVER_ENVIRONMENT_OPTION
|
||||
)
|
||||
@@ -107,6 +95,12 @@ class ScriptingCommandLineProcessor : CommandLineProcessor {
|
||||
value.takeUnless { it.isBlank() }?.toBoolean() ?: true
|
||||
)
|
||||
}
|
||||
DISABLE_SCRIPT_DEFINITIONS_AUTOLOADING_OPTION -> {
|
||||
configuration.put(
|
||||
ScriptingConfigurationKeys.DISABLE_SCRIPT_DEFINITIONS_AUTOLOADING_OPTION,
|
||||
value.takeUnless { it.isBlank() }?.toBoolean() ?: true
|
||||
)
|
||||
}
|
||||
LEGACY_SCRIPT_RESOLVER_ENVIRONMENT_OPTION -> {
|
||||
val currentEnv = configuration.getMap(ScriptingConfigurationKeys.LEGACY_SCRIPT_RESOLVER_ENVIRONMENT_OPTION).toMutableMap()
|
||||
// parses key/value pairs in the form <key>=<value>, where
|
||||
|
||||
+11
-6
@@ -67,16 +67,21 @@ class ScriptingCompilerConfigurationExtension(
|
||||
)
|
||||
}
|
||||
|
||||
configuration.addAll(
|
||||
ScriptingConfigurationKeys.SCRIPT_DEFINITIONS_SOURCES,
|
||||
listOf(
|
||||
val definitionsFromClasspath =
|
||||
if (configuration.getBoolean(ScriptingConfigurationKeys.DISABLE_SCRIPT_DEFINITIONS_FROM_CLASSPATH_OPTION)) null
|
||||
else
|
||||
ScriptDefinitionsFromClasspathDiscoverySource(
|
||||
configuration.jvmClasspathRoots,
|
||||
hostConfiguration,
|
||||
messageCollector.reporter
|
||||
),
|
||||
AutoloadedScriptDefinitions(hostConfiguration, this::class.java.classLoader, messageCollector.reporter)
|
||||
)
|
||||
)
|
||||
val autoloadedScriptDefinitions =
|
||||
if (configuration.getBoolean(ScriptingConfigurationKeys.DISABLE_SCRIPT_DEFINITIONS_AUTOLOADING_OPTION)) null
|
||||
else AutoloadedScriptDefinitions(hostConfiguration, this::class.java.classLoader, messageCollector.reporter)
|
||||
|
||||
configuration.addAll(
|
||||
ScriptingConfigurationKeys.SCRIPT_DEFINITIONS_SOURCES,
|
||||
listOfNotNull(definitionsFromClasspath, autoloadedScriptDefinitions)
|
||||
)
|
||||
|
||||
val scriptDefinitionProvider = ScriptDefinitionProvider.getInstance(project) as? CliScriptDefinitionProvider
|
||||
|
||||
Reference in New Issue
Block a user