workspacesMainService.ts 7.2 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 } from 'vs/base/node/pfs';
14
import { readFileSync, writeFileSync } 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
	}

58
	public resolveWorkspaceSync(path: string): IResolvedWorkspace {
59
		const isWorkspace = this.isInsideWorkspacesHome(path) || extname(path) === `.${WORKSPACE_EXTENSION}`;
60 61 62 63 64
		if (!isWorkspace) {
			return null; // does not look like a valid workspace config file
		}

		try {
65 66 67 68
			const rawWorkspace = JSON.parse(readFileSync(path, 'utf8'));

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

71 72 73
				return null; // looks like an invalid workspace file
			}

74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
			// TODO@Ben migration
			const legacyStoredWorkspace = rawWorkspace as ILegacyStoredWorkspace;
			if (typeof legacyStoredWorkspace.id === 'string') {
				delete legacyStoredWorkspace.id;
				(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
			};
98
		} catch (error) {
99 100
			this.logService.log(`${path} cannot be parsed as JSON file (${error}).`);

101 102
			return null; // unable to read or parse as workspace file
		}
B
Benjamin Pasero 已提交
103 104
	}

105 106 107 108
	private isInsideWorkspacesHome(path: string): boolean {
		return isParent(path, this.environmentService.workspacesHome, !isLinux /* ignore case */);
	}

B
Benjamin Pasero 已提交
109 110 111 112 113
	public createWorkspace(folders: string[]): TPromise<IWorkspaceIdentifier> {
		if (!folders.length) {
			return TPromise.wrapError(new Error('Creating a workspace requires at least one folder.'));
		}

114 115 116
		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 已提交
117

118
		return mkdirp(untitledWorkspaceConfigFolder).then(() => {
B
Benjamin Pasero 已提交
119
			const storedWorkspace: IStoredWorkspace = {
120 121 122
				folders: folders.map(folder => ({
					path: folder
				}))
B
Benjamin Pasero 已提交
123 124
			};

125 126 127
			return writeFile(untitledWorkspaceConfigPath, JSON.stringify(storedWorkspace, null, '\t')).then(() => ({
				id: this.getWorkspaceId(untitledWorkspaceConfigPath),
				configPath: untitledWorkspaceConfigPath
B
Benjamin Pasero 已提交
128 129 130 131
			}));
		});
	}

132 133 134 135 136 137
	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 已提交
138
	}
B
Benjamin Pasero 已提交
139 140 141 142

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

	public saveWorkspace(workspace: IWorkspaceIdentifier, target: string): TPromise<IWorkspaceIdentifier> {
B
Benjamin Pasero 已提交
145 146 147 148 149 150 151

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

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

155
			// Event
156
			this._onWorkspaceSaved.fire({ workspace: savedWorkspaceIdentifier, oldConfigPath: workspace.configPath });
157

158
			// Delete untitled workspace
159
			this.deleteUntitledWorkspaceSync(workspace);
160

161
			return savedWorkspaceIdentifier;
162 163
		});
	}
164

165
	public deleteUntitledWorkspaceSync(workspace: IWorkspaceIdentifier): void {
166
		if (!this.isUntitledWorkspace(workspace)) {
167
			return; // only supported for untitled workspaces
168 169
		}

170
		// Delete from disk
171
		this.doDeleteUntitledWorkspaceSync(workspace.configPath);
172 173

		// Event
174
		this._onUntitledWorkspaceDeleted.fire(workspace);
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

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