workspacesMainService.ts 2.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';

B
Benjamin Pasero 已提交
8
import { IWorkspacesMainService, IWorkspaceIdentifier, IStoredWorkspace, WORKSPACE_EXTNAME } from "vs/platform/workspaces/common/workspaces";
B
Benjamin Pasero 已提交
9 10 11 12 13
import { TPromise } from "vs/base/common/winjs.base";
import { isParent } from "vs/platform/files/common/files";
import { IEnvironmentService } from "vs/platform/environment/common/environment";
import { extname, join } from "path";
import { mkdirp, writeFile } from "vs/base/node/pfs";
14 15
import { readFileSync } from "fs";
import { isLinux } from "vs/base/common/platform";
B
Benjamin Pasero 已提交
16 17 18 19 20

export class WorkspacesMainService implements IWorkspacesMainService {

	public _serviceBrand: any;

B
Benjamin Pasero 已提交
21
	protected workspacesHome: string;
B
Benjamin Pasero 已提交
22 23 24 25 26

	constructor( @IEnvironmentService private environmentService: IEnvironmentService) {
		this.workspacesHome = environmentService.workspacesHome;
	}

27
	public resolveWorkspaceSync(path: string): IWorkspaceIdentifier {
28
		const isWorkspace = this.isInsideWorkspacesHome(path) || extname(path) === WORKSPACE_EXTNAME;
29 30 31 32 33 34
		if (!isWorkspace) {
			return null; // does not look like a valid workspace config file
		}

		try {
			const workspace = JSON.parse(readFileSync(path, 'utf8')) as IStoredWorkspace;
B
Benjamin Pasero 已提交
35
			if (typeof workspace.id !== 'string' || !Array.isArray(workspace.folders) || workspace.folders.length === 0) {
36 37 38 39 40 41 42 43 44 45
				return null; // looks like an invalid workspace file
			}

			return {
				id: workspace.id,
				configPath: path
			};
		} catch (error) {
			return null; // unable to read or parse as workspace file
		}
B
Benjamin Pasero 已提交
46 47
	}

48 49 50 51
	private isInsideWorkspacesHome(path: string): boolean {
		return isParent(path, this.environmentService.workspacesHome, !isLinux /* ignore case */);
	}

B
Benjamin Pasero 已提交
52 53 54 55 56
	public createWorkspace(folders: string[]): TPromise<IWorkspaceIdentifier> {
		if (!folders.length) {
			return TPromise.wrapError(new Error('Creating a workspace requires at least one folder.'));
		}

B
Benjamin Pasero 已提交
57 58 59 60 61 62 63 64 65 66 67 68
		const workspaceId = this.nextWorkspaceId();
		const workspaceConfigFolder = join(this.workspacesHome, workspaceId);
		const workspaceConfigPath = join(workspaceConfigFolder, 'workspace.json');

		return mkdirp(workspaceConfigFolder).then(() => {
			const storedWorkspace: IStoredWorkspace = {
				id: workspaceId,
				folders
			};

			return writeFile(workspaceConfigPath, JSON.stringify(storedWorkspace, null, '\t')).then(() => ({
				id: workspaceId,
69
				configPath: workspaceConfigPath
B
Benjamin Pasero 已提交
70 71 72 73 74 75 76 77
			}));
		});
	}

	private nextWorkspaceId(): string {
		return (Date.now() + Math.round(Math.random() * 1000)).toString();
	}
}