11//
22// TerminalKit.swift
3- // TerminalKit
43//
5- // Created by Banaple on 2020/01/06.
6- // Copyright © 2020 BUGKING. All rights reserved .
4+ //
5+ // Created by Kimun Kwon on 2021/06/24 .
76//
87
98import Foundation
109
11- public typealias Handler = ( TerminalKit ) -> ( )
10+ public typealias Handler = ( ( output : String ? , errput : String ? ) ) -> ( )
1211
1312public class TerminalKit {
14- private var commands : [ String ] !
15- private var queuePoint : Int = 0
16- public var task : Process !
17- public var output : String ? {
18- didSet {
19- if output != nil {
20- self . paragraph. append ( output!)
21- }
22- }
23- }
24- public var errput : String ? {
25- didSet {
26- if errput != nil {
27- self . paragraph. append ( errput!)
28- }
29- }
30- }
31-
32- public var paragraph : String = " "
13+ private var commands : [ String ]
14+ private var task : Process
3315
34- private init ( _ launchPath: String ? ) {
16+ private init ( _ launchPath: String ? ) {
3517 self . task = Process ( )
3618 self . task. launchPath = launchPath == nil ? " /bin/sh " : launchPath
19+ self . commands = [ ]
3720 }
3821
39- public convenience init ( _ command: String , launchPath: String ? = nil , isWaitUntilExit: Bool = true ) {
22+ public convenience init ( _ command: String , launchPath: String ? = nil , isWaitUntilExit: Bool = true ) {
4023 self . init ( launchPath)
4124 self . commands = [ command]
4225 self . setCommand ( isWaitUntilExit)
4326 }
4427
45- public convenience init ( _ commands: [ String ] ) {
46- self . init ( nil )
28+ public convenience init ( _ commands: [ String ] , launchPath : String ? = nil , isWaitUntilExit : Bool = true ) {
29+ self . init ( launchPath )
4730 self . commands = commands
48- self . setCommand ( )
31+ self . setCommand ( isWaitUntilExit )
4932 }
5033
51- private func setCommand( _ isWaitUntilExit: Bool = true ) {
34+ private func setCommand( _ isWaitUntilExit: Bool ) {
5235 let command = String ( self . commands. flatMap { $0 + " && " } . dropLast ( 2 ) )
5336 self . task. arguments = [ " -c " , command]
54- self . output = nil
55- self . errput = nil
5637 if isWaitUntilExit {
5738 self . task. waitUntilExit ( )
5839 }
@@ -76,46 +57,51 @@ extension TerminalKit {
7657 self . task. launch ( )
7758 }
7859 }
79-
80- public func launch( _ onTerminate: Handler ? = nil ) {
60+
61+ /// Invoked when the task is completed.
62+ public func launch( _ onCompleted: Handler ? = nil ) {
8163 let outPipe = Pipe ( )
8264 let errPipe = Pipe ( )
8365 task. standardOutput = outPipe
8466 task. standardError = errPipe
8567
8668 self . task. terminationHandler = { _ in
87- self . output = self . readPipe ( outPipe, false )
88- self . errput = self . readPipe ( errPipe, false )
89- onTerminate ? ( self )
69+ let output = self . readPipe ( outPipe, false )
70+ let errput = self . readPipe ( errPipe, false )
71+ outPipe. fileHandleForReading. closeFile ( )
72+ errPipe. fileHandleForReading. closeFile ( )
73+ onCompleted ? ( ( output, errput) )
9074 }
9175
9276 self . launch ( )
9377 }
9478
95- public func launch( onRunning : Handler ? , onTerminate : Handler ? ) {
79+ public func launch( onNext : Handler ? , onCompleted : Handler ? ) {
9680 let outPipe = Pipe ( )
9781 let errPipe = Pipe ( )
82+ var fullOutput : String = " "
83+ var fullErrput : String = " "
9884 task. standardOutput = outPipe
9985 task. standardError = errPipe
10086
101- outPipe. fileHandleForReading. readabilityHandler = { pipe in
102- self . output = self . readPipe ( outPipe, true )
103- self . errput = nil
104- onRunning ? ( self )
87+ outPipe. fileHandleForReading. readabilityHandler = { _ in
88+ if let output: String = self . readPipe ( outPipe, true ) {
89+ fullOutput. append ( output)
90+ onNext ? ( ( output, nil ) )
91+ }
10592 }
10693
107- errPipe. fileHandleForReading. readabilityHandler = { pipe in
108- self . output = nil
109- self . errput = self . readPipe ( errPipe, true )
110- onRunning ? ( self )
94+ errPipe. fileHandleForReading. readabilityHandler = { _ in
95+ if let errput: String = self . readPipe ( errPipe, true ) {
96+ fullErrput. append ( errput)
97+ onNext ? ( ( nil , errput) )
98+ }
11199 }
112100
113101 task. terminationHandler = { _ in
114102 outPipe. fileHandleForReading. closeFile ( )
115103 errPipe. fileHandleForReading. closeFile ( )
116- self . output = nil
117- self . errput = nil
118- onTerminate ? ( self )
104+ onCompleted ? ( ( fullOutput, fullErrput) )
119105 }
120106
121107 self . launch ( )
0 commit comments