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)
|
var noReflect: Boolean by FreezableVar(false)
|
||||||
|
|
||||||
@Argument(value = "-Xexpression", description = "Evaluate the given string as a Kotlin script")
|
@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(
|
@Argument(
|
||||||
value = "-script-templates",
|
value = "-script-templates",
|
||||||
|
|||||||
@@ -42,9 +42,17 @@ object Main {
|
|||||||
var collectingExpressions = false
|
var collectingExpressions = false
|
||||||
var needsCompiler = false
|
var needsCompiler = false
|
||||||
val arguments = arrayListOf<String>()
|
val arguments = arrayListOf<String>()
|
||||||
val expressions = arrayListOf<String>()
|
var expression: String? = null
|
||||||
var noReflect = false
|
var noReflect = false
|
||||||
|
|
||||||
|
fun setExpression(expr: String) {
|
||||||
|
if (expression == null) {
|
||||||
|
expression = expr
|
||||||
|
} else {
|
||||||
|
throw RunnerException("Only single -e/-expression argument supported")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var i = 0
|
var i = 0
|
||||||
while (i < args.size) {
|
while (i < args.size) {
|
||||||
val arg = args[i]
|
val arg = args[i]
|
||||||
@@ -58,7 +66,7 @@ object Main {
|
|||||||
|
|
||||||
if (collectingExpressions) {
|
if (collectingExpressions) {
|
||||||
if ("-expression" == arg || "-e" == arg) {
|
if ("-expression" == arg || "-e" == arg) {
|
||||||
expressions.add(next())
|
setExpression(next())
|
||||||
i++
|
i++
|
||||||
continue
|
continue
|
||||||
} else {
|
} else {
|
||||||
@@ -89,7 +97,7 @@ object Main {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if ("-expression" == arg || "-e" == arg) {
|
else if ("-expression" == arg || "-e" == arg) {
|
||||||
expressions.add(next())
|
setExpression(next())
|
||||||
collectingExpressions = true
|
collectingExpressions = true
|
||||||
needsCompiler = true
|
needsCompiler = true
|
||||||
}
|
}
|
||||||
@@ -125,8 +133,8 @@ object Main {
|
|||||||
classpath.addPath(KOTLIN_HOME.toString() + "/lib/kotlin-reflect.jar")
|
classpath.addPath(KOTLIN_HOME.toString() + "/lib/kotlin-reflect.jar")
|
||||||
}
|
}
|
||||||
|
|
||||||
if (expressions.isNotEmpty()) {
|
if (expression != null) {
|
||||||
runner = ExpressionRunner(expressions)
|
runner = ExpressionRunner(expression!!)
|
||||||
} else if (runner == null) {
|
} else if (runner == null) {
|
||||||
runner = ReplRunner()
|
runner = ReplRunner()
|
||||||
needsCompiler = true
|
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>) {
|
override fun run(classpath: List<URL>, arguments: List<String>, compilerClasspath: List<URL>) {
|
||||||
val compilerArgs = ArrayList<String>().apply {
|
val compilerArgs = ArrayList<String>().apply {
|
||||||
addClasspathArgIfNeeded(classpath)
|
addClasspathArgIfNeeded(classpath)
|
||||||
code.forEach {
|
add("-Xexpression=$code")
|
||||||
add("-Xexpression")
|
|
||||||
add(it)
|
|
||||||
}
|
|
||||||
addAll(arguments)
|
addAll(arguments)
|
||||||
}
|
}
|
||||||
runCompiler(compilerClasspath, compilerArgs)
|
runCompiler(compilerClasspath, compilerArgs)
|
||||||
|
|||||||
@@ -77,7 +77,7 @@ class K2JVMCompiler : CLICompiler<K2JVMCompilerArguments>() {
|
|||||||
configuration.configureAdvancedJvmOptions(arguments)
|
configuration.configureAdvancedJvmOptions(arguments)
|
||||||
|
|
||||||
if (arguments.buildFile == null && !arguments.version && !arguments.allowNoSourceFiles &&
|
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
|
// script or repl
|
||||||
if (arguments.script && arguments.freeArgs.isEmpty()) {
|
if (arguments.script && arguments.freeArgs.isEmpty()) {
|
||||||
@@ -92,7 +92,7 @@ class K2JVMCompiler : CLICompiler<K2JVMCompilerArguments>() {
|
|||||||
)
|
)
|
||||||
projectEnvironment.registerExtensionsFromPlugins(configuration)
|
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) }
|
val scriptingEvaluator = ScriptEvaluationExtension.getInstances(projectEnvironment.project).find { it.isAccepted(arguments) }
|
||||||
if (scriptingEvaluator == null) {
|
if (scriptingEvaluator == null) {
|
||||||
messageCollector.report(ERROR, "Unable to evaluate script, no scripting plugin loaded")
|
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");
|
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")
|
@TestMetadata("extraArgumentPassedInObsoleteForm.args")
|
||||||
public void testExtraArgumentPassedInObsoleteForm() throws Exception {
|
public void testExtraArgumentPassedInObsoleteForm() throws Exception {
|
||||||
runTest("compiler/testData/cli/jvm/extraArgumentPassedInObsoleteForm.args");
|
runTest("compiler/testData/cli/jvm/extraArgumentPassedInObsoleteForm.args");
|
||||||
|
|||||||
@@ -149,4 +149,16 @@ class LauncherScriptTest : TestCaseWithTmpdir() {
|
|||||||
workDirectory = tmpdir
|
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)
|
setupScriptConfiguration(configuration)
|
||||||
|
|
||||||
val script = when {
|
val script = when {
|
||||||
arguments is K2JVMCompilerArguments && arguments.expressions != null -> {
|
arguments is K2JVMCompilerArguments && arguments.expression != null -> {
|
||||||
StringScriptSource(arguments.expressions!!.joinToString("\n"), "script.kts")
|
StringScriptSource(arguments.expression!!, "script.kts")
|
||||||
}
|
}
|
||||||
arguments.script -> {
|
arguments.script -> {
|
||||||
val scriptFile = File(arguments.freeArgs.first())
|
val scriptFile = File(arguments.freeArgs.first())
|
||||||
|
|||||||
+1
-1
@@ -47,6 +47,6 @@ class JvmCliScriptEvaluationExtension : AbstractScriptEvaluationExtension() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun isAccepted(arguments: CommonCompilerArguments): Boolean =
|
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)
|
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 {
|
cacheLock.write {
|
||||||
cache.put(path, result)
|
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::commonSources,
|
||||||
K2JVMCompilerArguments::allWarningsAsErrors,
|
K2JVMCompilerArguments::allWarningsAsErrors,
|
||||||
K2JVMCompilerArguments::script,
|
K2JVMCompilerArguments::script,
|
||||||
K2JVMCompilerArguments::expressions,
|
K2JVMCompilerArguments::expression,
|
||||||
K2JVMCompilerArguments::scriptTemplates,
|
K2JVMCompilerArguments::scriptTemplates,
|
||||||
K2JVMCompilerArguments::scriptResolverEnvironment,
|
K2JVMCompilerArguments::scriptResolverEnvironment,
|
||||||
K2JVMCompilerArguments::disableStandardScript,
|
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> {
|
private fun getMainKtsClassPath(): List<File> {
|
||||||
return listOf(
|
return listOf(
|
||||||
|
|||||||
Reference in New Issue
Block a user