RLMPSimulatorManager.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. ////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2014 Realm Inc.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. //
  17. ////////////////////////////////////////////////////////////////////////////
  18. #import "RLMPSimulatorManager.h"
  19. static NSString *const RLMPBootedSimulatorKey = @"Booted";
  20. static NSTask *RLMPLaunchedTaskSynchonouslyWithProperty(NSString *path, NSArray *arguments, NSString *__autoreleasing *output)
  21. {
  22. // Setup task with given parameters
  23. NSTask *task = [[NSTask alloc] init];
  24. task.launchPath = path;
  25. task.arguments = arguments;
  26. // Setup output Pipe to created Task
  27. NSPipe *outputPipe = [NSPipe pipe];
  28. task.standardOutput = outputPipe;
  29. [task launch];
  30. [task waitUntilExit];
  31. NSData *outputData = [[outputPipe fileHandleForReading] readDataToEndOfFile];
  32. *output = [[NSString alloc] initWithData:outputData encoding:NSUTF8StringEncoding];
  33. return task;
  34. }
  35. @interface RLMPSimulatorManager ()
  36. @end
  37. @implementation RLMPSimulatorManager
  38. + (NSString *)bootedSimulatorUUID
  39. {
  40. NSString *deviceData = [self readDeviceData];
  41. __block NSString *bootedDeviceUUID;
  42. if (deviceData) {
  43. // Process output
  44. NSDictionary *deviceStatuses = [self processDeviceData:deviceData];
  45. [deviceStatuses enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *value, BOOL *stop) {
  46. if ([value isEqualToString:RLMPBootedSimulatorKey]) {
  47. bootedDeviceUUID = key;
  48. // Stop when we found single booted device
  49. *stop = YES;
  50. }
  51. }];
  52. }
  53. return bootedDeviceUUID;
  54. }
  55. + (NSString *)readDeviceData
  56. {
  57. // Find out Xcode path from mainBundle
  58. NSURL *bundleURL = [[NSBundle mainBundle] infoDictionary][@"CFBundleInfoPlistURL"];
  59. // Append with xcrun path
  60. NSString *pathToXcrun = @"Contents/Developer/usr/bin/xcrun";
  61. NSURL *fullURL = [[NSURL alloc] initWithString:pathToXcrun relativeToURL:bundleURL.baseURL];
  62. // Set parameters to get device detail
  63. NSArray *args = @[@"simctl", @"list", @"devices"];
  64. NSString *output;
  65. RLMPLaunchedTaskSynchonouslyWithProperty(fullURL.path, args, &output);
  66. return output;
  67. }
  68. + (NSDictionary *)processDeviceData:(NSString *)data
  69. {
  70. NSMutableDictionary *device = [NSMutableDictionary dictionary];
  71. NSScanner *scanner = [NSScanner scannerWithString:data];
  72. // Skip punctuation ( ) as we only want status inside
  73. scanner.charactersToBeSkipped = [NSCharacterSet punctuationCharacterSet];
  74. while (![scanner isAtEnd]) {
  75. NSString *deviceKey;
  76. NSString *deviceStatus;
  77. // Scan up to (
  78. [scanner scanUpToString:@"(" intoString:nil];
  79. [scanner scanUpToString:@")" intoString:&deviceKey];
  80. // Scan up to (
  81. [scanner scanUpToString:@"(" intoString:nil];
  82. [scanner scanUpToString:@")" intoString:&deviceStatus];
  83. [scanner scanUpToCharactersFromSet:[NSCharacterSet newlineCharacterSet] intoString:nil];
  84. if (deviceKey && deviceStatus) {
  85. [device setValue:deviceStatus forKey:deviceKey];
  86. }
  87. }
  88. return device;
  89. }
  90. @end