Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 123 lines (94 sloc) 3.448 kb
08757ad Pieter de Bie Add easy pipe handling library. Use it to find real git repository path
pieter authored
1 //
2 // PBEasyPipe.m
3 // GitX
4 //
5 // Created by Pieter de Bie on 16-06-08.
6 // Copyright 2008 __MyCompanyName__. All rights reserved.
7 //
8
9 #import "PBEasyPipe.h"
10
11
12 @implementation PBEasyPipe
13
14 + (NSFileHandle*) handleForCommand: (NSString*) cmd withArgs: (NSArray*) args
15 {
16 return [self handleForCommand:cmd withArgs:args inDir:nil];
17 }
18
faef971 Pieter de Bie EasyPipe: Add functions to ask for the return value of commands
pieter authored
19 + (NSTask *) taskForCommand:(NSString *)cmd withArgs:(NSArray *)args inDir:(NSString *)dir
08757ad Pieter de Bie Add easy pipe handling library. Use it to find real git repository path
pieter authored
20 {
21 NSTask* task = [[NSTask alloc] init];
22 task.launchPath = cmd;
23 task.arguments = args;
24 if (dir)
25 task.currentDirectoryPath = dir;
5a7d1e9 Pieter de Bie EasyPipe: Only show debug when user pref is set
pieter authored
26
27 if ([[NSUserDefaults standardUserDefaults] boolForKey:@"Show Debug Messages"])
28 NSLog(@"Starting command `%@ %@` in dir %@", cmd, [args componentsJoinedByString:@" "], dir);
29
08757ad Pieter de Bie Add easy pipe handling library. Use it to find real git repository path
pieter authored
30 NSPipe* pipe = [NSPipe pipe];
31 task.standardOutput = pipe;
faef971 Pieter de Bie EasyPipe: Add functions to ask for the return value of commands
pieter authored
32 return task;
33 }
34
35 + (NSFileHandle*) handleForCommand: (NSString*) cmd withArgs: (NSArray*) args inDir: (NSString*) dir
36 {
37 NSTask *task = [self taskForCommand:cmd withArgs:args inDir:dir];
38 NSFileHandle* handle = [task.standardOutput fileHandleForReading];
08757ad Pieter de Bie Add easy pipe handling library. Use it to find real git repository path
pieter authored
39
40 [task launch];
41 return handle;
42 }
43
44
45
faef971 Pieter de Bie EasyPipe: Add functions to ask for the return value of commands
pieter authored
46 + (NSString*) outputForCommand:(NSString *) cmd
47 withArgs:(NSArray *) args
48 inDir:(NSString *) dir
49 retValue:(int *) ret
08757ad Pieter de Bie Add easy pipe handling library. Use it to find real git repository path
pieter authored
50 {
faef971 Pieter de Bie EasyPipe: Add functions to ask for the return value of commands
pieter authored
51 NSTask *task = [self taskForCommand:cmd withArgs:args inDir:dir];
52 NSFileHandle* handle = [task.standardOutput fileHandleForReading];
53 [task launch];
4024753 Pieter de Bie PBEasyPipe: Add a new method to provide input
pieter authored
54
08757ad Pieter de Bie Add easy pipe handling library. Use it to find real git repository path
pieter authored
55 NSData* data = [handle readDataToEndOfFile];
7cdfdf0 Pieter de Bie Use UTF-8 for data decoding
pieter authored
56 NSString* string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
4024753 Pieter de Bie PBEasyPipe: Add a new method to provide input
pieter authored
57
faef971 Pieter de Bie EasyPipe: Add functions to ask for the return value of commands
pieter authored
58 // Strip trailing newline
08757ad Pieter de Bie Add easy pipe handling library. Use it to find real git repository path
pieter authored
59 if ([string hasSuffix:@"\n"])
60 string = [string substringToIndex:[string length]-1];
4024753 Pieter de Bie PBEasyPipe: Add a new method to provide input
pieter authored
61
62 [task waitUntilExit];
63 if (ret)
64 *ret = [task terminationStatus];
65 return string;
66 }
faef971 Pieter de Bie EasyPipe: Add functions to ask for the return value of commands
pieter authored
67
2676a20 Pieter de Bie Fix some compiler warnings
pieter authored
68 // TODO: Refactor this to use the function above
4024753 Pieter de Bie PBEasyPipe: Add a new method to provide input
pieter authored
69 + (NSString*) outputForCommand:(NSString *) cmd
70 withArgs:(NSArray *) args
71 inDir:(NSString *) dir
72 inputString:(NSString *)input
73 retValue:(int *) ret
74 {
75 NSTask *task = [self taskForCommand:cmd withArgs:args inDir:dir];
76 NSFileHandle* handle = [task.standardOutput fileHandleForReading];
77 task.standardInput = [NSPipe pipe];
78 NSFileHandle *inHandle = [task.standardInput fileHandleForWriting];
79 [inHandle writeData:[input dataUsingEncoding:NSUTF8StringEncoding]];
80 [inHandle closeFile];
81
82 [task launch];
83
84 NSData* data = [handle readDataToEndOfFile];
85 NSString* string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
86
87 // Strip trailing newline
88 if ([string hasSuffix:@"\n"])
89 string = [string substringToIndex:[string length]-1];
90
faef971 Pieter de Bie EasyPipe: Add functions to ask for the return value of commands
pieter authored
91 [task waitUntilExit];
92 if (ret)
93 *ret = [task terminationStatus];
08757ad Pieter de Bie Add easy pipe handling library. Use it to find real git repository path
pieter authored
94 return string;
95 }
faef971 Pieter de Bie EasyPipe: Add functions to ask for the return value of commands
pieter authored
96
97 // We don't use the above function because then we'd have to wait until the program was finished
98 // with running
4024753 Pieter de Bie PBEasyPipe: Add a new method to provide input
pieter authored
99
100 + (NSString*) outputForCommand: (NSString*) cmd withArgs: (NSArray*) args inDir: (NSString*) dir
faef971 Pieter de Bie EasyPipe: Add functions to ask for the return value of commands
pieter authored
101 {
102 NSTask *task = [self taskForCommand:cmd withArgs:args inDir:dir];
103 NSFileHandle* handle = [task.standardOutput fileHandleForReading];
4024753 Pieter de Bie PBEasyPipe: Add a new method to provide input
pieter authored
104
faef971 Pieter de Bie EasyPipe: Add functions to ask for the return value of commands
pieter authored
105 [task launch];
106
107 NSData* data = [handle readDataToEndOfFile];
108 NSString* string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
b0caa34 Pieter de Bie Revert "Wait for a child to clear it out."
pieter authored
109
faef971 Pieter de Bie EasyPipe: Add functions to ask for the return value of commands
pieter authored
110 // Strip trailing newline
111 if ([string hasSuffix:@"\n"])
112 string = [string substringToIndex:[string length]-1];
113 return string;
114 }
115
4024753 Pieter de Bie PBEasyPipe: Add a new method to provide input
pieter authored
116
08757ad Pieter de Bie Add easy pipe handling library. Use it to find real git repository path
pieter authored
117 + (NSString*) outputForCommand: (NSString*) cmd withArgs: (NSArray*) args
118 {
119 return [self outputForCommand:cmd withArgs:args inDir:nil];
120 }
cc6ac22 Pieter de Bie Allow double click to open file
pieter authored
121
08757ad Pieter de Bie Add easy pipe handling library. Use it to find real git repository path
pieter authored
122 @end
Something went wrong with that request. Please try again.