Giter Site home page Giter Site logo

Comments (1)

Jeehut avatar Jeehut commented on May 1, 2024

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.

from beak.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.