Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document how to run other tools on command line #36

Open
Jeehut opened this issue Jun 27, 2018 · 1 comment
Open

Document how to run other tools on command line #36

Jeehut opened this issue Jun 27, 2018 · 1 comment

Comments

@Jeehut
Copy link

Jeehut commented Jun 27, 2018

While Beak is very useful and supports SPM packages, sometimes it would make life much easier if one could run another command line tool to do a specific task instead of writing Swift code for that task. While it is not directly the duty of Beak to teach people how to run command line tools from within Swift, it perfectly makes sense from my point of view to help users by providing one "recommended" way of running them which we will make sure it always works (as long as Swift supports that method).

Especially it can teach Beak users that there's at least the possibility to use other tools from within the Beak script which shows how much more useful Beak can become than they might have initially thought.

@Jeehut
Copy link
Author

Jeehut commented Jun 27, 2018

One way could be to include something like the following code in a beak.swift file:

typealias CommandResult = (outputLines: [String], errorLines: [String], exitCode: Int32)

@discardableResult
func run(_ command: String) -> CommandResult {
    let commandComponents = command.components(separatedBy: .whitespacesAndNewlines)

    let commandLineTask = Process()
    commandLineTask.launchPath = "/usr/bin/env"
    commandLineTask.arguments = commandComponents

    let outputPipe = Pipe()
    commandLineTask.standardOutput = outputPipe
    let errorPipe = Pipe()
    commandLineTask.standardError = errorPipe

    commandLineTask.launch()

    var errorLines: [String] = []
    let errorData = errorPipe.fileHandleForReading.readDataToEndOfFile()

    if var errorDataString = String(data: errorData, encoding: .utf8) {
        errorDataString = errorDataString.trimmingCharacters(in: .newlines)
        errorLines = errorDataString.components(separatedBy: .newlines)
    }

    var outputLines: [String] = []
    let outputData = outputPipe.fileHandleForReading.readDataToEndOfFile()

    if var outputDataString = String(data: outputData, encoding: .utf8) {
        outputDataString = outputDataString.trimmingCharacters(in: .newlines)
        outputLines = outputDataString.components(separatedBy: .newlines)
    }

    commandLineTask.waitUntilExit()
    let exitCode = commandLineTask.terminationStatus

    return (outputLines, errorLines, exitCode)
}

This enables the following lines:

run("ruby -v") // => (outputLines: ["ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin17]"], errorLines: [""], exitCode: 0)
run("ruby -v").outputLines // => ["ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin17]"]
run("ruby -v").errorLines // => [""]
run("ruby -v").exitCode // => 0

Alternatively one could point to tools like SwiftShell and ShellOut) which both support SPM and therefore Beak.

@Jeehut Jeehut changed the title Document how to run other tools on the command line from Beak Document how to run other tools on command line Jun 27, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant