Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Examples/HybridMobileDeployCompanion/iOS/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,7 @@
<string></string>
<key>CodePushDeploymentKey</key>
<string>deployment-key-here</string>
<key>CodePushServerUrl</key>
<string>server-url-here</string>
</dict>
</plist>
6 changes: 3 additions & 3 deletions Examples/HybridMobileDeployCompanion/index.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ var HybridMobileDeployCompanion = React.createClass({
this.fetchData();
},
fetchData: function() {
HybridMobileDeploy.queryUpdate((err, update) => {
this.setState({ update: update, updateString: JSON.stringify(update) });
});
HybridMobileDeploy.queryUpdate((err, update) => {
this.setState({ update: update, updateString: JSON.stringify(update) });
});
},
getInitialState: function() {
return { update: false, updateString: "" };
Expand Down

Large diffs are not rendered by default.

20 changes: 14 additions & 6 deletions HybridMobileDeploy.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
Copy link
Contributor

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reduce redundancy by combining logic;

var defaultPackage = {appVersion: configuration.appVersion};
if (!err && localPackage !== null && localPackage.appVersion === configuration.appVersion) {
defaultPackage = localPackage;
} else if (err) {
console.log(err);
}

sdk.queryUpdateWithCurrentPackage(defaultPackage, callback);

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 = {
Expand Down
84 changes: 74 additions & 10 deletions HybridMobileDeploy.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ @implementation HybridMobileDeploy
+ (NSString *) getBundleFolderPath
{
NSString* home = NSHomeDirectory();
NSString* bundleFolder = [home stringByAppendingPathComponent:@"HybridMobileDeploy"];
NSString* bundleFolder = [home stringByAppendingPathComponent:@"HybridMobileDeploy/bundle"];
return bundleFolder;
}

Expand All @@ -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"];
Expand All @@ -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;
});
}

Expand All @@ -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
Expand All @@ -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]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

coding guidelines:
Put a new line space between closing brace '}' and next line unless next line also contains another closing brace '}'

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]);
}
}
});
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove the extra empty lines here..


@end
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"main": "HybridMobileDeploy.ios.js",
"keywords": "react-native",
"dependencies": {
"hybrid-mobile-deploy-sdk": "file:../website/sdk/bin",
"code-push": "file:../website/sdk/bin",
"semver": "^4.3.6"
},
"devDependencies": {
Expand Down