workspacesMainService.ts 8.3 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, IStoredWorkspaceFolder } 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';
B
Benjamin Pasero 已提交
15 16 17 18 19 20 21
import { isLinux } from 'vs/base/common/platform';
import { copy, delSync, readdirSync } from 'vs/base/node/extfs';
import { nfcall } from 'vs/base/common/async';
import Event, { Emitter } from 'vs/base/common/event';
import { ILogService } from 'vs/platform/log/common/log';
import { isEqual } from 'vs/base/common/paths';
import { coalesce } from 'vs/base/common/arrays';
22 23 24 25 26 27 28 29
import { createHash } from 'crypto';
import URI from 'vs/base/common/uri';

// TODO@Ben migration
export interface ILegacyStoredWorkspace {
	id: string;
	folders: string[];
}
B
Benjamin Pasero 已提交
30 31 32 33 34

export class WorkspacesMainService implements IWorkspacesMainService {

	public _serviceBrand: any;

B
Benjamin Pasero 已提交
35
	protected workspacesHome: string;
B
Benjamin Pasero 已提交
36

37
	private _onWorkspaceSaved: Emitter<IWorkspaceSavedEvent>;
38
	private _onUntitledWorkspaceDeleted: Emitter<IWorkspaceIdentifier>;
39

40 41
	constructor(
		@IEnvironmentService private environmentService: IEnvironmentService,
42
		@ILogService private logService: ILogService
43
	) {
B
Benjamin Pasero 已提交
44
		this.workspacesHome = environmentService.workspacesHome;
45

46
		this._onWorkspaceSaved = new Emitter<IWorkspaceSavedEvent>();
47
		this._onUntitledWorkspaceDeleted = new Emitter<IWorkspaceIdentifier>();
48 49 50 51
	}

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

54 55
	public get onUntitledWorkspaceDeleted(): Event<IWorkspaceIdentifier> {
		return this._onUntitledWorkspaceDeleted.event;
56 57
	}

B
Benjamin Pasero 已提交
58 59 60 61 62 63 64 65
	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()));
	}

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

B
Benjamin Pasero 已提交
71 72 73 74 75 76 77 78
		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 {
79
		try {
B
Benjamin Pasero 已提交
80
			const rawWorkspace = JSON.parse(contents);
81 82 83

			const workspace = rawWorkspace as IStoredWorkspace;
			if (!Array.isArray(workspace.folders) || workspace.folders.length === 0) {
84 85
				this.logService.log(`${path} looks like an invalid workspace file.`);

86 87 88
				return null; // looks like an invalid workspace file
			}

89 90
			// TODO@Ben migration
			const legacyStoredWorkspace = rawWorkspace as ILegacyStoredWorkspace;
91
			if (legacyStoredWorkspace.folders.some(folder => typeof folder === 'string')) {
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
				(rawWorkspace as IStoredWorkspace).folders = legacyStoredWorkspace.folders.map(folder => ({ path: URI.parse(folder).fsPath }));
				writeFileSync(path, JSON.stringify(rawWorkspace, null, '\t'));
			}

			let absoluteFolders: IStoredWorkspaceFolder[] = [];
			workspace.folders.forEach(folder => {
				if (isAbsolute(folder.path)) {
					absoluteFolders.push(folder);
				} else {
					absoluteFolders.push({
						path: resolve(dirname(path), folder.path) // relative paths get resolved against the workspace location
					});
				}
			});

			return {
				id: this.getWorkspaceId(path),
				configPath: path,
				folders: absoluteFolders
			};
112
		} catch (error) {
113 114
			this.logService.log(`${path} cannot be parsed as JSON file (${error}).`);

115 116
			return null; // unable to read or parse as workspace file
		}
B
Benjamin Pasero 已提交
117 118
	}

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

B
Benjamin Pasero 已提交
123
	public createWorkspace(folders: string[]): TPromise<IWorkspaceIdentifier> {
124 125 126 127 128 129 130 131 132 133 134 135
		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 已提交
136 137
		}

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

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

		return workspace;
	}

	private createUntitledWorkspace(folders: string[]): { workspace: IWorkspaceIdentifier, configParent: string, storedWorkspace: IStoredWorkspace } {
146 147 148
		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 已提交
149

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

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

166 167 168 169 170 171
	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 已提交
172
	}
B
Benjamin Pasero 已提交
173 174 175 176

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

	public saveWorkspace(workspace: IWorkspaceIdentifier, target: string): TPromise<IWorkspaceIdentifier> {
B
Benjamin Pasero 已提交
179 180 181 182 183 184 185

		// Return early if target is same as source
		if (isEqual(workspace.configPath, target, !isLinux)) {
			return TPromise.as(workspace);
		}

		// Copy to new target
186
		return nfcall(copy, workspace.configPath, target).then(() => {
187
			const savedWorkspaceIdentifier = { id: this.getWorkspaceId(target), configPath: target };
188

189
			// Event
190
			this._onWorkspaceSaved.fire({ workspace: savedWorkspaceIdentifier, oldConfigPath: workspace.configPath });
191

192
			// Delete untitled workspace
193
			this.deleteUntitledWorkspaceSync(workspace);
194

195
			return savedWorkspaceIdentifier;
196 197
		});
	}
198

199
	public deleteUntitledWorkspaceSync(workspace: IWorkspaceIdentifier): void {
200
		if (!this.isUntitledWorkspace(workspace)) {
201
			return; // only supported for untitled workspaces
202 203
		}

204
		// Delete from disk
205
		this.doDeleteUntitledWorkspaceSync(workspace.configPath);
206 207

		// Event
208
		this._onUntitledWorkspaceDeleted.fire(workspace);
209
	}
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239

	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 已提交
240
}