@@ -26,45 +26,72 @@ - (void)startInstallationIntoPluginsDirectory:(NSString *)directory withCallback
26
26
if (self.plugin .zipURL ) {
27
27
[[[NSURLSession sharedSession ] dataTaskWithURL: self .plugin.zipURL completionHandler: ^(NSData *data, NSURLResponse *response, NSError *error) {
28
28
if (data && !error) {
29
- dispatch_async (dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0 ), ^{
30
- NSError *zipError = nil ;
31
- ZZArchive *archive = [ZZArchive archiveWithData: data error: &zipError];
32
- if (archive && !zipError) {
33
- for (ZZArchiveEntry *entry in archive.entries ) {
34
- zipError = nil ;
35
- NSData *entryData = [entry newDataWithError: &zipError];
36
- if (entryData && !zipError) {
37
- NSString *writeToPath = [directory stringByAppendingPathComponent: entry.fileName];
38
- if (![[NSFileManager defaultManager ] fileExistsAtPath: [writeToPath stringByDeletingLastPathComponent ]]) {
39
- [[NSFileManager defaultManager ] createDirectoryAtPath: [writeToPath stringByDeletingLastPathComponent ] withIntermediateDirectories: YES attributes: nil error: NO ];
40
- }
41
- if ([[writeToPath pathExtension ] isEqualToString: @" bundle" ]) {
42
- continue ;
43
- }
44
- [entryData writeToFile: writeToPath atomically: YES ];
45
- } else {
46
- callback (NO , zipError);
47
- return ;
48
- }
49
- }
50
- // done:
51
- if (self.plugin .openPreferencesOnInstall ) {
52
- dispatch_async (dispatch_get_main_queue (), ^{
53
- AppDelegate *d = (id )[NSApp delegate ];
54
- [self .plugin presentOptionsInWindow: d.window];
55
- });
56
- }
57
- [[UpdateChecker shared ] justInstalledPlugin: self .plugin.name];
58
- callback (YES , nil );
59
- } else {
60
- callback (NO , zipError);
61
- }
62
- });
29
+ [self installPluginData: data intoPluginsDirectory: directory callback: callback];
63
30
} else {
64
31
callback (NO , error);
65
32
}
66
33
}] resume ];
67
34
}
68
35
}
69
36
37
+ - (NSString *)nameForPluginContainingPath : (NSString *)path {
38
+ for (NSString *comp in path.pathComponents .reverseObjectEnumerator ) {
39
+ if ([comp.pathExtension isEqualToString: @" bundle" ]) {
40
+ return comp.stringByDeletingPathExtension ;
41
+ }
42
+ }
43
+ return nil ;
44
+ }
45
+
46
+ - (void )installPluginData : (NSData *)data intoPluginsDirectory : (NSString *)directory callback : (void (^)(BOOL success, NSError *error))callback {
47
+ if (!data) {
48
+ callback (NO , nil );
49
+ return ;
50
+ }
51
+ dispatch_async (dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0 ), ^{
52
+ NSString *pluginName = nil ;
53
+ NSError *zipError = nil ;
54
+ ZZArchive *archive = [ZZArchive archiveWithData: data error: &zipError];
55
+ if (archive && !zipError) {
56
+ for (ZZArchiveEntry *entry in archive.entries ) {
57
+ zipError = nil ;
58
+ NSData *entryData = [entry newDataWithError: &zipError];
59
+ if (entryData && !zipError) {
60
+ NSString *writeToPath = [directory stringByAppendingPathComponent: entry.fileName];
61
+ if (![[NSFileManager defaultManager ] fileExistsAtPath: [writeToPath stringByDeletingLastPathComponent ]]) {
62
+ [[NSFileManager defaultManager ] createDirectoryAtPath: [writeToPath stringByDeletingLastPathComponent ] withIntermediateDirectories: YES attributes: nil error: NO ];
63
+ }
64
+ if ([[writeToPath pathExtension ] isEqualToString: @" bundle" ]) {
65
+ continue ;
66
+ }
67
+ if (!pluginName && [self nameForPluginContainingPath: writeToPath]) {
68
+ pluginName = [self nameForPluginContainingPath: writeToPath];
69
+ }
70
+ [entryData writeToFile: writeToPath atomically: YES ];
71
+ } else {
72
+ callback (NO , zipError);
73
+ return ;
74
+ }
75
+ }
76
+ if (!pluginName) {
77
+ callback (NO , nil );
78
+ return ;
79
+ }
80
+ _installedPluginName = pluginName;
81
+ PluginModel *pluginModel = [PluginModel installedPluginNamed: pluginName];
82
+ // done:
83
+ if (pluginModel.openPreferencesOnInstall ) {
84
+ dispatch_async (dispatch_get_main_queue (), ^{
85
+ AppDelegate *d = (id )[NSApp delegate ];
86
+ [pluginModel presentOptionsInWindow: d.window];
87
+ });
88
+ }
89
+ [[UpdateChecker shared ] justInstalledPlugin: self .plugin.name];
90
+ callback (YES , nil );
91
+ } else {
92
+ callback (NO , zipError);
93
+ }
94
+ });
95
+ }
96
+
70
97
@end
0 commit comments