update-manager.ts 5.8 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

B
Benjamin Pasero 已提交
6
'use strict';
E
Erich Gamma 已提交
7 8 9 10 11

import fs = require('fs');
import path = require('path');
import events = require('events');

B
Benjamin Pasero 已提交
12
import electron = require('electron');
E
Erich Gamma 已提交
13 14
import platform = require('vs/base/common/platform');
import env = require('vs/workbench/electron-main/env');
15
import settings = require('vs/workbench/electron-main/settings');
J
Joao Moreno 已提交
16 17
import {Win32AutoUpdaterImpl} from 'vs/workbench/electron-main/auto-updater.win32';
import {LinuxAutoUpdaterImpl} from 'vs/workbench/electron-main/auto-updater.linux';
E
Erich Gamma 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
import {manager as Lifecycle} from 'vs/workbench/electron-main/lifecycle';

export enum State {
	Uninitialized,
	Idle,
	CheckingForUpdate,
	UpdateAvailable,
	UpdateDownloaded
}

export enum ExplicitState {
	Implicit,
	Explicit
}

export interface IUpdate {
	releaseNotes: string;
	version: string;
	date: Date;
	quitAndUpdate: () => void;
}

40
interface IAutoUpdater extends NodeJS.EventEmitter {
B
Benjamin Pasero 已提交
41
	setFeedURL(url: string): void;
E
Erich Gamma 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
	checkForUpdates(): void;
}

export class UpdateManager extends events.EventEmitter {

	private _state: State;
	private explicitState: ExplicitState;
	private _availableUpdate: IUpdate;
	private _lastCheckDate: Date;
	private raw: IAutoUpdater;
	private _feedUrl: string;
	private _channel: string;

	constructor() {
		super();

		this._state = State.Uninitialized;
		this.explicitState = ExplicitState.Implicit;
		this._availableUpdate = null;
		this._lastCheckDate = null;
		this._feedUrl = null;
		this._channel = null;

		if (platform.isWindows) {
			this.raw = new Win32AutoUpdaterImpl();
J
Joao Moreno 已提交
67 68
		} else if (platform.isLinux) {
			this.raw = new LinuxAutoUpdaterImpl();
E
Erich Gamma 已提交
69
		} else if (platform.isMacintosh) {
B
Benjamin Pasero 已提交
70
			this.raw = electron.autoUpdater;
E
Erich Gamma 已提交
71 72 73 74 75 76 77 78 79 80
		}

		if (this.raw) {
			this.initRaw();
		}
	}

	private initRaw(): void {
		this.raw.on('error', (event: any, message: string) => {
			this.emit('error', event, message);
J
Joao Moreno 已提交
81
			this.setState(State.Idle);
E
Erich Gamma 已提交
82 83 84 85 86 87 88
		});

		this.raw.on('checking-for-update', () => {
			this.emit('checking-for-update');
			this.setState(State.CheckingForUpdate);
		});

J
Joao Moreno 已提交
89
		this.raw.on('update-available', (event, url: string) => {
J
Joao Moreno 已提交
90 91 92 93
			this.emit('update-available', url);

			let data: IUpdate = null;

J
Joao Moreno 已提交
94
			if (url) {
J
Joao Moreno 已提交
95 96 97 98 99 100 101 102 103
				data = {
					releaseNotes: '',
					version: '',
					date: new Date(),
					quitAndUpdate: () => electron.shell.openExternal(url)
				};
			}

			this.setState(State.UpdateAvailable, data);
E
Erich Gamma 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
		});

		this.raw.on('update-not-available', () => {
			this.emit('update-not-available', this.explicitState === ExplicitState.Explicit);
			this.setState(State.Idle);
		});

		this.raw.on('update-downloaded', (event: any, releaseNotes: string, version: string, date: Date, url: string, rawQuitAndUpdate: () => void) => {
			let data: IUpdate = {
				releaseNotes: releaseNotes,
				version: version,
				date: date,
				quitAndUpdate: () => this.quitAndUpdate(rawQuitAndUpdate)
			};

			this.emit('update-downloaded', data);
			this.setState(State.UpdateDownloaded, data);
		});
	}

	private quitAndUpdate(rawQuitAndUpdate: () => void): void {
		Lifecycle.quit().done(vetod => {
			if (vetod) {
				return;
			}

130 131 132 133 134 135 136
			// for some reason updating on Mac causes the local storage not to be flushed.
			// we workaround this issue by forcing an explicit flush of the storage data.
			// see also https://github.com/Microsoft/vscode/issues/172
			if (platform.isMacintosh) {
				electron.session.defaultSession.flushStorageData();
			}

E
Erich Gamma 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
			rawQuitAndUpdate();
		});
	}

	public get feedUrl(): string {
		return this._feedUrl;
	}

	public get channel(): string {
		return this._channel;
	}

	public initialize(): void {
		if (this.feedUrl) {
			return; // already initialized
		}

154 155
		const channel = UpdateManager.getUpdateChannel();
		const feedUrl = UpdateManager.getUpdateFeedUrl(channel);
E
Erich Gamma 已提交
156 157 158 159 160

		if (!feedUrl) {
			return; // updates not available
		}

161
		this._channel = channel;
E
Erich Gamma 已提交
162 163
		this._feedUrl = feedUrl;

B
Benjamin Pasero 已提交
164
		this.raw.setFeedURL(feedUrl);
E
Erich Gamma 已提交
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
		this.setState(State.Idle);

		// Check for updates on startup after 30 seconds
		let timer = setTimeout(() => this.checkForUpdates(), 30 * 1000);

		// Clear timer when checking for update
		this.on('error', (error: any, message: string) => console.error(error, message));

		// Clear timer when checking for update
		this.on('checking-for-update', () => clearTimeout(timer));

		// If update not found, try again in 10 minutes
		this.on('update-not-available', () => {
			timer = setTimeout(() => this.checkForUpdates(), 10 * 60 * 1000);
		});
	}

	public get state(): State {
		return this._state;
	}

	public get availableUpdate(): IUpdate {
		return this._availableUpdate;
	}

	public get lastCheckDate(): Date {
		return this._lastCheckDate;
	}

	public checkForUpdates(explicit = false): void {
		this.explicitState = explicit ? ExplicitState.Explicit : ExplicitState.Implicit;
		this._lastCheckDate = new Date();
		this.raw.checkForUpdates();
	}

	private setState(state: State, availableUpdate: IUpdate = null): void {
		this._state = state;
		this._availableUpdate = availableUpdate;
		this.emit('change');
	}

206 207 208 209 210
	private static getUpdateChannel(): string {
		const channel = settings.manager.getValue('update.channel') || 'default';
		return channel === 'none' ? null : env.quality;
	}

E
Erich Gamma 已提交
211
	private static getUpdateFeedUrl(channel: string): string {
212 213 214 215
		if (!channel) {
			return null;
		}

E
Erich Gamma 已提交
216 217 218 219
		if (platform.isWindows && !fs.existsSync(path.join(path.dirname(process.execPath), 'unins000.exe'))) {
			return null;
		}

J
Joao Moreno 已提交
220
		if (!env.updateUrl || !env.product.commit) {
E
Erich Gamma 已提交
221 222 223
			return null;
		}

J
Joao Moreno 已提交
224
		return `${ env.updateUrl }/api/update/${ env.getPlatformIdentifier() }/${ channel }/${ env.product.commit }`;
E
Erich Gamma 已提交
225 226 227 228
	}
}

export const Instance = new UpdateManager();