-
Notifications
You must be signed in to change notification settings - Fork 432
/
FBIDBCommandExecutor.m
826 lines (731 loc) · 31 KB
/
FBIDBCommandExecutor.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import "FBIDBCommandExecutor.h"
#import <FBSimulatorControl/FBSimulatorControl.h>
#import <FBDeviceControl/FBDeviceControl.h>
#import "FBIDBStorageManager.h"
#import "FBIDBError.h"
#import "FBIDBLogger.h"
#import "FBIDBPortsConfiguration.h"
#import "FBStorageUtils.h"
#import "FBTemporaryDirectory.h"
FBFileContainerKind const FBFileContainerKindCrashes = @"crashes";
FBFileContainerKind const FBFileContainerKindMedia = @"media";
FBFileContainerKind const FBFileContainerKindRoot = @"root";
FBFileContainerKind const FBFileContainerKindProvisioningProfiles = @"provisioning_profiles";
FBFileContainerKind const FBFileContainerKindMDMProfiles = @"mdm_profiles";
FBFileContainerKind const FBFileContainerKindSpringboardIcons = @"springboard_icons";
FBFileContainerKind const FBFileContainerKindWallpaper = @"wallpaper";
FBFileContainerKind const FBFileContainerKindDiskImages = @"disk_images";
@interface FBIDBCommandExecutor ()
@property (nonatomic, strong, readonly) id<FBiOSTarget> target;
@property (nonatomic, strong, readonly) FBIDBLogger *logger;
@property (nonatomic, strong, readonly) FBIDBPortsConfiguration *ports;
@end
@implementation FBIDBCommandExecutor
#pragma mark Initializers
+ (instancetype)commandExecutorForTarget:(id<FBiOSTarget>)target storageManager:(FBIDBStorageManager *)storageManager temporaryDirectory:(FBTemporaryDirectory *)temporaryDirectory ports:(FBIDBPortsConfiguration *)ports logger:(FBIDBLogger *)logger
{
return [[self alloc] initWithTarget:target storageManager:storageManager temporaryDirectory:temporaryDirectory ports:ports logger:[logger withName:@"grpc_handler"]];
}
- (instancetype)initWithTarget:(id<FBiOSTarget>)target storageManager:(FBIDBStorageManager *)storageManager temporaryDirectory:(FBTemporaryDirectory *)temporaryDirectory ports:(FBIDBPortsConfiguration *)ports logger:(FBIDBLogger *)logger
{
self = [super init];
if (!self) {
return nil;
}
_target = target;
_storageManager = storageManager;
_temporaryDirectory = temporaryDirectory;
_ports = ports;
_logger = logger;
return self;
}
#pragma mark Installation
- (FBFuture<NSDictionary<FBInstalledApplication *, id> *> *)list_apps:(BOOL)fetchProcessState
{
return [[FBFuture
futureWithFutures:@[
[self.target installedApplications],
fetchProcessState ? [self.target runningApplications] : [FBFuture futureWithResult:@{}],
]]
onQueue:self.target.workQueue map:^(NSArray<id> *results) {
NSArray<FBInstalledApplication *> *installed = results[0];
NSDictionary<NSString *, NSNumber *> *running = results[1];
NSMutableDictionary<FBInstalledApplication *, id> *listing = NSMutableDictionary.dictionary;
for (FBInstalledApplication *application in installed) {
listing[application] = running[application.bundle.identifier] ?: NSNull.null;
}
return listing;
}];
}
- (FBFuture<FBInstalledArtifact *> *)install_app_file_path:(NSString *)filePath
{
// Use .app directly, or extract an .ipa
if ([FBBundleDescriptor isApplicationAtPath:filePath]) {
return [self installAppBundle:[FBFutureContext futureContextWithFuture:[FBBundleDescriptor extractedApplicationAtPath:filePath]]];
} else {
return [self installExtractedApp:[self.temporaryDirectory withArchiveExtractedFromFile:filePath]];
}
}
- (FBFuture<FBInstalledArtifact *> *)install_app_stream:(FBProcessInput *)input
{
return [self installExtractedApp:[self.temporaryDirectory withArchiveExtractedFromStream:input]];
}
- (FBFuture<FBInstalledArtifact *> *)install_xctest_app_file_path:(NSString *)filePath
{
return [self installXctestFilePath:[FBFutureContext futureContextWithFuture:[FBFuture futureWithResult:[NSURL fileURLWithPath:filePath]]]];
}
- (FBFuture<FBInstalledArtifact *> *)install_xctest_app_stream:(FBProcessInput *)stream
{
return [self installXctest:[self.temporaryDirectory withArchiveExtractedFromStream:stream]];
}
- (FBFuture<FBInstalledArtifact *> *)install_dylib_file_path:(NSString *)filePath
{
return [self installFile:[FBFutureContext futureContextWithFuture:[FBFuture futureWithResult:[NSURL fileURLWithPath:filePath]]] intoStorage:self.storageManager.dylib];
}
- (FBFuture<FBInstalledArtifact *> *)install_dylib_stream:(FBProcessInput *)input name:(NSString *)name
{
return [self installFile:[self.temporaryDirectory withGzipExtractedFromStream:input name:name] intoStorage:self.storageManager.dylib];
}
- (FBFuture<FBInstalledArtifact *> *)install_framework_file_path:(NSString *)filePath
{
return [self installBundle:[FBFutureContext futureContextWithFuture:[FBFuture futureWithResult:[NSURL fileURLWithPath:filePath]]] intoStorage:self.storageManager.framework];
}
- (FBFuture<FBInstalledArtifact *> *)install_framework_stream:(FBProcessInput *)input
{
return [self installBundle:[self.temporaryDirectory withArchiveExtractedFromStream:input] intoStorage:self.storageManager.framework];
}
- (FBFuture<FBInstalledArtifact *> *)install_dsym_file_path:(NSString *)filePath
{
return [self installFile:[FBFutureContext futureContextWithFuture:[FBFuture futureWithResult:[NSURL fileURLWithPath:filePath]]] intoStorage:self.storageManager.dsym];
}
- (FBFuture<FBInstalledArtifact *> *)install_dsym_stream:(FBProcessInput *)input
{
return [self installFile:[self.temporaryDirectory withArchiveExtractedFromStream:input] intoStorage:self.storageManager.dsym];
}
#pragma mark Public Methods
- (FBFuture<NSData *> *)take_screenshot:(FBScreenshotFormat)format
{
return [[self
screenshotCommands]
onQueue:self.target.workQueue fmap:^(id<FBScreenshotCommands> commands) {
return [commands takeScreenshot:format];
}];
}
- (FBFuture<NSArray<NSDictionary<NSString *, id> *> *> *)accessibility_info_at_point:(nullable NSValue *)value nestedFormat:(BOOL)nestedFormat
{
return [[self
accessibilityCommands]
onQueue:self.target.workQueue fmap:^ FBFuture * (id<FBAccessibilityCommands> commands) {
if (value) {
return [commands accessibilityElementAtPoint:value.pointValue nestedFormat:nestedFormat];
} else {
return [commands accessibilityElementsWithNestedFormat:nestedFormat];
}
}];
}
- (FBFuture<NSNull *> *)add_media:(NSArray<NSURL *> *)filePaths
{
return [self.mediaCommands
onQueue:self.target.asyncQueue fmap:^FBFuture *(id<FBSimulatorMediaCommands> commands) {
return [commands addMedia:filePaths];
}];
}
- (FBFuture<NSNull *> *)set_location:(double)latitude longitude:(double)longitude
{
id<FBLocationCommands> commands = (id<FBLocationCommands>) self.target;
if (![commands conformsToProtocol:@protocol(FBLocationCommands)]) {
return [[FBIDBError
describeFormat:@"%@ does not conform to FBLocationCommands", commands]
failFuture];
}
return [commands overrideLocationWithLongitude:longitude latitude:latitude];
}
- (FBFuture<NSNull *> *)clear_keychain
{
return [self.keychainCommands
onQueue:self.target.workQueue fmap:^FBFuture *(id<FBSimulatorKeychainCommands> commands) {
return [commands clearKeychain];
}];
}
- (FBFuture<NSNull *> *)approve:(NSSet<FBSettingsApprovalService> *)services for_application:(NSString *)bundleID
{
return [self.settingsCommands
onQueue:self.target.workQueue fmap:^FBFuture *(id<FBSimulatorSettingsCommands> commands) {
return [commands grantAccess:[NSSet setWithObject:bundleID] toServices:services];
}];
}
- (FBFuture<NSNull *> *)approve_deeplink:(NSString *)scheme for_application:(NSString *)bundleID
{
return [self.settingsCommands
onQueue:self.target.workQueue fmap:^FBFuture *(id<FBSimulatorSettingsCommands> commands) {
return [commands grantAccess:[NSSet setWithObject:bundleID] toDeeplink:scheme];
}];
}
- (FBFuture<NSNull *> *)open_url:(NSString *)url
{
return [self.lifecycleCommands
onQueue:self.target.workQueue fmap:^FBFuture *(id<FBSimulatorLifecycleCommands> commands) {
return [commands openURL:[NSURL URLWithString:url]];
}];
}
- (FBFuture<NSNull *> *)remove_all_storage_and_clear_keychain
{
NSError *error = nil;
if (![self.storageManager clean:&error]) {
return [FBFuture futureWithError:error];
}
return [self clear_keychain];
}
- (FBFuture<NSNull *> *)uninstall_all_applications
{
return [[self list_apps:NO] onQueue:self.target.workQueue fmap:^FBFuture<NSNull *> *(NSDictionary<FBInstalledApplication *,id> *apps) {
NSMutableArray<FBFuture<NSNull *> *> *uninstall_futures = NSMutableArray.array;
for (FBInstalledApplication *app in apps) {
if (app.installType == FBApplicationInstallTypeUser){
[uninstall_futures addObject:[[self kill_application:app.bundle.identifier] onQueue:self.target.workQueue fmap:^FBFuture<NSNull *> *(id _) {
return [self uninstall_application:app.bundle.identifier];
}]];
}
}
return [FBFuture futureWithFutures:uninstall_futures];
}];
}
- (FBFuture<NSNull *> *)clean
{
if (self.target.state == FBiOSTargetStateShutdown) {
return [self remove_all_storage_and_clear_keychain];
}
return [[self uninstall_all_applications] onQueue:self.target.workQueue fmap:^FBFuture<NSNull *> *(id _) {
return [self remove_all_storage_and_clear_keychain];
}];
}
- (FBFuture<NSNull *> *)focus
{
return [self.lifecycleCommands
onQueue:self.target.workQueue fmap:^FBFuture *(id<FBSimulatorLifecycleCommands> commands) {
return [commands focus];
}];
}
- (FBFuture<NSNull *> *)update_contacts:(NSData *)dbTarData
{
return [[self.temporaryDirectory
withArchiveExtracted:dbTarData]
onQueue:self.target.workQueue pop:^(NSURL *tempDirectory) {
return [self.settingsCommands onQueue:self.target.workQueue fmap:^FBFuture *(id<FBSimulatorSettingsCommands> commands) {
return [commands updateContacts:tempDirectory.path];
}];
}];
}
- (FBFuture<NSSet<id<FBXCTestDescriptor>> *> *)list_test_bundles
{
return [FBFuture onQueue:self.target.workQueue resolve:^{
NSError *error;
NSSet<id<FBXCTestDescriptor>> *testDescriptors = [self.storageManager.xctest listTestDescriptorsWithError:&error];
if (testDescriptors == nil) {
return [FBFuture futureWithError:error];
}
return [FBFuture futureWithResult:testDescriptors];
}];
}
static const NSTimeInterval ListTestBundleTimeout = 60.0;
- (FBFuture<NSArray<NSString *> *> *)list_tests_in_bundle:(NSString *)bundleID with_app:(NSString *)appPath
{
if ([appPath isEqualToString:@""]) {
appPath = nil;
}
if ([self.storageManager.application.persistedBundleIDs containsObject:appPath]) {
// appPath is actually an app bundle ID
appPath = self.storageManager.application.persistedBundles[appPath].path;
}
return [FBFuture onQueue:self.target.workQueue resolve:^ FBFuture<NSArray<NSString *> *> * {
NSError *error = nil;
id<FBXCTestDescriptor> testDescriptor = [self.storageManager.xctest testDescriptorWithID:bundleID error:&error];
if (!testDescriptor) {
return [FBFuture futureWithError:error];
}
return [self.target listTestsForBundleAtPath:testDescriptor.url.path timeout:ListTestBundleTimeout withAppAtPath:appPath];
}];
}
- (FBFuture<NSNull *> *)uninstall_application:(NSString *)bundleID
{
return [self.target uninstallApplicationWithBundleID:bundleID];
}
- (FBFuture<NSNull *> *)kill_application:(NSString *)bundleID
{
return [self.target killApplicationWithBundleID:bundleID];
}
- (FBFuture<id<FBLaunchedApplication>> *)launch_app:(FBApplicationLaunchConfiguration *)configuration
{
NSMutableDictionary<NSString *, NSString *> *replacements = NSMutableDictionary.dictionary;
[replacements addEntriesFromDictionary:self.storageManager.replacementMapping];
[replacements addEntriesFromDictionary:self.target.replacementMapping];
NSDictionary<NSString *, NSString *> *environment = [self applyEnvironmentReplacements:configuration.environment replacements:replacements];
FBApplicationLaunchConfiguration *derived = [[FBApplicationLaunchConfiguration alloc]
initWithBundleID:configuration.bundleID
bundleName:configuration.bundleName
arguments:configuration.arguments
environment:environment
waitForDebugger:configuration.waitForDebugger
output:configuration.output
launchMode:configuration.launchMode];
return [self.target launchApplication:derived];
}
- (NSDictionary<NSString *, NSString *> *)applyEnvironmentReplacements:(NSDictionary<NSString *, NSString *> *)environment replacements:(NSDictionary<NSString *, NSString *> *)replacements
{
[self.logger logFormat:@"Original environment: %@", environment];
[self.logger logFormat:@"Existing replacement mapping: %@", replacements];
NSMutableDictionary<NSString *, NSString *> *interpolatedEnvironment = [NSMutableDictionary dictionaryWithCapacity:environment.count];
for (NSString *name in environment.allKeys) {
NSString *value = environment[name];
for (NSString *interpolationName in replacements.allKeys) {
NSString *interpolationValue = replacements[interpolationName];
value = [value stringByReplacingOccurrencesOfString:interpolationName withString:interpolationValue];
}
interpolatedEnvironment[name] = value;
}
[self.logger logFormat:@"Interpolated environment: %@", interpolatedEnvironment];
return interpolatedEnvironment;
}
- (FBFuture<FBIDBTestOperation *> *)xctest_run:(FBXCTestRunRequest *)request reporter:(id<FBXCTestReporter, FBXCTestReporterWithFiles>)reporter logger:(id<FBControlCoreLogger>)logger
{
return [request startWithBundleStorageManager:self.storageManager.xctest target:self.target reporter:reporter logger:logger temporaryDirectory:self.temporaryDirectory];
}
- (FBFuture<id<FBDebugServer>> *)debugserver_start:(NSString *)bundleID
{
return [[self
debugserver_prepare:bundleID]
onQueue:self.target.workQueue fmap:^(FBBundleDescriptor *application) {
return [self.target launchDebugServerForHostApplication:application port:self.ports.debugserverPort];
}];
}
- (FBFuture<id<FBDebugServer>> *)debugserver_status
{
return [FBFuture
onQueue:self.target.workQueue resolve:^{
id<FBDebugServer> debugServer = self.debugServer;
if (!debugServer) {
return [[FBControlCoreError
describe:@"No debug server running"]
failFuture];
}
return [FBFuture futureWithResult:debugServer];
}];
}
- (FBFuture<id<FBDebugServer>> *)debugserver_stop
{
return [[[self
debugserver_status]
onQueue:self.target.workQueue fmap:^(id<FBDebugServer> debugServer) {
return [[self.debugServer.completed cancel] mapReplace:debugServer];
}]
onQueue:self.target.workQueue doOnResolved:^(id _) {
self.debugServer = nil;
}];
}
- (FBFuture<NSArray<FBCrashLogInfo *> *> *)crash_list:(NSPredicate *)predicate
{
return [[self.target
crashes:predicate useCache:NO]
onQueue:self.target.asyncQueue map:^(NSArray<FBCrashLogInfo *> *crashes) {
return crashes;
}];
}
- (FBFuture<FBCrashLog *> *)crash_show:(NSPredicate *)predicate
{
return [[self.target
crashes:predicate useCache:YES]
onQueue:self.target.asyncQueue fmap:^(NSArray<FBCrashLogInfo *> *crashes) {
if (crashes.count > 1) {
return [[FBIDBError
describeFormat:@"More than one crash log matching %@", predicate]
failFuture];
}
if (crashes.count == 0) {
return [[FBIDBError
describeFormat:@"No crashes matching %@", predicate]
failFuture];
}
NSError *error = nil;
FBCrashLog *log = [crashes.firstObject obtainCrashLogWithError:&error];
if (!log) {
return [FBFuture futureWithError:error];
}
return [FBFuture futureWithResult:log];
}];
}
- (FBFuture<NSArray<FBCrashLogInfo *> *> *)crash_delete:(NSPredicate *)predicate
{
return [[self.target
pruneCrashes:predicate]
onQueue:self.target.asyncQueue map:^(NSArray<FBCrashLogInfo *> *crashes) {
return crashes;
}];
}
- (FBFuture<FBBundleDescriptor *> *)debugserver_prepare:(NSString *)bundleID
{
return [FBFuture
onQueue:self.target.workQueue resolve:^ FBFuture<FBBundleDescriptor *> * {
if (self.debugServer) {
return [[FBControlCoreError
describeFormat:@"Debug server is already running"]
failFuture];
}
NSDictionary<NSString *, FBBundleDescriptor *> *persisted = self.storageManager.application.persistedBundles;
FBBundleDescriptor *bundle = persisted[bundleID];
if (!bundle) {
return [[FBIDBError
describeFormat:@"%@ not persisted application and is therefore not debuggable. Suitable applications: %@", bundleID, [FBCollectionInformation oneLineDescriptionFromArray:persisted.allKeys]]
failFuture];
}
return [FBFuture futureWithResult:bundle];
}];
}
- (FBFuture<id<FBLogOperation>> *)tail_companion_logs:(id<FBDataConsumer>)consumer
{
return [self.logger tailToConsumer:consumer];
}
- (FBFuture<NSDictionary<NSString *, id> *> *)diagnostic_information
{
id<FBDiagnosticInformationCommands> commands = (id<FBDiagnosticInformationCommands>) self.target;
if (![commands conformsToProtocol:@protocol(FBDiagnosticInformationCommands)]) {
// Don't fail, just return empty.
return [FBFuture futureWithResult:@{}];
}
return [commands fetchDiagnosticInformation];
}
- (FBFuture<NSNull *> *)hid:(FBSimulatorHIDEvent *)event
{
return [self.connectToHID
onQueue:self.target.workQueue fmap:^FBFuture *(FBSimulatorHID *hid) {
return [event performOnHID:hid];
}];
}
- (FBFuture<NSNull *> *)set_hardware_keyboard_enabled:(BOOL)enabled
{
return [[self
settingsCommands]
onQueue:self.target.workQueue fmap:^(id<FBSimulatorSettingsCommands> commands) {
return [commands setHardwareKeyboardEnabled:enabled];
}];
}
- (FBFuture<NSNull *> *)set_locale_with_identifier:(NSString *)identifier
{
return [[self
settingsCommands]
onQueue:self.target.workQueue fmap:^(id<FBSimulatorSettingsCommands> commands) {
return [commands setLocaleWithIdentifier:identifier];
}];
}
- (FBFuture<NSString *> *)get_current_locale_identifier
{
return [[self
settingsCommands]
onQueue:self.target.workQueue fmap:^(id<FBSimulatorSettingsCommands> commands) {
return [commands getCurrentLocaleIdentifier];
}];
}
- (NSArray<NSString *> *)list_locale_identifiers
{
return NSLocale.availableLocaleIdentifiers;
}
#pragma mark File Commands
- (FBFuture<NSNull *> *)move_paths:(NSArray<NSString *> *)originPaths to_path:(NSString *)destinationPath containerType:(NSString *)containerType
{
return [[self
applicationDataContainerCommands:containerType]
onQueue:self.target.workQueue pop:^(id<FBFileContainer> container) {
NSMutableArray<FBFuture<NSNull *> *> *futures = NSMutableArray.array;
for (NSString *originPath in originPaths) {
[futures addObject:[container movePath:originPath toDestinationPath:destinationPath]];
}
return [[FBFuture futureWithFutures:futures] mapReplace:NSNull.null];
}];
}
- (FBFuture<NSNull *> *)push_file_from_tar:(NSData *)tarData to_path:(NSString *)destinationPath containerType:(NSString *)containerType
{
return [[self.temporaryDirectory
withArchiveExtracted:tarData]
onQueue:self.target.workQueue pop:^FBFuture *(NSURL *extractionDirectory) {
NSError *error;
NSArray<NSURL *> *paths = [NSFileManager.defaultManager contentsOfDirectoryAtURL:extractionDirectory includingPropertiesForKeys:@[NSURLIsDirectoryKey] options:0 error:&error];
if (!paths) {
return [FBFuture futureWithError:error];
}
return [self push_files:paths to_path:destinationPath containerType:containerType];
}];
}
- (FBFuture<NSNull *> *)push_files:(NSArray<NSURL *> *)paths to_path:(NSString *)destinationPath containerType:(NSString *)containerType
{
return [FBFuture
onQueue:self.target.asyncQueue resolve:^FBFuture<NSNull *> *{
return [[self
applicationDataContainerCommands:containerType]
onQueue:self.target.workQueue pop:^FBFuture *(id<FBFileContainer> container) {
NSMutableArray<FBFuture<NSNull *> *> *futures = NSMutableArray.array;
for (NSURL *originPath in paths) {
[futures addObject:[container copyPathOnHost:originPath toDestination:destinationPath]];
}
return [[FBFuture futureWithFutures:futures] mapReplace:NSNull.null];
}];
}];
}
- (FBFuture<NSString *> *)pull_file_path:(NSString *)path destination_path:(NSString *)destinationPath containerType:(NSString *)containerType
{
return [[self
applicationDataContainerCommands:containerType]
onQueue:self.target.workQueue pop:^FBFuture *(id<FBFileContainer> commands) {
return [commands copyItemInContainer:path toDestinationOnHost:destinationPath];
}];
}
- (FBFuture<NSData *> *)pull_file:(NSString *)path containerType:(NSString *)containerType
{
__block NSString *tempPath;
return [[[self.temporaryDirectory
withTemporaryDirectory]
onQueue:self.target.workQueue pend:^(NSURL *url) {
tempPath = [url.path stringByAppendingPathComponent:path.lastPathComponent];
return [[self
applicationDataContainerCommands:containerType]
onQueue:self.target.workQueue pop:^(id<FBFileContainer> container) {
return [container copyItemInContainer:path toDestinationOnHost:tempPath];
}];
}]
onQueue:self.target.workQueue pop:^(id _) {
return [FBArchiveOperations createGzippedTarDataForPath:tempPath queue:self.target.workQueue logger:self.target.logger];
}];
}
- (FBFuture<NSNull *> *)create_directory:(NSString *)directoryPath containerType:(NSString *)containerType
{
return [[self
applicationDataContainerCommands:containerType]
onQueue:self.target.workQueue pop:^(id<FBFileContainer> targetApplicationData) {
return [targetApplicationData createDirectory:directoryPath];
}];
}
- (FBFuture<NSNull *> *)remove_paths:(NSArray<NSString *> *)paths containerType:(NSString *)containerType
{
return [[self
applicationDataContainerCommands:containerType]
onQueue:self.target.workQueue pop:^FBFuture *(id<FBFileContainer> container) {
NSMutableArray<FBFuture<NSNull *> *> *futures = NSMutableArray.array;
for (NSString *path in paths) {
[futures addObject:[container removePath:path]];
}
return [[FBFuture futureWithFutures:futures] mapReplace:NSNull.null];
}];
}
- (FBFuture<NSArray<NSString *> *> *)list_path:(NSString *)path containerType:(NSString *)containerType
{
return [[self
applicationDataContainerCommands:containerType]
onQueue:self.target.workQueue pop:^FBFuture *(id<FBFileContainer> container) {
return [container contentsOfDirectory:path];
}];
}
- (FBFuture<NSDictionary<NSString *, NSArray<NSString *> *> *> *)list_paths:(NSArray<NSString *> *)paths containerType:(NSString *)containerType
{
return [[[self
applicationDataContainerCommands:containerType]
onQueue:self.target.workQueue pop:^FBFuture *(id<FBFileContainer> container) {
NSMutableArray<FBFuture<NSArray<NSString *> *> *> *futures = NSMutableArray.array;
for (NSString *path in paths) {
[futures addObject:[container contentsOfDirectory:path]];
}
return [FBFuture futureWithFutures:futures];
}]
onQueue:self.target.asyncQueue map:^ (NSArray<NSArray<NSString *> *> *listings) {
// Dictionary is constructed by attaching paths for ordering within array.
return [NSDictionary dictionaryWithObjects:listings forKeys:paths];
}];
}
#pragma mark Private Methods
- (FBFutureContext<id<FBFileContainer>> *)applicationDataContainerCommands:(NSString *)containerType
{
if ([containerType isEqualToString:FBFileContainerKindCrashes]) {
return [self.target crashLogFiles];
}
id<FBFileCommands> commands = (id<FBFileCommands>) self.target;
if (![commands conformsToProtocol:@protocol(FBFileCommands)]) {
return [[FBControlCoreError
describeFormat:@"Target doesn't conform to FBFileCommands protocol %@", commands]
failFutureContext];
}
if ([containerType isEqualToString:FBFileContainerKindMedia]) {
return [commands fileCommandsForMediaDirectory];
}
if ([containerType isEqualToString:FBFileContainerKindRoot]) {
return [commands fileCommandsForRootFilesystem];
}
if ([containerType isEqualToString:FBFileContainerKindProvisioningProfiles]) {
return [commands fileCommandsForProvisioningProfiles];
}
if ([containerType isEqualToString:FBFileContainerKindMDMProfiles]) {
return [commands fileCommandsForMDMProfiles];
}
if ([containerType isEqualToString:FBFileContainerKindSpringboardIcons]) {
return [commands fileCommandsForSpringboardIconLayout];
}
if ([containerType isEqualToString:FBFileContainerKindWallpaper]) {
return [commands fileCommandsForWallpaper];
}
if ([containerType isEqualToString:FBFileContainerKindDiskImages]) {
return [commands fileCommandsForDiskImages];
}
if (containerType == nil || containerType.length == 0) {
// The Default for no, or null container for back-compat.
return [self.target isKindOfClass:FBDevice.class] ? [commands fileCommandsForMediaDirectory] : [commands fileCommandsForRootFilesystem];
}
return [commands fileCommandsForContainerApplication:containerType];
}
- (FBFuture<id<FBScreenshotCommands>> *)screenshotCommands
{
id<FBScreenshotCommands> commands = (id<FBScreenshotCommands>) self.target;
if (![commands conformsToProtocol:@protocol(FBScreenshotCommands)]) {
return [[FBIDBError
describeFormat:@"Target doesn't conform to FBScreenshotCommands protocol %@", self.target]
failFuture];
}
return [FBFuture futureWithResult:commands];
}
- (FBFuture<id<FBSimulatorLifecycleCommands>> *)lifecycleCommands
{
id<FBSimulatorLifecycleCommands> commands = (id<FBSimulatorLifecycleCommands>) self.target;
if (![commands conformsToProtocol:@protocol(FBSimulatorLifecycleCommands)]) {
return [[FBIDBError
describeFormat:@"Target doesn't conform to FBSimulatorLifecycleCommands protocol %@", self.target]
failFuture];
}
return [FBFuture futureWithResult:commands];
}
- (FBFuture<id<FBSimulatorMediaCommands>> *)mediaCommands
{
id<FBSimulatorMediaCommands> commands = (id<FBSimulatorMediaCommands>) self.target;
if (![commands conformsToProtocol:@protocol(FBSimulatorMediaCommands)]) {
return [[FBIDBError
describeFormat:@"Target doesn't conform to FBSimulatorMediaCommands protocol %@", self.target]
failFuture];
}
return [FBFuture futureWithResult:commands];
}
- (FBFuture<id<FBSimulatorKeychainCommands>> *)keychainCommands
{
id<FBSimulatorKeychainCommands> commands = (id<FBSimulatorKeychainCommands>) self.target;
if (![commands conformsToProtocol:@protocol(FBSimulatorKeychainCommands)]) {
return [[FBIDBError
describeFormat:@"Target doesn't conform to FBSimulatorKeychainCommands protocol %@", self.target]
failFuture];
}
return [FBFuture futureWithResult:commands];
}
- (FBFuture<id<FBSimulatorSettingsCommands>> *)settingsCommands
{
id<FBSimulatorSettingsCommands> commands = (id<FBSimulatorSettingsCommands>) self.target;
if (![commands conformsToProtocol:@protocol(FBSimulatorSettingsCommands)]) {
return [[FBIDBError
describeFormat:@"Target doesn't conform to FBSimulatorSettingsCommands protocol %@", self.target]
failFuture];
}
return [FBFuture futureWithResult:commands];
}
- (FBFuture<id<FBAccessibilityCommands>> *)accessibilityCommands
{
id<FBAccessibilityCommands> commands = (id<FBAccessibilityCommands>) self.target;
if (![commands conformsToProtocol:@protocol(FBAccessibilityCommands)]) {
return [[FBIDBError
describeFormat:@"Target doesn't conform to FBAccessibilityCommands protocol %@", self.target]
failFuture];
}
return [FBFuture futureWithResult:commands];
}
- (FBFuture<FBSimulatorConnection *> *)connectToSimulatorConnection
{
return [[self
lifecycleCommands]
onQueue:self.target.workQueue fmap:^ FBFuture<FBSimulatorConnection *> * (id<FBSimulatorLifecycleCommands> commands) {
NSError *error = nil;
if (![FBSimulatorControlFrameworkLoader.xcodeFrameworks loadPrivateFrameworks:self.target.logger error:&error]) {
return [[FBIDBError
describeFormat:@"SimulatorKit is required for HID interactions: %@", error]
failFuture];
}
return [commands connect];
}];
}
- (FBFuture<FBSimulatorHID *> *)connectToHID
{
return [[self
connectToSimulatorConnection]
onQueue:self.target.workQueue fmap:^(FBSimulatorConnection *connection) {
return [connection connectToHID];
}];
}
- (FBFuture<FBInstalledArtifact *> *)installExtractedApp:(FBFutureContext<NSURL *> *)extractedAppContext
{
FBFutureContext<FBBundleDescriptor *> *bundleContext = [extractedAppContext onQueue:self.target.asyncQueue pend:^(NSURL *extractPath) {
return [FBBundleDescriptor findAppPathFromDirectory:extractPath];
}];
return [self installAppBundle:bundleContext];
}
- (FBFuture<FBInstalledArtifact *> *)installAppBundle:(FBFutureContext<FBBundleDescriptor *> *)bundleContext
{
return [[bundleContext
onQueue:self.target.workQueue pend:^(FBBundleDescriptor *appBundle){
if (!appBundle) {
return [FBFuture futureWithError:[FBControlCoreError errorForDescription:@"No app bundle could be extracted"]];
}
NSError *error = nil;
if (![self.storageManager.application checkArchitecture:appBundle error:&error]) {
return [FBFuture futureWithError:error];
}
return [[self.target installApplicationWithPath:appBundle.path] mapReplace:appBundle];
}]
onQueue:self.target.workQueue pop:^(FBBundleDescriptor *appBundle){
[self.logger logFormat:@"Persisting application bundle %@", appBundle];
return [self.storageManager.application saveBundle:appBundle];
}];
}
- (FBFuture<FBInstalledArtifact *> *)installXctest:(FBFutureContext<NSURL *> *)extractedXctest
{
return [extractedXctest
onQueue:self.target.workQueue pop:^(NSURL *extractionDirectory) {
return [self.storageManager.xctest saveBundleOrTestRunFromBaseDirectory:extractionDirectory];
}];
}
- (FBFuture<FBInstalledArtifact *> *)installXctestFilePath:(FBFutureContext<NSURL *> *)bundle
{
return [bundle
onQueue:self.target.workQueue pop:^(NSURL *xctestURL) {
return [self.storageManager.xctest saveBundleOrTestRun:xctestURL];
}];
}
- (FBFuture<FBInstalledArtifact *> *)installFile:(FBFutureContext<NSURL *> *)extractedFileContext intoStorage:(FBFileStorage *)storage
{
return [extractedFileContext
onQueue:self.target.workQueue pop:^(NSURL *extractedFile) {
NSError *error = nil;
FBInstalledArtifact *artifact = [storage saveFile:extractedFile error:&error];
if (!artifact) {
return [FBFuture futureWithError:error];
}
return [FBFuture futureWithResult:artifact];
}];
}
- (FBFuture<FBInstalledArtifact *> *)installBundle:(FBFutureContext<NSURL *> *)extractedDirectoryContext intoStorage:(FBBundleStorage *)storage
{
return [extractedDirectoryContext
onQueue:self.target.workQueue pop:^ FBFuture<FBInstalledArtifact *> * (NSURL *extractedDirectory) {
NSError *error = nil;
FBBundleDescriptor *bundle = [FBStorageUtils bundleInDirectory:extractedDirectory error:&error];
if (!bundle) {
return [FBFuture futureWithError:error];
}
return [storage saveBundle:bundle];
}];
}
@end