FLEXBundleShortcuts.m 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. //
  2. // FLEXBundleShortcuts.m
  3. // FLEX
  4. //
  5. // Created by Tanner Bennett on 12/12/19.
  6. // Copyright © 2019 Flipboard. All rights reserved.
  7. //
  8. #import "FLEXBundleShortcuts.h"
  9. #import "FLEXShortcut.h"
  10. #import "FLEXAlert.h"
  11. #import "FLEXRuntimeExporter.h"
  12. #import "FLEXTableListViewController.h"
  13. #import "FLEXFileBrowserTableViewController.h"
  14. #pragma mark -
  15. @implementation FLEXBundleShortcuts
  16. #pragma mark Overrides
  17. + (instancetype)forObject:(NSBundle *)bundle {
  18. return [self forObject:bundle additionalRows:@[
  19. [FLEXActionShortcut
  20. title:@"Browse Bundle Directory" subtitle:nil
  21. viewer:^UIViewController *(NSBundle *bundle) {
  22. return [FLEXFileBrowserTableViewController path:bundle.bundlePath];
  23. }
  24. accessoryType:^UITableViewCellAccessoryType(NSBundle *bundle) {
  25. return UITableViewCellAccessoryDisclosureIndicator;
  26. }
  27. ],
  28. [FLEXActionShortcut title:@"Browse Bundle as Database…" subtitle:nil
  29. selectionHandler:^(UIViewController *host, NSBundle *bundle) {
  30. [self promptToExportBundleAsDatabase:bundle host:host];
  31. }
  32. accessoryType:^UITableViewCellAccessoryType(NSBundle *bundle) {
  33. return UITableViewCellAccessoryDisclosureIndicator;
  34. }
  35. ],
  36. ]];
  37. }
  38. + (void)promptToExportBundleAsDatabase:(NSBundle *)bundle host:(UIViewController *)host {
  39. [FLEXAlert makeAlert:^(FLEXAlert *make) {
  40. make.title(@"Save As…").message(
  41. @"The database be saved in the Library folder. "
  42. "Depending on the number of classes, it may take "
  43. "10 minutes or more to finish exporting. 20,000 "
  44. "classes takes about 7 minutes."
  45. );
  46. make.configuredTextField(^(UITextField *field) {
  47. field.placeholder = @"FLEXRuntimeExport.objc.db";
  48. field.text = [NSString stringWithFormat:
  49. @"%@.objc.db", bundle.executablePath.lastPathComponent
  50. ];
  51. });
  52. make.button(@"Start").handler(^(NSArray<NSString *> *strings) {
  53. [self browseBundleAsDatabase:bundle host:host name:strings[0]];
  54. });
  55. make.button(@"Cancel").cancelStyle();
  56. } showFrom:host];
  57. }
  58. + (void)browseBundleAsDatabase:(NSBundle *)bundle host:(UIViewController *)host name:(NSString *)name {
  59. NSParameterAssert(name.length);
  60. UIAlertController *progress = [FLEXAlert makeAlert:^(FLEXAlert *make) {
  61. make.title(@"Generating Database");
  62. // Some iOS version glitch out of there is
  63. // no initial message and you add one later
  64. make.message(@"…");
  65. }];
  66. [host presentViewController:progress animated:YES completion:^{
  67. // Generate path to store db
  68. NSString *path = [NSSearchPathForDirectoriesInDomains(
  69. NSLibraryDirectory, NSUserDomainMask, YES
  70. )[0] stringByAppendingPathComponent:name];
  71. progress.message = [path stringByAppendingString:@"\n\nCreating database…"];
  72. // Generate db and show progress
  73. [FLEXRuntimeExporter createRuntimeDatabaseAtPath:path
  74. forImages:@[bundle.executablePath]
  75. progressHandler:^(NSString *status) {
  76. dispatch_async(dispatch_get_main_queue(), ^{
  77. progress.message = [progress.message
  78. stringByAppendingFormat:@"\n%@", status
  79. ];
  80. [progress.view setNeedsLayout];
  81. [progress.view layoutIfNeeded];
  82. });
  83. } completion:^(NSString *error) {
  84. // Display error if any
  85. if (error) {
  86. progress.title = @"Error";
  87. progress.message = error;
  88. [progress addAction:[UIAlertAction
  89. actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:nil]
  90. ];
  91. }
  92. // Browse database
  93. else {
  94. [progress dismissViewControllerAnimated:YES completion:nil];
  95. [host.navigationController pushViewController:[
  96. [FLEXTableListViewController alloc] initWithPath:path
  97. ] animated:YES];
  98. }
  99. }
  100. ];
  101. }];
  102. }
  103. @end