Allow only single expression eval in cli compiler and kotlin runner
#KT-35740 fixed also add tests and drop logger usage in the cli dependencies manager: the logger is normally unitialized in the usage scenarios, but related warnings are annoying.
This commit is contained in:
+1
-1
@@ -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<String>? by FreezableVar(null)
|
||||
var expression: String? by FreezableVar(null)
|
||||
|
||||
@Argument(
|
||||
value = "-script-templates",
|
||||
|
||||
@@ -42,9 +42,17 @@ object Main {
|
||||
var collectingExpressions = false
|
||||
var needsCompiler = false
|
||||
val arguments = arrayListOf<String>()
|
||||
val expressions = arrayListOf<String>()
|
||||
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
|
||||
|
||||
@@ -147,14 +147,11 @@ class ScriptRunner(private val path: String) : RunnerWithCompiler() {
|
||||
}
|
||||
}
|
||||
|
||||
class ExpressionRunner(private val code: List<String>) : RunnerWithCompiler() {
|
||||
class ExpressionRunner(private val code: String) : RunnerWithCompiler() {
|
||||
override fun run(classpath: List<URL>, arguments: List<String>, compilerClasspath: List<URL>) {
|
||||
val compilerArgs = ArrayList<String>().apply {
|
||||
addClasspathArgIfNeeded(classpath)
|
||||
code.forEach {
|
||||
add("-Xexpression")
|
||||
add(it)
|
||||
}
|
||||
add("-Xexpression=$code")
|
||||
addAll(arguments)
|
||||
}
|
||||
runCompiler(compilerClasspath, compilerArgs)
|
||||
|
||||
@@ -77,7 +77,7 @@ class K2JVMCompiler : CLICompiler<K2JVMCompilerArguments>() {
|
||||
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<K2JVMCompilerArguments>() {
|
||||
)
|
||||
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")
|
||||
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
-Xexpression=val x = 2; System.err.println((args + listOf(2,1).map { (it * x).toString() }).joinToString())
|
||||
--
|
||||
a
|
||||
b
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
a, b, 4, 2
|
||||
OK
|
||||
@@ -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");
|
||||
|
||||
@@ -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"
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
+2
-2
@@ -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())
|
||||
|
||||
+1
-1
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
-7
@@ -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)
|
||||
|
||||
+1
-1
@@ -99,7 +99,7 @@ internal fun reportArgumentsIgnoredGenerally(
|
||||
K2JVMCompilerArguments::commonSources,
|
||||
K2JVMCompilerArguments::allWarningsAsErrors,
|
||||
K2JVMCompilerArguments::script,
|
||||
K2JVMCompilerArguments::expressions,
|
||||
K2JVMCompilerArguments::expression,
|
||||
K2JVMCompilerArguments::scriptTemplates,
|
||||
K2JVMCompilerArguments::scriptResolverEnvironment,
|
||||
K2JVMCompilerArguments::disableStandardScript,
|
||||
|
||||
+10
@@ -60,6 +60,16 @@ class ScriptingWithCliCompilerTest {
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testExpressionWithComma() {
|
||||
runWithK2JVMCompiler(
|
||||
arrayOf(
|
||||
"-Xexpression",
|
||||
"listOf(1,2)"
|
||||
),
|
||||
listOf("\\[1, 2\\]")
|
||||
)
|
||||
}
|
||||
|
||||
private fun getMainKtsClassPath(): List<File> {
|
||||
return listOf(
|
||||
|
||||
Reference in New Issue
Block a user