diff --git a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JVMCompilerArguments.kt b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JVMCompilerArguments.kt index 2e2b92213dc..ce00755c54c 100644 --- a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JVMCompilerArguments.kt +++ b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JVMCompilerArguments.kt @@ -50,7 +50,7 @@ class K2JVMCompilerArguments : CommonCompilerArguments() { var noReflect: Boolean by FreezableVar(false) @Argument(value = "-Xexpression", description = "Evaluate the given string as a Kotlin script") - var expressions: Array? by FreezableVar(null) + var expression: String? by FreezableVar(null) @Argument( value = "-script-templates", diff --git a/compiler/cli/cli-runner/src/org/jetbrains/kotlin/runner/Main.kt b/compiler/cli/cli-runner/src/org/jetbrains/kotlin/runner/Main.kt index 6fc496d9cfa..fe7c94c71b8 100644 --- a/compiler/cli/cli-runner/src/org/jetbrains/kotlin/runner/Main.kt +++ b/compiler/cli/cli-runner/src/org/jetbrains/kotlin/runner/Main.kt @@ -42,9 +42,17 @@ object Main { var collectingExpressions = false var needsCompiler = false val arguments = arrayListOf() - val expressions = arrayListOf() + var expression: String? = null var noReflect = false + fun setExpression(expr: String) { + if (expression == null) { + expression = expr + } else { + throw RunnerException("Only single -e/-expression argument supported") + } + } + var i = 0 while (i < args.size) { val arg = args[i] @@ -58,7 +66,7 @@ object Main { if (collectingExpressions) { if ("-expression" == arg || "-e" == arg) { - expressions.add(next()) + setExpression(next()) i++ continue } else { @@ -89,7 +97,7 @@ object Main { } } else if ("-expression" == arg || "-e" == arg) { - expressions.add(next()) + setExpression(next()) collectingExpressions = true needsCompiler = true } @@ -125,8 +133,8 @@ object Main { classpath.addPath(KOTLIN_HOME.toString() + "/lib/kotlin-reflect.jar") } - if (expressions.isNotEmpty()) { - runner = ExpressionRunner(expressions) + if (expression != null) { + runner = ExpressionRunner(expression!!) } else if (runner == null) { runner = ReplRunner() needsCompiler = true diff --git a/compiler/cli/cli-runner/src/org/jetbrains/kotlin/runner/runners.kt b/compiler/cli/cli-runner/src/org/jetbrains/kotlin/runner/runners.kt index 9f1b9661cc4..70801d79815 100644 --- a/compiler/cli/cli-runner/src/org/jetbrains/kotlin/runner/runners.kt +++ b/compiler/cli/cli-runner/src/org/jetbrains/kotlin/runner/runners.kt @@ -147,14 +147,11 @@ class ScriptRunner(private val path: String) : RunnerWithCompiler() { } } -class ExpressionRunner(private val code: List) : RunnerWithCompiler() { +class ExpressionRunner(private val code: String) : RunnerWithCompiler() { override fun run(classpath: List, arguments: List, compilerClasspath: List) { val compilerArgs = ArrayList().apply { addClasspathArgIfNeeded(classpath) - code.forEach { - add("-Xexpression") - add(it) - } + add("-Xexpression=$code") addAll(arguments) } runCompiler(compilerClasspath, compilerArgs) diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/K2JVMCompiler.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/K2JVMCompiler.kt index a0358e15d82..e6e28fe294a 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/K2JVMCompiler.kt +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/K2JVMCompiler.kt @@ -77,7 +77,7 @@ class K2JVMCompiler : CLICompiler() { configuration.configureAdvancedJvmOptions(arguments) if (arguments.buildFile == null && !arguments.version && !arguments.allowNoSourceFiles && - (arguments.script || arguments.expressions != null || arguments.freeArgs.isEmpty())) { + (arguments.script || arguments.expression != null || arguments.freeArgs.isEmpty())) { // script or repl if (arguments.script && arguments.freeArgs.isEmpty()) { @@ -92,7 +92,7 @@ class K2JVMCompiler : CLICompiler() { ) projectEnvironment.registerExtensionsFromPlugins(configuration) - if (arguments.script || arguments.expressions != null) { + if (arguments.script || arguments.expression != null) { val scriptingEvaluator = ScriptEvaluationExtension.getInstances(projectEnvironment.project).find { it.isAccepted(arguments) } if (scriptingEvaluator == null) { messageCollector.report(ERROR, "Unable to evaluate script, no scripting plugin loaded") diff --git a/compiler/testData/cli/jvm/expression1.args b/compiler/testData/cli/jvm/expression1.args new file mode 100644 index 00000000000..0cfa0de1b50 --- /dev/null +++ b/compiler/testData/cli/jvm/expression1.args @@ -0,0 +1,4 @@ +-Xexpression=val x = 2; System.err.println((args + listOf(2,1).map { (it * x).toString() }).joinToString()) +-- +a +b diff --git a/compiler/testData/cli/jvm/expression1.out b/compiler/testData/cli/jvm/expression1.out new file mode 100644 index 00000000000..ee528105cba --- /dev/null +++ b/compiler/testData/cli/jvm/expression1.out @@ -0,0 +1,2 @@ +a, b, 4, 2 +OK \ No newline at end of file diff --git a/compiler/tests/org/jetbrains/kotlin/cli/CliTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/cli/CliTestGenerated.java index 42b71d80e2c..b3ae25e636e 100644 --- a/compiler/tests/org/jetbrains/kotlin/cli/CliTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/cli/CliTestGenerated.java @@ -230,6 +230,11 @@ public class CliTestGenerated extends AbstractCliTest { runTest("compiler/testData/cli/jvm/explicitlyDisabledSamConversions.args"); } + @TestMetadata("expression1.args") + public void testExpression1() throws Exception { + runTest("compiler/testData/cli/jvm/expression1.args"); + } + @TestMetadata("extraArgumentPassedInObsoleteForm.args") public void testExtraArgumentPassedInObsoleteForm() throws Exception { runTest("compiler/testData/cli/jvm/extraArgumentPassedInObsoleteForm.args"); diff --git a/compiler/tests/org/jetbrains/kotlin/cli/LauncherScriptTest.kt b/compiler/tests/org/jetbrains/kotlin/cli/LauncherScriptTest.kt index 1d09a87e6e3..b76b196f62b 100644 --- a/compiler/tests/org/jetbrains/kotlin/cli/LauncherScriptTest.kt +++ b/compiler/tests/org/jetbrains/kotlin/cli/LauncherScriptTest.kt @@ -149,4 +149,16 @@ class LauncherScriptTest : TestCaseWithTmpdir() { workDirectory = tmpdir ) } + + fun testRunnerExpression() { + runProcess( + "kotlin", + "-e", + "val x = 2; (args + listOf(2,1).map { (it * x).toString() }).joinToString()", + "--", + "a", + "b", + expectedStdout = "a, b, 4, 2\n" + ) + } } diff --git a/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/compiler/plugin/AbstractScriptEvaluationExtension.kt b/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/compiler/plugin/AbstractScriptEvaluationExtension.kt index ed8cee51a99..63066af362d 100644 --- a/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/compiler/plugin/AbstractScriptEvaluationExtension.kt +++ b/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/compiler/plugin/AbstractScriptEvaluationExtension.kt @@ -53,8 +53,8 @@ abstract class AbstractScriptEvaluationExtension : ScriptEvaluationExtension { setupScriptConfiguration(configuration) val script = when { - arguments is K2JVMCompilerArguments && arguments.expressions != null -> { - StringScriptSource(arguments.expressions!!.joinToString("\n"), "script.kts") + arguments is K2JVMCompilerArguments && arguments.expression != null -> { + StringScriptSource(arguments.expression!!, "script.kts") } arguments.script -> { val scriptFile = File(arguments.freeArgs.first()) diff --git a/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/compiler/plugin/JvmCliScriptEvaluationExtension.kt b/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/compiler/plugin/JvmCliScriptEvaluationExtension.kt index 1c2ad99a71e..9a17e2765f8 100644 --- a/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/compiler/plugin/JvmCliScriptEvaluationExtension.kt +++ b/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/compiler/plugin/JvmCliScriptEvaluationExtension.kt @@ -47,6 +47,6 @@ class JvmCliScriptEvaluationExtension : AbstractScriptEvaluationExtension() { } override fun isAccepted(arguments: CommonCompilerArguments): Boolean = - arguments is K2JVMCompilerArguments && (arguments.script || arguments.expressions != null) + arguments is K2JVMCompilerArguments && (arguments.script || arguments.expression != null) } diff --git a/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/compiler/plugin/definitions/CliScriptDependenciesProvider.kt b/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/compiler/plugin/definitions/CliScriptDependenciesProvider.kt index 4b653a560a6..231ef32ea30 100644 --- a/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/compiler/plugin/definitions/CliScriptDependenciesProvider.kt +++ b/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/compiler/plugin/definitions/CliScriptDependenciesProvider.kt @@ -40,11 +40,6 @@ class CliScriptDependenciesProvider(project: Project) : ScriptDependenciesProvid ServiceManager.getService(project, ScriptReportSink::class.java)?.attachReports(file.virtualFile, result.reports) - if (result is ResultWithDiagnostics.Success) { - log.info("[kts] new cached deps for $path: ${result.value.dependenciesClassPath.joinToString(File.pathSeparator)}") - } else { - log.info("[kts] new cached errors for $path:\n ${result.reports.joinToString("\n ") { it.message + if (it.exception == null) "" else ": ${it.exception}" }}") - } cacheLock.write { cache.put(path, result) } @@ -53,5 +48,3 @@ class CliScriptDependenciesProvider(project: Project) : ScriptDependenciesProvid } } } - -private val log = Logger.getInstance(ScriptDependenciesProvider::class.java) diff --git a/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/compiler/plugin/impl/errorReporting.kt b/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/compiler/plugin/impl/errorReporting.kt index 9572187142c..db6481c305d 100644 --- a/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/compiler/plugin/impl/errorReporting.kt +++ b/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/compiler/plugin/impl/errorReporting.kt @@ -99,7 +99,7 @@ internal fun reportArgumentsIgnoredGenerally( K2JVMCompilerArguments::commonSources, K2JVMCompilerArguments::allWarningsAsErrors, K2JVMCompilerArguments::script, - K2JVMCompilerArguments::expressions, + K2JVMCompilerArguments::expression, K2JVMCompilerArguments::scriptTemplates, K2JVMCompilerArguments::scriptResolverEnvironment, K2JVMCompilerArguments::disableStandardScript, diff --git a/plugins/scripting/scripting-compiler/tests/org/jetbrains/kotlin/scripting/compiler/plugin/ScriptingWithCliCompilerTest.kt b/plugins/scripting/scripting-compiler/tests/org/jetbrains/kotlin/scripting/compiler/plugin/ScriptingWithCliCompilerTest.kt index e8b889f462c..dc82869ae3a 100644 --- a/plugins/scripting/scripting-compiler/tests/org/jetbrains/kotlin/scripting/compiler/plugin/ScriptingWithCliCompilerTest.kt +++ b/plugins/scripting/scripting-compiler/tests/org/jetbrains/kotlin/scripting/compiler/plugin/ScriptingWithCliCompilerTest.kt @@ -60,6 +60,16 @@ class ScriptingWithCliCompilerTest { ) } + @Test + fun testExpressionWithComma() { + runWithK2JVMCompiler( + arrayOf( + "-Xexpression", + "listOf(1,2)" + ), + listOf("\\[1, 2\\]") + ) + } private fun getMainKtsClassPath(): List { return listOf(