workspacesMainService.ts 7.8 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, 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
	}

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

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

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

B
Benjamin Pasero 已提交
108
	public createWorkspace(folders: string[]): TPromise<IWorkspaceIdentifier> {
109 110 111 112 113 114 115 116 117 118 119 120
		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 已提交
121 122
		}

123 124 125 126 127 128 129 130
		mkdirSync(configParent);

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

		return workspace;
	}

	private createUntitledWorkspace(folders: string[]): { workspace: IWorkspaceIdentifier, configParent: string, storedWorkspace: IStoredWorkspace } {
131 132 133
		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 已提交
134

135 136 137 138 139
		const storedWorkspace: IStoredWorkspace = {
			folders: folders.map(folder => ({
				path: folder
			}))
		};
B
Benjamin Pasero 已提交
140

141 142
		return {
			workspace: {
143 144
				id: this.getWorkspaceId(untitledWorkspaceConfigPath),
				configPath: untitledWorkspaceConfigPath
145 146 147 148
			},
			configParent: untitledWorkspaceConfigFolder,
			storedWorkspace
		};
B
Benjamin Pasero 已提交
149 150
	}

151 152 153 154 155 156
	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 已提交
157
	}
B
Benjamin Pasero 已提交
158 159 160 161

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

	public saveWorkspace(workspace: IWorkspaceIdentifier, target: string): TPromise<IWorkspaceIdentifier> {
B
Benjamin Pasero 已提交
164 165 166 167 168 169 170

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

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

174
			// Event
175
			this._onWorkspaceSaved.fire({ workspace: savedWorkspaceIdentifier, oldConfigPath: workspace.configPath });
176

177
			// Delete untitled workspace
178
			this.deleteUntitledWorkspaceSync(workspace);
179

180
			return savedWorkspaceIdentifier;
181 182
		});
	}
183

184
	public deleteUntitledWorkspaceSync(workspace: IWorkspaceIdentifier): void {
185
		if (!this.isUntitledWorkspace(workspace)) {
186
			return; // only supported for untitled workspaces
187 188
		}

189
		// Delete from disk
190
		this.doDeleteUntitledWorkspaceSync(workspace.configPath);
191 192

		// Event
193
		this._onUntitledWorkspaceDeleted.fire(workspace);
194
	}
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224

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