|
| 1 | +// |
| 2 | +// CommandExecutor.swift |
| 3 | +// CommandExecutor |
| 4 | +// |
| 5 | +// Created by Omar Abdelhafith on 05/11/2015. |
| 6 | +// Copyright © 2015 Omar Abdelhafith. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import Foundation |
| 10 | +import Prompt |
| 11 | + |
| 12 | +typealias ExecutorReturnValue = (status: Int, standardOutput: TaskPipe, standardError: TaskPipe) |
| 13 | + |
| 14 | +class CommandExecutor { |
| 15 | + |
| 16 | + static var currentTaskExecutor: TaskExecutor = ActualTaskExecutor() |
| 17 | + |
| 18 | + class func execute(_ commandParts: [String]) -> ExecutorReturnValue { |
| 19 | + return currentTaskExecutor.execute(commandParts) |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | + |
| 24 | +protocol TaskExecutor { |
| 25 | + func execute(_ commandParts: [String]) -> ExecutorReturnValue |
| 26 | +} |
| 27 | + |
| 28 | +class DryTaskExecutor: TaskExecutor { |
| 29 | + |
| 30 | + func execute(_ commandParts: [String]) -> ExecutorReturnValue { |
| 31 | + let command = commandParts.joined(separator: " ") |
| 32 | + PromptSettings.print("Executed command '\(command)'") |
| 33 | + return (0, |
| 34 | + Dryipe(dataToReturn: "".data(using: String.Encoding.utf8)!), |
| 35 | + Dryipe(dataToReturn: "".data(using: String.Encoding.utf8)!)) |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +class ActualTaskExecutor: TaskExecutor { |
| 40 | + |
| 41 | + func execute(_ commandParts: [String]) -> ExecutorReturnValue { |
| 42 | + let task = Process() |
| 43 | + |
| 44 | + task.launchPath = "/usr/bin/env" |
| 45 | + task.arguments = commandParts |
| 46 | + |
| 47 | + let stdoutPipe = Pipe() |
| 48 | + let stderrPipe = Pipe() |
| 49 | + |
| 50 | + task.standardOutput = stdoutPipe |
| 51 | + task.standardError = stderrPipe |
| 52 | + task.launch() |
| 53 | + task.waitUntilExit() |
| 54 | + |
| 55 | + return (Int(task.terminationStatus), stdoutPipe, stderrPipe) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +class InteractiveTaskExecutor: TaskExecutor { |
| 60 | + |
| 61 | + func execute(_ commandParts: [String]) -> ExecutorReturnValue { |
| 62 | + |
| 63 | + let argv: [UnsafeMutablePointer<CChar>?] = commandParts.map{ $0.withCString(strdup) } |
| 64 | + defer { for case let arg? in argv { free(arg) } } |
| 65 | + |
| 66 | + var childFDActions: posix_spawn_file_actions_t? = nil |
| 67 | + var outputPipe: [Int32] = [-1, -1] |
| 68 | + |
| 69 | + posix_spawn_file_actions_init(&childFDActions) |
| 70 | + posix_spawn_file_actions_adddup2(&childFDActions, outputPipe[1], 1) |
| 71 | + posix_spawn_file_actions_adddup2(&childFDActions, outputPipe[1], 2) |
| 72 | + posix_spawn_file_actions_addclose(&childFDActions, outputPipe[0]) |
| 73 | + posix_spawn_file_actions_addclose(&childFDActions, outputPipe[1]) |
| 74 | + |
| 75 | + |
| 76 | + var pid: pid_t = 0 |
| 77 | + let result = posix_spawn(&pid, argv[0], &childFDActions, nil, argv + [nil], nil) |
| 78 | + |
| 79 | + let emptyPipe = Dryipe(dataToReturn: "".data(using: String.Encoding.utf8)!) |
| 80 | + return (Int(result), emptyPipe, emptyPipe) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +class DummyTaskExecutor: TaskExecutor { |
| 85 | + |
| 86 | + var commandsExecuted: [String] = [] |
| 87 | + let statusCodeToReturn: Int |
| 88 | + |
| 89 | + let errorToReturn: String |
| 90 | + let outputToReturn: String |
| 91 | + |
| 92 | + init(status: Int, output: String, error: String) { |
| 93 | + statusCodeToReturn = status |
| 94 | + outputToReturn = output |
| 95 | + errorToReturn = error |
| 96 | + } |
| 97 | + |
| 98 | + func execute(_ commandParts: [String]) -> ExecutorReturnValue { |
| 99 | + let command = commandParts.joined(separator: " ") |
| 100 | + commandsExecuted.append(command) |
| 101 | + |
| 102 | + return (statusCodeToReturn, |
| 103 | + Dryipe(dataToReturn: outputToReturn.data(using: String.Encoding.utf8)!), |
| 104 | + Dryipe(dataToReturn: errorToReturn.data(using: String.Encoding.utf8)!)) |
| 105 | + } |
| 106 | +} |
0 commit comments