OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #import <Foundation/Foundation.h> | 5 #import <Foundation/Foundation.h> |
6 #include <asl.h> | 6 #include <asl.h> |
7 #include <libgen.h> | 7 #include <libgen.h> |
8 #include <stdarg.h> | 8 #include <stdarg.h> |
9 #include <stdio.h> | 9 #include <stdio.h> |
10 | 10 |
(...skipping 433 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
444 // Creates the necessary directory structure under the given user home directory | 444 // Creates the necessary directory structure under the given user home directory |
445 // path. | 445 // path. |
446 // Returns YES if successful, NO if unable to create the directories. | 446 // Returns YES if successful, NO if unable to create the directories. |
447 BOOL CreateHomeDirSubDirs(NSString* userHomePath) { | 447 BOOL CreateHomeDirSubDirs(NSString* userHomePath) { |
448 NSFileManager* fileManager = [NSFileManager defaultManager]; | 448 NSFileManager* fileManager = [NSFileManager defaultManager]; |
449 | 449 |
450 // Create user home and subdirectories. | 450 // Create user home and subdirectories. |
451 NSArray* subDirsToCreate = [NSArray arrayWithObjects: | 451 NSArray* subDirsToCreate = [NSArray arrayWithObjects: |
452 @"Documents", | 452 @"Documents", |
453 @"Library/Caches", | 453 @"Library/Caches", |
454 @"Library/Preferences", | |
454 nil]; | 455 nil]; |
455 for (NSString* subDir in subDirsToCreate) { | 456 for (NSString* subDir in subDirsToCreate) { |
456 NSString* path = [userHomePath stringByAppendingPathComponent:subDir]; | 457 NSString* path = [userHomePath stringByAppendingPathComponent:subDir]; |
457 NSError* error; | 458 NSError* error; |
458 if (![fileManager createDirectoryAtPath:path | 459 if (![fileManager createDirectoryAtPath:path |
459 withIntermediateDirectories:YES | 460 withIntermediateDirectories:YES |
460 attributes:nil | 461 attributes:nil |
461 error:&error]) { | 462 error:&error]) { |
462 LogError(@"Unable to create directory: %@. Error: %@", | 463 LogError(@"Unable to create directory: %@. Error: %@", |
463 path, [error localizedDescription]); | 464 path, [error localizedDescription]); |
464 return NO; | 465 return NO; |
465 } | 466 } |
466 } | 467 } |
467 | 468 |
468 return YES; | 469 return YES; |
469 } | 470 } |
470 | 471 |
471 // Creates the necessary directory structure under the given user home directory | 472 // Creates the necessary directory structure under the given user home directory |
472 // path, then sets the path in the appropriate environment variable. | 473 // path, then sets the path in the appropriate environment variable. |
473 // Returns YES if successful, NO if unable to create or initialize the given | 474 // Returns YES if successful, NO if unable to create or initialize the given |
474 // directory. | 475 // directory. |
475 BOOL InitializeSimulatorUserHome(NSString* userHomePath) { | 476 BOOL InitializeSimulatorUserHome(NSString* userHomePath, NSString* deviceName) { |
476 if (!CreateHomeDirSubDirs(userHomePath)) | 477 if (!CreateHomeDirSubDirs(userHomePath)) |
477 return NO; | 478 return NO; |
478 | 479 |
480 NSMutableDictionary* plistDict = | |
TVL
2012/09/25 19:13:23
might be worth a comment here that the simulator h
lliabraa
2012/10/04 15:51:39
Done.
| |
481 [NSMutableDictionary dictionaryWithObject:deviceName | |
482 forKey:@"SimulateDevice"]; | |
483 NSString* plistPath = @"Library/Preferences/com.apple.iphonesimulator.plist"; | |
484 [plistDict writeToFile:[userHomePath stringByAppendingPathComponent:plistPath] | |
485 atomically:YES]; | |
486 | |
479 // Update the environment to use the specified directory as the user home | 487 // Update the environment to use the specified directory as the user home |
480 // directory. | 488 // directory. |
481 // Note: the third param of setenv specifies whether or not to overwrite the | 489 // Note: the third param of setenv specifies whether or not to overwrite the |
482 // variable's value if it has already been set. | 490 // variable's value if it has already been set. |
483 if ((setenv(kUserHomeEnvVariable, [userHomePath UTF8String], YES) == -1) || | 491 if ((setenv(kUserHomeEnvVariable, [userHomePath UTF8String], YES) == -1) || |
484 (setenv(kHomeEnvVariable, [userHomePath UTF8String], YES) == -1)) { | 492 (setenv(kHomeEnvVariable, [userHomePath UTF8String], YES) == -1)) { |
485 LogError(@"Unable to set environment variables for home directory."); | 493 LogError(@"Unable to set environment variables for home directory."); |
486 return NO; | 494 return NO; |
487 } | 495 } |
488 | 496 |
489 return YES; | 497 return YES; |
490 } | 498 } |
491 | 499 |
492 // Prints the usage information to stderr. | 500 // Prints the usage information to stderr. |
493 void PrintUsage() { | 501 void PrintUsage() { |
494 fprintf(stderr, "Usage: iossim [-d device] [-s sdkVersion] [-u homeDir] " | 502 fprintf(stderr, "Usage: iossim [-d device] [-s sdkVersion] [-u homeDir] " |
495 "[-e envKey=value]* [-t startupTimeout] <appPath> [<appArgs>]\n" | 503 "[-e envKey=value]* [-t startupTimeout] <appPath> [<appArgs>]\n" |
496 " where <appPath> is the path to the .app directory and appArgs are any" | 504 " where <appPath> is the path to the .app directory and appArgs are any" |
497 " arguments to send the simulated app.\n" | 505 " arguments to send the simulated app.\n" |
498 "\n" | 506 "\n" |
499 "Options:\n" | 507 "Options:\n" |
500 " -d Specifies the device (either 'iPhone' or 'iPad')." | 508 " -d Specifies the device (must be one of the values from the iOS" |
501 " Defaults to 'iPhone'.\n" | 509 " Simulator's Hardware -> Device menu. Defaults to 'iPhone'.\n" |
502 " -s Specifies the SDK version to use (e.g '4.3')." | 510 " -s Specifies the SDK version to use (e.g '4.3')." |
503 " Will use system default if not specified.\n" | 511 " Will use system default if not specified.\n" |
504 " -u Specifies a user home directory for the simulator." | 512 " -u Specifies a user home directory for the simulator." |
505 " Will create a new directory if not specified.\n" | 513 " Will create a new directory if not specified.\n" |
506 " -e Specifies an environment key=value pair that will be" | 514 " -e Specifies an environment key=value pair that will be" |
507 " set in the simulated application's environment.\n" | 515 " set in the simulated application's environment.\n" |
508 " -t Specifies the session startup timeout (in seconds)." | 516 " -t Specifies the session startup timeout (in seconds)." |
509 " Defaults to %d.\n", | 517 " Defaults to %d.\n", |
510 static_cast<int>(kDefaultSessionStartTimeoutSeconds)); | 518 static_cast<int>(kDefaultSessionStartTimeoutSeconds)); |
511 } | 519 } |
512 | 520 |
513 } // namespace | 521 } // namespace |
514 | 522 |
515 int main(int argc, char* const argv[]) { | 523 int main(int argc, char* const argv[]) { |
516 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; | 524 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; |
517 | 525 |
518 // basename() may modify the passed in string and it returns a pointer to an | 526 // basename() may modify the passed in string and it returns a pointer to an |
519 // internal buffer. Give it a copy to modify, and copy what it returns. | 527 // internal buffer. Give it a copy to modify, and copy what it returns. |
520 char* worker = strdup(argv[0]); | 528 char* worker = strdup(argv[0]); |
521 char* toolName = basename(worker); | 529 char* toolName = basename(worker); |
522 if (toolName != NULL) { | 530 if (toolName != NULL) { |
523 toolName = strdup(toolName); | 531 toolName = strdup(toolName); |
524 if (toolName != NULL) | 532 if (toolName != NULL) |
525 gToolName = toolName; | 533 gToolName = toolName; |
526 } | 534 } |
527 if (worker != NULL) | 535 if (worker != NULL) |
528 free(worker); | 536 free(worker); |
529 | 537 |
538 | |
530 NSString* appPath = nil; | 539 NSString* appPath = nil; |
531 NSString* appName = nil; | 540 NSString* appName = nil; |
532 NSString* sdkVersion = nil; | 541 NSString* sdkVersion = nil; |
533 NSString* deviceName = @"iPhone"; | 542 NSString* deviceName = @"iPhone"; |
534 NSString* simHomePath = nil; | 543 NSString* simHomePath = nil; |
535 NSMutableArray* appArgs = [NSMutableArray array]; | 544 NSMutableArray* appArgs = [NSMutableArray array]; |
536 NSMutableDictionary* appEnv = [NSMutableDictionary dictionary]; | 545 NSMutableDictionary* appEnv = [NSMutableDictionary dictionary]; |
537 NSTimeInterval sessionStartTimeout = kDefaultSessionStartTimeoutSeconds; | 546 NSTimeInterval sessionStartTimeout = kDefaultSessionStartTimeoutSeconds; |
538 | 547 |
539 // Parse the optional arguments | 548 // Parse the optional arguments |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
623 if (!systemRoot) { | 632 if (!systemRoot) { |
624 LogError(@"Invalid SDK version: %@", sdkVersion); | 633 LogError(@"Invalid SDK version: %@", sdkVersion); |
625 exit(EXIT_FAILURE); | 634 exit(EXIT_FAILURE); |
626 } | 635 } |
627 | 636 |
628 // Get the paths for stdout and stderr so the simulated app's output will show | 637 // Get the paths for stdout and stderr so the simulated app's output will show |
629 // up in the caller's stdout/stderr. | 638 // up in the caller's stdout/stderr. |
630 NSString* outputDir = CreateTempDirectory(@"iossim-XXXXXX"); | 639 NSString* outputDir = CreateTempDirectory(@"iossim-XXXXXX"); |
631 NSString* stdioPath = [outputDir stringByAppendingPathComponent:@"stdio.txt"]; | 640 NSString* stdioPath = [outputDir stringByAppendingPathComponent:@"stdio.txt"]; |
632 | 641 |
633 // Make sure the device name is legit. | 642 // Determine the deviceFamily based on the deviceName |
634 NSNumber* deviceFamily = nil; | 643 NSNumber* deviceFamily = nil; |
635 if (!deviceName || | 644 if (!deviceName || [deviceName hasPrefix:@"iPhone"]) { |
636 [@"iPhone" caseInsensitiveCompare:deviceName] == NSOrderedSame) { | |
637 deviceFamily = [NSNumber numberWithInt:kIPhoneFamily]; | 645 deviceFamily = [NSNumber numberWithInt:kIPhoneFamily]; |
638 } else if ([@"iPad" caseInsensitiveCompare:deviceName] == NSOrderedSame) { | 646 } else if ([deviceName hasPrefix:@"iPad"]) { |
639 deviceFamily = [NSNumber numberWithInt:kIPadFamily]; | 647 deviceFamily = [NSNumber numberWithInt:kIPadFamily]; |
640 } else { | 648 } else { |
641 LogError(@"Invalid device name: %@", deviceName); | 649 LogError(@"Invalid device name: %@. Must begin with 'iPhone' or 'iPad'", |
650 deviceName); | |
642 exit(EXIT_FAILURE); | 651 exit(EXIT_FAILURE); |
643 } | 652 } |
644 | 653 |
645 // Set up the user home directory for the simulator | 654 // Set up the user home directory for the simulator |
646 if (!simHomePath) { | 655 if (!simHomePath) { |
647 NSString* dirNameTemplate = | 656 NSString* dirNameTemplate = |
648 [NSString stringWithFormat:@"iossim-%@-%@-XXXXXX", appName, deviceName]; | 657 [NSString stringWithFormat:@"iossim-%@-%@-XXXXXX", appName, deviceName]; |
649 simHomePath = CreateTempDirectory(dirNameTemplate); | 658 simHomePath = CreateTempDirectory(dirNameTemplate); |
650 if (!simHomePath) { | 659 if (!simHomePath) { |
651 LogError(@"Unable to create unique directory for template %@", | 660 LogError(@"Unable to create unique directory for template %@", |
652 dirNameTemplate); | 661 dirNameTemplate); |
653 exit(EXIT_FAILURE); | 662 exit(EXIT_FAILURE); |
654 } | 663 } |
655 } | 664 } |
656 if (!InitializeSimulatorUserHome(simHomePath)) { | 665 if (!InitializeSimulatorUserHome(simHomePath, deviceName)) { |
657 LogError(@"Unable to initialize home directory for simulator: %@", | 666 LogError(@"Unable to initialize home directory for simulator: %@", |
658 simHomePath); | 667 simHomePath); |
659 exit(EXIT_FAILURE); | 668 exit(EXIT_FAILURE); |
660 } | 669 } |
661 | 670 |
662 // Create the config and simulator session. | 671 // Create the config and simulator session. |
663 DTiPhoneSimulatorSessionConfig* config = BuildSessionConfig(appSpec, | 672 DTiPhoneSimulatorSessionConfig* config = BuildSessionConfig(appSpec, |
664 systemRoot, | 673 systemRoot, |
665 stdioPath, | 674 stdioPath, |
666 stdioPath, | 675 stdioPath, |
(...skipping 20 matching lines...) Expand all Loading... | |
687 [error localizedDescription], | 696 [error localizedDescription], |
688 [error domain], static_cast<long int>([error code])); | 697 [error domain], static_cast<long int>([error code])); |
689 } | 698 } |
690 | 699 |
691 // Note that this code is only executed if the simulator fails to start | 700 // Note that this code is only executed if the simulator fails to start |
692 // because once the main run loop is started, only the delegate calling | 701 // because once the main run loop is started, only the delegate calling |
693 // exit() will end the program. | 702 // exit() will end the program. |
694 [pool drain]; | 703 [pool drain]; |
695 return EXIT_FAILURE; | 704 return EXIT_FAILURE; |
696 } | 705 } |
OLD | NEW |