Use closure instead of info dict

This commit is contained in:
Kornel Lesiński committed 2014-07-01 00:27:11 +01:00
1 parent 0e68e5e590
commit bf9ba560f0
2 files changed
+51 -103

No files matched your search

+25 -47
View File
@@ -10,60 +10,38 @@
#import <Cocoa/Cocoa.h>
#import "SUConstants.h"
static NSString * const SUPackageInstallerCommandKey = @"SUPackageInstallerCommand";
static NSString * const SUPackageInstallerArgumentsKey = @"SUPackageInstallerArguments";
static NSString * const SUPackageInstallerHostKey = @"SUPackageInstallerHost";
static NSString * const SUPackageInstallerDelegateKey = @"SUPackageInstallerDelegate";
static NSString * const SUPackageInstallerInstallationPathKey = @"SUPackageInstallerInstallationPathKey";
@implementation SUPackageInstaller
+ (void)finishInstallationWithInfo:(NSDictionary *)info
+ (void)performInstallationToPath:(NSString *)installationPath fromPath:(NSString *)path host:(SUHost *)host delegate:(id<SUInstallerDelegate>)delegate synchronously:(BOOL)synchronously versionComparator:(id<SUVersionComparison>)__unused comparator
{
[self finishInstallationToPath:info[SUPackageInstallerInstallationPathKey] withResult:YES host:info[SUPackageInstallerHostKey] error:nil delegate:info[SUPackageInstallerDelegateKey]];
}
// Run installer using the "open" command to ensure it is launched in front of current application.
// -W = wait until the app has quit.
// -n = Open another instance if already open.
// -b = app bundle identifier
NSString *command = @"/usr/bin/open";
NSArray *args = @[@"-W", @"-n", @"-b", @"com.apple.installer", path];
+ (void)performInstallationWithInfo:(NSDictionary *)info
{
@autoreleasepool {
NSTask *installer = [NSTask launchedTaskWithLaunchPath:info[SUPackageInstallerCommandKey] arguments:info[SUPackageInstallerArgumentsKey]];
[installer waitUntilExit];
if (![[NSFileManager defaultManager] fileExistsAtPath:command]) {
NSError *error = [NSError errorWithDomain:SUSparkleErrorDomain code:SUMissingInstallerToolError userInfo:@{ NSLocalizedDescriptionKey: @"Couldn't find Apple's installer tool!" }];
[self finishInstallationToPath:installationPath withResult:NO host:host error:error delegate:delegate];
return;
}
// Known bug: if the installation fails or is canceled, Sparkle goes ahead and restarts, thinking everything is fine.
dispatch_async(dispatch_get_main_queue(), ^{
[self finishInstallationWithInfo:info];
});
}
}
void (^performInstallation)() = ^void() {
NSTask *installer = [NSTask launchedTaskWithLaunchPath:command arguments:args];
[installer waitUntilExit];
+ (void)performInstallationToPath:(NSString *)installationPath fromPath:(NSString *)path host:(SUHost *)host delegate:(id <SUInstallerDelegate>)delegate synchronously:(BOOL)synchronously versionComparator:(id <SUVersionComparison>) __unused comparator
{
NSString *command;
NSArray *args;
// Known bug: if the installation fails or is canceled, Sparkle goes ahead and restarts, thinking everything is fine.
dispatch_async(dispatch_get_main_queue(), ^ {
[self finishInstallationToPath:installationPath withResult:YES host:host error:nil delegate:delegate];
});
};
// Run installer using the "open" command to ensure it is launched in front of current application.
// -W = wait until the app has quit.
// -n = Open another instance if already open.
// -b = app bundle identifier
command = @"/usr/bin/open";
args = @[@"-W", @"-n", @"-b", @"com.apple.installer", path];
if (![[NSFileManager defaultManager] fileExistsAtPath:command])
{
NSError *error = [NSError errorWithDomain:SUSparkleErrorDomain code:SUMissingInstallerToolError userInfo:@{NSLocalizedDescriptionKey: @"Couldn't find Apple's installer tool!"}];
[self finishInstallationToPath:installationPath withResult:NO host:host error:error delegate:delegate];
}
else
{
NSDictionary *info = @{SUPackageInstallerCommandKey: command, SUPackageInstallerArgumentsKey: args, SUPackageInstallerHostKey: host, SUPackageInstallerDelegateKey: delegate, SUPackageInstallerInstallationPathKey: installationPath};
if (synchronously)
[self performInstallationWithInfo:info];
else {
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[self performInstallationWithInfo:info];
});
}
}
if (synchronously) {
performInstallation();
} else {
dispatch_async(dispatch_get_global_queue(0, 0), performInstallation);
}
}
@end
+26 -56
View File
@@ -11,54 +11,9 @@
#import "SUConstants.h"
#import "SUHost.h"
static NSString * const SUInstallerPathKey = @"SUInstallerPath";
static NSString * const SUInstallerTargetPathKey = @"SUInstallerTargetPath";
static NSString * const SUInstallerTempNameKey = @"SUInstallerTempName";
static NSString * const SUInstallerHostKey = @"SUInstallerHost";
static NSString * const SUInstallerDelegateKey = @"SUInstallerDelegate";
static NSString * const SUInstallerResultKey = @"SUInstallerResult";
static NSString * const SUInstallerErrorKey = @"SUInstallerError";
static NSString * const SUInstallerInstallationPathKey = @"SUInstallerInstallationPath";
@implementation SUPlainInstaller
+ (void)finishInstallationWithInfo:(NSDictionary *)info
{
// *** GETS CALLED ON NON-MAIN THREAD!
[self finishInstallationToPath:info[SUInstallerInstallationPathKey] withResult:[info[SUInstallerResultKey] boolValue] host:info[SUInstallerHostKey] error:info[SUInstallerErrorKey] delegate:info[SUInstallerDelegateKey]];
}
+ (void)performInstallationWithInfo:(NSDictionary *)info
{
// *** GETS CALLED ON NON-MAIN THREAD!
@autoreleasepool {
NSError *error = nil;
NSString * oldPath = [info[SUInstallerHostKey] bundlePath];
NSString * installationPath = info[SUInstallerInstallationPathKey];
BOOL result = [self copyPathWithAuthentication:info[SUInstallerPathKey] overPath: installationPath temporaryName:info[SUInstallerTempNameKey] error:&error];
if( result )
{
BOOL haveOld = [[NSFileManager defaultManager] fileExistsAtPath: oldPath];
BOOL differentFromNew = ![oldPath isEqualToString: installationPath];
if( haveOld && differentFromNew )
[self _movePathToTrash: oldPath]; // On success, trash old copy if there's still one due to renaming.
}
NSMutableDictionary *mutableInfo = [[info mutableCopy] autorelease];
mutableInfo[SUInstallerResultKey] = @(result);
mutableInfo[SUInstallerInstallationPathKey] = installationPath;
if (!result && error)
mutableInfo[SUInstallerErrorKey] = error;
dispatch_async(dispatch_get_main_queue(), ^{
[self finishInstallationWithInfo:mutableInfo];
});
}
}
+ (void)performInstallationToPath:(NSString *)installationPath fromPath:(NSString *)path host:(SUHost *)host delegate:(id<SUInstallerDelegate>)delegate synchronously:(BOOL)synchronously versionComparator:(id <SUVersionComparison>)comparator
+ (void)performInstallationToPath:(NSString *)installationPath fromPath:(NSString *)path host:(SUHost *)host delegate:(id<SUInstallerDelegate>)delegate synchronously:(BOOL)synchronously versionComparator:(id<SUVersionComparison>)comparator
{
// Prevent malicious downgrades:
#if !PERMIT_AUTOMATED_DOWNGRADES
@@ -71,16 +26,31 @@ static NSString * const SUInstallerInstallationPathKey = @"SUInstallerInstallati
}
#endif
NSString *targetPath = [host installationPath];
NSString *tempName = [self temporaryNameForPath:targetPath];
NSDictionary *info = @{SUInstallerPathKey: path, SUInstallerTargetPathKey: targetPath, SUInstallerTempNameKey: tempName, SUInstallerHostKey: host, SUInstallerDelegateKey: delegate, SUInstallerInstallationPathKey: installationPath};
if (synchronously)
[self performInstallationWithInfo:info];
else {
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[self performInstallationWithInfo:info];
});
}
void (^performInstallation)() = ^void() {
NSError *error = nil;
NSString *oldPath = [host bundlePath];
NSString *tempName = [self temporaryNameForPath:[host installationPath]];
BOOL result = [self copyPathWithAuthentication:path overPath:installationPath temporaryName:tempName error:&error];
if (result) {
BOOL haveOld = [[NSFileManager defaultManager] fileExistsAtPath:oldPath];
BOOL differentFromNew = ![oldPath isEqualToString:installationPath];
if (haveOld && differentFromNew) {
[self _movePathToTrash:oldPath]; // On success, trash old copy if there's still one due to renaming.
}
}
dispatch_async(dispatch_get_main_queue(), ^ {
[self finishInstallationToPath:installationPath withResult:result host:host error:error delegate:delegate];
});
};
if (synchronously) {
performInstallation();
} else {
dispatch_async(dispatch_get_global_queue(0, 0), performInstallation);
}
}
@end