From eeb7c0246b747c0031bc9917060fec167d53b052 Mon Sep 17 00:00:00 2001 From: Bill Luoma Date: Thu, 24 Mar 2016 12:41:12 -0700 Subject: [PATCH] hacky fix for issue #85 https://github.com/nordnet/cordova-hot-code-push/issues/85 --- src/ios/HCPPlugin.m | 54 +++++++++++++++++++++++-- src/ios/Updater/HCPInstallationWorker.m | 35 ++++++++++++++-- src/ios/Utils/HCPAssetsFolderHelper.h | 5 +++ src/ios/Utils/HCPAssetsFolderHelper.m | 36 ++++++++++++++++- 4 files changed, 123 insertions(+), 7 deletions(-) diff --git a/src/ios/HCPPlugin.m b/src/ios/HCPPlugin.m index cbabb3aa..c1644d74 100644 --- a/src/ios/HCPPlugin.m +++ b/src/ios/HCPPlugin.m @@ -52,7 +52,8 @@ -(void)pluginInitialize { // install www folder if it is needed if ([self isWWwFolderNeedsToBeInstalled]) { - [self installWwwFolder]; + //[self installWwwFolder]; //BL + [self installWwwFolderSync]; //blocks on startup. BL return; } @@ -108,6 +109,51 @@ - (void)installWwwFolder { [HCPAssetsFolderHelper installWwwFolderToExternalStorageFolder:_filesStructure.wwwFolder]; } +//this blocks. for startup in wkwebview, BL +- (void)installWwwFolderSync { + + NSLog(@"%s in", __PRETTY_FUNCTION__); + + _isPluginReadyForWork = NO; + + // reset www folder installed flag + if (_pluginInternalPrefs.isWwwFolderInstalled) { + _pluginInternalPrefs.wwwFolderInstalled = NO; + [_pluginInternalPrefs saveToUserDefaults]; + } + + HCPAssetsFolderHelper *helperInstance = [HCPAssetsFolderHelper sharedInstance]; + NSError *error = [helperInstance installWwwFolderToExternalStorageFolderSync:_filesStructure.wwwFolder]; + + if (error) { + NSLog(@"%s error installing www to external storage: %@", __PRETTY_FUNCTION__, error); + } + else { + + // update stored config with new application build version + _pluginInternalPrefs.appBuildVersion = [NSBundle applicationBuildVersion]; + _pluginInternalPrefs.wwwFolderInstalled = YES; + [_pluginInternalPrefs saveToUserDefaults]; + + [self resetIndexPageToExternalStorage]; + + // fetch update + [self loadApplicationConfig]; + + if (_pluginXmlConfig.isUpdatesAutoDownloadAllowed && + ![HCPUpdateLoader sharedInstance].isDownloadInProgress && + ![HCPUpdateInstaller sharedInstance].isInstallationInProgress) { + [self _fetchUpdate:nil]; + } + } + + // allow work + _isPluginReadyForWork = YES; + + NSLog(@"%s out", __PRETTY_FUNCTION__); +} + + /** * Load application config from file system */ @@ -199,6 +245,7 @@ - (BOOL)_installUpdate:(NSString *)callbackID { NSString *newVersion = _pluginInternalPrefs.readyForInstallationReleaseVersionName; NSString *currentVersion = _pluginInternalPrefs.currentReleaseVersionName; + NSLog(@"%s newVersion: %@, currentVersion: %@", __PRETTY_FUNCTION__, newVersion, currentVersion); NSError *error = nil; [[HCPUpdateInstaller sharedInstance] installVersion:newVersion currentVersion:currentVersion error:&error]; if (error) { @@ -264,6 +311,7 @@ - (void)resetIndexPageToExternalStorage { // rewrite starting page www folder path: should load from external storage if ([self.viewController isKindOfClass:[CDVViewController class]]) { + NSLog(@"%s _filesStructure.wwwFolder.absoluteString: %@", __PRETTY_FUNCTION__, _filesStructure.wwwFolder.absoluteString); ((CDVViewController *)self.viewController).wwwFolderName = _filesStructure.wwwFolder.absoluteString; } else { NSLog(@"HotCodePushError: Can't make starting page to be from external storage. Main controller should be of type CDVViewController."); @@ -572,11 +620,11 @@ - (void)onUpdateInstallationErrorEvent:(NSNotification *)notification { * @param notification captured notification with the event details */ - (void)onUpdateInstalledEvent:(NSNotification *)notification { - _appConfig = notification.userInfo[kHCPEventUserInfoApplicationConfigKey]; + _appConfig = notification.userInfo[kHCPEventUserInfoApplicationConfigKey]; //this is just the current config passed by HCPInstallationWorker. BL _pluginInternalPrefs.readyForInstallationReleaseVersionName = @""; _pluginInternalPrefs.previousReleaseVersionName = _pluginInternalPrefs.currentReleaseVersionName; - _pluginInternalPrefs.currentReleaseVersionName = _appConfig.contentConfig.releaseVersion; + _pluginInternalPrefs.currentReleaseVersionName = _appConfig.contentConfig.releaseVersion; //current and previous now the same. problem? BL [_pluginInternalPrefs saveToUserDefaults]; _filesStructure = [[HCPFilesStructure alloc] initWithReleaseVersion:_pluginInternalPrefs.currentReleaseVersionName]; diff --git a/src/ios/Updater/HCPInstallationWorker.m b/src/ios/Updater/HCPInstallationWorker.m index 247993d5..e7e5270c 100644 --- a/src/ios/Updater/HCPInstallationWorker.m +++ b/src/ios/Updater/HCPInstallationWorker.m @@ -13,6 +13,7 @@ #import "NSError+HCPExtension.h" #import "NSData+HCPMD5.h" #import "HCPEvents.h" +#import "HCPPluginInternalPreferences.h" @interface HCPInstallationWorker() { HCPFilesStructure *_newReleaseFS; @@ -41,6 +42,8 @@ - (instancetype)initWithNewVersion:(NSString *)newVersion currentVersion:(NSStri if (self) { _newReleaseFS = [[HCPFilesStructure alloc] initWithReleaseVersion:newVersion]; _currentReleaseFS = [[HCPFilesStructure alloc] initWithReleaseVersion:currentVersion]; + NSLog(@"%s new: %@", __PRETTY_FUNCTION__, _newReleaseFS.wwwFolder.path); + NSLog(@"%s current: %@", __PRETTY_FUNCTION__, _currentReleaseFS.wwwFolder.path); } return self; @@ -85,8 +88,12 @@ - (void)dispatchEventWithError:(NSError *)error { * Send event that update was successfully installed */ - (void)dispatchSuccessEvent { + //NSNotification *notification = [HCPEvents notificationWithName:kHCPUpdateIsInstalledEvent //BL + // applicationConfig:_newConfig //BL + // taskId:self.workerId]; //BL + NSNotification *notification = [HCPEvents notificationWithName:kHCPUpdateIsInstalledEvent - applicationConfig:_newConfig + applicationConfig:_oldConfig //always load from same folder. BL taskId:self.workerId]; @@ -259,6 +266,20 @@ - (BOOL)moveDownloadedFilesToWwwFolder:(NSError **)error { } } + //blow away current. BL + if (![_fileManager removeItemAtURL:_currentReleaseFS.wwwFolder error:error]) { + errorMsg = [NSString stringWithFormat:@"Failed to remove current www folder: %@. Installation failed: %@", + _currentReleaseFS.wwwFolder.path, [(*error) underlyingErrorLocalizedDesription]]; + } + + // copy new files into current www folder. BL + if (![_fileManager copyItemAtURL:_newReleaseFS.wwwFolder toURL:_currentReleaseFS.wwwFolder error:error]) { + errorMsg = [NSString stringWithFormat:@"Failed to copy new files %@ into current www folder: %@. Installation failed.", + _currentReleaseFS.wwwFolder.path, [(*error) underlyingErrorLocalizedDesription]]; + + } + + if (errorMsg) { *error = [NSError errorWithCode:kHCPFailedToCopyNewContentFilesErrorCode description:errorMsg]; } @@ -266,12 +287,19 @@ - (BOOL)moveDownloadedFilesToWwwFolder:(NSError **)error { return (*error == nil); } + /** * Save loaded configs to the www folder. They are now our current configs. */ - (void)saveNewConfigsToWwwFolder { - [_manifestStorage store:_newManifest inFolder:_newReleaseFS.wwwFolder]; - [_configStorage store:_newConfig inFolder:_newReleaseFS.wwwFolder]; + //[_manifestStorage store:_newManifest inFolder:_newReleaseFS.wwwFolder]; //BL + //[_configStorage store:_newConfig inFolder:_newReleaseFS.wwwFolder]; //BL + + //use same folder for wkwebview issue #85. Probably better not use the date from chcp.json as a folder name since it's a lie now. BL + [_manifestStorage store:_newManifest inFolder:_currentReleaseFS.wwwFolder]; + [_configStorage store:_newConfig inFolder:_currentReleaseFS.wwwFolder]; + + } /** @@ -286,6 +314,7 @@ - (void)cleanUpOnFailure { */ - (void)cleanUpOnSucess { [_fileManager removeItemAtURL:_newReleaseFS.downloadFolder error:nil]; + } @end diff --git a/src/ios/Utils/HCPAssetsFolderHelper.h b/src/ios/Utils/HCPAssetsFolderHelper.h index 8ce42ead..2c98998f 100644 --- a/src/ios/Utils/HCPAssetsFolderHelper.h +++ b/src/ios/Utils/HCPAssetsFolderHelper.h @@ -19,4 +19,9 @@ */ + (void)installWwwFolderToExternalStorageFolder:(NSURL *)externalFolderURL; ++ (HCPAssetsFolderHelper *)sharedInstance; + +- (NSError*)installWwwFolderToExternalStorageFolderSync:(NSURL *)externalFolderURL; + + @end diff --git a/src/ios/Utils/HCPAssetsFolderHelper.m b/src/ios/Utils/HCPAssetsFolderHelper.m index 22509bc5..dc06f6a1 100644 --- a/src/ios/Utils/HCPAssetsFolderHelper.m +++ b/src/ios/Utils/HCPAssetsFolderHelper.m @@ -35,8 +35,8 @@ + (void)installWwwFolderToExternalStorageFolder:(NSURL *)externalFolderURL { }); } -#pragma mark Private API +//expose this. BL + (HCPAssetsFolderHelper *)sharedInstance { static HCPAssetsFolderHelper *sharedInstance = nil; static dispatch_once_t onceToken; @@ -47,6 +47,40 @@ + (HCPAssetsFolderHelper *)sharedInstance { return sharedInstance; } +//blocks. for startup. BL +- (NSError*)installWwwFolderToExternalStorageFolderSync:(NSURL *)externalFolderURL { + NSLog(@"%s in", __PRETTY_FUNCTION__); + self.isWorking = YES; + NSError *error = nil; + NSFileManager *fileManager = [NSFileManager defaultManager]; + BOOL isWWwFolderExists = [fileManager fileExistsAtPath:externalFolderURL.path]; + + // remove previous version of the www folder + if (isWWwFolderExists) { + [fileManager removeItemAtURL:[externalFolderURL URLByDeletingLastPathComponent] error:&error]; + } + + // create new www folder + if (![fileManager createDirectoryAtURL:[externalFolderURL URLByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:&error]) { + //[self dispatchErrorEvent:error]; + NSLog(@"%s out err: %@", __PRETTY_FUNCTION__, error); + + return error; + } + + // copy www folder from bundle to cache folder + NSURL *localWww = [NSURL fileURLWithPath:[NSBundle pathToWwwFolder] isDirectory:YES]; + [fileManager copyItemAtURL:localWww toURL:externalFolderURL error:&error]; + + self.isWorking = NO; + NSLog(@"%s out", __PRETTY_FUNCTION__); + + return error; +} + + +#pragma mark Private API + - (void)__installWwwFolderToExternalStorageFolder:(NSURL *)externalFolderURL { NSError *error = nil; NSFileManager *fileManager = [NSFileManager defaultManager];