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.INFO
|
||||||
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity.LOGGING
|
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity.LOGGING
|
||||||
import org.jetbrains.kotlin.cli.jvm.plugins.PluginCliParser
|
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.compiler.plugin.ComponentRegistrar
|
||||||
import org.jetbrains.kotlin.config.CompilerConfiguration
|
import org.jetbrains.kotlin.config.CompilerConfiguration
|
||||||
import org.jetbrains.kotlin.config.Services
|
import org.jetbrains.kotlin.config.Services
|
||||||
@@ -45,6 +47,7 @@ import java.io.PrintStream
|
|||||||
abstract class CLICompiler<A : CommonCompilerArguments> : CLITool<A>() {
|
abstract class CLICompiler<A : CommonCompilerArguments> : CLITool<A>() {
|
||||||
companion object {
|
companion object {
|
||||||
const val SCRIPT_PLUGIN_REGISTRAR_NAME = "org.jetbrains.kotlin.scripting.compiler.plugin.ScriptingCompilerConfigurationComponentRegistrar"
|
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
|
abstract val defaultPerformanceManager: CommonCompilerPerformanceManager
|
||||||
@@ -174,9 +177,10 @@ abstract class CLICompiler<A : CommonCompilerArguments> : CLITool<A>() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!arguments.disableDefaultScriptingPlugin) {
|
if (!arguments.disableDefaultScriptingPlugin) {
|
||||||
|
pluginOptions.addPlatformOptions(arguments)
|
||||||
val explicitOrLoadedScriptingPlugin =
|
val explicitOrLoadedScriptingPlugin =
|
||||||
pluginClasspaths.any { File(it).name.startsWith(PathUtil.KOTLIN_SCRIPTING_COMPILER_PLUGIN_NAME) } ||
|
pluginClasspaths.any { File(it).name.startsWith(PathUtil.KOTLIN_SCRIPTING_COMPILER_PLUGIN_NAME) } ||
|
||||||
tryLoadScriptingPluginFromCurrentClassLoader(configuration)
|
tryLoadScriptingPluginFromCurrentClassLoader(configuration, pluginOptions)
|
||||||
if (!explicitOrLoadedScriptingPlugin) {
|
if (!explicitOrLoadedScriptingPlugin) {
|
||||||
val kotlinPaths = paths ?: PathUtil.kotlinPathsForCompiler
|
val kotlinPaths = paths ?: PathUtil.kotlinPathsForCompiler
|
||||||
val libPath = kotlinPaths.libPath.takeIf { it.exists() && it.isDirectory } ?: File(".")
|
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 {
|
} else {
|
||||||
pluginOptions.add("plugin:kotlin.scripting:disable=true")
|
pluginOptions.add("plugin:kotlin.scripting:disable=true")
|
||||||
}
|
}
|
||||||
return PluginCliParser.loadPluginsSafe(pluginClasspaths, pluginOptions, configuration)
|
return PluginCliParser.loadPluginsSafe(pluginClasspaths, pluginOptions, configuration)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun tryLoadScriptingPluginFromCurrentClassLoader(configuration: CompilerConfiguration): Boolean = try {
|
private fun tryLoadScriptingPluginFromCurrentClassLoader(configuration: CompilerConfiguration, pluginOptions: List<String>): Boolean =
|
||||||
val pluginRegistrarClass = PluginCliParser::class.java.classLoader.loadClass(SCRIPT_PLUGIN_REGISTRAR_NAME)
|
try {
|
||||||
val pluginRegistrar = pluginRegistrarClass.newInstance() as? ComponentRegistrar
|
val pluginRegistrarClass = PluginCliParser::class.java.classLoader.loadClass(SCRIPT_PLUGIN_REGISTRAR_NAME)
|
||||||
if (pluginRegistrar != null) {
|
val pluginRegistrar = pluginRegistrarClass.getDeclaredConstructor().newInstance() as? ComponentRegistrar
|
||||||
configuration.add(ComponentRegistrar.PLUGIN_COMPONENT_REGISTRARS, pluginRegistrar)
|
if (pluginRegistrar != null) {
|
||||||
true
|
val cmdlineProcessorClass =
|
||||||
} else false
|
if (pluginOptions.isEmpty()) null
|
||||||
} catch (_: Throwable) {
|
else PluginCliParser::class.java.classLoader.loadClass(SCRIPT_PLUGIN_COMMANDLINE_PROCESSOR_NAME)!!
|
||||||
// TODO: add finer error processing and logging
|
val cmdlineProcessor = cmdlineProcessorClass?.getDeclaredConstructor()?.newInstance() as? CommandLineProcessor
|
||||||
false
|
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:
|
build:
|
||||||
[kotlin2js] Compiling [[TestData]/root1] => [[Temp]/out.js]
|
[kotlin2js] Compiling [[TestData]/root1] => [[Temp]/out.js]
|
||||||
[kotlin2js] logging: using Kotlin home directory [KotlinProjectHome]/dist/kotlinc
|
[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: 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
|
[kotlin2js] logging: compiling source files: [TestData]/root1/foo.kt
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ Buildfile: [TestData]/build.xml
|
|||||||
build:
|
build:
|
||||||
[kotlinc] Compiling [[TestData]/hello.kt] => [[Temp]/hello.jar]
|
[kotlinc] Compiling [[TestData]/hello.kt] => [[Temp]/hello.jar]
|
||||||
[kotlinc] logging: using Kotlin home directory [KotlinProjectHome]/dist/kotlinc
|
[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: using JVM IR backend
|
||||||
[kotlinc] logging: configuring the compilation environment
|
[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]
|
[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> =
|
val DISABLE_SCRIPT_DEFINITIONS_FROM_CLASSPATH_OPTION: CompilerConfigurationKey<Boolean> =
|
||||||
CompilerConfigurationKey.create("Do not extract script definitions from the compilation classpath")
|
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?>> =
|
val LEGACY_SCRIPT_RESOLVER_ENVIRONMENT_OPTION: CompilerConfigurationKey<MutableMap<String, Any?>> =
|
||||||
CompilerConfigurationKey.create("Script resolver environment")
|
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 org.jetbrains.kotlin.scripting.configuration.ScriptingConfigurationKeys
|
||||||
import java.io.File
|
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 {
|
class ScriptingCommandLineProcessor : CommandLineProcessor {
|
||||||
companion object {
|
companion object {
|
||||||
val DISABLE_SCRIPTING_PLUGIN_OPTION = CliOption(
|
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",
|
"disable-script-definitions-from-classpath", "true/false", "Do not extract script definitions from the compilation classpath",
|
||||||
required = false, allowMultipleOccurrences = false
|
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(
|
val LEGACY_SCRIPT_TEMPLATES_OPTION = CliOption(
|
||||||
"script-templates", "<fully qualified class name[,]>", "Script definition template classes",
|
"script-templates", "<fully qualified class name[,]>", "Script definition template classes",
|
||||||
required = false, allowMultipleOccurrences = true
|
required = false, allowMultipleOccurrences = true
|
||||||
@@ -73,6 +60,7 @@ class ScriptingCommandLineProcessor : CommandLineProcessor {
|
|||||||
SCRIPT_DEFINITIONS_CLASSPATH_OPTION,
|
SCRIPT_DEFINITIONS_CLASSPATH_OPTION,
|
||||||
DISABLE_STANDARD_SCRIPT_DEFINITION_OPTION,
|
DISABLE_STANDARD_SCRIPT_DEFINITION_OPTION,
|
||||||
DISABLE_SCRIPT_DEFINITIONS_FROM_CLSSPATH_OPTION,
|
DISABLE_SCRIPT_DEFINITIONS_FROM_CLSSPATH_OPTION,
|
||||||
|
DISABLE_SCRIPT_DEFINITIONS_AUTOLOADING_OPTION,
|
||||||
LEGACY_SCRIPT_TEMPLATES_OPTION,
|
LEGACY_SCRIPT_TEMPLATES_OPTION,
|
||||||
LEGACY_SCRIPT_RESOLVER_ENVIRONMENT_OPTION
|
LEGACY_SCRIPT_RESOLVER_ENVIRONMENT_OPTION
|
||||||
)
|
)
|
||||||
@@ -107,6 +95,12 @@ class ScriptingCommandLineProcessor : CommandLineProcessor {
|
|||||||
value.takeUnless { it.isBlank() }?.toBoolean() ?: true
|
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 -> {
|
LEGACY_SCRIPT_RESOLVER_ENVIRONMENT_OPTION -> {
|
||||||
val currentEnv = configuration.getMap(ScriptingConfigurationKeys.LEGACY_SCRIPT_RESOLVER_ENVIRONMENT_OPTION).toMutableMap()
|
val currentEnv = configuration.getMap(ScriptingConfigurationKeys.LEGACY_SCRIPT_RESOLVER_ENVIRONMENT_OPTION).toMutableMap()
|
||||||
// parses key/value pairs in the form <key>=<value>, where
|
// parses key/value pairs in the form <key>=<value>, where
|
||||||
|
|||||||
+11
-6
@@ -67,16 +67,21 @@ class ScriptingCompilerConfigurationExtension(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
configuration.addAll(
|
val definitionsFromClasspath =
|
||||||
ScriptingConfigurationKeys.SCRIPT_DEFINITIONS_SOURCES,
|
if (configuration.getBoolean(ScriptingConfigurationKeys.DISABLE_SCRIPT_DEFINITIONS_FROM_CLASSPATH_OPTION)) null
|
||||||
listOf(
|
else
|
||||||
ScriptDefinitionsFromClasspathDiscoverySource(
|
ScriptDefinitionsFromClasspathDiscoverySource(
|
||||||
configuration.jvmClasspathRoots,
|
configuration.jvmClasspathRoots,
|
||||||
hostConfiguration,
|
hostConfiguration,
|
||||||
messageCollector.reporter
|
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
|
val scriptDefinitionProvider = ScriptDefinitionProvider.getInstance(project) as? CliScriptDefinitionProvider
|
||||||
|
|||||||
Reference in New Issue
Block a user