https://kotlinlang.org logo
#scripting
Title
# scripting
e

elect

07/14/2022, 7:10 AM
I noticed that running the script via
main
(as suggested by docs) then the evaluation and configuration gets called. If I try using the IDE instead, no. Is this expected? Do I have to do something special for the Idea to recognize them? (ie meta-inf)
I guess I have to use the same script compilation configuration and evaluation for both
@KotlinScript
and host execution/evaluation, however at the moment I'm testing via
main
and I'm building my cfg as:
Copy code
private fun File.evaluate(): ResultWithDiagnostics<EvaluationResult> {
    val compilationConfiguration = createJvmCompilationConfigurationFromTemplate<Script> {
        jvm {
            dependenciesFromCurrentContext(wholeClasspath = true)
        }
        defaultImports(Import::class)
        refineConfiguration {
            onAnnotations(Import::class, handler = handler)
        }
    }
    return BasicJvmScriptingHost().eval(toScriptSource(), compilationConfiguration, ScriptEvaluationConfiguration().with {
        constructorArgs(name)
    })
}
the moment I try to extract this logic into a class (in order to pass it to
@KotlinScript
as well) like:
Copy code
class TfConfiguration : ScriptCompilationConfiguration(createJvmCompilationConfigurationFromTemplate<Script>(), body = {
    jvm {
        dependenciesFromCurrentContext(wholeClasspath = true)
    }
    defaultImports(Import::class)
    refineConfiguration {
        onAnnotations(Import::class, handler = handler)
    }
})
then I get this enormous stacktrace
i

ilya.chernikov

07/20/2022, 7:16 AM
Extracting configuration creation to classes (one for compilation conf and one for evaluation conf) is a good idea - this way various "universal hosts" like
kotlinc
or scripting subsystems in the IntelliJ is able to use the script. It seems you've already mentioned "simple main kts", so it shouws the correct way.
As of the stacktrace, it looks like you have some recursive call into configuration creation logic or something.
To make it recognizable in IntelliJ, the best approach would be to create an IntelliJ plugin that will implement the
ScriptDefinitionsProvider
extension point. But you can test it locally (disclaimer - this is not an officially supported way) by going to kotlin compilersettings page in the IntelliJ settings, find two empty entry fields at the bottom in the section "Scripting", and enter the FQN of your script definition class (the one marked with
@KotlinScript
) into the first one, and the classpath needed to load it into the second.
e

elect

07/20/2022, 7:54 AM
I gave up on this path. I'm using the kotlin smain project as a base to build things up on
thanks nonetheless
I just tried the unofficial way, I used the FQN (same as the filename under
META-INF.kotlin.script.templates
?) and the absolute path on the system to load it, but I keep getting
warning: default scripting plugin is disabled: The provided plugin org.jetbrains.kotlin.scripting.compiler.plugin.ScriptingCompilerConfigurationComponentRegistrar is not compatible with this version of compiler
error: unable to evaluate script, no scripting plugin loaded
i

ilya.chernikov

07/20/2022, 8:22 AM
I looks like this is broken the same way as with
smain
script. Please add this info to the issue then you'll be filing it.
e

elect

07/20/2022, 8:24 AM
🙏 1
33 Views