Skip to content

Commit

Permalink
add nightly ota updates
Browse files Browse the repository at this point in the history
  • Loading branch information
rA9stuff committed Nov 25, 2023
1 parent 9044049 commit eddd53a
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 33 deletions.
45 changes: 23 additions & 22 deletions LeetDown_M/LeetDownMain.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1208,22 +1208,35 @@ - (int)checkActivation {
return 0;
}

- (void) checkNotarizedUpdates {
NSString *urlString = @"https://api.github.com/repos/rA9stuff/LeetDown/releases/latest";
- (void) checkLDUpdates:(bool)nightly {

NSString *urlString = @"";
if (nightly) {
urlString = @"https://api.github.com/repos/rA9stuff/LeetDown/actions/artifacts";
}
else {
urlString = @"https://api.github.com/repos/rA9stuff/LeetDown/releases/latest";
}
[UpdateController sendGETRequestWithURL:urlString completion:^(NSDictionary *response, NSError *error) {
if (error) {
NSLog(@"LeetDown could not check for notarized updates: %@", error.localizedDescription);
NSLog(@"LeetDown could not check for updates: %@", error.localizedDescription);
}
else {
NSLog(@"Latest notarized LeetDown version: %@", response[@"tag_name"]);
if (nightly) {
NSString* hash = [response[@"artifacts"][0][@"workflow_run"][@"head_sha"] substringToIndex:7];
NSLog(@"Latest nightly LeetDown hash: %@", hash);
}
else {
NSLog(@"Latest notarized LeetDown version: %@", response[@"tag_name"]);
}

// check if current version number is less than the latest version number
if ([[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"] compare:response[@"tag_name"] options:NSNumericSearch] == NSOrderedAscending) {
// nsalert with update button
if ((!nightly && [[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"] compare:response[@"tag_name"] options:NSNumericSearch] == NSOrderedAscending) || (nightly && [response[@"artifacts"][0][@"workflow_run"][@"head_sha"] substringToIndex:7] != getPref(@"nightlyHash"))) {

dispatch_async(dispatch_get_main_queue(), ^(){
NSAlert *alert = [[NSAlert alloc] init];
[alert setMessageText:@"Update available"];
[alert setInformativeText:[NSString stringWithFormat: @"A new version of LeetDown (%@) is available. Would you like to download it?", response[@"tag_name"]]];
[alert setInformativeText:[NSString stringWithFormat: @"A new version of LeetDown (%@) is available. Would you like to download it?", nightly ? [response[@"artifacts"][0][@"workflow_run"][@"head_sha"] substringToIndex:7] : response[@"tag_name"]]];
[alert addButtonWithTitle:@"Update"];
[alert addButtonWithTitle:@"Cancel"];
[alert setAlertStyle:NSAlertStyleWarning];
Expand All @@ -1238,24 +1251,12 @@ - (void) checkNotarizedUpdates {
}];
}

- (void) checkNightlyUpdates {
NSString *urlString = @"https://api.github.com/repos/rA9stuff/LeetDown/actions/artifacts";
[UpdateController sendGETRequestWithURL:urlString completion:^(NSDictionary *response, NSError *error) {
if (error) {
NSLog(@"LeetDown could not check for nightly updates: %@", error.localizedDescription);
} else {
NSString* hash = [response[@"artifacts"][0][@"workflow_run"][@"head_sha"] substringToIndex:7];
NSLog(@"Latest nightly LeetDown hash: %@", hash);
}
}];
}

- (void) createUpdateView {

NSStoryboard *storyboard = [NSStoryboard storyboardWithName:@"Main" bundle:nil];
NSViewController *yourViewController = [storyboard instantiateControllerWithIdentifier:@"UpdateController"];
NSViewController *updateVC = [storyboard instantiateControllerWithIdentifier:@"UpdateController"];
dispatch_async(dispatch_get_main_queue(), ^(){
[self.view.window.contentViewController presentViewControllerAsSheet:yourViewController];
[self.view.window.contentViewController presentViewControllerAsSheet:updateVC];
});
}

Expand All @@ -1265,7 +1266,7 @@ - (void)viewDidLoad {
[super viewDidLoad];

dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{
[self checkNotarizedUpdates];
[self checkLDUpdates:([getPref(@"nightlyHash") isEqual: @""] ? false : true)];
});

LD_conditionVariable = [[NSCondition alloc] init];
Expand Down
56 changes: 45 additions & 11 deletions LeetDown_M/UpdateController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#import <AFNetworking/AFNetworking.h>
#import "UpdateController.h"
#import "PlistUtils.h"
#import "SSZipArchive/SSZipArchive.h"
#import <math.h>

@implementation UpdateController

Expand Down Expand Up @@ -63,21 +65,38 @@ - (void) updateLD {
NSLog(@"Error: %@", error.localizedDescription);
}
else {
NSString* downloadURL = response[@"assets"][0][@"browser_download_url"];
NSString* assetName = response[@"assets"][0][@"name"];
bool nightly = ![getPref(@"nightlyHash") isEqual: @""] ? true : false;
NSString* downloadURL = @"";
NSString* assetName = @"";

if (nightly) {
downloadURL = @"https://nightly.link/rA9stuff/LeetDown/workflows/ci/master/LeetDown-Nightly.zip";
assetName = @"LeetDown_Nightly.zip";
}
else {
downloadURL = response[@"assets"][0][@"browser_download_url"];
assetName = response[@"assets"][0][@"name"];
}
dispatch_async(dispatch_get_main_queue(),^{
[[self statusStr] setStringValue:@"Downloading..."];
});

// use nsfilemanager to move the updater to the temp directory
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager createDirectoryAtPath:[NSTemporaryDirectory() stringByAppendingString:@"LD"] withIntermediateDirectories:NO attributes:nil error:nil];

printf("Downloading %s from %s\n", assetName.UTF8String, downloadURL.UTF8String);

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
NSURL *formattedURL = [NSURL URLWithString:downloadURL];
NSURLRequest *request = [NSURLRequest requestWithURL:formattedURL];
NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
NSURL *fullURL = [documentsDirectoryURL URLByAppendingPathComponent:assetName];
NSURL *tempURL = [NSURL URLWithString: NSTemporaryDirectory()];
NSURL *fullURL = [[tempURL URLByAppendingPathComponent:@"LD"] URLByAppendingPathComponent:assetName];

// extremely hacky but will do for now...
NSString *fileURL = [@"file://" stringByAppendingString:[NSString stringWithFormat:@"%@", fullURL]];

[manager setDownloadTaskDidWriteDataBlock:^(NSURLSession *session, NSURLSessionDownloadTask *downloadTask, int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite) {

CGFloat written = totalBytesWritten;
Expand All @@ -89,6 +108,10 @@ - (void) updateLD {
writtenSTR = [writtenSTR substringToIndex:[writtenSTR length] -4];
totalSTR = [totalSTR substringToIndex:[totalSTR length] -4];
CGFloat percentageCompleted = written/total;
if (nightly) {
percentageCompleted /= -10000000;
}
NSLog(@"%f\n", percentageCompleted);
NSString *percentageSTR = [NSString stringWithFormat:@"%f", percentageCompleted];
percentageSTR = [percentageSTR substringToIndex:[percentageSTR length] -4];
if ([percentageSTR doubleValue] >= 0.10) {
Expand All @@ -105,7 +128,7 @@ - (void) updateLD {
}];

NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
return fullURL;
return [NSURL URLWithString: fileURL];
}
completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {

Expand All @@ -114,29 +137,40 @@ - (void) updateLD {
NSString *updaterPath = [[NSBundle mainBundle] resourcePath];
updaterPath = [[updaterPath substringToIndex:[updaterPath length] -9] stringByAppendingString:@"MacOS/LDUpdater"];

// use nsfilemanager to move the updater to the temp directory
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager createDirectoryAtPath:[tempPath stringByAppendingString:@"LD"] withIntermediateDirectories:NO attributes:nil error:nil];
[fileManager copyItemAtPath:updaterPath toPath:[tempPath stringByAppendingString:@"LD/LDUpdater"] error:&error];
if (error) {
printf("Error moving updater to temp directory: %s\n", error.localizedDescription.UTF8String);
}

if (nightly) {
// unzip the downloaded file with ssziparchive
NSString *zipPath = [tempPath stringByAppendingString:@"LD/LeetDown_Nightly.zip"];
NSString *destinationPath = [tempPath stringByAppendingString:@"LD/"];
[SSZipArchive unzipFileAtPath:zipPath toDestination:destinationPath];
}

dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{
dispatch_async(dispatch_get_main_queue(), ^{
[_updateProgress setDoubleValue:1.0];
[[self statusStr] setStringValue:@"Installing..."];
});

// find the file with .dmg extension
NSString *dmgFilePath = @"";
NSArray *dirContents = [fileManager contentsOfDirectoryAtPath:[tempPath stringByAppendingString:@"LD"] error:nil];
for (NSString *file in dirContents) {
if ([file containsString:@".dmg"]) {
dmgFilePath = [tempPath stringByAppendingString:[NSString stringWithFormat:@"LD/%@", file]];
}
}
sleep(1);
NSTask *LDUpdater = [[NSTask alloc] init];
[LDUpdater setLaunchPath:[NSTemporaryDirectory() stringByAppendingString:@"LD/LDUpdater"]];
[LDUpdater setArguments:@[[NSString stringWithFormat:@"%@", fullURL]]];
[LDUpdater setArguments:@[[NSString stringWithFormat:@"%@", (nightly ? dmgFilePath : fullURL)]]];
[LDUpdater launch];
dispatch_async(dispatch_get_main_queue(), ^{
[self.view.window.contentViewController dismissViewController:self];
[[NSApplication sharedApplication] terminate:nil];
});

});
}];
[downloadTask resume];
Expand Down

0 comments on commit eddd53a

Please sign in to comment.