diff --git a/Squirrel/SQRLUpdater.h b/Squirrel/SQRLUpdater.h index 8e780396..56f7fb19 100644 --- a/Squirrel/SQRLUpdater.h +++ b/Squirrel/SQRLUpdater.h @@ -67,6 +67,12 @@ extern NSString * const SQRLUpdaterJSONObjectErrorKey; @class RACDisposable; @class RACSignal; +/// Type of mode used to download the release +typedef enum { + JSONFILE=1, + RELEASESERVER +} SQRLUpdaterMode; + // Checks for, downloads, and installs updates. @interface SQRLUpdater : NSObject @@ -123,6 +129,19 @@ extern NSString * const SQRLUpdaterJSONObjectErrorKey; // Returns the initialized `SQRLUpdater`. - (id)initWithUpdateRequest:(NSURLRequest *)updateRequest; +// Initializes an updater that will send the given request to check for updates +// on a CDN reading a release file in json format. +// +// updateRequest - A request to send to check for updates. This request can be +// customized as desired, like by including an `Authorization` +// header to authenticate with a private update server, or +// pointing to a local URL for testing. This must not be nil. +// forVersion - the currently installed version +// +// Returns the initialized `SQRLUpdater`. +- (id)initWithUpdateRequest:(NSURLRequest *)updateRequest + forVersion:(NSString*)version; + // Initializes an updater that will send the given request to check for updates // and passes a block to provide requests for the update downloads. // @@ -133,7 +152,7 @@ extern NSString * const SQRLUpdaterJSONObjectErrorKey; // updateRequest param. // // Returns the initialized `SQRLUpdater`. -- (id)initWithUpdateRequest:(NSURLRequest *)updateRequest requestForDownload:(SQRLRequestForDownload)requestForDownload; +- (id)initWithUpdateRequest:(NSURLRequest *)updateRequest requestForDownload:(SQRLRequestForDownload)requestForDownload forVersion:(NSString*) version useMode:(SQRLUpdaterMode) mode; // Executes `checkForUpdatesCommand` (if enabled) every `interval` seconds. // diff --git a/Squirrel/SQRLUpdater.m b/Squirrel/SQRLUpdater.m index 3581215f..9f1d3e6a 100644 --- a/Squirrel/SQRLUpdater.m +++ b/Squirrel/SQRLUpdater.m @@ -40,12 +40,6 @@ // followed by a random string of characters. static NSString * const SQRLUpdaterUniqueTemporaryDirectoryPrefix = @"update."; -/// Type of mode used to download the release -typedef enum { - JSONFILE=1, - RELEASESERVER -} Mode; - @interface SQRLUpdater () @property (atomic, readwrite) SQRLUpdaterState state; @@ -152,24 +146,27 @@ - (id)init { - (id)initWithUpdateRequest:(NSURLRequest *)updateRequest { return [self initWithUpdateRequest:updateRequest requestForDownload:^(NSURL *downloadURL) { return [NSURLRequest requestWithURL:downloadURL]; - }]; + } forVersion:@"" useMode:RELEASESERVER]; } -- (id)initWithUpdateRequest:(NSURLRequest *)updateRequest requestForDownload:(SQRLRequestForDownload)requestForDownload { - //! foo.bar/releases.json?version=1.0 - - NSString* version = getenv("VERSION") ? @(getenv("VERSION")) : @""; +- (id)initWithUpdateRequest:(NSURLRequest *)updateRequest forVersion: (NSString*) version { + return [self initWithUpdateRequest:updateRequest requestForDownload:^(NSURL *downloadURL) { + return [NSURLRequest requestWithURL:downloadURL]; + } forVersion:version useMode:JSONFILE]; +} - Mode mode = RELEASESERVER; - if(getenv("MODE")) { - mode = [@(getenv("MODE")) isEqualToString:@"JSONFILE"] ? JSONFILE : RELEASESERVER; - } +- (id)initWithUpdateRequest:(NSURLRequest *)updateRequest requestForDownload:(SQRLRequestForDownload)requestForDownload + forVersion:(NSString*) version useMode:(SQRLUpdaterMode) mode { //! download simple file NSParameterAssert(updateRequest != nil); NSParameterAssert(requestForDownload != nil); + if(mode == JSONFILE) { + NSParameterAssert(version != nil); + } + self = [super init]; if (self == nil) return nil; diff --git a/SquirrelTests/SQRLUpdaterSpec.m b/SquirrelTests/SQRLUpdaterSpec.m index 25e17959..e06978a4 100644 --- a/SquirrelTests/SQRLUpdaterSpec.m +++ b/SquirrelTests/SQRLUpdaterSpec.m @@ -99,7 +99,7 @@ bool isRunningOnReadOnlyVolumeImp(id self, SEL _cmd) @"name": @"my-stub-release", @"notes": @"mock release for automated tests, json", @"pub_date": @"2017-03-09T15:24:55-05:00", - @"url": @"http://localhost/hipchatng-0.0.145.zip" + @"url": @"http://localhost/myapp-0.0.145.zip" }; NSError * err; @@ -116,7 +116,7 @@ bool isRunningOnReadOnlyVolumeImp(id self, SEL _cmd) @"name": @"my-stub-release", @"notes": @"mock release for automated tests, release server", @"pub_date": @"2017-03-09T15:24:55-05:00", - @"url": @"http://localhost/hipchatng-0.0.145.zip" + @"url": @"http://localmyappng-0.0.145.zip" }; NSError * err;