-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Query update with current package #3
Changes from all commits
bd41824
8ee234e
d7332ed
9829b41
f206ce3
2984125
153f6bf
a4e4957
718e67d
5c6ef09
850a234
43d6260
15c9d93
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,7 +8,7 @@ | |
| var NativeHybridMobileDeploy = require('react-native').NativeModules.HybridMobileDeploy; | ||
| var requestFetchAdapter = require("./request-fetch-adapter.js"); | ||
| var semver = require('semver'); | ||
| var Sdk = require("hybrid-mobile-deploy-sdk/script/acquisition-sdk").AcquisitionManager; | ||
| var Sdk = require("code-push/script/acquisition-sdk").AcquisitionManager; | ||
| var sdk; | ||
| var config; | ||
|
|
||
|
|
@@ -44,16 +44,24 @@ function queryUpdate(callback) { | |
| if (err) callback(err); | ||
| getSdk(function(err, sdk) { | ||
| if (err) callback(err); | ||
| var pkg = {appVersion: configuration.appVersion}; | ||
| sdk.queryUpdateWithCurrentPackage(pkg, callback); | ||
| NativeHybridMobileDeploy.getLocalPackage(function(err, localPackage) { | ||
| var queryPackage = {appVersion: configuration.appVersion}; | ||
| if (!err && localPackage && localPackage.appVersion === configuration.appVersion) { | ||
| queryPackage = localPackage; | ||
| } else if (err) { | ||
| console.log(err); | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reduce redundancy by combining logic; var defaultPackage = {appVersion: configuration.appVersion}; sdk.queryUpdateWithCurrentPackage(defaultPackage, callback);
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
|
|
||
| sdk.queryUpdateWithCurrentPackage(queryPackage, callback); | ||
| }); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| function installUpdate(update) { | ||
| getConfiguration(function(err, config) { | ||
| NativeHybridMobileDeploy.installUpdateFromUrl(config.serverUrl + "acquire/" + config.deploymentKey, (err) => console.log(err)); | ||
| }); | ||
| // Use the downloaded package info. Native code will save the package info | ||
| // so that the client knows what the current package version is. | ||
| NativeHybridMobileDeploy.installUpdate(update, JSON.stringify(update), (err) => console.log(err)); | ||
| } | ||
|
|
||
| var HybridMobileDeploy = { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,7 +15,7 @@ @implementation HybridMobileDeploy | |
| + (NSString *) getBundleFolderPath | ||
| { | ||
| NSString* home = NSHomeDirectory(); | ||
| NSString* bundleFolder = [home stringByAppendingPathComponent:@"HybridMobileDeploy"]; | ||
| NSString* bundleFolder = [home stringByAppendingPathComponent:@"HybridMobileDeploy/bundle"]; | ||
| return bundleFolder; | ||
| } | ||
|
|
||
|
|
@@ -26,6 +26,21 @@ + (NSString *) getBundlePath | |
| return [bundleFolderPath stringByAppendingPathComponent:appBundleName]; | ||
| } | ||
|
|
||
| + (NSString *) getPackageFolderPath | ||
| { | ||
| NSString* home = NSHomeDirectory(); | ||
| NSString* packageFolder = [home stringByAppendingPathComponent:@"HybridMobileDeploy/package"]; | ||
| return packageFolder; | ||
| } | ||
|
|
||
| + (NSString *) getPackagePath | ||
| { | ||
| NSString * packageFolderPath = [self getPackageFolderPath]; | ||
| NSString* appPackageName = @"localpackage.json"; | ||
| return [packageFolderPath stringByAppendingPathComponent:appPackageName]; | ||
| } | ||
|
|
||
|
|
||
| + (NSURL *) getNativeBundleURL | ||
| { | ||
| return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"]; | ||
|
|
@@ -43,11 +58,16 @@ + (NSURL *) getBundleUrl | |
| } | ||
| } | ||
|
|
||
| - (void) reloadBundle | ||
| + (void) loadBundle:(NSString*)rootComponent | ||
| { | ||
| dispatch_async(dispatch_get_main_queue(), ^{ | ||
| self.bridge.bundleURL = [HybridMobileDeploy getBundleUrl]; | ||
| [self.bridge reload]; | ||
| RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:[self getBundleUrl] | ||
| moduleName:rootComponent | ||
| launchOptions:nil]; | ||
|
|
||
| UIViewController *rootViewController = [[UIViewController alloc] init]; | ||
| rootViewController.view = rootView; | ||
| [UIApplication sharedApplication].delegate.window.rootViewController = rootViewController; | ||
| }); | ||
| } | ||
|
|
||
|
|
@@ -56,27 +76,28 @@ - (void) reloadBundle | |
| callback(@[[NSNull null], [HybridMobileDeployConfig getConfiguration]]); | ||
| } | ||
|
|
||
| RCT_EXPORT_METHOD(installUpdateFromUrl:(NSString*)updateUrl | ||
| RCT_EXPORT_METHOD(installUpdate:(NSDictionary*)updatePackage | ||
| packageJsonString:(NSString*) packageJsonString | ||
| callback:(RCTResponseSenderBlock)callback) | ||
| { | ||
| dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ | ||
| NSURL* url = [NSURL URLWithString:updateUrl]; | ||
| NSURL* url = [NSURL URLWithString:updatePackage[@"downloadUrl"]]; | ||
| NSError *err; | ||
|
|
||
| NSString *updateContents = [[NSString alloc] initWithContentsOfURL:url | ||
| encoding:NSUTF8StringEncoding | ||
| error:&err]; | ||
|
|
||
| if (err) { | ||
| // TODO send download url | ||
| callback(@[RCTMakeError(@"Error downloading url", err, [[NSDictionary alloc] initWithObjectsAndKeys:updateUrl,@"updateUrl", nil])]); | ||
| callback(@[RCTMakeError(@"Error downloading url", err, [[NSDictionary alloc] initWithObjectsAndKeys:[url absoluteString],@"updateUrl", nil])]); | ||
| } else { | ||
| dispatch_async(dispatch_get_main_queue(), ^{ | ||
| NSError *saveError; | ||
| NSString *bundleFolderPath = [HybridMobileDeploy getBundleFolderPath]; | ||
| if (![[NSFileManager defaultManager] fileExistsAtPath:bundleFolderPath]) { | ||
| [[NSFileManager defaultManager] createDirectoryAtPath:bundleFolderPath withIntermediateDirectories:YES attributes:nil error:&saveError]; | ||
| } | ||
|
|
||
| [updateContents writeToFile:[HybridMobileDeploy getBundlePath] | ||
| atomically:YES | ||
| encoding:NSUTF8StringEncoding | ||
|
|
@@ -85,12 +106,55 @@ - (void) reloadBundle | |
| // TODO send file path | ||
| callback(@[RCTMakeError(@"Error saving file", err, [[NSDictionary alloc] initWithObjectsAndKeys:[HybridMobileDeploy getBundlePath],@"bundlePath", nil])]); | ||
| } else { | ||
| [self reloadBundle]; | ||
| callback(@[[NSNull null]]); | ||
| // Save the package info too. | ||
| NSString *packageFolderPath = [HybridMobileDeploy getPackageFolderPath]; | ||
| if (![[NSFileManager defaultManager] fileExistsAtPath:packageFolderPath]) { | ||
| [[NSFileManager defaultManager] createDirectoryAtPath:packageFolderPath withIntermediateDirectories:YES attributes:nil error:&saveError]; | ||
| } | ||
|
|
||
| [packageJsonString writeToFile:[HybridMobileDeploy getPackagePath] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. coding guidelines: |
||
| atomically:YES | ||
| encoding:NSUTF8StringEncoding | ||
| error:&saveError]; | ||
|
|
||
| if (saveError) { | ||
| callback(@[RCTMakeError(@"Error saving file", err, [[NSDictionary alloc] initWithObjectsAndKeys:[HybridMobileDeploy getPackagePath],@"packagePath", nil])]); | ||
| } else { | ||
| [HybridMobileDeploy loadBundle:[HybridMobileDeployConfig getRootComponent]]; | ||
| callback(@[[NSNull null]]); | ||
| } | ||
| } | ||
| }); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
|
|
||
|
|
||
| RCT_EXPORT_METHOD(getLocalPackage: (RCTResponseSenderBlock)callback) | ||
| { | ||
|
|
||
| NSString *path = [HybridMobileDeploy getPackagePath]; | ||
|
|
||
| dispatch_async(dispatch_get_main_queue(), ^{ | ||
|
|
||
| NSError* readError; | ||
| NSString *content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&readError]; | ||
| if (readError) { | ||
| callback(@[RCTMakeError(@"Error finding local package ", readError, [[NSDictionary alloc] initWithObjectsAndKeys:path,@"packagePath", nil]), [NSNull null]]); | ||
| } else { | ||
| NSError * parseError; | ||
| NSData *data = [content dataUsingEncoding:NSUTF8StringEncoding]; | ||
| NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data | ||
| options:kNilOptions | ||
| error:&parseError]; | ||
| if (parseError) { | ||
| callback(@[RCTMakeError(@"Error parsing contents of local package ", parseError, [[NSDictionary alloc] initWithObjectsAndKeys:path,@"packagePath", nil]), [NSNull null]]); | ||
| } else { | ||
| callback(@[[NSNull null], json]); | ||
| } | ||
| } | ||
| }); | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove the extra empty lines here.. |
||
|
|
||
| @end | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
put spacing between if and opening paranthesis, similarly for else and braces.. that is our coding guidelines.
feedback throughout the code.
If someone has a link to the coding guideline page, that would be great for Geoffery.