forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi.internal.ts
More file actions
144 lines (129 loc) · 4.65 KB
/
api.internal.ts
File metadata and controls
144 lines (129 loc) · 4.65 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
// 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 } 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';
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');
}
}