forked from DanTheMan827/ios-app-signer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
NSTask-execute.swift
54 lines (47 loc) · 1.5 KB
/
NSTask-execute.swift
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
//
// NSTask-execute.swift
// AppSigner
//
// Created by Daniel Radtke on 11/3/15.
// Copyright © 2015 Daniel Radtke. All rights reserved.
//
import Foundation
struct AppSignerTaskOutput {
var output: String
var status: Int32
init(status: Int32, output: String){
self.status = status
self.output = output
}
}
extension Process {
func launchSyncronous() -> AppSignerTaskOutput {
self.standardInput = FileHandle.nullDevice
let pipe = Pipe()
self.standardOutput = pipe
self.standardError = pipe
let pipeFile = pipe.fileHandleForReading
self.launch()
let data = NSMutableData()
while self.isRunning {
data.append(pipeFile.availableData)
}
pipeFile.closeFile();
self.terminate();
if let output = String.init(data: data as Data, encoding: String.Encoding.utf8) {
return AppSignerTaskOutput(status: self.terminationStatus, output: output)
} else {
return AppSignerTaskOutput(status: self.terminationStatus, output: "")
}
}
func execute(_ launchPath: String, workingDirectory: String?, arguments: [String]?)->AppSignerTaskOutput{
self.launchPath = launchPath
if arguments != nil {
self.arguments = arguments
}
if workingDirectory != nil {
self.currentDirectoryPath = workingDirectory!
}
return self.launchSyncronous()
}
}