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
171 lines (155 loc) · 5.58 KB
/
api.internal.ts
File metadata and controls
171 lines (155 loc) · 5.58 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { EventEmitter, Terminal, Uri, Disposable, ConfigurationTarget } 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 { IInterpreterPathService } from '../common/types';
import { getConfiguration } from '../common/vscodeApis/workspaceApis';
export const ENVS_EXTENSION_ID = 'ms-python.vscode-python-envs';
let _useExt: boolean | undefined;
export function useEnvExtension(): boolean {
if (_useExt !== undefined) {
return _useExt;
}
const inExpSetting = getConfiguration('python').get<boolean>('useEnvironmentsExtension', 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) {
throw new Error('Python Environments extension not found.');
}
if (!extension?.isActive) {
await extension.activate();
}
_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();
return envExtApi.getEnvironment(scope);
}
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,
interpreterPath?: string,
): Promise<Terminal> {
const envExtApi = await getEnvExtApi();
let env;
if (interpreterPath) {
env = await envExtApi.resolveEnvironment(Uri.file(interpreterPath));
} else {
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,
interpreterPath?: string,
): Promise<Terminal> {
const envExtApi = await getEnvExtApi();
let env;
if (interpreterPath) {
env = await envExtApi.resolveEnvironment(Uri.file(interpreterPath));
} else {
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');
}
}
export function registerEnvExtFeatures(
disposables: Disposable[],
interpreterPathService: IInterpreterPathService,
): void {
if (useEnvExtension()) {
disposables.push(
onDidChangeEnvironmentEnvExt(async (e: DidChangeEnvironmentEventArgs) => {
const previousPath = interpreterPathService.get(e.uri);
if (previousPath !== e.new?.environmentPath.fsPath) {
if (e.uri) {
await interpreterPathService.update(
e.uri,
ConfigurationTarget.WorkspaceFolder,
e.new?.environmentPath.fsPath,
);
} else {
await interpreterPathService.update(
undefined,
ConfigurationTarget.Global,
e.new?.environmentPath.fsPath,
);
}
}
}),
);
}
}