kscripting / kscript

Scripting enhancements for Kotlin
MIT License
2.09k stars 126 forks source link

Match annotations behavior of kotlinc #388

Open gabrielfeo opened 1 year ago

gabrielfeo commented 1 year ago

It'd be great if kscript could match behavior to be 100% compatible with the 'Kotlin scripting support' annotations. Even if one writes a script aiming for the standard compiler command, I could run it with kscript and benefit from caching and its other features.

vararg support

Adding multiple repositories with a single annotation (source):

@file:Repository("https://jitpack.io", "https://repo.maven.apache.org/maven2")
@file:DependsOn("com.github.gabrielfeo:gradle-enterprise-api-kotlin:0.11.0", "org.jetbrains.kotlinx:dataframe:0.9.1")

but kscript doesn't support vararg in Repository, parsing the second arg as a repository username:

[kscript] Adding repository: Repository(id=, url=https://jitpack.io, user=https://repo.maven.apache.org/maven2, password=)

Line break support

Breaking line inside annotations is supported by kotlinc, but isn't supported by kscript:

Line break in DependsOn

@file:Repository("https://repo.maven.apache.org/maven2")
@file:DependsOn(
    "com.squareup.okio:okio:3.3.0",
    "org.jetbrains.kotlinx:dataframe:0.9.1",
)
[kscript] Adding repository: Repository(id=, url=https://repo.maven.apache.org/maven2, user=, password=)
[kscript] [ERROR] Compilation of scriplet failed:
[kscript] [ERROR] Command     : 'bash -c /opt/homebrew/Cellar/kotlin/1.8.0/libexec/bin/kotlinc   -d '/Users/gabriel.feo/.cache/kscript/jar_6d5e6178bda10b7c462025e5f8fe444a/scriplet.jar' '/Users/gabriel.feo/.cache/kscript/jar_6d5e6178bda10b7c462025e5f8fe444a/Dataframe_test_main.kts' '/Users/gabriel.feo/.cache/kscript/jar_6d5e6178bda10b7c462025e5f8fe444a/Main_Dataframe_test_main.kt''
[kscript] [ERROR] Exit Code   : 1   
[kscript] [ERROR] Stdout      : ''
[kscript] [ERROR] Stderr      : '/Users/gabriel.feo/.cache/kscript/jar_6d5e6178bda10b7c462025e5f8fe444a/Dataframe_test_main.kts:6:2: error: expecting an expression[nl])[nl] ^[nl]/Users/gabriel.feo/.cache/kscript/jar_6d5e6178bda10b7c462025e5f8fe444a/Dataframe_test_main.kts:3:7: error: unresolved reference: DependsOn[nl]@file:DependsOn([nl]      ^[nl]'
[kscript] [ERROR]

Line break in Repository

@file:Repository(
    "https://jitpack.io",
    "https://repo.maven.apache.org/maven2",
)
@file:DependsOn("com.squareup.okio:okio:3.3.0", "org.jetbrains.kotlinx:dataframe:0.9.1")
[kscript] Adding repository: Repository(id=, url=, user=, password=)

Note: using multiple Repository and DependsOn annotations has the same behavior in both kotlinc and kscript, a good workaround to make scripts compatible with both

@file:Repository("https://jitpack.io")
@file:Repository("https://repo.maven.apache.org/maven2")
@file:DependsOn("com.github.gabrielfeo:gradle-enterprise-api-kotlin:0.11.0")
@file:DependsOn("org.jetbrains.kotlinx:dataframe:0.9.1")
aartiPl commented 1 year ago

Yes, your comment is entirely valid. My ultimate goal is to switch to Kotlin scripting backend while keeping all the excellent features of KScript (like interoperability with shell, caching, etc.). The only question is how much effort to put in to stay perfectly compatible, while in the long run, I will switch to Kotlin scripting anyway. Currently, in my opinion, it is not worthy of effort, as I would like to switch to the Kotlin scripting backend after version 4.2, which I am about to release. But still, if anyone wants to improve KScript with such features, I am happy to apply patches.