Skip to content

Commit

Permalink
Modify to set path automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
kaneshin committed Feb 13, 2015
1 parent 8769d4c commit 5ee609f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
2 changes: 1 addition & 1 deletion LiveDiag/LDAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
1 change: 1 addition & 0 deletions LiveDiag/Library/LDUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

@interface LDUtils : NSObject

+ (BOOL)checkPaths;
+ (NSString *)pathTo:(NSString *)command;

@end
43 changes: 36 additions & 7 deletions LiveDiag/Library/LDUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,61 @@

@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];
NSString *path = [userDefaults stringForKey:[NSString stringWithFormat:@"path_%@", command]];
if (path == nil || [path isEqualTo:@""]) {
path = [LDUtils setPathWithCommand:command];
}
return path;
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[0];
path = paths.firstObject;
}
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setValue:path forKey:[NSString stringWithFormat:@"path_%@", command]];
Expand Down

0 comments on commit 5ee609f

Please sign in to comment.