ShellOut Documentation

repository·master·Indexed 21 days ago

https://github.com/johnsundell/shellout

A Swift package for executing shell commands from Swift scripts or command-line tools. It provides a high-level API for running commands, managing working directories, and includes pre-defined commands for Git, Swift Package Manager, fastlane, CocoaPods, and Marathon.

Tokens
1.2K
Snippets
5
Records
6
Agent score
25%

What's inside ShellOut

  1. Install ShellOut in a Swift Package Manager project

    master

    To use ShellOut as a dependency in a command line tool or application, add it to your Package.swift file:

    .package(url: "https://github.com/JohnSundell/ShellOut.git", from: "2.0.0")

    Then update your packages using $ swift package update.

    // In Package.swift
    .package(url: "https://github.com/JohnSundell/ShellOut.git", from: "2.0.0")
  2. Install ShellOut for Swift scripts using Marathon

    master

    To use ShellOut in a standalone Swift script, use the Marathon runner:

    1. Install Marathon.
    2. Add ShellOut to Marathon via CLI: $ marathon add https://github.com/JohnSundell/ShellOut.git OR add https://github.com/JohnSundell/ShellOut.git to your Marathonfile.
    3. Run your script: $ marathon run yourScript.swift.
    $ marathon add https://github.com/JohnSundell/ShellOut.git
    $ marathon run yourScript.swift
  3. Handle files, folders, and symlinks

    master

    Use the built-in file system commands to manipulate files and directories without writing raw shell scripts.

    try shellOut(to: .createFolder(named: "folder"))
    try shellOut(to: .createFile(named: "file", contents: "Hello world"))
    try shellOut(to: .moveFile(from: "path/a", to: "path/b"))
    try shellOut(to: .copyFile(from: "path/a", to: "path/b"))
    try shellOut(to: .openFile(at: "Project.xcodeproj"))
    try shellOut(to: .readFile(at: "Podfile"))
    try shellOut(to: .removeFile(from: "path/a"))
    try shellOut(to: .createSymlink(to: "target", at: "link"))
    try shellOut(to: .expandSymlink(at: "link"))
  4. Use pre-defined Git commands

    master

    ShellOut provides a set of pre-defined commands for Git to avoid manual string construction. Use the .git...() methods to perform common Git operations.

    try shellOut(to: .gitInit())
    try shellOut(to: .gitClone(url: repositoryURL))
    try shellOut(to: .gitCommit(message: "A scripted commit!"))
    try shellOut(to: .gitPush())
    try shellOut(to: .gitPull(remote: "origin", branch: "release"))
    try shellOut(to: .gitSubmoduleUpdate())
    try shellOut(to: .gitCheckout(branch: "my-feature"))
  5. Use pre-defined commands for Swift Package Manager, fastlane, CocoaPods, and Marathon

    master

    ShellOut includes specialized commands for common iOS/macOS development tools:

    Swift Package Manager

    • .createSwiftPackage(withType:)
    • .updateSwiftPackages()
    • .generateSwiftPackageXcodeProject()
    • .buildSwiftPackage()
    • .testSwiftPackage()

    fastlane

    • .runFastlane(usingLane:)

    CocoaPods

    • .updateCocoaPods()
    • .installCocoaPods()

    Marathon

    • .runMarathonScript(at:arguments:)
    • .updateMarathonPackages()
  6. Run shell commands with shellOut()

    master

    You can execute arbitrary shell commands by calling shellOut(). You can pass a single command string or an array of command strings to run multiple commands sequentially. You can also specify a working directory using the at: parameter.

    If a command fails, ShellOut throws a ShellOutError which contains the message (the content of STDERR) and the output (the content of STDOUT).

    // Run a single command with arguments
    let output = try shellOut(to: "echo", arguments: ["Hello world"])
    print(output) // Hello world
    
    // Run multiple commands at a specific path
    try shellOut(to: ["mkdir NewFolder", "echo \"Hello again\" > NewFolder/File"], at: "~/CurrentFolder")
    let output = try shellOut(to: "cat File", at: "~/CurrentFolder/NewFolder")
    print(output) // Hello again
    
    // Error handling
    do {
        try shellOut(to: "totally-invalid")
    } catch {
        let error = error as! ShellOutError
        print(error.message) // Prints STDERR
        print(error.output) // Prints STDOUT
    }