forked from mrsone40/.github-workflows
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettings.ts
More file actions
114 lines (100 loc) · 4.12 KB
/
settings.ts
File metadata and controls
114 lines (100 loc) · 4.12 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { ConfigurationChangeEvent, ConfigurationScope, WorkspaceConfiguration, WorkspaceFolder } from 'vscode';
import { getInterpreterDetails } from './python';
import { getConfiguration, getWorkspaceFolders } from './vscodeapi';
export interface ISettings {
cwd: string;
workspace: string;
args: string[];
path: string[];
interpreter: string[];
importStrategy: string;
showNotifications: string;
}
export function getExtensionSettings(namespace: string, includeInterpreter?: boolean): Promise<ISettings[]> {
return Promise.all(getWorkspaceFolders().map((w) => getWorkspaceSettings(namespace, w, includeInterpreter)));
}
function resolveVariables(value: string[], workspace?: WorkspaceFolder): string[] {
const substitutions = new Map<string, string>();
const home = process.env.HOME || process.env.USERPROFILE;
if (home) {
substitutions.set('${userHome}', home);
}
if (workspace) {
substitutions.set('${workspaceFolder}', workspace.uri.fsPath);
}
substitutions.set('${cwd}', process.cwd());
getWorkspaceFolders().forEach((w) => {
substitutions.set('${workspaceFolder:' + w.name + '}', w.uri.fsPath);
});
return value.map((s) => {
for (const [key, value] of substitutions) {
s = s.replace(key, value);
}
return s;
});
}
export function getInterpreterFromSetting(namespace: string, scope?: ConfigurationScope) {
const config = getConfiguration(namespace, scope);
return config.get<string[]>('interpreter');
}
export async function getWorkspaceSettings(
namespace: string,
workspace: WorkspaceFolder,
includeInterpreter?: boolean,
): Promise<ISettings> {
const config = getConfiguration(namespace, workspace.uri);
let interpreter: string[] = [];
if (includeInterpreter) {
interpreter = getInterpreterFromSetting(namespace, workspace) ?? [];
if (interpreter.length === 0) {
interpreter = (await getInterpreterDetails(workspace.uri)).path ?? [];
}
}
const workspaceSetting = {
cwd: workspace.uri.fsPath,
workspace: workspace.uri.toString(),
args: resolveVariables(config.get<string[]>(`args`) ?? [], workspace),
path: resolveVariables(config.get<string[]>(`path`) ?? [], workspace),
interpreter: resolveVariables(interpreter, workspace),
importStrategy: config.get<string>(`importStrategy`) ?? 'useBundled',
showNotifications: config.get<string>(`showNotifications`) ?? 'off',
};
return workspaceSetting;
}
function getGlobalValue<T>(config: WorkspaceConfiguration, key: string, defaultValue: T): T {
const inspect = config.inspect<T>(key);
return inspect?.globalValue ?? inspect?.defaultValue ?? defaultValue;
}
export async function getGlobalSettings(namespace: string, includeInterpreter?: boolean): Promise<ISettings> {
const config = getConfiguration(namespace);
let interpreter: string[] = [];
if (includeInterpreter) {
interpreter = getGlobalValue<string[]>(config, 'interpreter', []);
if (interpreter === undefined || interpreter.length === 0) {
interpreter = (await getInterpreterDetails()).path ?? [];
}
}
const setting = {
cwd: process.cwd(),
workspace: process.cwd(),
args: getGlobalValue<string[]>(config, 'args', []),
path: getGlobalValue<string[]>(config, 'path', []),
interpreter: interpreter,
importStrategy: getGlobalValue<string>(config, 'importStrategy', 'useBundled'),
showNotifications: getGlobalValue<string>(config, 'showNotifications', 'off'),
};
return setting;
}
export function checkIfConfigurationChanged(e: ConfigurationChangeEvent, namespace: string): boolean {
const settings = [
`${namespace}.args`,
`${namespace}.path`,
`${namespace}.interpreter`,
`${namespace}.importStrategy`,
`${namespace}.showNotifications`,
];
const changed = settings.map((s) => e.affectsConfiguration(s));
return changed.includes(true);
}