From bf9ba560f035bed5c873eafa0663f1e1bd2679e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kornel=20Lesin=CC=81ski?= Date: Mon, 30 Jun 2014 23:24:35 +0100 Subject: [PATCH] Use closure instead of info dict --- Sparkle/SUPackageInstaller.m | 72 +++++++++++-------------------- Sparkle/SUPlainInstaller.m | 82 ++++++++++++------------------------ 2 files changed, 51 insertions(+), 103 deletions(-) diff --git a/Sparkle/SUPackageInstaller.m b/Sparkle/SUPackageInstaller.m index e5d19fdf..9e5a3527 100644 --- a/Sparkle/SUPackageInstaller.m +++ b/Sparkle/SUPackageInstaller.m @@ -10,60 +10,38 @@ #import #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)delegate synchronously:(BOOL)synchronously versionComparator:(id)__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 )delegate synchronously:(BOOL)synchronously versionComparator:(id ) __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 diff --git a/Sparkle/SUPlainInstaller.m b/Sparkle/SUPlainInstaller.m index 3c5d1730..80dc54bc 100644 --- a/Sparkle/SUPlainInstaller.m +++ b/Sparkle/SUPlainInstaller.m @@ -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)delegate synchronously:(BOOL)synchronously versionComparator:(id )comparator ++ (void)performInstallationToPath:(NSString *)installationPath fromPath:(NSString *)path host:(SUHost *)host delegate:(id)delegate synchronously:(BOOL)synchronously versionComparator:(id)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