diff --git a/native/atom_application.h b/native/atom_application.h index e93905a5a..152e35fcd 100644 --- a/native/atom_application.h +++ b/native/atom_application.h @@ -5,12 +5,13 @@ class AtomCefClient; @interface AtomApplication : NSApplication { NSWindowController *_backgroundWindowController; - NSMutableDictionary *_arguments; + NSDictionary *_arguments; BOOL handlingSendEvent_; } + (id)applicationWithArguments:(char **)argv count:(int)argc; + (CefSettings)createCefSettings; ++ (NSDictionary *)parseArguments:(char **)argv count:(int)argc; - (void)open:(NSString *)path; - (IBAction)runSpecs:(id)sender; - (IBAction)runBenchmarks:(id)sender; @@ -18,6 +19,6 @@ class AtomCefClient; - (NSDictionary *)arguments; - (void)runBenchmarksThenExit:(BOOL)exitWhenDone; -@property (nonatomic, readonly) NSDictionary *arguments; +@property (nonatomic, retain) NSDictionary *arguments; @end \ No newline at end of file diff --git a/native/atom_application.mm b/native/atom_application.mm index bbb1bf21b..dddec0a63 100644 --- a/native/atom_application.mm +++ b/native/atom_application.mm @@ -12,10 +12,66 @@ + (id)applicationWithArguments:(char **)argv count:(int)argc { AtomApplication *application = (AtomApplication *)[super sharedApplication]; CefInitialize(CefMainArgs(argc, argv), [self createCefSettings], new AtomCefApp); - [application parseArguments:argv count:argc]; + application.arguments = [self parseArguments:argv count:argc]; + return application; } ++ (NSDictionary *)parseArguments:(char **)argv count:(int)argc { + NSMutableDictionary *arguments = [[NSMutableDictionary alloc] init]; + + // Defaults + #ifdef RESOURCE_PATH + [arguments setObject:[NSString stringWithUTF8String:RESOURCE_PATH] forKey:@"resource-path"]; + #endif + + int opt; + int longindex; + + if (argc > 2 && strcmp(argv[argc - 2], "-NSDocumentRevisionsDebugMode") == 0) { // Because Xcode inserts useless command-line args by default: http://trac.wxwidgets.org/ticket/13732 + argc -= 2; // Ignore last two arguments + } + + static struct option longopts[] = { + { "executed-from", optional_argument, NULL, 'K' }, + { "resource-path", optional_argument, NULL, 'r' }, + { "benchmark", optional_argument, NULL, 'b' }, + { "test", optional_argument, NULL, 't' }, + { NULL, 0, NULL, 0 } + }; + + while ((opt = getopt_long(argc, argv, "r:K:bth?", longopts, &longindex)) != -1) { + NSString *key = [NSString stringWithUTF8String:longopts[longindex].name]; + NSString *value = [NSString stringWithUTF8String:optarg]; + + switch (opt) { + case 'K': + case 'r': + case 'b': + case 't': + [arguments setObject:value forKey:key]; + break; + default: + printf("usage: atom [--resource-path=] []"); + } + } + + argc -= optind; + argv += optind; + + if (argc > 0) { + NSString *path = [NSString stringWithUTF8String:argv[0]]; + NSString *executedFromPath =[arguments objectForKey:@"executed-from"]; + if (![path isAbsolutePath] && executedFromPath) { + path = [executedFromPath stringByAppendingPathComponent:path]; + } + path = [path stringByStandardizingPath]; + [arguments setObject:path forKey:@"path"]; + } + + return arguments; +} + + (NSString *)supportDirectory { NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString *executableName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleExecutable"]; @@ -25,7 +81,7 @@ NSError *error; BOOL success = [fs createDirectoryAtPath:supportDirectory withIntermediateDirectories:YES attributes:nil error:&error]; if (!success) { - NSLog(@"Can't create support directory '%@' because %@", supportDirectory, [error localizedDescription]); + NSLog(@"Warning: Can't create support directory '%@' because %@", supportDirectory, [error localizedDescription]); supportDirectory = @""; } @@ -70,67 +126,19 @@ [[AtomWindowController alloc] initBenchmarksThenExit:exitWhenDone]; } -- (void)parseArguments:(char **)argv count:(int)argc { - _arguments = [[NSMutableDictionary alloc] init]; - - // Defaults - #ifdef RESOURCE_PATH - [_arguments setObject:[NSString stringWithUTF8String:RESOURCE_PATH] forKey:@"resource-path"]; - #endif - - - int opt; - int longindex; - - if (argc > 2 && strcmp(argv[argc - 2], "-NSDocumentRevisionsDebugMode") == 0) { // Because Xcode inserts useless command-line args by default: http://trac.wxwidgets.org/ticket/13732 - argc -= 2; // Ignore last two arguments - } - - static struct option longopts[] = { - { "resource-path", optional_argument, NULL, 'r' }, - { "benchmark", optional_argument, NULL, 'b' }, - { "test", optional_argument, NULL, 't' }, - { NULL, 0, NULL, 0 } - }; - - while ((opt = getopt_long(argc, argv, "r:bth?", longopts, &longindex)) != -1) { - switch (opt) { - case 'r': - [_arguments setObject:[NSString stringWithUTF8String:optarg] forKey:@"resource-path"]; - break; - case 'b': - [_arguments setObject:[NSNumber numberWithBool:YES] forKey:@"benchmark"]; - break; - case 't': - [_arguments setObject:[NSNumber numberWithBool:YES] forKey:@"test"]; - break; - default: - printf("usage: atom [--resource-path=] []"); - } - } - - argc -= optind; - argv += optind; - - if (argc > 0) { - [_arguments setObject:[NSString stringWithUTF8String:argv[0]] forKey:@"path"]; - } -} - # pragma mark NSApplicationDelegate - (void)applicationWillFinishLaunching:(NSNotification *)notification { _backgroundWindowController = [[AtomWindowController alloc] initInBackground]; - if ([_arguments objectForKey:@"benchmark"]) { + if ([self.arguments objectForKey:@"benchmark"]) { [self runBenchmarksThenExit:true]; } - else if ([_arguments objectForKey:@"test"]) { + else if ([self.arguments objectForKey:@"test"]) { [self runSpecsThenExit:true]; } else { - NSLog(@"%@", [_arguments objectForKey:@"path"]); - [self open:@"/Users/corey/atom"];//[_arguments objectForKey:@"path"]]; + [self open:[self.arguments objectForKey:@"path"]]; } }