@@ -44,6 +44,7 @@ import org.jetbrains.kotlin.javac.JavacWrapper
44
44
import org.jetbrains.kotlin.load.java.JvmAbi
45
45
import org.jetbrains.kotlin.load.kotlin.incremental.components.IncrementalCompilationComponents
46
46
import org.jetbrains.kotlin.script.KotlinScriptDefinitionFromAnnotatedTemplate
47
+ import org.jetbrains.kotlin.script.KotlinScriptDefinitionProvider
47
48
import org.jetbrains.kotlin.script.StandardScriptDefinition
48
49
import org.jetbrains.kotlin.util.PerformanceCounter
49
50
import org.jetbrains.kotlin.utils.KotlinPaths
@@ -88,14 +89,7 @@ class K2JVMCompiler : CLICompiler<K2JVMCompilerArguments>() {
88
89
return INTERNAL_ERROR
89
90
}
90
91
91
- if (arguments.script) {
92
- if (arguments.freeArgs.isEmpty()) {
93
- messageCollector.report(ERROR , " Specify script source path to evaluate" )
94
- return COMPILATION_ERROR
95
- }
96
- configuration.addKotlinSourceRoot(arguments.freeArgs[0 ])
97
- }
98
- else if (arguments.module == null ) {
92
+ if (! arguments.script && arguments.module == null ) {
99
93
for (arg in arguments.freeArgs) {
100
94
val file = File (arg)
101
95
if (file.extension == JavaFileType .DEFAULT_EXTENSION ) {
@@ -116,6 +110,10 @@ class K2JVMCompiler : CLICompiler<K2JVMCompilerArguments>() {
116
110
configuration.put(CommonConfigurationKeys .MODULE_NAME , arguments.moduleName ? : JvmAbi .DEFAULT_MODULE_NAME )
117
111
118
112
if (arguments.module == null && arguments.freeArgs.isEmpty() && ! arguments.version) {
113
+ if (arguments.script) {
114
+ messageCollector.report(ERROR , " Specify script source path to evaluate" )
115
+ return COMPILATION_ERROR
116
+ }
119
117
ReplFromTerminal .run (rootDisposable, configuration)
120
118
return ExitCode .OK
121
119
}
@@ -174,13 +172,26 @@ class K2JVMCompiler : CLICompiler<K2JVMCompilerArguments>() {
174
172
KotlinToJVMBytecodeCompiler .compileModules(environment, directory)
175
173
}
176
174
else if (arguments.script) {
177
- val scriptArgs = arguments.freeArgs.subList(1 , arguments.freeArgs.size)
175
+ val sourcePath = arguments.freeArgs.first()
176
+ configuration.addKotlinSourceRoot(sourcePath)
178
177
179
178
configuration.put(JVMConfigurationKeys .RETAIN_OUTPUT_IN_MEMORY , true )
180
179
181
180
val environment = createEnvironmentWithScriptingSupport(rootDisposable, configuration, arguments, messageCollector)
182
181
? : return COMPILATION_ERROR
183
182
183
+ val scriptDefinitionProvider = KotlinScriptDefinitionProvider .getInstance(environment.project)!!
184
+ val scriptFile = File (sourcePath)
185
+ if (scriptFile.isDirectory || ! scriptDefinitionProvider.isScript(scriptFile)) {
186
+ val extensionHint =
187
+ if (configuration.get(JVMConfigurationKeys .SCRIPT_DEFINITIONS ) == listOf (StandardScriptDefinition )) " (.kts)"
188
+ else " "
189
+ messageCollector.report(ERROR , " Specify path to the script file$extensionHint as the first argument" )
190
+ return COMPILATION_ERROR
191
+ }
192
+
193
+ val scriptArgs = arguments.freeArgs.subList(1 , arguments.freeArgs.size)
194
+
184
195
return KotlinToJVMBytecodeCompiler .compileAndExecuteScript(environment, paths, scriptArgs)
185
196
}
186
197
else {
0 commit comments