1
0
Code Issues Pull Requests Packages Projects Releases Wiki Activity GitHub Gitee
react-native-pushy/ios/RCTHotUpdate/RCTHotUpdate.m

455 lines
17 KiB
Mathematica
Raw Normal View History

2016-02-23 17:31:47 +08:00
//
// RCTHotUpdate.m
// RCTHotUpdate
//
// Created by LvBingru on 2/19/16.
// Copyright © 2016 erica. All rights reserved.
//
#import "RCTHotUpdate.h"
#import "RCTHotUpdateDownloader.h"
#import "RCTEventDispatcher.h"
#import "RCTConvert.h"
2016-04-02 14:24:33 +08:00
#import "RCTHotUpdateManager.h"
#import "RCTLog.h"
2016-02-23 17:31:47 +08:00
2016-04-02 14:24:33 +08:00
//
2016-04-05 22:58:50 +08:00
static NSString *const keyUpdateInfo = @"REACTNATIVECN_HOTUPDATE_INFO_KEY";
static NSString *const paramPackageVersion = @"packageVersion";
2016-04-05 17:51:02 +08:00
static NSString *const paramLastVersion = @"lastVersion";
static NSString *const paramCurrentVersion = @"currentVersion";
static NSString *const paramIsFirstTime = @"isFirstTime";
static NSString *const paramIsFirstLoadOk = @"isFirstLoadOK";
2016-04-06 09:42:34 +08:00
static NSString *const keyFirstLoadLoadMarked = @"REACTNATIVECN_HOTUPDATE_FIRSTLOADMARKED_KEY";
2016-04-05 22:58:50 +08:00
static NSString *const keyRolledBackMarked = @"REACTNATIVECN_HOTUPDATE_ROLLEDBACKMARKED_KEY";
static NSString *const KeyPackageUpdatedMarked = @"REACTNATIVECN_HOTUPDATE_ISPACKAGEUPDATEDMARKED_KEY";
2016-02-23 19:51:58 +08:00
2016-04-02 14:24:33 +08:00
// app info
static NSString * const AppVersionKey = @"appVersion";
static NSString * const BuildVersionKey = @"buildVersion";
// file def
static NSString * const BUNDLE_FILE_NAME = @"index.bundlejs";
static NSString * const SOURCE_PATCH_NAME = @"__diff.json";
static NSString * const BUNDLE_PATCH_NAME = @"index.bundlejs.patch";
// error def
static NSString * const ERROR_OPTIONS = @"options error";
static NSString * const ERROR_BSDIFF = @"bsdiff error";
static NSString * const ERROR_FILE_OPERATION = @"file operation error";
// event def
static NSString * const EVENT_PROGRESS_DOWNLOAD = @"RCTHotUpdateDownloadProgress";
static NSString * const EVENT_PROGRESS_UNZIP = @"RCTHotUpdateUnzipProgress";
2016-04-06 09:42:34 +08:00
static NSString * const PARAM_PROGRESS_HASHNAME = @"hashname";
2016-04-02 14:24:33 +08:00
static NSString * const PARAM_PROGRESS_RECEIVED = @"received";
static NSString * const PARAM_PROGRESS_TOTAL = @"total";
typedef NS_ENUM(NSInteger, HotUpdateType) {
HotUpdateTypeFullDownload = 1,
2016-04-05 17:51:02 +08:00
HotUpdateTypePatchFromPackage = 2,
HotUpdateTypePatchFromPpk = 3,
2016-04-02 14:24:33 +08:00
};
@implementation RCTHotUpdate {
RCTHotUpdateManager *_fileManager;
}
2016-02-23 17:31:47 +08:00
@synthesize bridge = _bridge;
@synthesize methodQueue = _methodQueue;
RCT_EXPORT_MODULE(RCTHotUpdate);
2016-04-05 17:51:02 +08:00
+ (NSURL *)bundleURL
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
2016-04-05 22:58:50 +08:00
NSDictionary *updateInfo = [defaults dictionaryForKey:keyUpdateInfo];
if (updateInfo) {
NSString *curPackageVersion = [RCTHotUpdate packageVersion];
NSString *packageVersion = [updateInfo objectForKey:paramPackageVersion];
2016-04-05 17:51:02 +08:00
2016-04-05 22:58:50 +08:00
BOOL needClearUpdateInfo = ![curPackageVersion isEqualToString:packageVersion];
if (needClearUpdateInfo) {
[defaults setObject:nil forKey:keyUpdateInfo];
[defaults setObject:@(YES) forKey:KeyPackageUpdatedMarked];
2016-04-05 17:51:02 +08:00
[defaults synchronize];
2016-04-05 22:58:50 +08:00
// ...need clear files later
2016-04-05 17:51:02 +08:00
}
2016-04-05 22:58:50 +08:00
else {
NSString *curVersion = updateInfo[paramCurrentVersion];
NSString *lastVersion = updateInfo[paramLastVersion];
BOOL isFirstTime = [updateInfo[paramIsFirstTime] boolValue];
BOOL isFirstLoadOK = [updateInfo[paramIsFirstLoadOk] boolValue];
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
}
2016-04-06 09:42:34 +08:00
else if (isFirstTime){
NSMutableDictionary *newInfo = [updateInfo mutableCopy];
newInfo[paramIsFirstTime] = @(NO);
[defaults setObject:newInfo forKey:keyUpdateInfo];
[defaults setObject:@(YES) forKey:keyFirstLoadLoadMarked];
[defaults synchronize];
}
2016-04-05 17:51:02 +08:00
2016-04-05 22:58:50 +08:00
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;
}
2016-04-05 17:51:02 +08:00
}
}
}
return [RCTHotUpdate binaryBundleURL];
}
2016-02-23 17:31:47 +08:00
- (NSDictionary *)constantsToExport
{
2016-04-05 17:51:02 +08:00
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
2016-04-05 17:58:12 +08:00
NSMutableDictionary *ret = [@{} mutableCopy];
ret[@"downloadRootDir"] = [RCTHotUpdate downloadDir];
2016-04-05 22:58:50 +08:00
ret[@"packageVersion"] = [RCTHotUpdate packageVersion];
ret[@"isRolledBack"] = [defaults objectForKey:keyRolledBackMarked];
2016-04-06 09:42:34 +08:00
ret[@"isFirstTime"] = [defaults objectForKey:keyFirstLoadLoadMarked];
2016-04-05 22:58:50 +08:00
NSDictionary *updateInfo = [defaults dictionaryForKey:keyUpdateInfo];
ret[@"currentVersion"] = [updateInfo objectForKey:paramCurrentVersion];
2016-04-05 17:58:12 +08:00
2016-04-05 22:58:50 +08:00
// clear isFirstTime
2016-04-06 09:42:34 +08:00
if ([[defaults objectForKey:keyFirstLoadLoadMarked] boolValue]) {
[defaults setObject:nil forKey:keyFirstLoadLoadMarked];
2016-04-05 22:58:50 +08:00
}
// clear rolledbackmark
if ([[defaults objectForKey:keyRolledBackMarked] boolValue]) {
[defaults setObject:nil forKey:keyRolledBackMarked];
[self clearInvalidFiles];
2016-04-05 17:51:02 +08:00
}
2016-04-05 22:58:50 +08:00
// clear packageupdatemarked
if ([[defaults objectForKey:KeyPackageUpdatedMarked] boolValue]) {
[defaults setObject:nil forKey:KeyPackageUpdatedMarked];
[self clearInvalidFiles];
}
[defaults synchronize];
2016-04-05 17:51:02 +08:00
return ret;
2016-02-23 17:31:47 +08:00
}
- (instancetype)init
{
self = [super init];
if (self) {
2016-04-02 14:24:33 +08:00
_fileManager = [RCTHotUpdateManager new];
2016-02-23 17:31:47 +08:00
}
return self;
}
2016-04-02 14:24:33 +08:00
RCT_EXPORT_METHOD(downloadUpdate:(NSDictionary *)options
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
2016-02-23 17:31:47 +08:00
{
2016-04-02 14:24:33 +08:00
[self hotUpdate:HotUpdateTypeFullDownload options:options callback:^(NSError *error) {
if (error) {
[self reject:reject error:error];
}
else {
resolve(nil);
}
}];
2016-02-23 17:31:47 +08:00
}
2016-04-05 17:51:02 +08:00
RCT_EXPORT_METHOD(downloadPatchFromPackage:(NSDictionary *)options
2016-04-02 14:24:33 +08:00
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
2016-02-23 17:31:47 +08:00
{
2016-04-05 17:51:02 +08:00
[self hotUpdate:HotUpdateTypePatchFromPackage options:options callback:^(NSError *error) {
2016-02-23 17:31:47 +08:00
if (error) {
2016-04-02 14:24:33 +08:00
[self reject:reject error:error];
2016-02-23 17:31:47 +08:00
}
else {
2016-04-02 14:24:33 +08:00
resolve(nil);
}
}];
}
2016-04-05 17:51:02 +08:00
RCT_EXPORT_METHOD(downloadPatchFromPpk:(NSDictionary *)options
2016-04-02 14:24:33 +08:00
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
{
2016-04-05 17:51:02 +08:00
[self hotUpdate:HotUpdateTypePatchFromPpk options:options callback:^(NSError *error) {
2016-04-02 14:24:33 +08:00
if (error) {
[self reject:reject error:error];
}
else {
resolve(nil);
2016-02-23 17:31:47 +08:00
}
}];
}
RCT_EXPORT_METHOD(setNeedUpdate:(NSDictionary *)options)
{
2016-02-23 19:51:58 +08:00
NSString *hashName = options[@"hashName"];
if (hashName.length) {
2016-04-05 22:58:50 +08:00
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];
2016-02-23 19:51:58 +08:00
}
2016-02-23 17:31:47 +08:00
}
RCT_EXPORT_METHOD(reloadUpdate:(NSDictionary *)options)
{
2016-02-23 19:51:58 +08:00
NSString *hashName = options[@"hashName"];
if (hashName.length) {
2016-04-05 22:58:50 +08:00
[self setNeedUpdate:options];
// reload
2016-02-23 19:51:58 +08:00
dispatch_async(dispatch_get_main_queue(), ^{
[_bridge setValue:[[self class] bundleURL] forKey:@"bundleURL"];
[_bridge reload];
});
}
2016-02-23 17:31:47 +08:00
}
2016-04-06 10:21:00 +08:00
RCT_EXPORT_METHOD(markSuccess)
2016-02-23 17:31:47 +08:00
{
2016-04-05 17:51:02 +08:00
// update package info
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
2016-04-05 22:58:50 +08:00
NSMutableDictionary *packageInfo = [[defaults objectForKey:keyUpdateInfo] mutableCopy];
2016-04-05 17:51:02 +08:00
[packageInfo setObject:@(NO) forKey:paramIsFirstTime];
[packageInfo setObject:@(YES) forKey:paramIsFirstLoadOk];
2016-04-05 22:58:50 +08:00
[defaults setObject:packageInfo forKey:keyUpdateInfo];
2016-04-05 17:51:02 +08:00
[defaults synchronize];
// clear other package dir
2016-04-05 22:58:50 +08:00
[self clearInvalidFiles];
2016-02-23 17:31:47 +08:00
}
2016-04-02 14:24:33 +08:00
#pragma mark - private
- (void)hotUpdate:(HotUpdateType)type options:(NSDictionary *)options callback:(void (^)(NSError *error))callback
2016-02-23 17:31:47 +08:00
{
2016-04-02 14:24:33 +08:00
NSString *updateUrl = [RCTConvert NSString:options[@"updateUrl"]];
NSString *hashName = [RCTConvert NSString:options[@"hashName"]];
if (updateUrl.length<=0 || hashName.length<=0) {
callback([self errorWithMessage:ERROR_OPTIONS]);
return;
}
NSString *originHashName = [RCTConvert NSString:options[@"originHashName"]];
2016-04-05 17:51:02 +08:00
if (type == HotUpdateTypePatchFromPpk && originHashName<=0) {
2016-04-02 14:24:33 +08:00
callback([self errorWithMessage:ERROR_OPTIONS]);
return;
2016-02-23 17:31:47 +08:00
}
2016-04-05 17:51:02 +08:00
NSString *dir = [RCTHotUpdate downloadDir];
2016-04-02 14:24:33 +08:00
BOOL success = [_fileManager createDir:dir];
if (!success) {
callback([self errorWithMessage:ERROR_FILE_OPERATION]);
return;
}
2016-04-04 23:22:09 +08:00
NSString *zipFilePath = [dir stringByAppendingPathComponent:[NSString stringWithFormat:@"%@%@",hashName, [self zipExtension:type]]];
2016-04-02 14:24:33 +08:00
NSString *unzipDir = [dir stringByAppendingPathComponent:hashName];
RCTLogInfo(@"RNUpdate -- download file %@", updateUrl);
[RCTHotUpdateDownloader download:updateUrl savePath:zipFilePath progressHandler:^(long long receivedBytes, long long totalBytes) {
[self.bridge.eventDispatcher sendAppEventWithName:EVENT_PROGRESS_DOWNLOAD
body:@{
2016-04-06 09:42:34 +08:00
PARAM_PROGRESS_HASHNAME:hashName,
2016-04-02 14:24:33 +08:00
PARAM_PROGRESS_RECEIVED:[NSNumber numberWithLongLong:receivedBytes],
PARAM_PROGRESS_TOTAL:[NSNumber numberWithLongLong:totalBytes]
}];
} completionHandler:^(NSString *path, NSError *error) {
if (error) {
callback(error);
}
else {
RCTLogInfo(@"RNUpdate -- unzip file %@", zipFilePath);
NSString *unzipFilePath = [dir stringByAppendingPathComponent:hashName];
2016-04-04 23:22:09 +08:00
[_fileManager unzipFileAtPath:zipFilePath toDestination:unzipFilePath progressHandler:^(NSString *entry,long entryNumber, long total) {
2016-04-02 14:24:33 +08:00
[self.bridge.eventDispatcher sendAppEventWithName:EVENT_PROGRESS_UNZIP
body:@{
2016-04-06 09:42:34 +08:00
PARAM_PROGRESS_HASHNAME:hashName,
2016-04-02 14:24:33 +08:00
PARAM_PROGRESS_RECEIVED:[NSNumber numberWithLong:entryNumber],
PARAM_PROGRESS_TOTAL:[NSNumber numberWithLong:total]
}];
} completionHandler:^(NSString *path, BOOL succeeded, NSError *error) {
dispatch_async(_methodQueue, ^{
if (error) {
callback(error);
}
else {
switch (type) {
2016-04-05 17:51:02 +08:00
case HotUpdateTypePatchFromPackage:
2016-04-02 14:24:33 +08:00
{
2016-04-05 15:25:45 +08:00
NSString *sourceOrigin = [[NSBundle mainBundle] resourcePath];
NSString *bundleOrigin = [[RCTHotUpdate binaryBundleURL] path];
2016-04-06 10:34:11 +08:00
[self patch:hashName fromBundle:bundleOrigin source:sourceOrigin callback:callback];
2016-04-02 14:24:33 +08:00
}
break;
2016-04-05 17:51:02 +08:00
case HotUpdateTypePatchFromPpk:
2016-04-02 14:24:33 +08:00
{
NSString *lastVertionDir = [dir stringByAppendingPathComponent:originHashName];
2016-04-05 15:25:45 +08:00
NSString *sourceOrigin = lastVertionDir;
2016-04-02 14:24:33 +08:00
NSString *bundleOrigin = [lastVertionDir stringByAppendingPathComponent:BUNDLE_FILE_NAME];
2016-04-06 10:34:11 +08:00
[self patch:hashName fromBundle:bundleOrigin source:sourceOrigin callback:callback];
2016-04-02 14:24:33 +08:00
}
break;
default:
callback(nil);
break;
}
}
});
}];
}
}];
2016-02-23 17:31:47 +08:00
}
2016-04-06 10:34:11 +08:00
- (void)patch:(NSString *)hashName fromBundle:(NSString *)bundleOrigin source:(NSString *)sourceOrigin callback:(void (^)(NSError *error))callback
2016-02-23 17:31:47 +08:00
{
2016-04-05 17:51:02 +08:00
NSString *unzipDir = [[RCTHotUpdate downloadDir] stringByAppendingPathComponent:hashName];
2016-04-02 14:24:33 +08:00
NSString *sourcePatch = [unzipDir stringByAppendingPathComponent:SOURCE_PATCH_NAME];
NSString *bundlePatch = [unzipDir stringByAppendingPathComponent:BUNDLE_PATCH_NAME];
2016-02-23 17:31:47 +08:00
2016-04-02 14:24:33 +08:00
NSString *destination = [unzipDir stringByAppendingPathComponent:BUNDLE_FILE_NAME];
[_fileManager bsdiffFileAtPath:bundlePatch fromOrigin:bundleOrigin toDestination:destination completionHandler:^(BOOL success) {
if (success) {
NSData *data = [NSData dataWithContentsOfFile:sourcePatch];
NSError *error = nil;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
if (error) {
callback(error);
return;
}
2016-04-06 13:05:52 +08:00
2016-04-02 14:24:33 +08:00
NSDictionary *copies = json[@"copies"];
2016-04-06 13:05:52 +08:00
NSDictionary *deletes = json[@"deletes"];
[_fileManager copyFiles:copies fromDir:sourceOrigin toDir:unzipDir deletes:deletes completionHandler:^(NSError *error) {
2016-04-02 14:24:33 +08:00
if (error) {
callback(error);
}
else {
callback(nil);
}
}];
2016-02-23 17:31:47 +08:00
}
2016-04-02 14:24:33 +08:00
else {
callback([self errorWithMessage:ERROR_BSDIFF]);
}
}];
}
2016-04-05 22:58:50 +08:00
- (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]) {
2016-04-06 13:05:52 +08:00
[_fileManager removeFile:[downloadDir stringByAppendingPathComponent:fileName] completionHandler:nil];
2016-04-05 22:58:50 +08:00
}
}
}
2016-04-02 14:24:33 +08:00
- (NSString *)zipExtension:(HotUpdateType)type
{
switch (type) {
case HotUpdateTypeFullDownload:
return @".ppk";
2016-04-05 17:51:02 +08:00
case HotUpdateTypePatchFromPackage:
2016-04-02 14:24:33 +08:00
return @".apk.patch";
2016-04-05 17:51:02 +08:00
case HotUpdateTypePatchFromPpk:
2016-04-02 14:24:33 +08:00
return @".ppk.patch";
default:
break;
2016-02-23 17:31:47 +08:00
}
2016-04-02 14:24:33 +08:00
}
- (void)reject:(RCTPromiseRejectBlock)reject error:(NSError *)error
{
reject([NSString stringWithFormat: @"%lu", (long)error.code], error.localizedDescription, error);
2016-02-23 17:31:47 +08:00
}
2016-04-02 14:24:33 +08:00
- (NSError *)errorWithMessage:(NSString *)errorMessage
{
return [NSError errorWithDomain:@"cn.reactnative.hotupdate"
code:-1
2016-04-05 17:51:02 +08:00
userInfo:@{ NSLocalizedDescriptionKey: errorMessage}];
2016-04-02 14:24:33 +08:00
}
2016-04-05 17:51:02 +08:00
+ (NSString *)downloadDir
2016-04-02 14:24:33 +08:00
{
NSString *directory = [NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES) firstObject];
NSString *downloadDir = [directory stringByAppendingPathComponent:@"reactnativecnhotupdate"];
return downloadDir;
}
+ (NSURL *)binaryBundleURL
{
NSURL *url = [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
return url;
}
2016-04-05 22:58:50 +08:00
+ (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;
}
2016-04-02 14:24:33 +08:00
@end