diff --git a/LiveDiag/LDAppDelegate.m b/LiveDiag/LDAppDelegate.m index 43775b1..b5aef78 100644 --- a/LiveDiag/LDAppDelegate.m +++ b/LiveDiag/LDAppDelegate.m @@ -14,7 +14,7 @@ @implementation LDAppDelegate - (void)applicationDidFinishLaunching:(NSNotification *)aNotification { //check path settings, any commands - if([LDUtils pathTo:@"blockdiag"] == nil || [LDUtils pathTo:@"seqdiag"] == nil || [LDUtils pathTo:@"actdiag"] == nil || [LDUtils pathTo:@"nwdiag"] == nil || [LDUtils pathTo:@"rackdiag"] == nil) { + if (![LDUtils checkPaths]) { NSAlert *alert = [NSAlert alertWithMessageText:@"Path Setting" defaultButton:@"OK" alternateButton:@"Cancel" otherButton:nil informativeTextWithFormat:@"Please specify a path to blockdiag and the other."]; if ([alert runModal] == NSAlertDefaultReturn) { LDAppDelegate *delegate = (LDAppDelegate *)[[NSApplication sharedApplication] delegate]; diff --git a/LiveDiag/Library/LDUtils.h b/LiveDiag/Library/LDUtils.h index 7e52b21..57e83b6 100644 --- a/LiveDiag/Library/LDUtils.h +++ b/LiveDiag/Library/LDUtils.h @@ -10,6 +10,7 @@ @interface LDUtils : NSObject ++ (BOOL)checkPaths; + (NSString *)pathTo:(NSString *)command; @end diff --git a/LiveDiag/Library/LDUtils.m b/LiveDiag/Library/LDUtils.m index 9d2ac03..2fafc72 100644 --- a/LiveDiag/Library/LDUtils.m +++ b/LiveDiag/Library/LDUtils.m @@ -10,9 +10,65 @@ @implementation LDUtils ++ (BOOL)checkPaths { + __block NSArray *commands = @[@"blockdiag", @"seqdiag", @"actdiag", @"nwdiag", @"rackdiag"]; + + BOOL (^validatePaths)() = ^BOOL() { + __block BOOL r; + [commands enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { + NSString *path = [LDUtils pathTo:obj]; + r = (path != nil && ![path isEqualToString:@""]); + *stop = !r; + }]; + return r; + }; + + BOOL result = validatePaths(); + if (!result) { + [commands enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { + NSString *path = [LDUtils pathTo:obj]; + if (path == nil || [path isEqualToString:@""]) { + [LDUtils setPathWithCommand:obj]; + } + }]; + result = validatePaths(); + } + return result; +} + + (NSString *)pathTo:(NSString *)command { NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; return [userDefaults stringForKey:[NSString stringWithFormat:@"path_%@", command]]; } ++ (NSString *)setPathWithCommand:(NSString *)command { + NSTask *task = [[NSTask alloc] init]; + NSPipe *pipe = [NSPipe pipe]; + + [task setStandardOutput:pipe]; + [task setCurrentDirectoryPath:[[NSBundle mainBundle].bundlePath stringByDeletingLastPathComponent]]; + [task setLaunchPath:@"/bin/bash"]; + + NSString *run = [NSString stringWithFormat:@"which %@", command]; + NSArray *args = [NSArray arrayWithObjects:@"-l", @"-c", run, nil]; + [task setArguments:args]; + [task launch]; + [task waitUntilExit]; + + if ([task terminationStatus] != 0) { + // Exit if not exists. + return nil; + } + + NSData *data = pipe.fileHandleForReading.availableData; + NSString *path = [NSString stringWithFormat:@"%s", data.bytes]; + NSArray *paths = [path componentsSeparatedByString:@"\n"]; + if (paths.count > 0) { + path = paths.firstObject; + } + NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; + [userDefaults setValue:path forKey:[NSString stringWithFormat:@"path_%@", command]]; + return path; +} + @end