From 77fa51176fa38b9e74dd8b3e94181c8c833593b1 Mon Sep 17 00:00:00 2001 From: Charlie Heizer Date: Wed, 19 Jul 2017 17:46:06 -0700 Subject: [PATCH 1/2] Added environment property Added terminationStatus to onError --- GCDTask.h | 3 ++- GCDTask.m | 13 ++++++++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/GCDTask.h b/GCDTask.h index 028bb41..6b6ad36 100644 --- a/GCDTask.h +++ b/GCDTask.h @@ -26,6 +26,7 @@ @property (strong) NSString* launchPath; @property (strong) NSArray* arguments; +@property (strong) NSDictionary* environment; @property BOOL hasExecuted; @property __block dispatch_source_t stdoutSource; @property __block dispatch_source_t stderrSource; @@ -35,7 +36,7 @@ - (void) launchWithOutputBlock: (void (^)(NSData* stdOutData)) stdOut andErrorBlock: (void (^)(NSData* stdErrData)) stdErr onLaunch: (void (^)()) launched - onExit: (void (^)()) exit; + onExit: (void (^)(int)) exit; - (BOOL) WriteStringToStandardInput: (NSString*) input; - (BOOL) WriteDataToStandardInput: (NSData*) input; diff --git a/GCDTask.m b/GCDTask.m index 8a8a75d..2749b12 100644 --- a/GCDTask.m +++ b/GCDTask.m @@ -18,7 +18,7 @@ - (id) init - (void) launchWithOutputBlock: (void (^)(NSData* stdOutData)) stdOut andErrorBlock: (void (^)(NSData* stdErrData)) stdErr onLaunch: (void (^)()) launched - onExit: (void (^)()) exit + onExit: (void (^)(int)) exit { executingTask = [[NSTask alloc] init]; @@ -46,9 +46,12 @@ - (void) launchWithOutputBlock: (void (^)(NSData* stdOutData)) stdOut } } - [executingTask setArguments:_arguments]; - + + /* Add Environment variables */ + if (_environment) { + [executingTask setEnvironment:_environment]; + } /* Setup pipes */ stdinPipe = [NSPipe pipe]; @@ -106,7 +109,7 @@ - (void) launchWithOutputBlock: (void (^)(NSData* stdOutData)) stdOut dispatch_source_cancel(_stdoutSource); dispatch_async(dispatch_get_main_queue(), ^{ if(exit) - exit(); + exit([executingTask terminationStatus]); }); } @@ -153,7 +156,7 @@ - (void) launchWithOutputBlock: (void (^)(NSData* stdOutData)) stdOut dispatch_source_cancel(_stdoutSource); dispatch_source_cancel(_stderrSource); if(exit) - exit(); + exit([executingTask terminationStatus]); }; [executingTask launch]; From 9834c542045d3ce34887af5b01627e4b36213742 Mon Sep 17 00:00:00 2001 From: Charlie Heizer Date: Wed, 19 Jul 2017 17:50:40 -0700 Subject: [PATCH 2/2] Updated ReadMe with exitStatus usage --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 3ce60c5..0bbb054 100644 --- a/README.md +++ b/README.md @@ -21,8 +21,8 @@ Example usage: NSLog(@"ERR: %@", output); } onLaunch:^{ NSLog(@"Task has started running."); - } onExit:^{ - NSLog(@"Task has now quit."); + } onExit:^(int exitStatus) { + NSLog(@"Task has now quit. Exit status %d",exitStatus); }]; ```