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

'use strict';

import nls = require('vs/nls');
import severity from 'vs/base/common/severity';
J
Johannes Rieken 已提交
10 11
import { TPromise } from 'vs/base/common/winjs.base';
import { Action } from 'vs/base/common/actions';
12
import { IMessageService, CloseAction, Severity } from 'vs/platform/message/common/message';
13 14
import pkg from 'vs/platform/node/package';
import product from 'vs/platform/node/product';
J
Joao Moreno 已提交
15
import URI from 'vs/base/common/uri';
J
Joao Moreno 已提交
16
import { IWorkbenchEditorService } from 'vs/workbench/services/editor/common/editorService';
J
Joao Moreno 已提交
17
import { IInstantiationService, ServicesAccessor } from 'vs/platform/instantiation/common/instantiation';
J
Joao Moreno 已提交
18
import { ReleaseNotesInput } from 'vs/workbench/parts/update/electron-browser/releaseNotesInput';
J
Joao Moreno 已提交
19
import { IRequestService } from 'vs/platform/request/node/request';
J
Joao Moreno 已提交
20 21
import { asText } from 'vs/base/node/request';
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
22 23
import { Keybinding } from 'vs/base/common/keyCodes';
import { KeybindingLabels } from 'vs/base/common/keybinding';
J
Joao Moreno 已提交
24
import { IOpenerService } from 'vs/platform/opener/common/opener';
25 26
import { IWorkbenchContribution } from 'vs/workbench/common/contributions';
import { IStorageService, StorageScope } from 'vs/platform/storage/common/storage';
27
import { IUpdateService } from 'vs/platform/update/common/update';
28
import * as semver from 'semver';
E
Erich Gamma 已提交
29

30 31 32 33
class ApplyUpdateAction extends Action {
	constructor( @IUpdateService private updateService: IUpdateService) {
		super('update.applyUpdate', nls.localize('updateNow', "Update Now"), null, true);
	}
E
Erich Gamma 已提交
34

35 36 37 38
	run(): TPromise<void> {
		return this.updateService.quitAndInstall();
	}
}
E
Erich Gamma 已提交
39

J
Joao Moreno 已提交
40 41 42 43 44 45 46
const NotNowAction = new Action(
	'update.later',
	nls.localize('later', "Later"),
	null,
	true,
	() => TPromise.as(true)
);
E
Erich Gamma 已提交
47

J
Joao Moreno 已提交
48
const releaseNotesCache: { [version: string]: TPromise<string>; } = Object.create(null);
N
Nick Olinger 已提交
49

J
Joao Moreno 已提交
50 51 52 53 54 55 56 57 58 59 60
export function loadReleaseNotes(accessor: ServicesAccessor, version: string): TPromise<string> {
	const requestService = accessor.get(IRequestService);
	const keybindingService = accessor.get(IKeybindingService);
	const match = /^(\d+\.\d)\./.exec(version);

	if (!match) {
		return TPromise.as(null);
	}

	const versionLabel = match[1].replace(/\./g, '_');
	const baseUrl = 'https://code.visualstudio.com/raw';
J
Johannes Rieken 已提交
61
	const url = `${baseUrl}/v${versionLabel}.md`;
J
Joao Moreno 已提交
62
	const unassigned = nls.localize('unassigned', "unassigned");
J
Joao Moreno 已提交
63 64 65 66 67 68

	const patchKeybindings = (text: string): string => {
		const kb = (match: string, kb: string) => {
			const keybinding = keybindingService.lookupKeybindings(kb)[0];

			if (!keybinding) {
J
Joao Moreno 已提交
69
				return unassigned;
J
Joao Moreno 已提交
70 71 72 73 74 75
			}

			return keybindingService.getLabelFor(keybinding);
		};

		const kbstyle = (match: string, kb: string) => {
76
			const code = KeybindingLabels.fromUserSettingsLabel(kb);
J
Joao Moreno 已提交
77 78

			if (!code) {
J
Joao Moreno 已提交
79
				return unassigned;
J
Joao Moreno 已提交
80 81 82 83 84
			}

			const keybinding = new Keybinding(code);

			if (!keybinding) {
J
Joao Moreno 已提交
85
				return unassigned;
J
Joao Moreno 已提交
86 87 88 89 90 91 92 93 94 95
			}

			return keybindingService.getLabelFor(keybinding);
		};

		return text
			.replace(/kb\(([a-z.\d\-]+)\)/gi, kb)
			.replace(/kbstyle\(([^\)]+)\)/gi, kbstyle);
	};

J
Joao Moreno 已提交
96 97
	if (!releaseNotesCache[version]) {
		releaseNotesCache[version] = requestService.request({ url })
N
Nick Olinger 已提交
98
			.then(asText)
J
Joao Moreno 已提交
99
			.then(text => patchKeybindings(text));
N
Nick Olinger 已提交
100
	}
J
Joao Moreno 已提交
101 102

	return releaseNotesCache[version];
J
Joao Moreno 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
}

export class OpenLatestReleaseNotesInBrowserAction extends Action {

	constructor(
		@IOpenerService private openerService: IOpenerService
	) {
		super('update.openLatestReleaseNotes', nls.localize('releaseNotes', "Release Notes"), null, true);
	}

	run(): TPromise<any> {
		const uri = URI.parse(product.releaseNotesUrl);
		return this.openerService.open(uri);
	}
}

J
Joao Moreno 已提交
119
export abstract class AbstractShowReleaseNotesAction extends Action {
J
Joao Moreno 已提交
120 121

	constructor(
B
Benjamin Pasero 已提交
122 123
		id: string,
		label: string,
J
Joao Moreno 已提交
124 125 126
		private returnValue: boolean,
		private version: string,
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService,
B
Benjamin Pasero 已提交
127
		@IInstantiationService private instantiationService: IInstantiationService
J
Joao Moreno 已提交
128
	) {
J
Joao Moreno 已提交
129
		super(id, label, null, true);
J
Joao Moreno 已提交
130 131 132
	}

	run(): TPromise<boolean> {
J
Joao Moreno 已提交
133 134 135 136 137 138 139
		if (!this.enabled) {
			return TPromise.as(false);
		}

		this.enabled = false;

		return this.instantiationService.invokeFunction(loadReleaseNotes, this.version)
B
Benjamin Pasero 已提交
140
			.then(text => this.editorService.openEditor(this.instantiationService.createInstance(ReleaseNotesInput, this.version, text), { pinned: true }))
J
Joao Moreno 已提交
141 142 143 144 145
			.then(() => true)
			.then(null, () => {
				const action = this.instantiationService.createInstance(OpenLatestReleaseNotesInBrowserAction);
				return action.run().then(() => false);
			});
J
Joao Moreno 已提交
146 147
	}
}
E
Erich Gamma 已提交
148

J
Joao Moreno 已提交
149 150 151 152 153 154
export class ShowReleaseNotesAction extends AbstractShowReleaseNotesAction {

	constructor(
		returnValue: boolean,
		version: string,
		@IWorkbenchEditorService editorService: IWorkbenchEditorService,
B
Benjamin Pasero 已提交
155
		@IInstantiationService instantiationService: IInstantiationService
J
Joao Moreno 已提交
156
	) {
B
Benjamin Pasero 已提交
157
		super('update.showReleaseNotes', nls.localize('releaseNotes', "Release Notes"), returnValue, version, editorService, instantiationService);
J
Joao Moreno 已提交
158 159 160 161 162 163 164 165 166 167 168 169
	}
}

export class ShowCurrentReleaseNotesAction extends AbstractShowReleaseNotesAction {

	static ID = 'update.showCurrentReleaseNotes';
	static LABEL = nls.localize('showReleaseNotes', "Show Release Notes");

	constructor(
		id = ShowCurrentReleaseNotesAction.ID,
		label = ShowCurrentReleaseNotesAction.LABEL,
		@IWorkbenchEditorService editorService: IWorkbenchEditorService,
B
Benjamin Pasero 已提交
170
		@IInstantiationService instantiationService: IInstantiationService
J
Joao Moreno 已提交
171
	) {
B
Benjamin Pasero 已提交
172
		super(id, label, true, pkg.version, editorService, instantiationService);
J
Joao Moreno 已提交
173 174 175
	}
}

J
Joao Moreno 已提交
176 177
export class DownloadAction extends Action {

J
Joao Moreno 已提交
178
	constructor(private url: string, @IUpdateService private updateService: IUpdateService) {
J
Joao Moreno 已提交
179 180 181 182
		super('update.download', nls.localize('downloadNow', "Download Now"), null, true);
	}

	run(): TPromise<void> {
J
Joao Moreno 已提交
183
		return this.updateService.quitAndInstall();
J
Joao Moreno 已提交
184 185
	}
}
J
Joao Moreno 已提交
186

187 188
const LinkAction = (id: string, message: string, licenseUrl: string) => new Action(
	id, message, null, true,
B
Benjamin Pasero 已提交
189
	() => { window.open(licenseUrl); return TPromise.as(null); }
190 191 192 193 194 195
);

export class UpdateContribution implements IWorkbenchContribution {

	private static KEY = 'releaseNotes/lastVersion';
	getId() { return 'vs.update'; }
E
Erich Gamma 已提交
196 197

	constructor(
198 199 200
		@IStorageService storageService: IStorageService,
		@IInstantiationService instantiationService: IInstantiationService,
		@IMessageService messageService: IMessageService,
201
		@IUpdateService updateService: IUpdateService,
202
		@IWorkbenchEditorService editorService: IWorkbenchEditorService
E
Erich Gamma 已提交
203
	) {
204 205 206 207 208 209
		const lastVersion = storageService.get(UpdateContribution.KEY, StorageScope.GLOBAL, '');

		// was there an update?
		if (product.releaseNotesUrl && lastVersion && pkg.version !== lastVersion) {
			instantiationService.invokeFunction(loadReleaseNotes, pkg.version)
				.then(
B
Benjamin Pasero 已提交
210
				text => editorService.openEditor(instantiationService.createInstance(ReleaseNotesInput, pkg.version, text), { pinned: true }),
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
				() => {
					messageService.show(Severity.Info, {
						message: nls.localize('read the release notes', "Welcome to {0} v{1}! Would you like to read the Release Notes?", product.nameLong, pkg.version),
						actions: [
							instantiationService.createInstance(OpenLatestReleaseNotesInBrowserAction),
							CloseAction
						]
					});
				});
		}

		// should we show the new license?
		if (product.licenseUrl && lastVersion && semver.satisfies(lastVersion, '<1.0.0') && semver.satisfies(pkg.version, '>=1.0.0')) {
			messageService.show(Severity.Info, {
				message: nls.localize('licenseChanged', "Our license terms have changed, please go through them.", product.nameLong, pkg.version),
				actions: [
					LinkAction('update.showLicense', nls.localize('license', "Read License"), product.licenseUrl),
					CloseAction
				]
			});
		}

		storageService.store(UpdateContribution.KEY, pkg.version, StorageScope.GLOBAL);

235 236
		updateService.onUpdateReady(update => {
			const applyUpdateAction = instantiationService.createInstance(ApplyUpdateAction);
237
			const releaseNotesAction = instantiationService.createInstance(ShowReleaseNotesAction, false, update.version);
J
Joao Moreno 已提交
238

239
			messageService.show(severity.Info, {
240
				message: nls.localize('updateAvailable', "{0} will be updated after it restarts.", product.nameLong),
241
				actions: [applyUpdateAction, NotNowAction, releaseNotesAction]
E
Erich Gamma 已提交
242 243 244
			});
		});

245
		updateService.onUpdateAvailable(update => {
J
Joao Moreno 已提交
246
			const downloadAction = instantiationService.createInstance(DownloadAction, update.version);
247
			const releaseNotesAction = instantiationService.createInstance(ShowReleaseNotesAction, false, update.version);
J
Joao Moreno 已提交
248

249
			messageService.show(severity.Info, {
J
Joao Moreno 已提交
250
				message: nls.localize('thereIsUpdateAvailable', "There is an available update."),
J
Joao Moreno 已提交
251
				actions: [downloadAction, NotNowAction, releaseNotesAction]
J
Joao Moreno 已提交
252 253 254
			});
		});

255 256 257 258 259
		updateService.onUpdateNotAvailable(explicit => {
			if (!explicit) {
				return;
			}

260
			messageService.show(severity.Info, nls.localize('noUpdatesAvailable', "There are no updates currently available."));
E
Erich Gamma 已提交
261
		});
J
Joao Moreno 已提交
262 263

		updateService.onError(err => messageService.show(severity.Error, err));
E
Erich Gamma 已提交
264
	}
265
}