-
Notifications
You must be signed in to change notification settings - Fork 30.3k
/
Copy pathsystemVariables.ts
89 lines (74 loc) · 2.63 KB
/
systemVariables.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import * as Paths from 'vs/base/common/paths';
import URI from 'vs/base/common/uri';
import { AbstractSystemVariables } from 'vs/base/common/parsers';
import * as WorkbenchEditorCommon from 'vs/workbench/common/editor';
import { IWorkbenchEditorService } from 'vs/workbench/services/editor/common/editorService';
import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace';
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
export class SystemVariables extends AbstractSystemVariables {
private _workspaceRoot: string;
private _execPath: string;
// Optional workspaceRoot there to be used in tests.
constructor(
private editorService: IWorkbenchEditorService,
contextService: IWorkspaceContextService,
environmentService: IEnvironmentService,
workspaceRoot: URI = null,
envVariables: { [key: string]: string } = process.env
) {
super();
let fsPath = '';
if (workspaceRoot || (contextService && contextService.getWorkspace())) {
fsPath = workspaceRoot ? workspaceRoot.fsPath : contextService.getWorkspace().resource.fsPath;
}
this._workspaceRoot = Paths.normalize(fsPath, true);
this._execPath = environmentService.execPath;
Object.keys(envVariables).forEach(key => {
this[`env.${key}`] = envVariables[key];
});
}
public get execPath(): string {
return this._execPath;
}
public set execPath(value: string) {
this._execPath = value;
}
public get cwd(): string {
return this.workspaceRoot;
}
public get workspaceRoot(): string {
return this._workspaceRoot;
}
public get file(): string {
return this.getFilePath();
}
public get relativeFile(): string {
return (this.workspaceRoot) ? Paths.relative(this.workspaceRoot, this.file) : this.file;
}
public get fileBasename(): string {
return Paths.basename(this.getFilePath());
}
public get fileDirname(): string {
return Paths.dirname(this.getFilePath());
}
public get fileExtname(): string {
return Paths.extname(this.getFilePath());
}
private getFilePath(): string {
let input = this.editorService.getActiveEditorInput();
if (!input) {
return '';
}
let fei = WorkbenchEditorCommon.asFileEditorInput(input);
if (!fei) {
return '';
}
let resource = fei.getResource();
return Paths.normalize(resource.fsPath, true);
}
}