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

Commit 0420caa

Browse files
committed
Exposing deploymentKey to CodePush
1 parent d388ad2 commit 0420caa

File tree

2 files changed

+77
-63
lines changed

2 files changed

+77
-63
lines changed

CodePush.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* This method assumes that your JS bundle is named "main.jsbundle"
1313
* and therefore, if it isn't, you should use either the bundleURLForResource:
1414
* or bundleURLForResource:withExtension: methods to override that behavior.
15-
*/
15+
*/
1616
+ (NSURL *)bundleURL;
1717

1818
+ (NSURL *)bundleURLForResource:(NSString *)resourceName;
@@ -22,6 +22,13 @@
2222

2323
+ (NSString *)getApplicationSupportDirectory;
2424

25+
/*
26+
* This methods allows dynamically setting the app's
27+
* deployment key, in addition to setting it via
28+
* the Info.plist file's CodePushDeploymentKey setting.
29+
*/
30+
+ (void)setDeploymentKey:(NSString *)deploymentKey;
31+
2532
// The below methods are only used during tests.
2633
+ (BOOL)isUsingTestConfiguration;
2734
+ (void)setUsingTestConfiguration:(BOOL)shouldUseTestConfiguration;
@@ -62,7 +69,7 @@ failCallback:(void (^)(NSError *err))failCallback;
6269
@interface CodePushPackage : NSObject
6370

6471
+ (void)installPackage:(NSDictionary *)updatePackage
65-
error:(NSError **)error;
72+
error:(NSError **)error;
6673

6774
+ (NSDictionary *)getCurrentPackage:(NSError **)error;
6875
+ (NSString *)getCurrentPackageFolderPath:(NSError **)error;

CodePush.m

Lines changed: 68 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ @implementation CodePush {
1313

1414
RCT_EXPORT_MODULE()
1515

16+
#pragma mark - Private constants
17+
1618
static BOOL needToReportRollback = NO;
1719
static BOOL isRunningBinaryVersion = NO;
1820
static BOOL testConfigurationFlag = NO;
@@ -38,9 +40,8 @@ @implementation CodePush {
3840
static NSString *const PackageHashKey = @"packageHash";
3941
static NSString *const PackageIsPendingKey = @"isPending";
4042

41-
@synthesize bridge = _bridge;
43+
#pragma mark - Public Obj-C API
4244

43-
// Public Obj-C API (see header for method comments)
4445
+ (NSURL *)bundleURL
4546
{
4647
return [self bundleURLForResource:@"main"];
@@ -91,7 +92,7 @@ + (NSURL *)bundleURLForResource:(NSString *)resourceName
9192
#ifndef DEBUG
9293
[CodePush clearUpdates];
9394
#endif
94-
95+
9596
NSLog(logMessageFormat, binaryJsBundleUrl);
9697
isRunningBinaryVersion = YES;
9798
return binaryJsBundleUrl;
@@ -113,7 +114,12 @@ + (BOOL)isUsingTestConfiguration
113114
return testConfigurationFlag;
114115
}
115116

116-
/*
117+
+ (void)setDeploymentKey:(NSString *)deploymentKey
118+
{
119+
[CodePushConfig current].deploymentKey = deploymentKey;
120+
}
121+
122+
/*
117123
* This is used to enable an environment in which tests can be run.
118124
* Specifically, it flips a boolean flag that causes bundles to be
119125
* saved to a test folder and enables the ability to modify
@@ -134,8 +140,9 @@ + (void)clearUpdates
134140
[self removeFailedUpdates];
135141
}
136142

143+
#pragma mark - Private API methods
137144

138-
// Private API methods
145+
@synthesize bridge = _bridge;
139146

140147
/*
141148
* This method is used by the React Native bridge to allow
@@ -147,11 +154,11 @@ - (NSDictionary *)constantsToExport
147154
{
148155
// Export the values of the CodePushInstallMode enum
149156
// so that the script-side can easily stay in sync
150-
return @{
151-
@"codePushInstallModeOnNextRestart":@(CodePushInstallModeOnNextRestart),
152-
@"codePushInstallModeImmediate": @(CodePushInstallModeImmediate),
153-
@"codePushInstallModeOnNextResume": @(CodePushInstallModeOnNextResume)
154-
};
157+
return @{
158+
@"codePushInstallModeOnNextRestart":@(CodePushInstallModeOnNextRestart),
159+
@"codePushInstallModeImmediate": @(CodePushInstallModeImmediate),
160+
@"codePushInstallModeOnNextResume": @(CodePushInstallModeOnNextResume)
161+
};
155162
};
156163

157164
- (void)dealloc
@@ -257,12 +264,12 @@ - (BOOL)isPendingUpdate:(NSString*)packageHash
257264
{
258265
NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
259266
NSDictionary *pendingUpdate = [preferences objectForKey:PendingUpdateKey];
260-
267+
261268
// If there is a pending update whose "state" isn't loading, then we consider it "pending".
262269
// Additionally, if a specific hash was provided, we ensure it matches that of the pending update.
263270
BOOL updateIsPending = pendingUpdate &&
264-
[pendingUpdate[PendingUpdateIsLoadingKey] boolValue] == NO &&
265-
(!packageHash || [pendingUpdate[PendingUpdateHashKey] isEqualToString:packageHash]);
271+
[pendingUpdate[PendingUpdateIsLoadingKey] boolValue] == NO &&
272+
(!packageHash || [pendingUpdate[PendingUpdateHashKey] isEqualToString:packageHash]);
266273

267274
return updateIsPending;
268275
}
@@ -380,41 +387,41 @@ - (void)savePendingUpdate:(NSString *)packageHash
380387
[preferences synchronize];
381388
}
382389

383-
// JavaScript-exported module methods
390+
#pragma mark - JavaScript-exported module methods
384391

385392
/*
386393
* This is native-side of the RemotePackage.download method
387394
*/
388395
RCT_EXPORT_METHOD(downloadUpdate:(NSDictionary*)updatePackage
389-
resolver:(RCTPromiseResolveBlock)resolve
390-
rejecter:(RCTPromiseRejectBlock)reject)
396+
resolver:(RCTPromiseResolveBlock)resolve
397+
rejecter:(RCTPromiseRejectBlock)reject)
391398
{
392399
[CodePushPackage downloadPackage:updatePackage
393-
// The download is progressing forward
394-
progressCallback:^(long long expectedContentLength, long long receivedContentLength) {
395-
// Notify the script-side about the progress
396-
[self.bridge.eventDispatcher
397-
sendDeviceEventWithName:@"CodePushDownloadProgress"
398-
body:@{
399-
@"totalBytes":[NSNumber numberWithLongLong:expectedContentLength],
400-
@"receivedBytes":[NSNumber numberWithLongLong:receivedContentLength]
401-
}];
402-
}
403-
// The download completed
404-
doneCallback:^{
405-
NSError *err;
406-
NSDictionary *newPackage = [CodePushPackage getPackage:updatePackage[PackageHashKey] error:&err];
407-
408-
if (err) {
409-
return reject(err);
410-
}
411-
412-
resolve(newPackage);
413-
}
414-
// The download failed
415-
failCallback:^(NSError *err) {
416-
reject(err);
417-
}];
400+
// The download is progressing forward
401+
progressCallback:^(long long expectedContentLength, long long receivedContentLength) {
402+
// Notify the script-side about the progress
403+
[self.bridge.eventDispatcher
404+
sendDeviceEventWithName:@"CodePushDownloadProgress"
405+
body:@{
406+
@"totalBytes":[NSNumber numberWithLongLong:expectedContentLength],
407+
@"receivedBytes":[NSNumber numberWithLongLong:receivedContentLength]
408+
}];
409+
}
410+
// The download completed
411+
doneCallback:^{
412+
NSError *err;
413+
NSDictionary *newPackage = [CodePushPackage getPackage:updatePackage[PackageHashKey] error:&err];
414+
415+
if (err) {
416+
return reject(@"",@"",err);
417+
}
418+
419+
resolve(newPackage);
420+
}
421+
// The download failed
422+
failCallback:^(NSError *err) {
423+
reject(@"",@"",err);
424+
}];
418425
}
419426

420427
/*
@@ -424,7 +431,7 @@ - (void)savePendingUpdate:(NSString *)packageHash
424431
* app version, as well as the deployment key that was configured in the Info.plist file.
425432
*/
426433
RCT_EXPORT_METHOD(getConfiguration:(RCTPromiseResolveBlock)resolve
427-
rejecter:(RCTPromiseRejectBlock)reject)
434+
rejecter:(RCTPromiseRejectBlock)reject)
428435
{
429436
resolve([[CodePushConfig current] configuration]);
430437
}
@@ -433,21 +440,21 @@ - (void)savePendingUpdate:(NSString *)packageHash
433440
* This method is the native side of the CodePush.getCurrentPackage method.
434441
*/
435442
RCT_EXPORT_METHOD(getCurrentPackage:(RCTPromiseResolveBlock)resolve
436-
rejecter:(RCTPromiseRejectBlock)reject)
443+
rejecter:(RCTPromiseRejectBlock)reject)
437444
{
438445
dispatch_async(dispatch_get_main_queue(), ^{
439446
NSError *error;
440447
NSMutableDictionary *package = [[CodePushPackage getCurrentPackage:&error] mutableCopy];
441448

442449
if (error) {
443-
reject(error);
450+
reject(@"",@"",error);
444451
}
445452

446453
// Add the "isPending" virtual property to the package at this point, so that
447454
// the script-side doesn't need to immediately call back into native to populate it.
448455
BOOL isPendingUpdate = [self isPendingUpdate:[package objectForKey:PackageHashKey]];
449456
[package setObject:@(isPendingUpdate) forKey:PackageIsPendingKey];
450-
457+
451458
resolve(package);
452459
});
453460
}
@@ -456,17 +463,17 @@ - (void)savePendingUpdate:(NSString *)packageHash
456463
* This method is the native side of the LocalPackage.install method.
457464
*/
458465
RCT_EXPORT_METHOD(installUpdate:(NSDictionary*)updatePackage
459-
installMode:(CodePushInstallMode)installMode
460-
resolver:(RCTPromiseResolveBlock)resolve
461-
rejecter:(RCTPromiseRejectBlock)reject)
466+
installMode:(CodePushInstallMode)installMode
467+
resolver:(RCTPromiseResolveBlock)resolve
468+
rejecter:(RCTPromiseRejectBlock)reject)
462469
{
463470
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
464471
NSError *error;
465472
[CodePushPackage installPackage:updatePackage
466473
error:&error];
467474

468475
if (error) {
469-
reject(error);
476+
reject(@"",@"",error);
470477
} else {
471478
[self savePendingUpdate:updatePackage[PackageHashKey]
472479
isLoading:NO];
@@ -492,8 +499,8 @@ - (void)savePendingUpdate:(NSString *)packageHash
492499
* module, and is only used internally to populate the RemotePackage.failedInstall property.
493500
*/
494501
RCT_EXPORT_METHOD(isFailedUpdate:(NSString *)packageHash
495-
resolve:(RCTPromiseResolveBlock)resolve
496-
reject:(RCTPromiseRejectBlock)reject)
502+
resolve:(RCTPromiseResolveBlock)resolve
503+
reject:(RCTPromiseRejectBlock)reject)
497504
{
498505
BOOL isFailedHash = [self isFailedHash:packageHash];
499506
resolve(@(isFailedHash));
@@ -504,14 +511,14 @@ - (void)savePendingUpdate:(NSString *)packageHash
504511
* module, and is only used internally to populate the LocalPackage.isFirstRun property.
505512
*/
506513
RCT_EXPORT_METHOD(isFirstRun:(NSString *)packageHash
507-
resolve:(RCTPromiseResolveBlock)resolve
508-
rejecter:(RCTPromiseRejectBlock)reject)
514+
resolve:(RCTPromiseResolveBlock)resolve
515+
rejecter:(RCTPromiseRejectBlock)reject)
509516
{
510517
NSError *error;
511518
BOOL isFirstRun = _isFirstRunAfterUpdate
512-
&& nil != packageHash
513-
&& [packageHash length] > 0
514-
&& [packageHash isEqualToString:[CodePushPackage getCurrentPackageHash:&error]];
519+
&& nil != packageHash
520+
&& [packageHash length] > 0
521+
&& [packageHash isEqualToString:[CodePushPackage getCurrentPackageHash:&error]];
515522

516523
resolve(@(isFirstRun));
517524
}
@@ -520,18 +527,18 @@ - (void)savePendingUpdate:(NSString *)packageHash
520527
* This method is the native side of the CodePush.notifyApplicationReady() method.
521528
*/
522529
RCT_EXPORT_METHOD(notifyApplicationReady:(RCTPromiseResolveBlock)resolve
523-
rejecter:(RCTPromiseRejectBlock)reject)
530+
rejecter:(RCTPromiseRejectBlock)reject)
524531
{
525532
[CodePush removePendingUpdate];
526533
resolve([NSNull null]);
527534
}
528535

529536
/*
530-
* This method is checks if a new status update exists (new version was installed,
537+
* This method is checks if a new status update exists (new version was installed,
531538
* or an update failed) and return its details (version label, status).
532539
*/
533540
RCT_EXPORT_METHOD(getNewStatusReport:(RCTPromiseResolveBlock)resolve
534-
rejecter:(RCTPromiseRejectBlock)reject)
541+
rejecter:(RCTPromiseRejectBlock)reject)
535542
{
536543
if (needToReportRollback) {
537544
// Check if there was a rollback that was not yet reported
@@ -589,7 +596,7 @@ - (void)savePendingUpdate:(NSString *)packageHash
589596
/*
590597
* This method is the native side of the CodePush.downloadAndReplaceCurrentBundle()
591598
* method, which replaces the current bundle with the one downloaded from
592-
* removeBundleUrl. It is only to be used during tests and no-ops if the test
599+
* removeBundleUrl. It is only to be used during tests and no-ops if the test
593600
* configuration flag is not set.
594601
*/
595602
RCT_EXPORT_METHOD(downloadAndReplaceCurrentBundle:(NSString *)remoteBundleUrl)
@@ -599,4 +606,4 @@ - (void)savePendingUpdate:(NSString *)packageHash
599606
}
600607
}
601608

602-
@end
609+
@end

0 commit comments

Comments
 (0)