diff --git a/ios/RCTHotUpdate/RCTHotUpdate.m b/ios/RCTHotUpdate/RCTHotUpdate.m index 2fd4619..1a9afa6 100644 --- a/ios/RCTHotUpdate/RCTHotUpdate.m +++ b/ios/RCTHotUpdate/RCTHotUpdate.m @@ -361,8 +361,11 @@ RCT_EXPORT_METHOD(markSuccess) callback(error); return; } + NSDictionary *copies = json[@"copies"]; - [_fileManager copyFiles:copies fromDir:sourceOrigin toDir:unzipDir completionHandler:^(NSError *error) { + NSDictionary *deletes = json[@"deletes"]; + + [_fileManager copyFiles:copies fromDir:sourceOrigin toDir:unzipDir deletes:deletes completionHandler:^(NSError *error) { if (error) { callback(error); } @@ -392,7 +395,7 @@ RCT_EXPORT_METHOD(markSuccess) for(NSString *fileName in list) { if (![fileName isEqualToString:curVersion]) { - [_fileManager removeFile:curVersion completionHandler:nil]; + [_fileManager removeFile:[downloadDir stringByAppendingPathComponent:fileName] completionHandler:nil]; } } } diff --git a/ios/RCTHotUpdate/RCTHotUpdateManager.h b/ios/RCTHotUpdate/RCTHotUpdateManager.h index 94425ce..b80ed09 100644 --- a/ios/RCTHotUpdate/RCTHotUpdateManager.h +++ b/ios/RCTHotUpdate/RCTHotUpdateManager.h @@ -26,6 +26,7 @@ - (void)copyFiles:(NSDictionary *)filesDic fromDir:(NSString *)fromDir toDir:(NSString *)toDir + deletes:(NSDictionary *)deletes completionHandler:(void (^)(NSError *error))completionHandler; - (void)removeFile:(NSString *)filePath diff --git a/ios/RCTHotUpdate/RCTHotUpdateManager.m b/ios/RCTHotUpdate/RCTHotUpdateManager.m index a89a04f..bada051 100644 --- a/ios/RCTHotUpdate/RCTHotUpdateManager.m +++ b/ios/RCTHotUpdate/RCTHotUpdateManager.m @@ -91,9 +91,27 @@ - (void)copyFiles:(NSDictionary *)filesDic fromDir:(NSString *)fromDir toDir:(NSString *)toDir + deletes:(NSDictionary *)deletes completionHandler:(void (^)(NSError *error))completionHandler { dispatch_async(_opQueue, ^{ + NSFileManager *fm = [NSFileManager defaultManager]; + + // merge old files + if (deletes!= nil) { + NSError *error = nil; + NSString *srcDir = [fromDir stringByAppendingPathComponent:@"assets"]; + NSString *desDir = [toDir stringByAppendingPathComponent:@"assets"]; + [self _mergeContentsOfPath:srcDir intoPath:desDir deletes:deletes error:&error]; + if (error) { + if (completionHandler) { + completionHandler(error); + } + return; + } + } + + // copy files for (NSString *to in filesDic.allKeys) { NSString *from = filesDic[to]; if (from.length <=0) { @@ -102,8 +120,11 @@ completionHandler:(void (^)(NSError *error))completionHandler NSString *fromPath = [fromDir stringByAppendingPathComponent:from]; NSString *toPath = [toDir stringByAppendingPathComponent:to]; + if ([fm fileExistsAtPath:toPath]) { + [fm removeItemAtPath:toPath error:nil]; + } NSError *error = nil; - [[NSFileManager defaultManager] copyItemAtPath:fromPath toPath:toPath error:&error]; + [fm copyItemAtPath:fromPath toPath:toPath error:&error]; if (error) { if (completionHandler) { completionHandler(error); @@ -129,4 +150,44 @@ completionHandler:(void (^)(NSError *error))completionHandler }); } +- (void)_mergeContentsOfPath:(NSString *)srcDir intoPath:(NSString *)dstDir deletes:(NSDictionary *)deletes error:(NSError**)err +{ + NSFileManager *fm = [NSFileManager defaultManager]; + NSDirectoryEnumerator *srcDirEnum = [fm enumeratorAtPath:srcDir]; + NSString *subPath; + while ((subPath = [srcDirEnum nextObject])) { + + NSString *srcFullPath = [srcDir stringByAppendingPathComponent:subPath]; + NSString *potentialDstPath = [dstDir stringByAppendingPathComponent:subPath]; + + BOOL isDirectory = ([fm fileExistsAtPath:srcFullPath isDirectory:&isDirectory] && isDirectory); + if (isDirectory) { + if (![fm fileExistsAtPath:potentialDstPath isDirectory:nil]) { + [fm createDirectoryAtPath:potentialDstPath withIntermediateDirectories:YES attributes:nil error:err]; + if (err && *err) { + return; + } + } + } + else { + BOOL inDeletes = NO; + if (deletes) { + NSString *path = [@"assets" stringByAppendingPathComponent:subPath]; + for (NSString *del in deletes) { + if ([subPath isEqualToString:path]) { + inDeletes = YES; + break; + } + } + } + if (!inDeletes && ![fm fileExistsAtPath:potentialDstPath]) { + [fm copyItemAtPath:srcFullPath toPath:potentialDstPath error:err]; + if (err && *err) { + return; + } + } + } + } +} + @end