Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve auto updater #869

Merged
merged 25 commits into from
Nov 12, 2018
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ release/
*.tgz
.idea
.DS_Store
dev-app-update.yml
6 changes: 3 additions & 3 deletions desktop/config-updater.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"forceUpdate": false,
"updater": {
"url":
"https://app.simplenote.com/desktop/{platform}/version?compare={version}",
"downloadUrl": "https://github.com/Automattic/simplenote-electron/releases/latest",
"changelogUrl": "https://github.com/Automattic/simplenote-electron/blob/master/changelog.md",
adlk marked this conversation as resolved.
Show resolved Hide resolved
"apiUrl": "https://api.github.com/repos/automattic/simplenote-electron/releases/latest",
"delay": 2000,
"interval": 600000
}
Expand Down
67 changes: 20 additions & 47 deletions desktop/updater/auto-updater/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,60 +3,33 @@
/**
* External Dependencies
*/
const EventEmitter = require('events').EventEmitter;
const util = require('util');
const electron = require( 'electron' );
const autoUpdater = electron.autoUpdater;
const dialog = electron.dialog;
const { autoUpdater } = require('electron-updater');

/**
* Internal dependencies
*/
const AppQuit = require( '../..//app-quit' );
const Updater = require('../lib/Updater');
const AppQuit = require('../../app-quit');

function AutoUpdater( url ) {
this.hasPrompted = false;
this.url = url;
class AutoUpdater extends Updater {
constructor(changelogUrl, options = {}) {
super(changelogUrl, options);

autoUpdater.on( 'error', () => this.emit( 'end' ) );
autoUpdater.on( 'update-not-available', () => this.emit( 'end' ) );
autoUpdater.on( 'update-downloaded', this.onDownloaded.bind( this ) );
autoUpdater.on('error', this.onError.bind(this));
autoUpdater.on('update-not-available', this.onNotAvailable.bind(this));
autoUpdater.on('update-downloaded', this.onDownloaded.bind(this));

try {
autoUpdater.setFeedURL( url );
} catch (e) {
console.log( e.message );
}
}
autoUpdater.autoInstallOnAppQuit = false;
}

ping() {
autoUpdater.checkForUpdates();
}

util.inherits(AutoUpdater, EventEmitter);

AutoUpdater.prototype.ping = function() {
autoUpdater.checkForUpdates();
};

AutoUpdater.prototype.onDownloaded = function( event, releaseNotes, releaseName, releaseDate, updateUrl, quitAndUpdate ) {
const updateDialogOptions = {
buttons: [ 'Update & Restart', 'Cancel' ],
title: 'Update Available',
message: releaseName,
detail: releaseNotes
};

if ( this.hasPrompted === false ) {
this.hasPrompted = true;

dialog.showMessageBox( updateDialogOptions, button => {
this.hasPrompted = false;

if ( button === 0 ) {
AppQuit.allowQuit();
quitAndUpdate();
} else {
this.emit( 'end' );
}
} );
}
};
onConfirm() {
AppQuit.allowQuit();
autoUpdater.quitAndInstall();
}
}

module.exports = AutoUpdater;
41 changes: 15 additions & 26 deletions desktop/updater/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
/**
* External Dependencies
*/
const electron = require('electron');
const app = electron.app;
const dialog = electron.dialog;
const { app } = require('electron');

/**
* Internal dependencies
Expand All @@ -17,32 +15,23 @@ const ManualUpdater = require('./manual-updater');

let updater = false;

function urlBuilder(version) {
let platformString = 'osx';

if (platform.isWindows()) {
platformString = 'windows';
} else if (platform.isLinux()) {
platformString = 'linux';
}

if (config.forceUpdate) {
version = '0.0.1';
}

return config.updater.url
.replace('{platform}', platformString)
.replace('{version}', version);
}

module.exports = function() {
app.on('will-finish-launching', function() {
let url = urlBuilder(app.getVersion());

if (platform.isOSX()) {
updater = new AutoUpdater(url);
if (platform.isOSX() || platform.isWindows() || process.env.APPIMAGE) {
updater = new AutoUpdater({
changelogUrl: config.updater.changelogUrl,
});
} else {
updater = new ManualUpdater(url);
updater = new ManualUpdater({
downloadUrl: config.updater.downloadUrl,
apiUrl: config.updater.apiUrl,
changelogUrl: config.updater.changelogUrl,
options: {
dialogMessage:
'{name} {newVersion} is now available — you have {currentVersion}. Would you like to download it now?',
confirmLabel: 'Download',
},
});
}

// Start one straight away
Expand Down
114 changes: 114 additions & 0 deletions desktop/updater/lib/Updater.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
const EventEmitter = require('events').EventEmitter;
const { app, dialog, shell } = require('electron');
const platform = require('../platform');

class Updater extends EventEmitter {
constructor(changelogUrl, options) {
super();

this.changelogUrl = changelogUrl;
this.confirmLabel = options.confirmLabel || 'Update & Restart';
this.dialogTitle =
options.dialogTitle || 'A new version of {name} is available!';
this.dialogMessage =
options.dialogMessage ||
'{name} {newVersion} is now available — you have {currentVersion}. Would you like to update now?';

this._version = '';
this._hasPrompted = false;
}

ping() {}

onDownloaded(event) {
console.log('Update downloaded');

// electron-updater provides us with new version
if (event.version) {
this.setVersion(event.version);
}

this.notify();
}

onNotAvailable() {
console.log('Update is not available');
}

onError(event) {
console.log('Update error', event);
}

onConfirm() {}

onCancel() {}

onChangelog() {
shell.openExternal(this.changelogUrl);
}

notify() {
const updateDialogOptions = {
buttons: [
this.sanitizeButtonLabel(this.confirmLabel),
'Cancel',
'Release notes',
],
title: 'Update Available',
message: this.expandMacros(this.dialogTitle),
detail: this.expandMacros(this.dialogMessage),
};

if (!this._hasPrompted) {
this._hasPrompted = true;

dialog.showMessageBox(updateDialogOptions, button => {
this._hasPrompted = false;

if (button === 0) {
// Confirm
this.onConfirm();
} else if (button === 2) {
// Open changelog
this.onChangelog();
} else {
this.onCancel();
}

this.emit('end');
});
}
}

setVersion(version) {
this._version = version;
}

expandMacros(originalText) {
const macros = {
name: app.getName(),
currentVersion: app.getVersion(),
newVersion: this._version,
};

let text = originalText;

for (const key in macros) {
if (macros.hasOwnProperty(key)) {
text = text.replace(new RegExp(`{${key}}`, 'ig'), macros[key]);
}
}

return text;
}

sanitizeButtonLabel(value) {
if (platform.isWindows()) {
return value.replace('&', '&&');
}

return value;
}
}

module.exports = Updater;
111 changes: 59 additions & 52 deletions desktop/updater/manual-updater/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,68 +3,75 @@
/**
* External Dependencies
*/
const EventEmitter = require('events').EventEmitter;
const util = require('util');
const electron = require( 'electron' );
const https = require( 'https' );
const { app, shell } = require('electron');
const fetch = require('electron-fetch').default;
const yaml = require('js-yaml');
const semver = require('semver');

const dialog = electron.dialog;
const shell = electron.shell;
/**
* Internal dependencies
*/
const Updater = require('../lib/Updater');

function ManualUpdater( url ) {
this.hasPrompted = false;
this.url = url;
}
class ManualUpdater extends Updater {
constructor({ apiUrl, downloadUrl, changelogUrl, options = {} }) {
super(changelogUrl, options);

util.inherits( ManualUpdater, EventEmitter );
this.apiUrl = apiUrl;
this.downloadUrl = downloadUrl;
}

ManualUpdater.prototype.ping = function() {
https.get( this.url, response => {
let body = '';
async ping() {
const options = {
headers: {
'User-Agent': `Simplenote/${app.getVersion()}`,
},
};

if ( response.statusCode !== 200 ) {
this.emit( 'end' );
}
try {
const releaseResp = await fetch(this.apiUrl, options);

response.on( 'data', chunk => body += chunk );
response.on( 'end', () => {
if ( body ) {
try {
let update = JSON.parse( body );
this.onAvailable( update );
} catch ( e ) {
this.emit( 'end' );
console.log( e.message );
}
}
} );
} ).on( 'error', error => {
this.emit( 'end' );
console.log( error.message );
} );
};
if (releaseResp.status !== 200) {
return;
}

ManualUpdater.prototype.onAvailable = function( update ) {
const updateDialogOptions = {
buttons: [ 'Download', 'Cancel' ],
title: 'Update Available',
message: update.name,
detail: update.notes + '\n\nYou will need to download and install the new version manually.'
};
const releaseBody = await releaseResp.json();

if ( ! this.hasPrompted ) {
this.hasPrompted = true;
const releaseAsset = releaseBody.assets.find(
release => release.name === 'latest.yml'
);
if (releaseAsset) {
const configResp = await fetch(
releaseAsset.browser_download_url,
options
);

dialog.showMessageBox( updateDialogOptions, button => {
this.hasPrompted = false;
if (configResp.status !== 200) {
return;
}

if ( button === 0 ) {
shell.openExternal( update.url );
}
const configBody = await configResp.text();
const releaseConfig = yaml.safeLoad(configBody);

this.emit( 'end' );
} );
}
};
if (semver.lt(app.getVersion(), releaseConfig.version)) {
console.log(
'New update is available, prompting user to update to',
releaseConfig.version
);
this.setVersion(releaseConfig.version);
this.notify();
} else {
return;
}
}
} catch (err) {
console.log(err.message);
}
}

onConfirm() {
shell.openExternal(this.downloadUrl);
}
}

module.exports = ManualUpdater;