-
Notifications
You must be signed in to change notification settings - Fork 27
/
TCTaskHelper.m
50 lines (34 loc) · 1.22 KB
/
TCTaskHelper.m
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
//
// TCTaskHelper.m
//
// Created by Tim Perfitt on 2/20/17.
// Copyright © 2017 Twocanoes Software, Inc. All rights reserved.
//
#import "TCTaskHelper.h"
@implementation TCTaskHelper
+(TCTaskHelper *)sharedTaskHelper{
static TCTaskHelper *sharedTaskHelper;
if(sharedTaskHelper==nil){
sharedTaskHelper=[[TCTaskHelper alloc] init];
}
return sharedTaskHelper;
}
-(NSString *)runCommand:(NSString *)command withOptions:(NSArray *)inOptions{
NSLog(@"running %@ %@",command, [inOptions componentsJoinedByString:@" "]);
NSPipe *pipe = [NSPipe pipe];
NSMutableString *outputString=[NSMutableString string];
NSTask *task = [NSTask new];
[task setLaunchPath:command];
[task setArguments:inOptions];
[task setStandardOutput:pipe];
[task launch];
NSData *data = [[pipe fileHandleForReading] readDataToEndOfFile];
// [task waitUntilExit];
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSArray *lines=[string componentsSeparatedByString:@"\n"];
for (NSString *line in lines) {
[outputString appendString:line];
}
return [NSString stringWithString:outputString];
}
@end