Skip to content

Commit

Permalink
Merge pull request #21 from kaneshin/autopath
Browse files Browse the repository at this point in the history
Detect command path when launching
  • Loading branch information
dataich committed Aug 17, 2015
2 parents 44cd8ca + 5ee609f commit f356a3d
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
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
56 changes: 56 additions & 0 deletions LiveDiag/Library/LDUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit f356a3d

Please sign in to comment.