|
| 1 | +# Script.swift |
| 2 | +Script.swift is a small library that lets you write a Swift script with ease by using method chaining, inspired by [bitfield/script](https://github.com/bitfield/script). |
| 3 | + |
| 4 | +## Install |
| 5 | +**Via Swift Package Manager** |
| 6 | + |
| 7 | +Add this line in `dependencies` of your `Package.swift`. |
| 8 | +```swift |
| 9 | +.package(url: "https://github.com/KS1019/Script.swift", from: "0.0.1") |
| 10 | +``` |
| 11 | + |
| 12 | +**Via [`swift-sh`](https://github.com/mxcl/swift-sh)** |
| 13 | + |
| 14 | +Add this line at the top of your Swift script. Refer to swift-sh documentation for more details. |
| 15 | +```swift |
| 16 | +import ScriptSwift // KS1019/Script.swift ~> 0.0.1 |
| 17 | +``` |
| 18 | +## Usage |
| 19 | + |
| 20 | +| Method | Contents | |
| 21 | +| ------ | -------- | |
| 22 | +| `exec` | Executes commands | |
| 23 | +| `ifExists` | Continues only if a file exists | |
| 24 | +| `write` | Writes data to a file | |
| 25 | +| `read` | Reads data from a file | |
| 26 | +| `countLines` | Returns the number of lines | |
| 27 | +| `concat` | Returns an array of string representation of files | |
| 28 | +| `asArray` | Returns lines of a string as an array | |
| 29 | + |
| 30 | +See [documentation](https://ks1019.github.io/Script.swift/documentation/ScriptSwift/) for more details. |
| 31 | + |
| 32 | +### Using `Script.swift` with [`swift-sh`](https://github.com/mxcl/swift-sh) |
| 33 | +When you want a script, you typically want it in a single file. With usual setup using Swift Package Manager to interact with external libraries, you would end up a directory with `Package.swift`, which is bit much as a script. |
| 34 | + |
| 35 | +By using `swift-sh`, you only have to take care of a single Swift file for your script, which makes your Swift script as portable as possible. |
| 36 | +## Examples |
| 37 | +<table> |
| 38 | +<thead> |
| 39 | + <tr> |
| 40 | + <th>Source</th> |
| 41 | + <th>Script</th> |
| 42 | + </tr> |
| 43 | +</thead> |
| 44 | +<tbody> |
| 45 | +<tr> |
| 46 | +<td> |
| 47 | + |
| 48 | +[`echo.swift`](https://github.com/KS1019/Script.swift/Examples/echo.swift) |
| 49 | + |
| 50 | +</td> |
| 51 | +<td> |
| 52 | + |
| 53 | +```swift |
| 54 | +Script() |
| 55 | + .exec(#"echo "testing""#) |
| 56 | + .stdout() |
| 57 | +``` |
| 58 | + |
| 59 | +</td> |
| 60 | + |
| 61 | +</tr> |
| 62 | +</tbody> |
| 63 | +</table> |
| 64 | + |
| 65 | +## Acknowledgements |
| 66 | +- [JohnSundell/Files](https://github.com/johnsundell/files) |
| 67 | +- [JohnSundell/ShellOut](https://github.com/johnsundell/shellout) |
| 68 | +- [mxcl/swift-sh](https://github.com/mxcl/swift-sh) |
| 69 | +- [bitfield/script](https://github.com/bitfield/script) |
0 commit comments