The content of this website is only deployed under the domain
𝙨𝙘𝙤𝙩𝙩𝙮𝙚𝙪𝙣𝙜.𝙩𝙤𝙥(scottyeung[dot]top). Any other domain sites are unauthorized
illegal mirrors.
importkotlin.script.experimental.annotations.KotlinScript// The KotlinScript annotation marks a class that can serve as a reference to the script definition for
// `createJvmCompilationConfigurationFromTemplate` call as well as for the discovery mechanism
// The marked class also become the base class for defined script type (unless redefined in the configuration)
@KotlinScript(// file name extension by which this script type is recognized by mechanisms built into scripting compiler plugin
// and IDE support, it is recommendend to use double extension with the last one being "kts", so some non-specific
// scripting support could be used, e.g. in IDE, if the specific support is not installed.
fileExtension="simplescript.kts")// the class is used as the script base class, therefore it should be open or abstract
abstractclassSimpleScript
可以通过配置 @KotlinScript 注解来声明脚本的编译配置和执行配置
1
2
3
4
5
6
7
8
9
10
11
@KotlinScript(fileExtension="smain.kts",// the class or object that defines script compilation configuration for this type of scripts
compilationConfiguration=SimpleMainKtsScriptDefinition::class,// the class or object that defines script evaluation configuration for this type of scripts
evaluationConfiguration=MainKtsEvaluationConfiguration::class)// the class is used as the script base class, therefore it should be open or abstract. Also the constructor parameters
// of the base class are copied to the script constructor, so with this definition the script will require `args` to be
// passed to the constructor, and `args` could be used in the script as a defined variable.
abstractclassSimpleMainKtsScript(valargs:Array<String>)
根据声明的 script class 来创建运行的 host。host 的创建其实就是通过之前注解声明的 Script class 来创建编译的 configuration,另外根据需要来创建 evaluate 时的 configuration,来编译执行。
1
2
3
4
5
6
7
8
9
valcompilationConfiguration=createJvmCompilationConfigurationFromTemplate<SimpleScript>{jvm{// 声明需要的依赖,用于编译
dependenciesFromCurrentContext("script"/* script library jar name (exact or without a version) */)}}BasicJvmScriptingHost().eval(scriptFile.toScriptSource(),compilationConfiguration,null)
而且官方仓库还说 idea 支持 main.kts 脚本的补全提示,看起来就更香了,用起来也还行,补全还是挺香的,尤其是加上 GitHub Copliot,起飞。
Starting from the Kotlin IntelliJ plugin version 1.3.70, the .main.kts scripts are supported automatically in the IntelliJ IDEA, provided that they are placed outside of the regular source folders. E.g. if this project is imported into the IntelliJ, the demo scripts in the scripts folders should be properly highlighted and support navigation, including navigation into imported libraries.
这样调研了一轮,看起来用 kotlin 脚本,方便地跑起来现在已经是没有问题了,问题就在于如何方便地写。写个小东西打开 idea 是否方便呢?如果是以前的 Windows,打开一次得等上个几分钟,那可以直接否掉了。但是现在新电脑打开 idea 几乎是秒开,看起来使用 idea 来写脚本好像又变得可以接受了。