workspacesMainService.ts 9.7 KB
Newer Older
B
Benjamin Pasero 已提交
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';

8
import { IWorkspacesMainService, IWorkspaceIdentifier, IStoredWorkspace, WORKSPACE_EXTENSION, IWorkspaceSavedEvent, UNTITLED_WORKSPACE_NAME, IResolvedWorkspace } from 'vs/platform/workspaces/common/workspaces';
9
import { TPromise } from 'vs/base/common/winjs.base';
B
Benjamin Pasero 已提交
10 11
import { isParent } from 'vs/platform/files/common/files';
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
12
import { extname, join, dirname, isAbsolute, resolve } from 'path';
B
Benjamin Pasero 已提交
13
import { mkdirp, writeFile, readFile } from 'vs/base/node/pfs';
14
import { readFileSync, writeFileSync, existsSync, mkdirSync } from 'fs';
15
import { isLinux, isMacintosh } from 'vs/base/common/platform';
16
import { delSync, readdirSync } from 'vs/base/node/extfs';
B
Benjamin Pasero 已提交
17 18
import Event, { Emitter } from 'vs/base/common/event';
import { ILogService } from 'vs/platform/log/common/log';
19
import { isEqual } from 'vs/base/common/paths';
B
Benjamin Pasero 已提交
20
import { coalesce } from 'vs/base/common/arrays';
21
import { createHash } from 'crypto';
22
import * as json from 'vs/base/common/json';
23 24
import * as jsonEdit from 'vs/base/common/jsonEdit';
import { applyEdit } from 'vs/base/common/jsonFormatter';
25
import { massageFolderPathForWorkspace } from 'vs/platform/workspaces/node/workspaces';
26

B
Benjamin Pasero 已提交
27 28 29 30
export class WorkspacesMainService implements IWorkspacesMainService {

	public _serviceBrand: any;

B
Benjamin Pasero 已提交
31
	protected workspacesHome: string;
B
Benjamin Pasero 已提交
32

33
	private _onWorkspaceSaved: Emitter<IWorkspaceSavedEvent>;
34
	private _onUntitledWorkspaceDeleted: Emitter<IWorkspaceIdentifier>;
35

36 37
	constructor(
		@IEnvironmentService private environmentService: IEnvironmentService,
38
		@ILogService private logService: ILogService
39
	) {
B
Benjamin Pasero 已提交
40
		this.workspacesHome = environmentService.workspacesHome;
41

42
		this._onWorkspaceSaved = new Emitter<IWorkspaceSavedEvent>();
43
		this._onUntitledWorkspaceDeleted = new Emitter<IWorkspaceIdentifier>();
44 45 46 47
	}

	public get onWorkspaceSaved(): Event<IWorkspaceSavedEvent> {
		return this._onWorkspaceSaved.event;
B
Benjamin Pasero 已提交
48 49
	}

50 51
	public get onUntitledWorkspaceDeleted(): Event<IWorkspaceIdentifier> {
		return this._onUntitledWorkspaceDeleted.event;
52 53
	}

B
Benjamin Pasero 已提交
54 55 56 57 58 59 60 61
	public resolveWorkspace(path: string): TPromise<IResolvedWorkspace> {
		if (!this.isWorkspacePath(path)) {
			return TPromise.as(null); // does not look like a valid workspace config file
		}

		return readFile(path).then(contents => this.doResolveWorkspace(path, contents.toString()));
	}

62
	public resolveWorkspaceSync(path: string): IResolvedWorkspace {
B
Benjamin Pasero 已提交
63
		if (!this.isWorkspacePath(path)) {
64 65 66
			return null; // does not look like a valid workspace config file
		}

B
Benjamin Pasero 已提交
67 68 69 70 71 72 73 74
		return this.doResolveWorkspace(path, readFileSync(path, 'utf8'));
	}

	private isWorkspacePath(path: string): boolean {
		return this.isInsideWorkspacesHome(path) || extname(path) === `.${WORKSPACE_EXTENSION}`;
	}

	private doResolveWorkspace(path: string, contents: string): IResolvedWorkspace {
75
		try {
76
			const workspace = this.doParseStoredWorkspace(path, contents);
77

78
			// relative paths get resolved against the workspace location
79
			workspace.folders.forEach(folder => {
80
				if (!isAbsolute(folder.path)) {
81
					folder.path = resolve(dirname(path), folder.path);
82 83 84 85 86 87
				}
			});

			return {
				id: this.getWorkspaceId(path),
				configPath: path,
88
				folders: workspace.folders
89
			};
90
		} catch (error) {
91 92 93 94 95 96 97
			this.logService.log(error.toString());
		}

		return null;
	}

	private doParseStoredWorkspace(path: string, contents: string): IStoredWorkspace {
98 99

		// Parse workspace file
100 101
		let storedWorkspace: IStoredWorkspace;
		try {
102
			storedWorkspace = json.parse(contents); // use fault tolerant parser
103 104 105
		} catch (error) {
			throw new Error(`${path} cannot be parsed as JSON file (${error}).`);
		}
106

107 108 109 110 111 112
		// Filter out folders which do not have a path set
		if (Array.isArray(storedWorkspace.folders)) {
			storedWorkspace.folders = storedWorkspace.folders.filter(folder => !!folder.path);
		}

		// Validate
113 114
		if (!Array.isArray(storedWorkspace.folders) || storedWorkspace.folders.length === 0) {
			throw new Error(`${path} looks like an invalid workspace file.`);
115
		}
116 117

		return storedWorkspace;
B
Benjamin Pasero 已提交
118 119
	}

120 121 122 123
	private isInsideWorkspacesHome(path: string): boolean {
		return isParent(path, this.environmentService.workspacesHome, !isLinux /* ignore case */);
	}

B
Benjamin Pasero 已提交
124
	public createWorkspace(folders: string[]): TPromise<IWorkspaceIdentifier> {
125 126 127 128 129 130 131 132 133 134 135 136
		const { workspace, configParent, storedWorkspace } = this.createUntitledWorkspace(folders);

		return mkdirp(configParent).then(() => {
			return writeFile(workspace.configPath, JSON.stringify(storedWorkspace, null, '\t')).then(() => workspace);
		});
	}

	public createWorkspaceSync(folders: string[]): IWorkspaceIdentifier {
		const { workspace, configParent, storedWorkspace } = this.createUntitledWorkspace(folders);

		if (!existsSync(this.workspacesHome)) {
			mkdirSync(this.workspacesHome);
B
Benjamin Pasero 已提交
137 138
		}

139 140 141 142 143 144 145 146
		mkdirSync(configParent);

		writeFileSync(workspace.configPath, JSON.stringify(storedWorkspace, null, '\t'));

		return workspace;
	}

	private createUntitledWorkspace(folders: string[]): { workspace: IWorkspaceIdentifier, configParent: string, storedWorkspace: IStoredWorkspace } {
147 148 149
		const randomId = (Date.now() + Math.round(Math.random() * 1000)).toString();
		const untitledWorkspaceConfigFolder = join(this.workspacesHome, randomId);
		const untitledWorkspaceConfigPath = join(untitledWorkspaceConfigFolder, UNTITLED_WORKSPACE_NAME);
B
Benjamin Pasero 已提交
150

151 152 153 154 155
		const storedWorkspace: IStoredWorkspace = {
			folders: folders.map(folder => ({
				path: folder
			}))
		};
B
Benjamin Pasero 已提交
156

157 158
		return {
			workspace: {
159 160
				id: this.getWorkspaceId(untitledWorkspaceConfigPath),
				configPath: untitledWorkspaceConfigPath
161 162 163 164
			},
			configParent: untitledWorkspaceConfigFolder,
			storedWorkspace
		};
B
Benjamin Pasero 已提交
165 166
	}

167 168 169 170 171 172
	public getWorkspaceId(workspaceConfigPath: string): string {
		if (!isLinux) {
			workspaceConfigPath = workspaceConfigPath.toLowerCase(); // sanitize for platform file system
		}

		return createHash('md5').update(workspaceConfigPath).digest('hex');
B
Benjamin Pasero 已提交
173
	}
B
Benjamin Pasero 已提交
174 175 176 177

	public isUntitledWorkspace(workspace: IWorkspaceIdentifier): boolean {
		return this.isInsideWorkspacesHome(workspace.configPath);
	}
178

179
	public saveWorkspace(workspace: IWorkspaceIdentifier, targetConfigPath: string): TPromise<IWorkspaceIdentifier> {
B
Benjamin Pasero 已提交
180 181

		// Return early if target is same as source
182
		if (isEqual(workspace.configPath, targetConfigPath, !isLinux)) {
B
Benjamin Pasero 已提交
183 184 185
			return TPromise.as(workspace);
		}

186
		// Read the contents of the workspace file and resolve it
187 188
		return readFile(workspace.configPath).then(raw => {
			const rawWorkspaceContents = raw.toString();
189 190
			let storedWorkspace: IStoredWorkspace;
			try {
191
				storedWorkspace = this.doParseStoredWorkspace(workspace.configPath, rawWorkspaceContents);
192 193 194 195 196 197 198 199 200 201 202
			} catch (error) {
				return TPromise.wrapError(error);
			}

			const sourceConfigFolder = dirname(workspace.configPath);
			const targetConfigFolder = dirname(targetConfigPath);

			// Rewrite absolute paths to relative paths if the target workspace folder
			// is a parent of the location of the workspace file itself. Otherwise keep
			// using absolute paths.
			storedWorkspace.folders.forEach(folder => {
203 204 205 206
				if (!isAbsolute(folder.path)) {
					folder.path = resolve(sourceConfigFolder, folder.path); // relative paths get resolved against the workspace location
				}

207
				folder.path = massageFolderPathForWorkspace(folder.path, targetConfigFolder, storedWorkspace.folders);
208 209 210 211 212 213 214 215
			});

			// Preserve as much of the existing workspace as possible by using jsonEdit
			// and only changing the folders portion.
			let newRawWorkspaceContents = rawWorkspaceContents;
			const edits = jsonEdit.setProperty(rawWorkspaceContents, ['folders'], storedWorkspace.folders, { insertSpaces: false, tabSize: 4, eol: (isLinux || isMacintosh) ? '\n' : '\r\n' });
			edits.forEach(edit => {
				newRawWorkspaceContents = applyEdit(rawWorkspaceContents, edit);
216
			});
217

218
			return writeFile(targetConfigPath, newRawWorkspaceContents).then(() => {
219
				const savedWorkspaceIdentifier = { id: this.getWorkspaceId(targetConfigPath), configPath: targetConfigPath };
220

221 222
				// Event
				this._onWorkspaceSaved.fire({ workspace: savedWorkspaceIdentifier, oldConfigPath: workspace.configPath });
223

224 225 226 227 228
				// Delete untitled workspace
				this.deleteUntitledWorkspaceSync(workspace);

				return savedWorkspaceIdentifier;
			});
229 230
		});
	}
231

232
	public deleteUntitledWorkspaceSync(workspace: IWorkspaceIdentifier): void {
233
		if (!this.isUntitledWorkspace(workspace)) {
234
			return; // only supported for untitled workspaces
235 236
		}

237
		// Delete from disk
238
		this.doDeleteUntitledWorkspaceSync(workspace.configPath);
239 240

		// Event
241
		this._onUntitledWorkspaceDeleted.fire(workspace);
242
	}
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272

	private doDeleteUntitledWorkspaceSync(configPath: string): void {
		try {
			delSync(dirname(configPath));
		} catch (error) {
			this.logService.log(`Unable to delete untitled workspace ${configPath} (${error}).`);
		}
	}

	public getUntitledWorkspacesSync(): IWorkspaceIdentifier[] {
		let untitledWorkspacePaths: string[] = [];
		try {
			untitledWorkspacePaths = readdirSync(this.workspacesHome).map(folder => join(this.workspacesHome, folder, UNTITLED_WORKSPACE_NAME));
		} catch (error) {
			this.logService.log(`Unable to read folders in ${this.workspacesHome} (${error}).`);
		}

		const untitledWorkspaces: IWorkspaceIdentifier[] = coalesce(untitledWorkspacePaths.map(untitledWorkspacePath => {
			const workspace = this.resolveWorkspaceSync(untitledWorkspacePath);
			if (!workspace) {
				this.doDeleteUntitledWorkspaceSync(untitledWorkspacePath);

				return null; // invalid workspace
			}

			return { id: workspace.id, configPath: untitledWorkspacePath };
		}));

		return untitledWorkspaces;
	}
B
Benjamin Pasero 已提交
273
}