Skip to content

KS1019/Script.swift

Folders and files

NameName
Last commit message
Last commit date

Latest commit

92fa44d · Mar 17, 2024

History

46 Commits
Mar 15, 2024
Mar 25, 2022
Mar 15, 2024
Nov 4, 2023
Mar 17, 2024
Mar 15, 2024
Mar 23, 2022
Mar 23, 2022
Mar 24, 2022
Jun 19, 2022
Mar 23, 2022
Jun 20, 2022
Mar 15, 2024
Mar 15, 2024

Repository files navigation

Script.swift

Script.swift is a small library that lets you write a Swift script with ease by using method chaining, inspired by bitfield/script.

carbon-image

Install

Via Swift Package Manager

Add this line in dependencies of your Package.swift.

.package(url: "https://github.com/KS1019/Script.swift", from: "0.0.1")

Via swift-sh

Add this line at the top of your Swift script. Refer to swift-sh documentation for more details.

import Scripting // KS1019/Script.swift ~> 0.0.1

Usage

Method Contents
exec Executes commands
ifExists Continues only if a file exists
write Writes data to a file
read Reads data from a file
countLines Returns the number of lines
concat Returns an array of string representation of files
asArray Returns lines of a string as an array

See documentation for more details.

Using Script.swift with swift-sh

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.

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.

Examples

Source Script

echo.swift

Script()
    .exec(#"echo "testing""#)
    .stdout()

Acknowledgements