bug fixed
This commit is contained in:
parent
b97782427a
commit
c247530ce8
@ -14,13 +14,14 @@
|
|||||||
#import "RCTLog.h"
|
#import "RCTLog.h"
|
||||||
|
|
||||||
//
|
//
|
||||||
//static NSString *const curVersionKey = @"REACTNATIVECNHOTUPDATECURVERSIONKEY";
|
static NSString *const keyUpdateInfo = @"REACTNATIVECN_HOTUPDATE_INFO_KEY";
|
||||||
static NSString *const rnHotUpdatePackageInfoKey = @"REACTNATIVECNHOTUPDATEPACKAGEINFOKEY";
|
static NSString *const paramPackageVersion = @"packageVersion";
|
||||||
static NSString *const paramLastVersion = @"lastVersion";
|
static NSString *const paramLastVersion = @"lastVersion";
|
||||||
static NSString *const paramCurrentVersion = @"currentVersion";
|
static NSString *const paramCurrentVersion = @"currentVersion";
|
||||||
static NSString *const paramIsFirstTime = @"isFirstTime";
|
static NSString *const paramIsFirstTime = @"isFirstTime";
|
||||||
static NSString *const paramIsFirstLoadOk = @"isFirstLoadOK";
|
static NSString *const paramIsFirstLoadOk = @"isFirstLoadOK";
|
||||||
static NSString *const paramIsRolledBack = @"isRolledBack";
|
static NSString *const keyRolledBackMarked = @"REACTNATIVECN_HOTUPDATE_ROLLEDBACKMARKED_KEY";
|
||||||
|
static NSString *const KeyPackageUpdatedMarked = @"REACTNATIVECN_HOTUPDATE_ISPACKAGEUPDATEDMARKED_KEY";
|
||||||
|
|
||||||
// app info
|
// app info
|
||||||
static NSString * const AppVersionKey = @"appVersion";
|
static NSString * const AppVersionKey = @"appVersion";
|
||||||
@ -61,38 +62,56 @@ RCT_EXPORT_MODULE(RCTHotUpdate);
|
|||||||
+ (NSURL *)bundleURL
|
+ (NSURL *)bundleURL
|
||||||
{
|
{
|
||||||
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
|
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
|
||||||
NSDictionary *packageInfo = [defaults dictionaryForKey:rnHotUpdatePackageInfoKey];
|
|
||||||
|
|
||||||
if (packageInfo) {
|
NSDictionary *updateInfo = [defaults dictionaryForKey:keyUpdateInfo];
|
||||||
NSString *curVersion = packageInfo[paramCurrentVersion];
|
if (updateInfo) {
|
||||||
NSString *lastVersion = packageInfo[paramLastVersion];
|
NSString *curPackageVersion = [RCTHotUpdate packageVersion];
|
||||||
|
NSString *packageVersion = [updateInfo objectForKey:paramPackageVersion];
|
||||||
|
|
||||||
BOOL isFirstTime = [packageInfo[paramIsFirstTime] boolValue];
|
BOOL needClearUpdateInfo = ![curPackageVersion isEqualToString:packageVersion];
|
||||||
BOOL isFirstLoadOK = [packageInfo[paramIsFirstLoadOk] boolValue];
|
if (needClearUpdateInfo) {
|
||||||
|
[defaults setObject:nil forKey:keyUpdateInfo];
|
||||||
NSString *loadVersioin = curVersion;
|
[defaults setObject:@(YES) forKey:KeyPackageUpdatedMarked];
|
||||||
if ((isFirstTime == NO && isFirstLoadOK == NO) || (loadVersioin.length<=0 && lastVersion.length>0)) {
|
|
||||||
loadVersioin = lastVersion;
|
|
||||||
|
|
||||||
// roll back to last version
|
|
||||||
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
|
|
||||||
if (lastVersion.length) {
|
|
||||||
[defaults setObject:@{paramCurrentVersion:lastVersion, paramIsFirstTime:@(NO), paramIsFirstLoadOk:@(YES), paramIsRolledBack:@(YES)} forKey:rnHotUpdatePackageInfoKey];
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
[defaults setObject:@{paramIsRolledBack:@(YES)} forKey:rnHotUpdatePackageInfoKey];
|
|
||||||
}
|
|
||||||
[defaults setObject:packageInfo forKey:rnHotUpdatePackageInfoKey];
|
|
||||||
[defaults synchronize];
|
[defaults synchronize];
|
||||||
|
// ...need clear files later
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
if (loadVersioin.length) {
|
NSString *curVersion = updateInfo[paramCurrentVersion];
|
||||||
NSString *downloadDir = [RCTHotUpdate downloadDir];
|
NSString *lastVersion = updateInfo[paramLastVersion];
|
||||||
|
|
||||||
NSString *bundlePath = [[downloadDir stringByAppendingPathComponent:loadVersioin] stringByAppendingPathComponent:BUNDLE_FILE_NAME];
|
BOOL isFirstTime = [updateInfo[paramIsFirstTime] boolValue];
|
||||||
if ([[NSFileManager defaultManager] fileExistsAtPath:bundlePath isDirectory:NULL]) {
|
BOOL isFirstLoadOK = [updateInfo[paramIsFirstLoadOk] boolValue];
|
||||||
NSURL *bundleURL = [NSURL fileURLWithPath:bundlePath];
|
|
||||||
return bundleURL;
|
NSString *loadVersioin = curVersion;
|
||||||
|
BOOL needRollback = (isFirstTime == NO && isFirstLoadOK == NO) || (loadVersioin.length<=0 && lastVersion.length>0);
|
||||||
|
if (needRollback) {
|
||||||
|
loadVersioin = lastVersion;
|
||||||
|
|
||||||
|
if (lastVersion.length) {
|
||||||
|
// roll back to last version
|
||||||
|
[defaults setObject:@{paramCurrentVersion:lastVersion,
|
||||||
|
paramIsFirstTime:@(NO),
|
||||||
|
paramIsFirstLoadOk:@(YES),
|
||||||
|
paramPackageVersion:curPackageVersion}
|
||||||
|
forKey:keyUpdateInfo];
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// roll back to bundle
|
||||||
|
[defaults setObject:nil forKey:keyUpdateInfo];
|
||||||
|
}
|
||||||
|
[defaults setObject:@(YES) forKey:keyRolledBackMarked];
|
||||||
|
[defaults synchronize];
|
||||||
|
// ...need clear files later
|
||||||
|
}
|
||||||
|
|
||||||
|
if (loadVersioin.length) {
|
||||||
|
NSString *downloadDir = [RCTHotUpdate downloadDir];
|
||||||
|
|
||||||
|
NSString *bundlePath = [[downloadDir stringByAppendingPathComponent:loadVersioin] stringByAppendingPathComponent:BUNDLE_FILE_NAME];
|
||||||
|
if ([[NSFileManager defaultManager] fileExistsAtPath:bundlePath isDirectory:NULL]) {
|
||||||
|
NSURL *bundleURL = [NSURL fileURLWithPath:bundlePath];
|
||||||
|
return bundleURL;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -102,24 +121,36 @@ RCT_EXPORT_MODULE(RCTHotUpdate);
|
|||||||
|
|
||||||
- (NSDictionary *)constantsToExport
|
- (NSDictionary *)constantsToExport
|
||||||
{
|
{
|
||||||
NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
|
|
||||||
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
|
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
|
||||||
NSMutableDictionary *packageInfo = [[defaults dictionaryForKey:rnHotUpdatePackageInfoKey] mutableCopy];
|
|
||||||
|
|
||||||
NSMutableDictionary *ret = [@{} mutableCopy];
|
NSMutableDictionary *ret = [@{} mutableCopy];
|
||||||
ret[@"downloadRootDir"] = [RCTHotUpdate downloadDir];
|
ret[@"downloadRootDir"] = [RCTHotUpdate downloadDir];
|
||||||
ret[@"packageVersion"] = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
|
ret[@"packageVersion"] = [RCTHotUpdate packageVersion];
|
||||||
ret[@"currentVersion"] = [packageInfo objectForKey:paramCurrentVersion];
|
ret[@"isRolledBack"] = [defaults objectForKey:keyRolledBackMarked];
|
||||||
ret[@"isFirstTime"] = [packageInfo objectForKey:paramIsFirstTime];
|
NSDictionary *updateInfo = [defaults dictionaryForKey:keyUpdateInfo];
|
||||||
ret[@"isRolledBack"] = [packageInfo objectForKey:paramIsRolledBack];
|
ret[@"currentVersion"] = [updateInfo objectForKey:paramCurrentVersion];
|
||||||
|
ret[@"isFirstTime"] = [updateInfo objectForKey:paramIsFirstTime];
|
||||||
|
|
||||||
if (packageInfo) {
|
// clear isFirstTime
|
||||||
// clear isFirstTime and isRolledBack
|
if (updateInfo) {
|
||||||
packageInfo[paramIsFirstTime] = @(NO);
|
NSMutableDictionary *newInfo = [updateInfo mutableCopy];
|
||||||
packageInfo[paramIsRolledBack] = @(NO);
|
newInfo[paramIsFirstTime] = @(NO);
|
||||||
[defaults setObject:packageInfo forKey:rnHotUpdatePackageInfoKey];
|
[defaults setObject:newInfo forKey:keyUpdateInfo];
|
||||||
[defaults synchronize];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// clear rolledbackmark
|
||||||
|
if ([[defaults objectForKey:keyRolledBackMarked] boolValue]) {
|
||||||
|
[defaults setObject:nil forKey:keyRolledBackMarked];
|
||||||
|
[self clearInvalidFiles];
|
||||||
|
}
|
||||||
|
|
||||||
|
// clear packageupdatemarked
|
||||||
|
if ([[defaults objectForKey:KeyPackageUpdatedMarked] boolValue]) {
|
||||||
|
[defaults setObject:nil forKey:KeyPackageUpdatedMarked];
|
||||||
|
[self clearInvalidFiles];
|
||||||
|
}
|
||||||
|
[defaults synchronize];
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -178,7 +209,22 @@ RCT_EXPORT_METHOD(setNeedUpdate:(NSDictionary *)options)
|
|||||||
{
|
{
|
||||||
NSString *hashName = options[@"hashName"];
|
NSString *hashName = options[@"hashName"];
|
||||||
if (hashName.length) {
|
if (hashName.length) {
|
||||||
[self saveCurVersion:hashName];
|
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
|
||||||
|
NSString *lastVersion = nil;
|
||||||
|
if ([defaults objectForKey:keyUpdateInfo]) {
|
||||||
|
NSDictionary *updateInfo = [defaults objectForKey:keyUpdateInfo];
|
||||||
|
lastVersion = updateInfo[paramCurrentVersion];
|
||||||
|
}
|
||||||
|
|
||||||
|
NSMutableDictionary *newInfo = [@{} mutableCopy];
|
||||||
|
newInfo[paramCurrentVersion] = hashName;
|
||||||
|
newInfo[paramLastVersion] = lastVersion;
|
||||||
|
newInfo[paramIsFirstTime] = @(YES);
|
||||||
|
newInfo[paramIsFirstLoadOk] = @(NO);
|
||||||
|
newInfo[paramPackageVersion] = [RCTHotUpdate packageVersion];
|
||||||
|
[defaults setObject:newInfo forKey:keyUpdateInfo];
|
||||||
|
|
||||||
|
[defaults synchronize];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -186,7 +232,9 @@ RCT_EXPORT_METHOD(reloadUpdate:(NSDictionary *)options)
|
|||||||
{
|
{
|
||||||
NSString *hashName = options[@"hashName"];
|
NSString *hashName = options[@"hashName"];
|
||||||
if (hashName.length) {
|
if (hashName.length) {
|
||||||
[self saveCurVersion:hashName];
|
[self setNeedUpdate:options];
|
||||||
|
|
||||||
|
// reload
|
||||||
dispatch_async(dispatch_get_main_queue(), ^{
|
dispatch_async(dispatch_get_main_queue(), ^{
|
||||||
[_bridge setValue:[[self class] bundleURL] forKey:@"bundleURL"];
|
[_bridge setValue:[[self class] bundleURL] forKey:@"bundleURL"];
|
||||||
[_bridge reload];
|
[_bridge reload];
|
||||||
@ -198,29 +246,14 @@ RCT_EXPORT_METHOD(markSuccuss)
|
|||||||
{
|
{
|
||||||
// update package info
|
// update package info
|
||||||
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
|
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
|
||||||
NSMutableDictionary *packageInfo = [[defaults objectForKey:rnHotUpdatePackageInfoKey] mutableCopy];
|
NSMutableDictionary *packageInfo = [[defaults objectForKey:keyUpdateInfo] mutableCopy];
|
||||||
[packageInfo setObject:@(NO) forKey:paramIsFirstTime];
|
[packageInfo setObject:@(NO) forKey:paramIsFirstTime];
|
||||||
[packageInfo setObject:@(YES) forKey:paramIsFirstLoadOk];
|
[packageInfo setObject:@(YES) forKey:paramIsFirstLoadOk];
|
||||||
[packageInfo setObject:@(NO) forKey:paramIsRolledBack];
|
[defaults setObject:packageInfo forKey:keyUpdateInfo];
|
||||||
|
|
||||||
[defaults setObject:packageInfo forKey:rnHotUpdatePackageInfoKey];
|
|
||||||
[defaults synchronize];
|
[defaults synchronize];
|
||||||
|
|
||||||
// clear other package dir
|
// clear other package dir
|
||||||
NSString *downloadDir = [RCTHotUpdate downloadDir];
|
[self clearInvalidFiles];
|
||||||
NSString *curVersion = [packageInfo objectForKey:paramCurrentVersion];
|
|
||||||
|
|
||||||
NSError *error = nil;
|
|
||||||
NSArray *list = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:downloadDir error:&error];
|
|
||||||
if (error) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
for(NSString *fileName in list) {
|
|
||||||
if (![fileName isEqualToString:curVersion]) {
|
|
||||||
[_fileManager removeFile:curVersion completionHandler:nil];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#pragma mark - private
|
#pragma mark - private
|
||||||
@ -335,6 +368,26 @@ RCT_EXPORT_METHOD(markSuccuss)
|
|||||||
}];
|
}];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (void)clearInvalidFiles
|
||||||
|
{
|
||||||
|
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
|
||||||
|
NSDictionary *updateInfo = [defaults objectForKey:keyUpdateInfo];
|
||||||
|
NSString *curVersion = [updateInfo objectForKey:paramCurrentVersion];
|
||||||
|
|
||||||
|
NSString *downloadDir = [RCTHotUpdate downloadDir];
|
||||||
|
NSError *error = nil;
|
||||||
|
NSArray *list = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:downloadDir error:&error];
|
||||||
|
if (error) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for(NSString *fileName in list) {
|
||||||
|
if (![fileName isEqualToString:curVersion]) {
|
||||||
|
[_fileManager removeFile:curVersion completionHandler:nil];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
- (NSString *)zipExtension:(HotUpdateType)type
|
- (NSString *)zipExtension:(HotUpdateType)type
|
||||||
{
|
{
|
||||||
switch (type) {
|
switch (type) {
|
||||||
@ -354,23 +407,6 @@ RCT_EXPORT_METHOD(markSuccuss)
|
|||||||
reject([NSString stringWithFormat: @"%lu", (long)error.code], error.localizedDescription, error);
|
reject([NSString stringWithFormat: @"%lu", (long)error.code], error.localizedDescription, error);
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)saveCurVersion:(NSString *)hashCode
|
|
||||||
{
|
|
||||||
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
|
|
||||||
NSMutableDictionary *packageInfo = [[defaults objectForKey:rnHotUpdatePackageInfoKey] mutableCopy];
|
|
||||||
if (packageInfo == nil) {
|
|
||||||
packageInfo = [@{} mutableCopy];
|
|
||||||
}
|
|
||||||
packageInfo[paramLastVersion] = packageInfo[paramCurrentVersion];
|
|
||||||
packageInfo[paramCurrentVersion] = hashCode;
|
|
||||||
packageInfo[paramIsFirstTime] = @(YES);
|
|
||||||
packageInfo[paramIsFirstLoadOk] = @(NO);
|
|
||||||
packageInfo[paramIsRolledBack] = @(NO);
|
|
||||||
|
|
||||||
[defaults setObject:packageInfo forKey:rnHotUpdatePackageInfoKey];
|
|
||||||
[defaults synchronize];
|
|
||||||
}
|
|
||||||
|
|
||||||
- (NSError *)errorWithMessage:(NSString *)errorMessage
|
- (NSError *)errorWithMessage:(NSString *)errorMessage
|
||||||
{
|
{
|
||||||
return [NSError errorWithDomain:@"cn.reactnative.hotupdate"
|
return [NSError errorWithDomain:@"cn.reactnative.hotupdate"
|
||||||
@ -392,4 +428,16 @@ RCT_EXPORT_METHOD(markSuccuss)
|
|||||||
return url;
|
return url;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
+ (NSString *)packageVersion
|
||||||
|
{
|
||||||
|
static NSString *version = nil;
|
||||||
|
|
||||||
|
static dispatch_once_t onceToken;
|
||||||
|
dispatch_once(&onceToken, ^{
|
||||||
|
NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
|
||||||
|
version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
|
||||||
|
});
|
||||||
|
return version;
|
||||||
|
}
|
||||||
|
|
||||||
@end
|
@end
|
Loading…
Reference in New Issue
Block a user