forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathapi.internal.ts
More file actions
179 lines (162 loc) · 6.1 KB
/
api.internal.ts
File metadata and controls
179 lines (162 loc) · 6.1 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { EventEmitter, Terminal, Uri, Disposable } from 'vscode';
import { getExtension } from '../common/vscodeApis/extensionsApi';
import {
GetEnvironmentScope,
PythonBackgroundRunOptions,
PythonEnvironment,
PythonEnvironmentApi,
PythonProcess,
RefreshEnvironmentsScope,
DidChangeEnvironmentEventArgs,
} from './types';
import { executeCommand } from '../common/vscodeApis/commandApis';
import { getConfiguration, getWorkspaceFolders } from '../common/vscodeApis/workspaceApis';
import { traceError, traceLog } from '../logging';
import { Interpreters } from '../common/utils/localize';
export const ENVS_EXTENSION_ID = 'ms-python.vscode-python-envs';
export function isEnvExtensionInstalled(): boolean {
return !!getExtension(ENVS_EXTENSION_ID);
}
/**
* Returns true if the Python Environments extension is installed and not explicitly
* disabled by the user. Mirrors the envs extension's own activation logic: it
* deactivates only when `python.useEnvironmentsExtension` is explicitly set to false
* at the global, workspace, or workspace-folder level.
*/
export function shouldEnvExtHandleActivation(): boolean {
if (!isEnvExtensionInstalled()) {
return false;
}
const config = getConfiguration('python');
const inspection = config.inspect<boolean>('useEnvironmentsExtension');
if (inspection?.globalValue === false || inspection?.workspaceValue === false) {
return false;
}
// The envs extension also checks folder-scoped settings in multi-root workspaces.
// Any single folder with the setting set to false causes the envs extension to
// deactivate entirely (window-wide), so we must mirror that here.
const workspaceFolders = getWorkspaceFolders();
if (workspaceFolders) {
for (const folder of workspaceFolders) {
const folderConfig = getConfiguration('python', folder.uri);
const folderInspection = folderConfig.inspect<boolean>('useEnvironmentsExtension');
if (folderInspection?.workspaceFolderValue === false) {
return false;
}
}
}
return true;
}
let _useExt: boolean | undefined;
export function useEnvExtension(): boolean {
if (_useExt !== undefined) {
return _useExt;
}
const config = getConfiguration('python');
const inExpSetting = config?.get<boolean>('useEnvironmentsExtension', false) ?? false;
// If extension is installed and in experiment, then use it.
_useExt = !!getExtension(ENVS_EXTENSION_ID) && inExpSetting;
return _useExt;
}
const onDidChangeEnvironmentEnvExtEmitter: EventEmitter<DidChangeEnvironmentEventArgs> = new EventEmitter<
DidChangeEnvironmentEventArgs
>();
export function onDidChangeEnvironmentEnvExt(
listener: (e: DidChangeEnvironmentEventArgs) => unknown,
thisArgs?: unknown,
disposables?: Disposable[],
): Disposable {
return onDidChangeEnvironmentEnvExtEmitter.event(listener, thisArgs, disposables);
}
let _extApi: PythonEnvironmentApi | undefined;
export async function getEnvExtApi(): Promise<PythonEnvironmentApi> {
if (_extApi) {
return _extApi;
}
const extension = getExtension(ENVS_EXTENSION_ID);
if (!extension) {
traceError(Interpreters.envExtActivationFailed);
throw new Error('Python Environments extension not found.');
}
if (!extension?.isActive) {
try {
await extension.activate();
} catch (ex) {
traceError(Interpreters.envExtActivationFailed, ex);
throw ex;
}
}
traceLog(Interpreters.envExtDiscoveryAttribution);
_extApi = extension.exports as PythonEnvironmentApi;
_extApi.onDidChangeEnvironment((e) => {
onDidChangeEnvironmentEnvExtEmitter.fire(e);
});
return _extApi;
}
export async function runInBackground(
environment: PythonEnvironment,
options: PythonBackgroundRunOptions,
): Promise<PythonProcess> {
const envExtApi = await getEnvExtApi();
return envExtApi.runInBackground(environment, options);
}
export async function getEnvironment(scope: GetEnvironmentScope): Promise<PythonEnvironment | undefined> {
const envExtApi = await getEnvExtApi();
const env = await envExtApi.getEnvironment(scope);
if (!env) {
traceLog(Interpreters.envExtNoActiveEnvironment);
}
return env;
}
export async function resolveEnvironment(pythonPath: string): Promise<PythonEnvironment | undefined> {
const envExtApi = await getEnvExtApi();
return envExtApi.resolveEnvironment(Uri.file(pythonPath));
}
export async function refreshEnvironments(scope: RefreshEnvironmentsScope): Promise<void> {
const envExtApi = await getEnvExtApi();
return envExtApi.refreshEnvironments(scope);
}
export async function runInTerminal(
resource: Uri | undefined,
args?: string[],
cwd?: string | Uri,
show?: boolean,
): Promise<Terminal> {
const envExtApi = await getEnvExtApi();
const env = await getEnvironment(resource);
const project = resource ? envExtApi.getPythonProject(resource) : undefined;
if (env && resource) {
return envExtApi.runInTerminal(env, {
cwd: cwd ?? project?.uri ?? process.cwd(),
args,
show,
});
}
throw new Error('Invalid arguments to run in terminal');
}
export async function runInDedicatedTerminal(
resource: Uri | undefined,
args?: string[],
cwd?: string | Uri,
show?: boolean,
): Promise<Terminal> {
const envExtApi = await getEnvExtApi();
const env = await getEnvironment(resource);
const project = resource ? envExtApi.getPythonProject(resource) : undefined;
if (env) {
return envExtApi.runInDedicatedTerminal(resource ?? 'global', env, {
cwd: cwd ?? project?.uri ?? process.cwd(),
args,
show,
});
}
throw new Error('Invalid arguments to run in dedicated terminal');
}
export async function clearCache(): Promise<void> {
const envExtApi = await getEnvExtApi();
if (envExtApi) {
await executeCommand('python-envs.clearCache');
}
}