Add new compiler argument for passing script resolver environment

This commit is contained in:
Ilya Chernikov
2017-03-31 15:38:11 +02:00
parent 234ad39fd6
commit 0199ec18b1
4 changed files with 41 additions and 1 deletions
@@ -111,6 +111,10 @@ public class K2JVMCompilerArguments extends CommonCompilerArguments {
@Argument(value = "Xload-builtins-from-dependencies", description = "Load definitions of built-in declarations from module dependencies, instead of from the compiler")
public boolean loadBuiltInsFromDependencies;
@Argument(value = "Xscript-resolver-environment", description = "Script resolver environment in key-value pairs (the value could be quoted and escaped)")
@ValueDescription("<key=value[,]>")
public String[] scriptResolverEnvironment;
// Paths to output directories for friend modules.
public String[] friendPaths;
@@ -214,13 +214,14 @@ class K2JVMCompiler : CLICompiler<K2JVMCompilerArguments>() {
}
}
private fun createEnvironmentWithScriptingSupport(rootDisposable: Disposable,
configuration: CompilerConfiguration,
arguments: K2JVMCompilerArguments,
messageCollector: MessageCollector
): KotlinCoreEnvironment? {
val scriptResolverEnv = hashMapOf<String, Any?>()
val scriptResolverEnv = createScriptResolverEnvironment(arguments, messageCollector) ?: return null
configureScriptDefinitions(arguments.scriptTemplates, configuration, messageCollector, scriptResolverEnv)
if (!messageCollector.hasErrors()) {
val environment = createCoreEnvironment(rootDisposable, configuration)
@@ -415,6 +416,27 @@ class K2JVMCompiler : CLICompiler<K2JVMCompilerArguments>() {
}
configuration.add(JVMConfigurationKeys.SCRIPT_DEFINITIONS, StandardScriptDefinition)
}
fun createScriptResolverEnvironment(arguments: K2JVMCompilerArguments, messageCollector: MessageCollector): HashMap<String, Any?>? {
val scriptResolverEnv = hashMapOf<String, Any?>()
// parses key/value pairs in the form <key>=<value>, where
// <key> - is a single word (\w+ pattern)
// <value> - optionally quoted string with allowed escaped chars (only double-quote and backslash chars are supported)
// TODO: implement generic unescaping
val envParseRe = """(\w+)=(?:"((?:\\.|[^"])*)"|([^\s]*))""".toRegex()
val unescapeRe = """\\(["\\])""".toRegex()
if (arguments.scriptResolverEnvironment != null) {
for (envParam in arguments.scriptResolverEnvironment) {
val match = envParseRe.matchEntire(envParam)
if (match == null || match.groupValues.size < 4 || match.groupValues[1].isBlank()) {
messageCollector.report(CompilerMessageSeverity.ERROR, "Unable to parse script-resolver-environment argument $envParam", CompilerMessageLocation.NO_LOCATION)
return null
}
scriptResolverEnv.put(match.groupValues[1], match.groupValues.drop(2).firstOrNull { it.isNotEmpty() }?.let { unescapeRe.replace(it, "\$1") })
}
}
return scriptResolverEnv
}
}
}
+2
View File
@@ -11,6 +11,8 @@ where advanced options include:
-Xadd-compiler-builtins Add definitions of built-in declarations to the compilation classpath (useful with -no-stdlib)
-Xload-builtins-from-dependencies
Load definitions of built-in declarations from module dependencies, instead of from the compiler
-Xscript-resolver-environment <key=value[,]>
Script resolver environment in key-value pairs (the value could be quoted and escaped)
-Xno-inline Disable method inlining
-Xrepeat <count> Repeat compilation (for performance analysis)
-Xskip-metadata-version-check Load classes with bad metadata version anyway (incl. pre-release classes)
@@ -92,6 +92,18 @@ class CompilerApiTest : KotlinIntegrationTestBase() {
}
}
fun testScriptResolverEnvironmentArgsParsing() {
fun args(body: K2JVMCompilerArguments.() -> Unit): K2JVMCompilerArguments =
K2JVMCompilerArguments().apply(body)
val messageCollector = TestMessageCollector()
Assert.assertEquals(hashMapOf("abc" to "def", "11" to "ab cd \\ \""),
K2JVMCompiler.createScriptResolverEnvironment(
args { scriptResolverEnvironment = arrayOf("abc=def", """11="ab cd \\ \""""") },
messageCollector))
}
fun testHelloAppLocal() {
val messageCollector = TestMessageCollector()
val jar = tmpdir.absolutePath + File.separator + "hello.jar"