-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathShell.swift
More file actions
93 lines (74 loc) · 3.22 KB
/
Shell.swift
File metadata and controls
93 lines (74 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//
// Shell.swift
// XcodeHelper
//
// Created by dhcdht on 2016/11/1.
// Copyright © 2016年 XH. All rights reserved.
//
import Cocoa
enum ShellError: Error {
case TaskExitWithErrorStatus(String)
}
/// 执行命令行脚本命令
class Shell: NSObject {
typealias ShellExecuteCommandCompletionBlock = (_ taskOutput: String?, _ error: Error?) -> Void
func executeCommand(command: String, arguments: [String], completion: @escaping ShellExecuteCommandCompletionBlock) -> Void {
self.executeCommand(command: command, arguments: arguments, workingDirectory: nil, completion: completion)
}
func executeCommand(command: String, arguments: [String], workingDirectory: String?, completion: @escaping ShellExecuteCommandCompletionBlock) -> Void {
self.taskOutput = Data()
let shellTask = Process()
if let path = workingDirectory {
shellTask.currentDirectoryPath = path
}
shellTask.launchPath = command
shellTask.arguments = arguments
self.setupShellOutputForTask(task: shellTask)
self.setupStdErrorOutputForTask(task: shellTask)
self.setupTerminationHandlerForTask(task: shellTask, completion: completion)
self.tryToLaunchTask(shellTask: shellTask, completion: completion)
}
// MARK: - Private
private var taskOutput: Data?
private func setupShellOutputForTask(task: Process) -> Void {
let pipe = Pipe()
pipe.fileHandleForReading.readabilityHandler = { [weak self] (file: FileHandle) -> Void in
self?.taskOutput?.append(file.availableData)
}
task.standardOutput = pipe
}
private func setupStdErrorOutputForTask(task: Process) -> Void {
let pipe = Pipe()
pipe.fileHandleForReading.readabilityHandler = { [weak self] (file: FileHandle) -> Void in
self?.taskOutput?.append(file.availableData)
}
task.standardError = pipe
}
private func setupTerminationHandlerForTask(task: Process, completion: @escaping ShellExecuteCommandCompletionBlock) -> Void {
task.terminationHandler = { (task: Process) -> Void in
OperationQueue.main.addOperation({
guard let taskOutput = self.taskOutput else {
// TODO: error
completion(nil, nil)
return;
}
let output = String(data: taskOutput, encoding: .utf8)
if task.terminationStatus == 0 {
completion(output, nil)
} else {
let description = "Task exited with status \(task.terminationStatus)\n\n\(task.launchPath) \(task.arguments?.joined(separator: "0"))"
completion(output, ShellError.TaskExitWithErrorStatus(description))
}
})
if let pipe = task.standardOutput as? Pipe {
pipe.fileHandleForReading.readabilityHandler = nil
}
if let pipe = task.standardError as? Pipe {
pipe.fileHandleForReading.readabilityHandler = nil
}
}
}
private func tryToLaunchTask(shellTask: Process, completion: ShellExecuteCommandCompletionBlock) -> Void {
shellTask.launch()
}
}