Skip to content

Commit cccda87

Browse files
authored
Merge pull request #5 from jakubriegel/readme
Update readme.md
2 parents 69f3c26 + af73f5b commit cccda87

File tree

1 file changed

+122
-34
lines changed

1 file changed

+122
-34
lines changed

readme.md

+122-34
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,46 @@
11
# Kotlin Shell
22

33
## about
4-
Kotlin Shell is a prototype tool for performing shell programming in Kotlin and KotlinScript.
5-
It provides shell-like API which takes advantage of Kotlin high level possibilities.
4+
Kotlin Shell is a prototype tool for performing shell programming in Kotlin and Kotlin script.
5+
It provides shell-like API which takes advantage of Kotlin features.
66

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

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+
```
28+
929
## content
1030
* [about](#about)
31+
* [intro](#intro)
1132
* [content](#content)
12-
* [get and run](#get-and-run)
13-
+ [library](#library)
14-
+ [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)
1539
- [kshell command](#kshell-command)
1640
* [command line](#command-line)
1741
* [shebang](#shebang)
1842
- [kotlinc command](#kotlinc-command)
43+
+ [in Kotlin programs](#in-kotlin-programs)
1944
* [usage](#usage)
2045
+ [writing scripts](#writing-scripts)
2146
- [in Kotlin code](#in-kotlin-code)
@@ -28,19 +53,20 @@ For examples go to the [examples](#examples) section.
2853
* [kts process](#kts-process)
2954
- [multiple calls to processes](#multiple-calls-to-processes)
3055
+ [pipelines](#pipelines)
31-
- [piping logic](#piping-logic)
32-
* [introduction](#introduction)
56+
- [piping overview](#piping-overview)
57+
* [piping introduction](#piping-introduction)
3358
* [piping grammar](#piping-grammar)
3459
- [creating pipeline](#creating-pipeline)
3560
- [forking stderr](#forking-stderr)
3661
- [lambdas in pipelines](#lambdas-in-pipelines)
3762
- [lambdas suitable for piping](#lambdas-suitable-for-piping)
3863
* [example](#example)
3964
+ [detaching](#detaching)
65+
- [detaching overview](#detaching-overview)
4066
- [detaching process](#detaching-process)
4167
- [detaching pipeline](#detaching-pipeline)
4268
- [attaching](#attaching)
43-
+ [demonizing](#demonizing)
69+
+ [daemonizing](#daemonizing)
4470
+ [environment](#environment)
4571
- [system environment](#system-environment)
4672
- [shell environment](#shell-environment)
@@ -53,14 +79,30 @@ For examples go to the [examples](#examples) section.
5379
- [custom shell methods](#custom-shell-methods)
5480
- [special properties](#special-properties)
5581
+ [sub shells](#sub-shells)
56-
+ [dependencies](#dependencies)
57-
- [external dependencies](#external-dependencies)
58-
- [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)
5988
* [examples](#examples)
60-
## get and run
61-
### library
89+
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.
93+
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.
101+
102+
### as library
62103
[![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)
63104

105+
Gradle:
64106
```kotlin
65107
repositories {
66108
maven("https://dl.bintray.com/jakubriegel/kotlin-shell")
@@ -75,9 +117,11 @@ Kotlin Shell features slf4j logging. To use it add logging implementation or NOP
75117
```kotlin
76118
implementation("org.slf4j:slf4j-nop:1.7.26")
77119
```
78-
### scripting
79-
[![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)
80120

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
81125
Kotlin Shell scripts have `sh.kts` extension.
82126

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

111162
## usage
112163
### 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+
113166
#### in Kotlin code
114-
with new coroutine scope:
167+
With given scope:
115168
```kotlin
116-
shell {
169+
shell (
170+
scope = myScope
171+
) {
117172
"echo hello world!"()
118173
}
119174
```
120175

121-
with given scope:
176+
With new coroutine scope:
122177
```kotlin
123-
shell (
124-
scope = myScope
125-
) {
178+
shell {
126179
"echo hello world!"()
127180
}
128181
```
182+
183+
129184
#### in Kotlin Script
130185
##### blocking api
131186
The blocking api features basic shell commands without the need of wrapping it into coroutines calls:
@@ -135,7 +190,7 @@ The blocking api features basic shell commands without the need of wrapping it i
135190
It can be accessed in Kotlin code as well by using `ScriptingShell` class.
136191

137192
##### non blocking api
138-
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:
139194
```kotlin
140195
shell {
141196
"echo hello world!"()
@@ -185,8 +240,8 @@ To run equivalent process multiple times call `ProcessExecutable.copy()`
185240
### pipelines
186241
Pipelines can operate on processes, lambdas, files, strings, byte packages and streams.
187242

188-
#### piping logic
189-
##### introduction
243+
#### piping overview
244+
##### piping introduction
190245
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:
191246
* `stdin` is always empty and closed `ProcessReceiveChannel`, which effectively acts like `/dev/null`. It It can be accessed elsewhere by `nullin` member.
192247
* `stdout` is a rendezvous `ProcessSendChannel`, that passes everything to `System.out`.
@@ -304,6 +359,9 @@ shell {
304359
```
305360

306361
### 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+
307365
#### detaching process
308366
To detach process use `detach()` function:
309367
```kotlin
@@ -350,18 +408,18 @@ To access detached processes use `detachedPipelines` member. It stores list of p
350408
#### attaching
351409
To attach detached job (process or pipeline) use `fg()`:
352410
* `fg(Int)` accepting detached job id. By default it will use `1` as id.
353-
* `fg(Process)` accepting detached process
354-
* `fg(Pipeline)` accepting detached pipeline
411+
* `fg(Process)` accepting detached process
412+
* `fg(Pipeline)` accepting detached pipeline
355413

356414
To join all detached jobs call `joinDetached()`
357415

358-
### demonizing
416+
### daemonizing
359417
> At the current stage demonizing processes and pipelines is implemented in very unstable and experimental way.
360418
>
361419
> Though it should not be used.
362420
363421
### environment
364-
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.
365423

366424
To access the environment call:
367425
* `environment` list or `env` command for `shell environment`
@@ -373,7 +431,7 @@ To access the environment call:
373431
`system environment` is copied to `shell environment` at its creation. To access system environment any time call `systemEnv`
374432

375433
#### shell environment
376-
`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.
377435

378436
To set environment use `export`:
379437
```kotlin
@@ -499,6 +557,7 @@ where `T` is desired return type or `Unit`. Such functions can be declared outsi
499557
* `variables`
500558

501559
### sub shells
560+
#### creating sub shells
502561
To create sub shell use `shell` block:
503562
```koltin
504563
shell {
@@ -524,20 +583,49 @@ shell {
524583

525584
Sub shells suspend execution of the parent shell.
526585

527-
### dependencies
528-
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+
```
529615
530-
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).
531619
532-
#### external dependencies
620+
##### external dependencies
533621
External dependencies from maven repositories can be added via `@file:Repository` `@file:DependsOn` annotation:
534622
```kotlin
535623
@file:Repository("MAVEN_REPOSITORY_URL")
536624
@file:DependsOn("GROUP:PACKAGE:VERSION")
537625
```
538626
then they can be imported with standard `import` statement.
539627
540-
#### internal dependencies
628+
##### internal dependencies
541629
To import something from local file use `@file:Import`:
542630
```kotlin
543631
@file:Import("SCRIPT.sh.kts")

0 commit comments

Comments
 (0)