Skip to content

Commit

Permalink
using env variables instead of query parameter to switch between rele…
Browse files Browse the repository at this point in the history
…ase server and json file mode

  use: MODE=JSONFILE VERSION=1.5.0 open MyApp.app
defaults to release server
  • Loading branch information
Thomas Moenicke authored and MarshallOfSound committed Jan 10, 2018
1 parent 8536201 commit 3f398df
Showing 1 changed file with 14 additions and 29 deletions.
43 changes: 14 additions & 29 deletions Squirrel/SQRLUpdater.m
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@
// followed by a random string of characters.
static NSString * const SQRLUpdaterUniqueTemporaryDirectoryPrefix = @"update.";

/// Type of method used to download the release
/// Type of mode used to download the release
typedef enum {
JSONFILE=1,
RELEASESERVER
} Method;
} Mode;

@interface SQRLUpdater ()

Expand Down Expand Up @@ -157,22 +157,12 @@ - (id)initWithUpdateRequest:(NSURLRequest *)updateRequest {

- (id)initWithUpdateRequest:(NSURLRequest *)updateRequest requestForDownload:(SQRLRequestForDownload)requestForDownload {
//! foo.bar/releases.json?version=1.0
NSString *version = nil;
NSURLComponents* url = [[NSURLComponents alloc] initWithURL:updateRequest.URL resolvingAgainstBaseURL:FALSE];
NSArray* queryItems = url.queryItems;
Method mtd = RELEASESERVER;
for(NSURLQueryItem* obj in queryItems) {
if([obj.name isEqualToString:@"version"]) {
version = obj.value;
}
if([obj.name isEqualToString:@"method"]) {
NSString* method = obj.value;
if([method isEqualToString:@"JSON"])
mtd = JSONFILE;
if([method isEqualToString:@"ReleaseServer"])
mtd = RELEASESERVER;

}

NSString* version = getenv("VERSION") ? @(getenv("VERSION")) : @"";

Mode mode = RELEASESERVER;
if(getenv("MODE")) {
mode = [@(getenv("MODE")) isEqualToString:@"JSONFILE"] ? JSONFILE : RELEASESERVER;
}

//! download simple file
Expand All @@ -183,8 +173,6 @@ - (id)initWithUpdateRequest:(NSURLRequest *)updateRequest requestForDownload:(SQ
self = [super init];
if (self == nil) return nil;

//! if ends with json, use the new way

_requestForDownload = [requestForDownload copy];
_updateRequest = [NSURLRequest requestWithURL:updateRequest.URL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60.0];
_updateClass = SQRLUpdate.class;
Expand Down Expand Up @@ -226,7 +214,7 @@ - (id)initWithUpdateRequest:(NSURLRequest *)updateRequest requestForDownload:(SQ
if ([response isKindOfClass:NSHTTPURLResponse.class]) {
NSHTTPURLResponse *httpResponse = (id)response;

if(mtd == RELEASESERVER) {
if(mode == RELEASESERVER) {
if (!(httpResponse.statusCode >= 200 && httpResponse.statusCode <= 299)) {
NSDictionary *errorInfo = @{
NSLocalizedDescriptionKey: NSLocalizedString(@"Update check failed", nil),
Expand All @@ -252,7 +240,7 @@ - (id)initWithUpdateRequest:(NSURLRequest *)updateRequest requestForDownload:(SQ
return [RACSignal error:error];
}

if(mtd == JSONFILE) {
if(mode == JSONFILE) {
NSError *error = nil;
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:bodyData options:0 error:&error];

Expand All @@ -274,9 +262,9 @@ - (id)initWithUpdateRequest:(NSURLRequest *)updateRequest requestForDownload:(SQ

//! @todo find latest
NSArray *releases = dict[@"releases"];
for(id obj in releases) {
if([currentRelease isEqualToString:obj[@"version"]]) {
bodyData = [[NSJSONSerialization dataWithJSONObject:obj[@"updateTo"]
for(NSDictionary* release in releases) {
if([currentRelease isEqualToString:release[@"version"]]) {
bodyData = [[NSJSONSerialization dataWithJSONObject:release[@"updateTo"]
options:0 error:&error] copy];
break;
}
Expand Down Expand Up @@ -448,9 +436,6 @@ - (RACSignal *)unarchiveAndPrepareData:(NSData *)data withName:(NSString *)name
NSError *error = nil;
if (![NSFileManager.defaultManager removeItemAtURL:zipOutputURL error:&error]) {
NSLog(@"Error removing downloaded archive at %@: %@", zipOutputURL, error.sqrl_verboseDescription);
} else {
[self updateBundleMatchingCurrentApplicationInDirectory:downloadDirectory];

}
}];
}]
Expand Down Expand Up @@ -560,7 +545,7 @@ - (RACSignal *)updateBundleMatchingCurrentApplicationInDirectory:(NSURL *)direct
NSLog(@"Could not open application bundle at %@", URL);
return NO;
}
return [bundle.bundleIdentifier isEqual:bundle.bundleIdentifier];
return [bundle.bundleIdentifier isEqual:NSRunningApplication.currentApplication.bundleIdentifier];
}];

if (updateBundleURL != nil) {
Expand Down

0 comments on commit 3f398df

Please sign in to comment.