Merge branch 'master' of https://github.com/reactnativecn/react-native-pushy
This commit is contained in:
commit
834dee1e8b
15
LICENSE
Normal file
15
LICENSE
Normal file
@ -0,0 +1,15 @@
|
||||
Update Plugin for React Native
|
||||
|
||||
Powered by ReactNative.cn
|
||||
|
||||
Copyright (c) Hangzhou Erica Network Technology Co., Ltd.
|
||||
|
||||
All rights reserved.
|
||||
|
||||
MIT License
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
@ -4,6 +4,7 @@ import android.content.Context;
|
||||
import android.os.AsyncTask;
|
||||
import android.util.Log;
|
||||
|
||||
import com.facebook.stetho.inspector.elements.ShadowDocument;
|
||||
import com.squareup.okhttp.OkHttpClient;
|
||||
import com.squareup.okhttp.Request;
|
||||
import com.squareup.okhttp.Response;
|
||||
@ -46,7 +47,7 @@ class DownloadTask extends AsyncTask<DownloadTaskParams, Void, Void> {
|
||||
System.loadLibrary("rnupdate");
|
||||
}
|
||||
|
||||
private void removeDirectory(File file) {
|
||||
private void removeDirectory(File file) throws IOException {
|
||||
if (UpdateContext.DEBUG) {
|
||||
Log.d("RNUpdate", "Removing " + file);
|
||||
}
|
||||
@ -60,7 +61,9 @@ class DownloadTask extends AsyncTask<DownloadTaskParams, Void, Void> {
|
||||
removeDirectory(f);
|
||||
}
|
||||
}
|
||||
file.delete();
|
||||
if (!file.delete()) {
|
||||
throw new IOException("Failed to delete directory");
|
||||
}
|
||||
}
|
||||
|
||||
private void downloadFile(String url, File writePath) throws IOException {
|
||||
@ -389,8 +392,24 @@ class DownloadTask extends AsyncTask<DownloadTaskParams, Void, Void> {
|
||||
Log.d("RNUpdate", "Unzip finished");
|
||||
}
|
||||
}
|
||||
private void doCleanUp(DownloadTaskParams param) {
|
||||
|
||||
private void doCleanUp(DownloadTaskParams param) throws IOException {
|
||||
if (UpdateContext.DEBUG) {
|
||||
Log.d("RNUpdate", "Start cleaning up");
|
||||
}
|
||||
File root = param.unzipDirectory;
|
||||
for (File sub : root.listFiles()) {
|
||||
if (sub.getName().charAt(0) == '.') {
|
||||
continue;
|
||||
}
|
||||
if (sub.isFile()) {
|
||||
sub.delete();
|
||||
} else {
|
||||
if (sub.getName().equals(param.hash) || sub.getName().equals(param.originHash)) {
|
||||
continue;
|
||||
}
|
||||
removeDirectory(sub);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -27,6 +27,16 @@ public class UpdateContext {
|
||||
}
|
||||
|
||||
this.sp = context.getSharedPreferences("update", Context.MODE_PRIVATE);
|
||||
|
||||
String packageVersion = getPackageVersion();
|
||||
if (!packageVersion.equals(this.sp.getString("packageVersion", null))) {
|
||||
SharedPreferences.Editor editor = sp.edit();
|
||||
editor.clear();
|
||||
editor.putString("packageVersion", packageVersion);
|
||||
editor.apply();
|
||||
|
||||
this.clearUp();
|
||||
}
|
||||
}
|
||||
|
||||
public String getRootDir() {
|
||||
@ -98,7 +108,7 @@ public class UpdateContext {
|
||||
editor.putString("lastVersion", hashName);
|
||||
}
|
||||
editor.putBoolean("firstTime", true);
|
||||
editor.putBoolean("shouldRollback", false);
|
||||
editor.putBoolean("firstTimeOk", false);
|
||||
editor.putBoolean("rolledBack", false);
|
||||
editor.apply();
|
||||
}
|
||||
@ -115,10 +125,17 @@ public class UpdateContext {
|
||||
return sp.getBoolean("rolledBack", false);
|
||||
}
|
||||
|
||||
public void clearFirstTimeMark() {
|
||||
public void markSuccess() {
|
||||
SharedPreferences.Editor editor = sp.edit();
|
||||
editor.putBoolean("firstTimeOk", true);
|
||||
editor.apply();
|
||||
|
||||
this.clearUp();
|
||||
}
|
||||
|
||||
public void clearFirstTime() {
|
||||
SharedPreferences.Editor editor = sp.edit();
|
||||
editor.putBoolean("firstTime", false);
|
||||
editor.putBoolean("shouldRollback", false);
|
||||
editor.apply();
|
||||
|
||||
this.clearUp();
|
||||
@ -145,40 +162,40 @@ public class UpdateContext {
|
||||
}
|
||||
|
||||
public String getBundleUrl(String defaultAssetsUrl) {
|
||||
// Test should rollback.
|
||||
if (sp.getBoolean("shouldRollback", false)) {
|
||||
this.rollBack();
|
||||
}
|
||||
String currentVersion = getCurrentVersion();
|
||||
if (currentVersion == null) {
|
||||
return defaultAssetsUrl;
|
||||
}
|
||||
if (sp.getBoolean("firstTime", false)) {
|
||||
SharedPreferences.Editor editor = sp.edit();
|
||||
editor.putBoolean("shouldRollback", true);
|
||||
editor.apply();
|
||||
// Test should rollback.
|
||||
if (!sp.getBoolean("firstTime", false)) {
|
||||
if (!sp.getBoolean("firstTimeOk", true)) {
|
||||
// Not firstTime, but not ok, so we roll back.
|
||||
currentVersion = this.rollBack();
|
||||
}
|
||||
}
|
||||
return (new File(rootDir, currentVersion+"/index.bundlejs").toString());
|
||||
}
|
||||
|
||||
private void rollBack() {
|
||||
private String rollBack() {
|
||||
String lastVersion = sp.getString("lastVersion", null);
|
||||
if (lastVersion == null) {
|
||||
throw new Error("This should never happen");
|
||||
}
|
||||
SharedPreferences.Editor editor = sp.edit();
|
||||
editor.putString("currentVersion", lastVersion);
|
||||
editor.putBoolean("shouldRollback", false);
|
||||
editor.putBoolean("firstTimeOk", true);
|
||||
editor.putBoolean("firstTime", false);
|
||||
editor.putBoolean("rolledBack", true);
|
||||
editor.apply();
|
||||
return lastVersion;
|
||||
}
|
||||
|
||||
private void clearUp() {
|
||||
DownloadTaskParams params = new DownloadTaskParams();
|
||||
params.type = DownloadTaskParams.TASK_TYPE_CLEARUP;
|
||||
params.hash = sp.getString("currentVersion", null);
|
||||
params.originHash = sp.getString("lastVersion", null);;
|
||||
params.originHash = sp.getString("lastVersion", null);
|
||||
params.unzipDirectory = rootDir;
|
||||
params.listener = new DownloadFileListener() {
|
||||
@Override
|
||||
public void onDownloadCompleted() {
|
||||
|
@ -34,7 +34,11 @@ public class UpdateModule extends ReactContextBaseJavaModule{
|
||||
constants.put("downloadRootDir", updateContext.getRootDir());
|
||||
constants.put("packageVersion", updateContext.getPackageVersion());
|
||||
constants.put("currentVersion", updateContext.getCurrentVersion());
|
||||
constants.put("isFirstTime", updateContext.isFirstTime());
|
||||
boolean isFirstTime = updateContext.isFirstTime();
|
||||
constants.put("isFirstTime", isFirstTime);
|
||||
if (isFirstTime) {
|
||||
updateContext.clearFirstTime();
|
||||
}
|
||||
boolean isRolledBack = updateContext.isRolledBack();
|
||||
constants.put("isRolledBack", isRolledBack);
|
||||
if (isRolledBack) {
|
||||
@ -129,11 +133,11 @@ public class UpdateModule extends ReactContextBaseJavaModule{
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
public void clearFirstTimeMark() {
|
||||
public void markSuccess() {
|
||||
UiThreadUtil.runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
updateContext.clearFirstTimeMark();
|
||||
updateContext.markSuccess();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
10
lib/index.js
10
lib/index.js
@ -60,19 +60,19 @@ export async function downloadUpdate(options) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (options.diff) {
|
||||
if (options.diffUrl) {
|
||||
await HotUpdate.downloadPatchFromPpk({
|
||||
updateUrl: options.diffUrl,
|
||||
hashName: options.hash,
|
||||
originHash: currentVersion,
|
||||
});
|
||||
} else if (options.diff) {
|
||||
} else if (options.pdiffUrl) {
|
||||
await HotUpdate.downloadPatchFromPackage({
|
||||
updateUrl: options.pdiffUrl,
|
||||
hashName: options.hash,
|
||||
});
|
||||
} else {
|
||||
await HotUpdate.downloadPatchFromPackage({
|
||||
await HotUpdate.downloadUpdate({
|
||||
updateUrl: options.updateUrl,
|
||||
hashName: options.hash,
|
||||
});
|
||||
@ -87,6 +87,6 @@ export async function switchVersionLater(hash) {
|
||||
HotUpdate.setNeedUpdate({hashName:hash});
|
||||
}
|
||||
|
||||
export function clearFirstTimeMark() {
|
||||
HotUpdate.clearFirstTimeMark();
|
||||
export function markSuccess() {
|
||||
HotUpdate.markSuccess();
|
||||
}
|
||||
|
@ -88,7 +88,7 @@ export const commands = {
|
||||
selectApp: async function({args, options}) {
|
||||
const {platform} = options;
|
||||
checkPlatform(platform);
|
||||
const id = args[0] || (await chooseApp()).id;
|
||||
const id = args[0] || (await chooseApp(platform)).id;
|
||||
|
||||
let updateInfo = {};
|
||||
if (await fs.exists('update.json')) {
|
||||
|
@ -32,9 +32,6 @@ exports.run = function () {
|
||||
console.log('Not loggined.\nRun `pushy login` at your project directory to login.');
|
||||
return;
|
||||
}
|
||||
console.log(err.message);
|
||||
setTimeout(()=>{
|
||||
throw err;
|
||||
});
|
||||
console.error(err.message);
|
||||
});
|
||||
};
|
||||
|
@ -2,7 +2,7 @@
|
||||
"name": "react-native-update",
|
||||
"version": "1.0.1",
|
||||
"description": "react-native hot update",
|
||||
"main": "index.js",
|
||||
"main": "lib/index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1",
|
||||
"prepublish": "node_modules/.bin/babel local-cli/src --out-dir local-cli/lib"
|
||||
|
Loading…
Reference in New Issue
Block a user