Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

EasyPipe: Add functions to ask for the return value of commands

  • Loading branch information...
commit faef971e184d5ce4392daeb5d628ec922ed5638b 1 parent 1472487
Pieter de Bie authored September 22, 2008
5  PBEasyPipe.h
@@ -18,4 +18,9 @@
18 18
 
19 19
 + (NSString*) outputForCommand: (NSString*) cmd withArgs: (NSArray*) args;
20 20
 + (NSString*) outputForCommand: (NSString*) cmd withArgs: (NSArray*) args inDir: (NSString*) dir;
  21
++ (NSString*) outputForCommand:(NSString *) cmd
  22
+					  withArgs:(NSArray *)  args
  23
+						 inDir:(NSString *) dir
  24
+				      retValue:(int *)      ret;
  25
+
21 26
 @end
48  PBEasyPipe.m
@@ -16,19 +16,24 @@ + (NSFileHandle*) handleForCommand: (NSString*) cmd withArgs: (NSArray*) args
16 16
 	return [self handleForCommand:cmd withArgs:args inDir:nil];
17 17
 }
18 18
 
19  
-+ (NSFileHandle*) handleForCommand: (NSString*) cmd withArgs: (NSArray*) args inDir: (NSString*) dir
  19
++ (NSTask *) taskForCommand:(NSString *)cmd withArgs:(NSArray *)args inDir:(NSString *)dir
20 20
 {
21 21
 	NSTask* task = [[NSTask alloc] init];
22 22
 	task.launchPath = cmd;
23 23
 	task.arguments = args;
24 24
 	if (dir)
25 25
 		task.currentDirectoryPath = dir;
  26
+	
26 27
 	NSLog(@"Starting `cmd %@ %@` in dir %@", cmd, [args componentsJoinedByString:@" "], dir);
27 28
 	NSPipe* pipe = [NSPipe pipe];
28 29
 	task.standardOutput = pipe;
29  
-	
30  
-	NSFileHandle* handle = [NSFileHandle fileHandleWithStandardOutput];
31  
-	handle = [pipe fileHandleForReading];
  30
+	return task;
  31
+}
  32
+
  33
++ (NSFileHandle*) handleForCommand: (NSString*) cmd withArgs: (NSArray*) args inDir: (NSString*) dir
  34
+{
  35
+	NSTask *task = [self taskForCommand:cmd withArgs:args inDir:dir];
  36
+	NSFileHandle* handle = [task.standardOutput fileHandleForReading];
32 37
 	
33 38
 	[task launch];
34 39
 	return handle;
@@ -36,16 +41,45 @@ + (NSFileHandle*) handleForCommand: (NSString*) cmd withArgs: (NSArray*) args in
36 41
 
37 42
 
38 43
 
39  
-+ (NSString*) outputForCommand: (NSString*) cmd withArgs: (NSArray*) args inDir: (NSString*) dir
  44
++ (NSString*) outputForCommand:(NSString *) cmd
  45
+					  withArgs:(NSArray *)  args
  46
+						 inDir:(NSString *) dir
  47
+				      retValue:(int *)      ret
40 48
 {
41  
-	NSFileHandle* handle = [self handleForCommand:cmd withArgs: args inDir: dir];
  49
+	NSTask *task = [self taskForCommand:cmd withArgs:args inDir:dir];
  50
+	NSFileHandle* handle = [task.standardOutput fileHandleForReading];
  51
+	[task launch];
  52
+
42 53
 	NSData* data = [handle readDataToEndOfFile];
43 54
 	NSString* string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  55
+
  56
+	// Strip trailing newline
44 57
 	if ([string hasSuffix:@"\n"])
45 58
 		string = [string substringToIndex:[string length]-1];
46  
-	
  59
+
  60
+	[task waitUntilExit];
  61
+	if (ret)
  62
+		*ret = [task terminationStatus];
47 63
 	return string;
48 64
 }	
  65
+
  66
+// We don't use the above function because then we'd have to wait until the program was finished
  67
+// with running
  68
++ (NSString*) outputForCommand: (NSString*) cmd withArgs: (NSArray*) args inDir: (NSString*) dir
  69
+{
  70
+	NSTask *task = [self taskForCommand:cmd withArgs:args inDir:dir];
  71
+	NSFileHandle* handle = [task.standardOutput fileHandleForReading];
  72
+	[task launch];
  73
+	
  74
+	NSData* data = [handle readDataToEndOfFile];
  75
+	NSString* string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  76
+	
  77
+	// Strip trailing newline
  78
+	if ([string hasSuffix:@"\n"])
  79
+		string = [string substringToIndex:[string length]-1];
  80
+	return string;
  81
+}
  82
+
49 83
 + (NSString*) outputForCommand: (NSString*) cmd withArgs: (NSArray*) args
50 84
 {
51 85
 	return [self outputForCommand:cmd withArgs:args inDir:nil];
2  PBGitRepository.h
@@ -22,7 +22,9 @@ extern NSString* PBGitRepositoryErrorDomain;
22 22
 - (NSFileHandle*) handleForCommand:(NSString*) cmd;
23 23
 - (NSFileHandle*) handleForArguments:(NSArray*) args;
24 24
 - (NSString*) outputForCommand:(NSString*) cmd;
  25
+- (NSString*) outputForCommand:(NSString *)str retValue:(int *)ret;
25 26
 - (NSString*) outputForArguments:(NSArray*) args;
  27
+- (NSString*) outputForArguments:(NSArray*) args retValue:(int *)ret;
26 28
 
27 29
 - (BOOL) reloadRefs;
28 30
 - (void) addRef:(PBGitRef *)ref fromParameters:(NSArray *)params;
11  PBGitRepository.m
@@ -278,11 +278,22 @@ - (NSString*) outputForCommand:(NSString *)cmd
278 278
 	return [self outputForArguments: arguments];
279 279
 }
280 280
 
  281
+- (NSString*) outputForCommand:(NSString *)str retValue:(int *)ret;
  282
+{
  283
+	NSArray* arguments = [str componentsSeparatedByString:@" "];
  284
+	return [self outputForArguments: arguments retValue: ret];
  285
+}
  286
+
281 287
 - (NSString*) outputForArguments:(NSArray*) arguments
282 288
 {
283 289
 	return [PBEasyPipe outputForCommand:gitPath withArgs:arguments inDir: self.fileURL.path];
284 290
 }
285 291
 
  292
+- (NSString*) outputForArguments:(NSArray *)arguments retValue:(int *)ret;
  293
+{
  294
+	return [PBEasyPipe outputForCommand:gitPath withArgs:arguments inDir: self.fileURL.path retValue: ret];
  295
+}
  296
+
286 297
 - (NSString*) parseReference:(NSString *)reference
287 298
 {
288 299
 	return [self outputForArguments:[NSArray arrayWithObjects: @"rev-parse", reference, nil]];

0 notes on commit faef971

Please sign in to comment.
Something went wrong with that request. Please try again.