Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Commit b773e86

Browse files
committed
save resource name and extension instead
1 parent cb00cd1 commit b773e86

File tree

1 file changed

+31
-14
lines changed

1 file changed

+31
-14
lines changed

CodePush.m

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ @implementation CodePush {
1818
static BOOL needToReportRollback = NO;
1919
static BOOL isRunningBinaryVersion = NO;
2020
static BOOL testConfigurationFlag = NO;
21-
static NSString *binaryBundleDate = nil;
2221

2322
// These constants represent valid deployment statuses
2423
static NSString *const DeploymentFailed = @"DeploymentFailed";
@@ -39,26 +38,39 @@ @implementation CodePush {
3938
static NSString *const PackageHashKey = @"packageHash";
4039
static NSString *const PackageIsPendingKey = @"isPending";
4140

41+
// These values are used to save the bundleURL and extension for the JS bundle
42+
// in the binary.
43+
static NSString *binaryJsName = @"main";
44+
static NSString *binaryJsExtension = @"jsbundle";
45+
46+
4247
#pragma mark - Public Obj-C API
4348

49+
+ (NSURL *)binaryJsBundleUrl
50+
{
51+
return [[NSBundle mainBundle] URLForResource:binaryJsName withExtension:binaryJsExtension];
52+
}
53+
4454
+ (NSURL *)bundleURL
4555
{
46-
return [self bundleURLForResource:@"main"];
56+
return [self bundleURLForResource:binaryJsName];
4757
}
4858

4959
+ (NSURL *)bundleURLForResource:(NSString *)resourceName
5060
{
61+
binaryJsName = resourceName;
5162
return [self bundleURLForResource:resourceName
52-
withExtension:@"jsbundle"];
63+
withExtension:binaryJsExtension];
5364
}
5465

5566
+ (NSURL *)bundleURLForResource:(NSString *)resourceName
5667
withExtension:(NSString *)resourceExtension
5768
{
69+
binaryJsName = resourceName;
70+
binaryJsExtension = resourceExtension;
5871
NSError *error;
5972
NSString *packageFile = [CodePushPackage getCurrentPackageBundlePath:&error];
60-
NSURL *binaryJsBundleUrl = [[NSBundle mainBundle] URLForResource:resourceName withExtension:resourceExtension];
61-
[self setBinaryBundleDate:binaryJsBundleUrl];
73+
NSURL *binaryJsBundleUrl = [self binaryJsBundleUrl];
6274

6375
NSString *logMessageFormat = @"Loading JS bundle from %@";
6476

@@ -79,7 +91,7 @@ + (NSURL *)bundleURLForResource:(NSString *)resourceName
7991
NSString *packageDate = [currentPackageMetadata objectForKey:BinaryBundleDateKey];
8092
NSString *packageAppVersion = [currentPackageMetadata objectForKey:@"appVersion"];
8193

82-
if ([binaryBundleDate isEqualToString:packageDate] && ([CodePush isUsingTestConfiguration] ||[binaryAppVersion isEqualToString:packageAppVersion])) {
94+
if ([[self modifiedDateStringFromFileUrl:binaryJsBundleUrl] isEqualToString:packageDate] && ([CodePush isUsingTestConfiguration] ||[binaryAppVersion isEqualToString:packageAppVersion])) {
8395
// Return package file because it is newer than the app store binary's JS bundle
8496
NSURL *packageUrl = [[NSURL alloc] initFileURLWithPath:packageFile];
8597
NSLog(logMessageFormat, packageUrl);
@@ -112,13 +124,17 @@ + (BOOL)isUsingTestConfiguration
112124
}
113125

114126
/*
115-
* This caches the binary's jsbundle modified date in memory as a string.
127+
* This returns the modified date as a string for a given file URL.
116128
*/
117-
+ (void)setBinaryBundleDate:(NSURL *)binaryJsBundleUrl
129+
+ (NSString *)modifiedDateStringFromFileUrl:(NSURL *)fileUrl
118130
{
119-
NSDictionary *binaryFileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:[binaryJsBundleUrl path] error:nil];
120-
NSDate *binaryDate = [binaryFileAttributes objectForKey:NSFileModificationDate];
121-
binaryBundleDate = [NSString stringWithFormat:@"%f", [binaryDate timeIntervalSince1970]];
131+
if (fileUrl != nil) {
132+
NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:[fileUrl path] error:nil];
133+
NSDate *modifiedDate = [fileAttributes objectForKey:NSFileModificationDate];
134+
return [NSString stringWithFormat:@"%f", [modifiedDate timeIntervalSince1970]];
135+
} else {
136+
return nil;
137+
}
122138
}
123139

124140
+ (void)setDeploymentKey:(NSString *)deploymentKey
@@ -377,9 +393,10 @@ - (void)savePendingUpdate:(NSString *)packageHash
377393
rejecter:(RCTPromiseRejectBlock)reject)
378394
{
379395
dispatch_async(dispatch_get_main_queue(), ^{
380-
NSDictionary* mutableUpdatePackage = [updatePackage mutableCopy];
381-
if (binaryBundleDate != nil) {
382-
[mutableUpdatePackage setValue:binaryBundleDate
396+
NSDictionary *mutableUpdatePackage = [updatePackage mutableCopy];
397+
NSURL *binaryJsBundleUrl = [CodePush binaryJsBundleUrl];
398+
if (binaryJsBundleUrl != nil) {
399+
[mutableUpdatePackage setValue:[CodePush modifiedDateStringFromFileUrl:binaryJsBundleUrl]
383400
forKey:BinaryBundleDateKey];
384401
}
385402

0 commit comments

Comments
 (0)