workspaceEditingService.ts 11.5 KB
Newer Older
1 2 3 4 5 6 7
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

'use strict';

B
Benjamin Pasero 已提交
8 9
import { IWorkspaceEditingService } from 'vs/workbench/services/workspace/common/workspaceEditing';
import URI from 'vs/base/common/uri';
S
Sandeep Somavarapu 已提交
10
import * as nls from 'vs/nls';
11
import { TPromise } from 'vs/base/common/winjs.base';
12
import { IWorkspaceContextService, WorkbenchState } from 'vs/platform/workspace/common/workspace';
S
Sandeep Somavarapu 已提交
13
import { IWindowService, IEnterWorkspaceResult } from 'vs/platform/windows/common/windows';
S
Sandeep Somavarapu 已提交
14
import { IJSONEditingService, JSONEditingError, JSONEditingErrorCode } from 'vs/workbench/services/configuration/common/jsonEditing';
15
import { IWorkspaceIdentifier, IWorkspaceFolderCreationData } from 'vs/platform/workspaces/common/workspaces';
16
import { IWorkspaceConfigurationService } from 'vs/workbench/services/configuration/common/configuration';
17
import { WorkspaceService } from 'vs/workbench/services/configuration/node/configurationService';
18
import { migrateStorageToMultiRootWorkspace } from 'vs/platform/storage/common/migration';
19
import { IStorageService } from 'vs/platform/storage/common/storage';
20 21 22
import { StorageService } from 'vs/platform/storage/common/storageService';
import { ConfigurationScope, IConfigurationRegistry, Extensions as ConfigurationExtensions } from 'vs/platform/configuration/common/configurationRegistry';
import { Registry } from 'vs/platform/registry/common/platform';
23
import { IExtensionService } from 'vs/workbench/services/extensions/common/extensions';
24 25
import { IBackupFileService } from 'vs/workbench/services/backup/common/backup';
import { BackupFileService } from 'vs/workbench/services/backup/node/backupFileService';
S
Sandeep Somavarapu 已提交
26
import { ICommandService } from 'vs/platform/commands/common/commands';
27 28 29
import { distinct } from 'vs/base/common/arrays';
import { isLinux } from 'vs/base/common/platform';
import { isEqual } from 'vs/base/common/resources';
30
import { INotificationService, Severity } from 'vs/platform/notification/common/notification';
31 32 33

export class WorkspaceEditingService implements IWorkspaceEditingService {

B
Benjamin Pasero 已提交
34
	_serviceBrand: any;
35 36

	constructor(
37
		@IJSONEditingService private jsonEditingService: IJSONEditingService,
38
		@IWorkspaceContextService private contextService: WorkspaceService,
39
		@IWindowService private windowService: IWindowService,
40 41
		@IWorkspaceConfigurationService private workspaceConfigurationService: IWorkspaceConfigurationService,
		@IStorageService private storageService: IStorageService,
42
		@IExtensionService private extensionService: IExtensionService,
S
Sandeep Somavarapu 已提交
43
		@IBackupFileService private backupFileService: IBackupFileService,
44
		@INotificationService private notificationService: INotificationService,
S
Sandeep Somavarapu 已提交
45
		@ICommandService private commandService: ICommandService
46 47 48
	) {
	}

B
Benjamin Pasero 已提交
49
	updateFolders(index: number, deleteCount?: number, foldersToAdd?: IWorkspaceFolderCreationData[], donotNotifyError?: boolean): TPromise<void> {
B
Benjamin Pasero 已提交
50 51
		const folders = this.contextService.getWorkspace().folders;

52 53 54
		let foldersToDelete: URI[] = [];
		if (typeof deleteCount === 'number') {
			foldersToDelete = folders.slice(index, index + deleteCount).map(f => f.uri);
B
Benjamin Pasero 已提交
55 56
		}

57 58 59 60 61 62
		const wantsToDelete = foldersToDelete.length > 0;
		const wantsToAdd = Array.isArray(foldersToAdd) && foldersToAdd.length > 0;

		if (!wantsToAdd && !wantsToDelete) {
			return TPromise.as(void 0); // return early if there is nothing to do
		}
B
Benjamin Pasero 已提交
63 64 65 66 67 68 69 70

		// Add Folders
		if (wantsToAdd && !wantsToDelete) {
			return this.doAddFolders(foldersToAdd, index, donotNotifyError);
		}

		// Delete Folders
		if (wantsToDelete && !wantsToAdd) {
71
			return this.removeFolders(foldersToDelete);
B
Benjamin Pasero 已提交
72 73
		}

74
		// Add & Delete Folders
75 76
		else {

77 78 79
			// if we are in single-folder state and the folder is replaced with
			// other folders, we handle this specially and just enter workspace
			// mode with the folders that are being added.
80 81 82 83 84 85 86 87 88 89 90
			if (this.includesSingleFolderWorkspace(foldersToDelete)) {
				return this.createAndEnterWorkspace(foldersToAdd);
			}

			// if we are not in workspace-state, we just add the folders
			if (this.contextService.getWorkbenchState() !== WorkbenchState.WORKSPACE) {
				return this.doAddFolders(foldersToAdd, index, donotNotifyError);
			}

			// finally, update folders within the workspace
			return this.doUpdateFolders(foldersToAdd, foldersToDelete, index, donotNotifyError);
91
		}
92
	}
93

94 95 96
	private doUpdateFolders(foldersToAdd: IWorkspaceFolderCreationData[], foldersToDelete: URI[], index?: number, donotNotifyError: boolean = false): TPromise<void> {
		return this.contextService.updateFolders(foldersToAdd, foldersToDelete, index)
			.then(() => null, error => donotNotifyError ? TPromise.wrapError(error) : this.handleWorkspaceConfigurationEditingError(error));
B
Benjamin Pasero 已提交
97 98
	}

B
Benjamin Pasero 已提交
99
	addFolders(foldersToAdd: IWorkspaceFolderCreationData[], donotNotifyError: boolean = false): TPromise<void> {
B
Benjamin Pasero 已提交
100 101 102 103
		return this.doAddFolders(foldersToAdd, void 0, donotNotifyError);
	}

	private doAddFolders(foldersToAdd: IWorkspaceFolderCreationData[], index?: number, donotNotifyError: boolean = false): TPromise<void> {
104 105 106 107 108
		const state = this.contextService.getWorkbenchState();

		// If we are in no-workspace or single-folder workspace, adding folders has to
		// enter a workspace.
		if (state !== WorkbenchState.WORKSPACE) {
109 110 111
			let newWorkspaceFolders = this.contextService.getWorkspace().folders.map(folder => ({ uri: folder.uri } as IWorkspaceFolderCreationData));
			newWorkspaceFolders.splice(typeof index === 'number' ? index : newWorkspaceFolders.length, 0, ...foldersToAdd);
			newWorkspaceFolders = distinct(newWorkspaceFolders, folder => isLinux ? folder.uri.toString() : folder.uri.toString().toLowerCase());
112 113 114 115 116 117 118 119 120

			if (state === WorkbenchState.EMPTY && newWorkspaceFolders.length === 0 || state === WorkbenchState.FOLDER && newWorkspaceFolders.length === 1) {
				return TPromise.as(void 0); // return if the operation is a no-op for the current state
			}

			return this.createAndEnterWorkspace(newWorkspaceFolders);
		}

		// Delegate addition of folders to workspace service otherwise
B
Benjamin Pasero 已提交
121
		return this.contextService.addFolders(foldersToAdd, index)
122
			.then(() => null, error => donotNotifyError ? TPromise.wrapError(error) : this.handleWorkspaceConfigurationEditingError(error));
S
Sandeep Somavarapu 已提交
123 124
	}

B
Benjamin Pasero 已提交
125
	removeFolders(foldersToRemove: URI[], donotNotifyError: boolean = false): TPromise<void> {
126 127

		// If we are in single-folder state and the opened folder is to be removed,
128 129 130
		// we create an empty workspace and enter it.
		if (this.includesSingleFolderWorkspace(foldersToRemove)) {
			return this.createAndEnterWorkspace([]);
131 132 133 134
		}

		// Delegate removal of folders to workspace service otherwise
		return this.contextService.removeFolders(foldersToRemove)
135
			.then(() => null, error => donotNotifyError ? TPromise.wrapError(error) : this.handleWorkspaceConfigurationEditingError(error));
S
Sandeep Somavarapu 已提交
136 137
	}

138 139 140 141 142 143 144 145 146
	private includesSingleFolderWorkspace(folders: URI[]): boolean {
		if (this.contextService.getWorkbenchState() === WorkbenchState.FOLDER) {
			const workspaceFolder = this.contextService.getWorkspace().folders[0];
			return (folders.some(folder => isEqual(folder, workspaceFolder.uri, !isLinux)));
		}

		return false;
	}

B
Benjamin Pasero 已提交
147
	createAndEnterWorkspace(folders?: IWorkspaceFolderCreationData[], path?: string): TPromise<void> {
148
		return this.doEnterWorkspace(() => this.windowService.createAndEnterWorkspace(folders, path));
149 150
	}

B
Benjamin Pasero 已提交
151
	saveAndEnterWorkspace(path: string): TPromise<void> {
152
		return this.doEnterWorkspace(() => this.windowService.saveAndEnterWorkspace(path));
153 154
	}

S
Sandeep Somavarapu 已提交
155 156 157
	private handleWorkspaceConfigurationEditingError(error: JSONEditingError): TPromise<void> {
		switch (error.code) {
			case JSONEditingErrorCode.ERROR_INVALID_FILE:
158 159
				this.onInvalidWorkspaceConfigurationFileError();
				return TPromise.as(void 0);
S
Sandeep Somavarapu 已提交
160
			case JSONEditingErrorCode.ERROR_FILE_DIRTY:
161 162
				this.onWorkspaceConfigurationFileDirtyError();
				return TPromise.as(void 0);
S
Sandeep Somavarapu 已提交
163
		}
164
		this.notificationService.error(error.message);
S
Sandeep Somavarapu 已提交
165 166 167
		return TPromise.as(void 0);
	}

168
	private onInvalidWorkspaceConfigurationFileError(): void {
S
Sandeep Somavarapu 已提交
169
		const message = nls.localize('errorInvalidTaskConfiguration', "Unable to write into workspace configuration file. Please open the file to correct errors/warnings in it and try again.");
170
		this.askToOpenWorkspaceConfigurationFile(message);
S
Sandeep Somavarapu 已提交
171 172
	}

173
	private onWorkspaceConfigurationFileDirtyError(): void {
S
Sandeep Somavarapu 已提交
174
		const message = nls.localize('errorWorkspaceConfigurationFileDirty', "Unable to write into workspace configuration file because the file is dirty. Please save it and try again.");
175
		this.askToOpenWorkspaceConfigurationFile(message);
S
Sandeep Somavarapu 已提交
176 177
	}

178 179 180 181 182 183 184
	private askToOpenWorkspaceConfigurationFile(message: string): void {
		this.notificationService.prompt(Severity.Error, message,
			[{
				label: nls.localize('openWorkspaceConfigurationFile', "Open Workspace Configuration"),
				run: () => this.commandService.executeCommand('workbench.action.openWorkspaceConfigFile')
			}]
		);
S
Sandeep Somavarapu 已提交
185 186
	}

187
	private doEnterWorkspace(mainSidePromise: () => TPromise<IEnterWorkspaceResult>): TPromise<void> {
188

189
		// Stop the extension host first to give extensions most time to shutdown
190
		this.extensionService.stopExtensionHost();
S
Sandeep Somavarapu 已提交
191
		let extensionHostStarted: boolean = false;
192

193 194
		const startExtensionHost = () => {
			this.extensionService.startExtensionHost();
S
Sandeep Somavarapu 已提交
195
			extensionHostStarted = true;
196 197
		};

198
		return mainSidePromise().then(result => {
199

200 201 202
			// Migrate storage and settings if we are to enter a workspace
			if (result) {
				return this.migrate(result.workspace).then(() => {
203

204
					// Reinitialize backup service
B
Benjamin Pasero 已提交
205 206 207
					if (this.backupFileService instanceof BackupFileService) {
						this.backupFileService.initialize(result.backupPath);
					}
208 209

					// Reinitialize configuration service
210
					const workspaceImpl = this.contextService as WorkspaceService;
S
Sandeep Somavarapu 已提交
211
					return workspaceImpl.initialize(result.workspace, startExtensionHost);
212 213 214
				});
			}

215
			return TPromise.as(void 0);
S
Sandeep Somavarapu 已提交
216 217 218 219
		}).then(null, error => {
			if (!extensionHostStarted) {
				startExtensionHost(); // start the extension host if not started
			}
B
Benjamin Pasero 已提交
220 221
			return TPromise.wrapError(error);
		});
222 223
	}

B
Benjamin Pasero 已提交
224
	private migrate(toWorkspace: IWorkspaceIdentifier): TPromise<void> {
225 226

		// Storage (UI State) migration
B
Benjamin Pasero 已提交
227
		this.migrateStorage(toWorkspace);
228

229 230 231 232 233 234
		// Settings migration (only if we come from a folder workspace)
		if (this.contextService.getWorkbenchState() === WorkbenchState.FOLDER) {
			return this.copyWorkspaceSettings(toWorkspace);
		}

		return TPromise.as(void 0);
235 236
	}

B
Benjamin Pasero 已提交
237
	private migrateStorage(toWorkspace: IWorkspaceIdentifier): void {
238 239 240

		// TODO@Ben revisit this when we move away from local storage to a file based approach
		const storageImpl = this.storageService as StorageService;
B
Benjamin Pasero 已提交
241 242
		const newWorkspaceId = migrateStorageToMultiRootWorkspace(storageImpl.workspaceId, toWorkspace, storageImpl.workspaceStorage);
		storageImpl.setWorkspaceId(newWorkspaceId);
243 244
	}

B
Benjamin Pasero 已提交
245
	copyWorkspaceSettings(toWorkspace: IWorkspaceIdentifier): TPromise<void> {
246 247 248
		const configurationProperties = Registry.as<IConfigurationRegistry>(ConfigurationExtensions.Configuration).getConfigurationProperties();
		const targetWorkspaceConfiguration = {};
		for (const key of this.workspaceConfigurationService.keys().workspace) {
S
Sandeep Somavarapu 已提交
249
			if (configurationProperties[key] && !configurationProperties[key].notMultiRootAdopted && configurationProperties[key].scope === ConfigurationScope.WINDOW) {
250
				targetWorkspaceConfiguration[key] = this.workspaceConfigurationService.inspect(key).workspace;
251 252 253
			}
		}

B
Benjamin Pasero 已提交
254
		return this.jsonEditingService.write(URI.file(toWorkspace.configPath), { key: 'settings', value: targetWorkspaceConfiguration }, true);
255
	}
J
Johannes Rieken 已提交
256
}