Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e995fd8

Browse files
committed
Add main-kts demo scripts
1 parent c551db6 commit e995fd8

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-0
lines changed

‎jvm/main-kts/MainKts.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/usr/bin/env kotlin
2+
3+
@file:Repository("https://dl.bintray.com/jakubriegel/kotlin-shell")
4+
@file:DependsOn("eu.jrie.jetbrains:kotlin-shell-core:0.2.1")
5+
@file:DependsOn("org.slf4j:slf4j-simple:1.7.28")
6+
@file:CompilerOptions("-Xopt-in=kotlin.RequiresOptIn")
7+
@file:OptIn(kotlinx.coroutines.ExperimentalCoroutinesApi::class)
8+
9+
import eu.jrie.jetbrains.kotlinshell.shell.*
10+
11+
shell {
12+
if (args.isEmpty()) {
13+
"ls -l"()
14+
} else {
15+
var lines = 0
16+
var words = 0
17+
var chars = 0
18+
19+
var wasSpace = false
20+
21+
pipeline {
22+
"cat ${args[0]}".process() pipe
23+
streamLambda { strm, _, _ ->
24+
while (true) {
25+
val byte = strm.read()
26+
if (byte < 0) break
27+
val ch = byte.toChar()
28+
chars++
29+
if (ch == '\n') lines++
30+
val isSpace = ch == '\n' || ch == '\t' || ch == ' '
31+
if (!wasSpace && isSpace) {
32+
wasSpace = true
33+
} else if (wasSpace && !isSpace) {
34+
words++
35+
wasSpace = false
36+
}
37+
}
38+
}
39+
}
40+
41+
println("My wc:")
42+
println("$lines $words $chars")
43+
println("System wc:")
44+
"wc ${args[0]}"()
45+
}
46+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env kotlin
2+
3+
@file:Repository("https://jcenter.bintray.com")
4+
@file:DependsOn("org.jetbrains.kotlinx:kotlinx-html-jvm:0.6.11")
5+
6+
import kotlinx.html.*; import kotlinx.html.stream.*; import kotlinx.html.attributes.*
7+
8+
val addressee = args.firstOrNull() ?: "World"
9+
10+
print(createHTML().html {
11+
body {
12+
h1 { +"Hello, $addressee!" }
13+
}
14+
})
15+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /