From 15483815fffb481e02ff23ab3e6178f01aaa5727 Mon Sep 17 00:00:00 2001 From: Zorg Date: Sat, 30 Apr 2016 22:52:04 -0400 Subject: [PATCH] Pass path to relaunch to sparkle-cli We also now listen on termination for the app that we are going to re-launch, rather than the bundle we are updating. --- Sparkle/Autoupdate/AppInstaller.m | 21 ++++++++++++----- sparkle-cli/AppDelegate.h | 2 +- sparkle-cli/AppDelegate.m | 33 +++++++++++++++++++++------ sparkle-cli/SUCommandLineUserDriver.h | 2 +- sparkle-cli/SUCommandLineUserDriver.m | 12 +++++----- sparkle-cli/main.m | 6 ++--- 6 files changed, 52 insertions(+), 24 deletions(-) diff --git a/Sparkle/Autoupdate/AppInstaller.m b/Sparkle/Autoupdate/AppInstaller.m index 242ec375..2f9e8cda 100644 --- a/Sparkle/Autoupdate/AppInstaller.m +++ b/Sparkle/Autoupdate/AppInstaller.m @@ -341,12 +341,21 @@ static const NSTimeInterval SUDisplayProgressTimeDelay = 0.7; SULog(@"Error: Failed to match host bundle identifiers %@ and %@", self.hostBundleIdentifier, bundleIdentifier); [self cleanupAndExitWithStatus:EXIT_FAILURE]; } else { - self.host = host; - self.installationData = installationData; - - self.terminationListener = [[TerminationListener alloc] initWithBundle:hostBundle]; - - [self extractAndInstallUpdate]; + NSBundle *relaunchBundle = [NSBundle bundleWithPath:nonNullInstallationData.relaunchPath]; + if (relaunchBundle == nil) { + SULog(@"Error: Failed to locate relaunch bundle path %@", nonNullInstallationData.relaunchPath); + [self cleanupAndExitWithStatus:EXIT_FAILURE]; + } else { + self.host = host; + self.installationData = installationData; + + // We use the relaunch path for the bundle to listen for termination instead of the host path + // For a plug-in this makes a big difference; we want to wait until the app hosting the plug-in terminates + // Otherwise for an app, the relaunch path and host path should be identical + self.terminationListener = [[TerminationListener alloc] initWithBundle:relaunchBundle]; + + [self extractAndInstallUpdate]; + } } } }); diff --git a/sparkle-cli/AppDelegate.h b/sparkle-cli/AppDelegate.h index 223fd42e..d674f275 100644 --- a/sparkle-cli/AppDelegate.h +++ b/sparkle-cli/AppDelegate.h @@ -12,7 +12,7 @@ NS_ASSUME_NONNULL_BEGIN @interface AppDelegate : NSObject -- (nullable instancetype)initWithBundlePath:(const char *)bundlePath; +- (instancetype)initWithUpdateBundlePath:(const char *)updateBundlePath relaunchBundlePath:(const char *)relaunchBundlePath; @end diff --git a/sparkle-cli/AppDelegate.m b/sparkle-cli/AppDelegate.m index e310ac8e..d0fec4c0 100644 --- a/sparkle-cli/AppDelegate.m +++ b/sparkle-cli/AppDelegate.m @@ -13,29 +13,43 @@ @interface AppDelegate () @property (nonatomic, readonly) SUUpdater *updater; +@property (nonatomic, readonly) NSString *relaunchBundlePath; @end @implementation AppDelegate @synthesize updater = _updater; +@synthesize relaunchBundlePath = _relaunchBundlePath; -- (instancetype)initWithBundlePath:(const char *)bundlePath +- (instancetype)initWithUpdateBundlePath:(const char *)updateBundlePathString relaunchBundlePath:(const char *)relaunchBundlePathString { self = [super init]; if (self != nil) { - NSString *bundlePathString = [[NSString alloc] initWithUTF8String:bundlePath]; - if (bundlePathString == nil) { + NSString *updateBundlePath = [[NSString alloc] initWithUTF8String:updateBundlePathString]; + if (updateBundlePath == nil) { return nil; } - NSBundle *bundle = [NSBundle bundleWithPath:bundlePathString]; - if (bundle == nil) { + NSString *relaunchBundlePath = [[NSString alloc] initWithUTF8String:relaunchBundlePathString]; + if (relaunchBundlePath == nil) { return nil; } - id userDriver = [[SUCommandLineUserDriver alloc] initWithBundle:bundle]; - _updater = [[SUUpdater alloc] initWithHostBundle:bundle userDriver:userDriver delegate:self]; + NSBundle *updateBundle = [NSBundle bundleWithPath:updateBundlePath]; + if (updateBundle == nil) { + return nil; + } + + NSBundle *relaunchBundle = [NSBundle bundleWithPath:relaunchBundlePath]; + if (relaunchBundle == nil) { + return nil; + } + + _relaunchBundlePath = relaunchBundlePath; + + id userDriver = [[SUCommandLineUserDriver alloc] initWithRelaunchBundle:relaunchBundle]; + _updater = [[SUUpdater alloc] initWithHostBundle:updateBundle userDriver:userDriver delegate:self]; } return self; } @@ -45,6 +59,11 @@ return YES; } +- (NSString *)pathToRelaunchForUpdater:(SUUpdater *)__unused updater +{ + return self.relaunchBundlePath; +} + - (void)applicationDidFinishLaunching:(NSNotification *)__unused aNotification { // Kick off a scheduled update check once we start the updater later diff --git a/sparkle-cli/SUCommandLineUserDriver.h b/sparkle-cli/SUCommandLineUserDriver.h index 542963e4..1aadfd07 100644 --- a/sparkle-cli/SUCommandLineUserDriver.h +++ b/sparkle-cli/SUCommandLineUserDriver.h @@ -13,7 +13,7 @@ NS_ASSUME_NONNULL_BEGIN @interface SUCommandLineUserDriver : NSObject -- (instancetype)initWithBundle:(NSBundle *)bundle; +- (instancetype)initWithRelaunchBundle:(NSBundle *)relaunchBundle; @end diff --git a/sparkle-cli/SUCommandLineUserDriver.m b/sparkle-cli/SUCommandLineUserDriver.m index be676622..14cc5cd0 100644 --- a/sparkle-cli/SUCommandLineUserDriver.m +++ b/sparkle-cli/SUCommandLineUserDriver.m @@ -11,7 +11,7 @@ @interface SUCommandLineUserDriver () -@property (nonatomic, readonly) NSBundle *bundle; +@property (nonatomic, readonly) NSBundle *relaunchBundle; @property (nonatomic, readonly) SUUserDriverCoreComponent *coreComponent; @property (nonatomic) NSUInteger bytesDownloaded; @property (nonatomic) NSUInteger bytesToDownload; @@ -20,16 +20,16 @@ @implementation SUCommandLineUserDriver -@synthesize bundle = _bundle; +@synthesize relaunchBundle = _relaunchBundle; @synthesize coreComponent = _coreComponent; @synthesize bytesDownloaded = _bytesDownloaded; @synthesize bytesToDownload = _bytesToDownload; -- (instancetype)initWithBundle:(NSBundle *)bundle +- (instancetype)initWithRelaunchBundle:(NSBundle *)relaunchBundle { self = [super init]; if (self != nil) { - _bundle = bundle; + _relaunchBundle = relaunchBundle; _coreComponent = [[SUUserDriverCoreComponent alloc] initWithDelegate:nil]; } return self; @@ -201,11 +201,11 @@ - (NSRunningApplication *)targetRunningApplication { - NSString *bundleIdentifier = self.bundle.bundleIdentifier; + NSString *bundleIdentifier = self.relaunchBundle.bundleIdentifier; if (bundleIdentifier != nil) { NSArray *runningApplications = [NSRunningApplication runningApplicationsWithBundleIdentifier:bundleIdentifier]; // Make sure we *don't* use NSURLs for equality comparison; turns out to not work that great - NSString *bundlePath = self.bundle.bundleURL.path; + NSString *bundlePath = self.relaunchBundle.bundleURL.path; if (bundlePath != nil) { for (NSRunningApplication *runningApplication in runningApplications) { NSString *candidatePath = runningApplication.bundleURL.path; diff --git a/sparkle-cli/main.m b/sparkle-cli/main.m index 21a2de08..173687c0 100644 --- a/sparkle-cli/main.m +++ b/sparkle-cli/main.m @@ -13,12 +13,12 @@ int main(int argc, const char *argv[]) { @autoreleasepool { - if (argc != 2) { - printf("Usage: %s \n", argv[0]); + if (argc != 3) { + printf("Usage: %s \n", argv[0]); return EXIT_FAILURE; } - AppDelegate *delegate = [[AppDelegate alloc] initWithBundlePath:argv[1]]; + AppDelegate *delegate = [[AppDelegate alloc] initWithUpdateBundlePath:argv[1] relaunchBundlePath:argv[2]]; if (delegate == nil) { printf("Error: Failed to initialize sparkle. Is the bundle you specified valid?\n"); return EXIT_FAILURE;