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:
Ilya Chernikov
2020-01-08 18:43:44 +01:00
parent 7ffb64ec57
commit 995c6a32b8
13 changed files with 55 additions and 24 deletions
@@ -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")