Skip to content

Commit af73f5b

Browse files
authored
Update readme.md
1 parent 3e696fc commit af73f5b

File tree

1 file changed

+116
-38
lines changed

1 file changed

+116
-38
lines changed

readme.md

+116-38
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,41 @@ It provides shell-like API which takes advantage of Kotlin features.
66

77
For examples go to the [examples](#examples) section.
88

9-
## hello world
10-
example
9+
## intro
10+
Creating processes is extremly easy in Kotlin Shell:
11+
```kotlin
12+
shell {
13+
"echo hello world"()
14+
}
15+
16+
// echo hello world
17+
```
18+
19+
Piping is also supported:
20+
```kotlin
21+
shell {
22+
val toUpper = stringLambda { it.toUpper() to "" }
23+
pipeline { file("data.txt") pipe "grep abc".process() pipe toUpper }
24+
}
25+
26+
// cat data.txt | grep abc | tr '[:lower:]' '[:upper:]'
27+
```
1128

1229
## content
1330
* [about](#about)
31+
* [intro](#intro)
1432
* [content](#content)
15-
* [get and run](#get-and-run)
16-
+ [library](#library)
17-
+ [scripting](#scripting)
33+
* [preliminary](#preliminary)
34+
* [how to get Kotlin Shell](#how-to-get-kotlin-shell)
35+
+ [for scripting](#for-scripting)
36+
+ [as library](#as-library)
37+
* [how to run the scripts](#how-to-run-the-scripts)
38+
+ [in Kotlin script](#in-kotlin-script)
1839
- [kshell command](#kshell-command)
1940
* [command line](#command-line)
2041
* [shebang](#shebang)
2142
- [kotlinc command](#kotlinc-command)
43+
+ [in Kotlin programs](#in-kotlin-programs)
2244
* [usage](#usage)
2345
+ [writing scripts](#writing-scripts)
2446
- [in Kotlin code](#in-kotlin-code)
@@ -31,19 +53,20 @@ example
3153
* [kts process](#kts-process)
3254
- [multiple calls to processes](#multiple-calls-to-processes)
3355
+ [pipelines](#pipelines)
34-
- [piping logic](#piping-logic)
35-
* [introduction](#introduction)
56+
- [piping overview](#piping-overview)
57+
* [piping introduction](#piping-introduction)
3658
* [piping grammar](#piping-grammar)
3759
- [creating pipeline](#creating-pipeline)
3860
- [forking stderr](#forking-stderr)
3961
- [lambdas in pipelines](#lambdas-in-pipelines)
4062
- [lambdas suitable for piping](#lambdas-suitable-for-piping)
4163
* [example](#example)
4264
+ [detaching](#detaching)
65+
- [detaching overview](#detaching-overview)
4366
- [detaching process](#detaching-process)
4467
- [detaching pipeline](#detaching-pipeline)
4568
- [attaching](#attaching)
46-
+ [demonizing](#demonizing)
69+
+ [daemonizing](#daemonizing)
4770
+ [environment](#environment)
4871
- [system environment](#system-environment)
4972
- [shell environment](#shell-environment)
@@ -56,21 +79,30 @@ example
5679
- [custom shell methods](#custom-shell-methods)
5780
- [special properties](#special-properties)
5881
+ [sub shells](#sub-shells)
59-
+ [dependencies](#dependencies)
60-
- [external dependencies](#external-dependencies)
61-
- [internal dependencies](#internal-dependencies)
82+
- [creating sub shells](#creating-sub-shells)
83+
- [sub shells use cases](#sub-shells-use-cases)
84+
+ [scripting specific features](#scripting-specific-features)
85+
- [dependencies](#dependencies)
86+
* [external dependencies](#external-dependencies)
87+
* [internal dependencies](#internal-dependencies)
6288
* [examples](#examples)
6389

64-
## pre
65-
- supported platforms
66-
- tested platforms
67-
- Windows?
90+
## preliminary
91+
The library is designed primary for Unix-like operating systems and was tested fully on MacOS.
92+
Windows support is not planned at the moment.
6893

94+
## how to get Kotlin Shell
95+
### for scripting
96+
[![scripting](https://api.bintray.com/packages/jakubriegel/kotlin-shell/kotlin-shell-kts/images/download.svg) ](https://bintray.com/jakubriegel//kotlin-shell/kotlin-shell-kts/_latestVersion)
97+
98+
Use `kshell` command for running scripts from command line. To read more about it and download the command go [here](https://github.com/jakubriegel/kshell).
99+
100+
You can also [download](https://bintray.com/jakubriegel//kotlin-shell/kotlin-shell-kts/_latestVersion) binaries of `kotlin-shell-kts` to use the script definition in custom way.
69101

70-
## how run scripts
71-
### library
102+
### as library
72103
[![library](https://api.bintray.com/packages/jakubriegel/kotlin-shell/kotlin-shell-core/images/download.svg) ](https://bintray.com/jakubriegel//kotlin-shell/kotlin-shell-core/_latestVersion)
73104

105+
Gradle:
74106
```kotlin
75107
repositories {
76108
maven("https://dl.bintray.com/jakubriegel/kotlin-shell")
@@ -85,9 +117,11 @@ Kotlin Shell features slf4j logging. To use it add logging implementation or NOP
85117
```kotlin
86118
implementation("org.slf4j:slf4j-nop:1.7.26")
87119
```
88-
### scripting
89-
[![scripting](https://api.bintray.com/packages/jakubriegel/kotlin-shell/kotlin-shell-kts/images/download.svg) ](https://bintray.com/jakubriegel//kotlin-shell/kotlin-shell-kts/_latestVersion)
90120

121+
You can also [download](https://bintray.com/jakubriegel//kotlin-shell/kotlin-shell-core/_latestVersion) binaries of `kotlin-shell-core` to use the library in any other project.
122+
123+
## how to run the scripts
124+
### in Kotlin script
91125
Kotlin Shell scripts have `sh.kts` extension.
92126

93127
Some environment variables may be set to customize script execution. Go to the [environment](#environment) section to learn more.
@@ -117,25 +151,36 @@ example:
117151
```
118152
kotlinc -cp lib/kotlin-shell-kts-all.jar -Dkotlin.script.classpath -script hello.sh.kts
119153
```
154+
### in Kotlin programs
155+
Calling the `shell` block will provide access to Kotlin Shell API:
156+
```kotlin
157+
shell {
158+
// code
159+
}
160+
```
120161

121162
## usage
122163
### writing scripts
164+
Kotlin Shell is driven by [kotlinx.io](https://github.com/Kotlin/kotlinx-io) and [kotlinx.coroutines](https://github.com/Kotlin/kotlinx.coroutines). Therefore the API is fully non-blockign and most functions are suspending. To take advantage of that, you need to pass the script as `suspend fun` and `CoroutineScope` as parameters to the suspending `shell` block.
165+
123166
#### in Kotlin code
124-
with new coroutine scope:
167+
With given scope:
125168
```kotlin
126-
shell {
169+
shell (
170+
scope = myScope
171+
) {
127172
"echo hello world!"()
128173
}
129174
```
130175

131-
with given scope:
176+
With new coroutine scope:
132177
```kotlin
133-
shell (
134-
scope = myScope
135-
) {
178+
shell {
136179
"echo hello world!"()
137180
}
138181
```
182+
183+
139184
#### in Kotlin Script
140185
##### blocking api
141186
The blocking api features basic shell commands without the need of wrapping it into coroutines calls:
@@ -145,7 +190,7 @@ The blocking api features basic shell commands without the need of wrapping it i
145190
It can be accessed in Kotlin code as well by using `ScriptingShell` class.
146191

147192
##### non blocking api
148-
The `shell()` function gives access to full api of `kotlin-shell`:
193+
The `shell` block gives access to full api of `kotlin-shell`. It receives `GlobalScope` as implicit parameter:
149194
```kotlin
150195
shell {
151196
"echo hello world!"()
@@ -195,8 +240,8 @@ To run equivalent process multiple times call `ProcessExecutable.copy()`
195240
### pipelines
196241
Pipelines can operate on processes, lambdas, files, strings, byte packages and streams.
197242

198-
#### piping logic
199-
##### introduction
243+
#### piping overview
244+
##### piping introduction
200245
Every executable element in Kotlin Shell receives its own `ExecutionContext`, which consist of `stdin`, `stdout` and `stderr` implemented as `Channels`. In the library channels are used under aliases `ProcessChannel`, `ProcessSendChannel` and `ProcessReceiveChannel` their unit is always `ByteReadPacket`. `Shell` itself is an `ExecutionContext` and provides default channels:
201246
* `stdin` is always empty and closed `ProcessReceiveChannel`, which effectively acts like `/dev/null`. It It can be accessed elsewhere by `nullin` member.
202247
* `stdout` is a rendezvous `ProcessSendChannel`, that passes everything to `System.out`.
@@ -314,6 +359,9 @@ shell {
314359
```
315360

316361
### detaching
362+
#### detaching overview
363+
Detached process or pipeline is being executed asynchronous to the shell. It can be attached or awaited at any time. Also all of not-ended detached jobs will be awaited after the end of the script before finishig `shell` block.
364+
317365
#### detaching process
318366
To detach process use `detach()` function:
319367
```kotlin
@@ -360,18 +408,18 @@ To access detached processes use `detachedPipelines` member. It stores list of p
360408
#### attaching
361409
To attach detached job (process or pipeline) use `fg()`:
362410
* `fg(Int)` accepting detached job id. By default it will use `1` as id.
363-
* `fg(Process)` accepting detached process
364-
* `fg(Pipeline)` accepting detached pipeline
411+
* `fg(Process)` accepting detached process
412+
* `fg(Pipeline)` accepting detached pipeline
365413

366414
To join all detached jobs call `joinDetached()`
367415

368-
### demonizing
416+
### daemonizing
369417
> At the current stage demonizing processes and pipelines is implemented in very unstable and experimental way.
370418
>
371419
> Though it should not be used.
372420
373421
### environment
374-
Environment in Kotlin Shell is divided into two parts `shell environment` and `shell variables`. The environment from system is also inherited
422+
Environment in Kotlin Shell is divided into two parts `shell environment` and `shell variables`. The environment from system is also copied.
375423

376424
To access the environment call:
377425
* `environment` list or `env` command for `shell environment`
@@ -383,7 +431,7 @@ To access the environment call:
383431
`system environment` is copied to `shell environment` at its creation. To access system environment any time call `systemEnv`
384432

385433
#### shell environment
386-
`shell environment` is inherited by `Shell` from the system. It can be modified and is copied to sub shells.
434+
`shell environment` is copied to `Shell` from the system. It can be modified and is copied to sub shells.
387435

388436
To set environment use `export`:
389437
```kotlin
@@ -509,6 +557,7 @@ where `T` is desired return type or `Unit`. Such functions can be declared outsi
509557
* `variables`
510558

511559
### sub shells
560+
#### creating sub shells
512561
To create sub shell use `shell` block:
513562
```koltin
514563
shell {
@@ -534,20 +583,49 @@ shell {
534583

535584
Sub shells suspend execution of the parent shell.
536585

537-
### dependencies
538-
Kotlin Shell scripts support adding dependencies
586+
#### sub shells use cases
587+
Sub shell can be used to provide custom environment for commands:
588+
```kotlin
589+
shell {
590+
export("KEY" to "ONE")
591+
shell (
592+
vars = mapOf("KEY" to "TWO")
593+
) {
594+
"echo ${env("KEY")} // TWO
595+
}
596+
597+
// rest of the script
598+
}
599+
```
600+
601+
Or to temporarly change the directory:
602+
```kotlin
603+
shell {
604+
"echo ${env("PWD")} // ../dir
605+
606+
shell (
607+
dir = file("bin")
608+
) {
609+
"echo ${env("PWD")} // ../dir/bin
610+
}
611+
612+
// rest of the script
613+
}
614+
```
539615
540-
Kotlin Shell uses dependencies mechanism from `kotlin-main-kts`.
616+
### scripting specific features
617+
#### dependencies
618+
Kotlin Shell scripts support external and internal dependencies. The mechanism from `kotlin-main-kts` is being used. Learn more about it in [KEEP](https://github.com/Kotlin/KEEP/blob/master/proposals/scripting-support.md) and [blog post](https://blog.jetbrains.com/kotlin/2018/09/kotlin-1-3-rc-is-here-migrate-your-coroutines/#scripting).
541619
542-
#### external dependencies
620+
##### external dependencies
543621
External dependencies from maven repositories can be added via `@file:Repository` `@file:DependsOn` annotation:
544622
```kotlin
545623
@file:Repository("MAVEN_REPOSITORY_URL")
546624
@file:DependsOn("GROUP:PACKAGE:VERSION")
547625
```
548626
then they can be imported with standard `import` statement.
549627
550-
#### internal dependencies
628+
##### internal dependencies
551629
To import something from local file use `@file:Import`:
552630
```kotlin
553631
@file:Import("SCRIPT.sh.kts")

0 commit comments

Comments
 (0)