|
| 1 | + |
| 2 | +# Kotlin Scripting Examples: `kotlin-main-kts` usages |
| 3 | + |
| 4 | +Scripts demonstrating the usage of the `kotlin-main-kts` script definition jar, distributied with the Kotlin compiler |
| 5 | +and IntelliJ plugin. |
| 6 | + |
| 7 | +*See also [simplified implementation in this repository](../simple-main-kts/SimpleMainKts.md)* |
| 8 | + |
| 9 | +## Description |
| 10 | + |
| 11 | +The purpose of the `main-kts` is to allow writing simple but extendable utility scripts for the usage in the command |
| 12 | +line, replacing the simple Kotlin programs with `main` function (hence the `main` in its name). |
| 13 | + |
| 14 | +### Usage |
| 15 | + |
| 16 | +For example a script (note that the file should have an extension corresponding to the script definition, in this case |
| 17 | +`.smain.kts`): |
| 18 | + |
| 19 | +```kotlin |
| 20 | +println("Hello, ${args[0]}") |
| 21 | +``` |
| 22 | + |
| 23 | +could be executed with the command |
| 24 | + |
| 25 | +``` |
| 26 | +kotlinc -cp <path/to/kotlin-main-kts.jar> script.main.kts |
| 27 | +``` |
| 28 | + |
| 29 | +and starting from Kotlin version 1.3.70 it could even be used without explicit `kotlin-main-kts.jar` in the classpath, |
| 30 | +provided that the compiler could find the required jars. In addition starting from 1.3.70 `kotlin` runner supports |
| 31 | +scripts the same way as `kotlinc -script` combination: |
| 32 | + |
| 33 | +``` |
| 34 | +kotlin script.main.kts |
| 35 | +``` |
| 36 | + |
| 37 | +or even as simple as |
| 38 | + |
| 39 | +``` |
| 40 | +./script.main.kts |
| 41 | +``` |
| 42 | + |
| 43 | +provided that the shebang line is added to the script and works in the given OS shell. *(See examples below.)* |
| 44 | + |
| 45 | +### Caching |
| 46 | + |
| 47 | +The compiled scripts are cashed to the directory defined by an environmen variable `KOTLIN_MAIN_KTS_COMPILED_SCRIPTS_CACHE_DIR` |
| 48 | +*(`$TEMP/main.kts.compiled.cache` by default)*, and if the script is not changed, the compiled one is executed from the cache. |
| 49 | + |
| 50 | +### IntelliJ support |
| 51 | + |
| 52 | +Starting from the Kotlin IntelliJ plugin version 1.3.70, the `.main.kts` scripts are supported automatically in the |
| 53 | +IntelliJ IDEA, provided that they are placed outside of the regular source folders. E.g. if this project is imported into |
| 54 | +the IntelliJ, the demo scripts in the [`scripts`](scripts) folders should be properly highlighted and support navigation, |
| 55 | +including navigation into imported libraries. |
| 56 | + |
| 57 | +## Demo Scripts |
| 58 | + |
| 59 | +- [`kotlinx-html.main.kts`](scripts/kotlinx-html.main.kts) demonstrates usage of the |
| 60 | +[`kotlinx-html` library](https://github.com/Kotlin/kotlinx.html) by JetBrains, to generate HTML output |
| 61 | +- [`kotlin-shell.main.kts`](scripts/kotlin-shell.main.kts) demonstrates usage of the |
| 62 | +[`kotlin-shell` library](https://github.com/jakubriegel/kotlin-shell) by Jakub Riegel, to execute OS shell commands |
| 63 | +with easy interaction with Kotlin code |
0 commit comments