forked from microsoft/vscode-python-environments
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunAsTask.ts
More file actions
55 lines (49 loc) · 1.78 KB
/
runAsTask.ts
File metadata and controls
55 lines (49 loc) · 1.78 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
import {
ShellExecution,
Task,
TaskExecution,
TaskPanelKind,
TaskRevealKind,
TaskScope,
Uri,
WorkspaceFolder,
} from 'vscode';
import { PythonTaskExecutionOptions } from '../../api';
import { getWorkspaceFolder } from '../../common/workspace.apis';
import { PythonEnvironment } from '../../api';
import { executeTask } from '../../common/tasks.apis';
function getWorkspaceFolderOrDefault(uri?: Uri): WorkspaceFolder | TaskScope {
const workspace = uri ? getWorkspaceFolder(uri) : undefined;
return workspace ?? TaskScope.Global;
}
export async function runAsTask(
environment: PythonEnvironment,
options: PythonTaskExecutionOptions,
extra?: { reveal?: TaskRevealKind },
): Promise<TaskExecution> {
const workspace: WorkspaceFolder | TaskScope = getWorkspaceFolderOrDefault(options.project?.uri);
let executable =
environment.execInfo?.activatedRun?.executable ?? environment.execInfo?.run.executable ?? 'python';
// Quote the executable if it contains spaces and is not already quoted
if (executable.includes(' ') && !(executable.startsWith('"') && executable.endsWith('"'))) {
executable = `"${executable}"`;
}
const args = environment.execInfo?.activatedRun?.args ?? environment.execInfo?.run.args ?? [];
const allArgs = [...args, ...options.args];
const task = new Task(
{ type: 'python' },
workspace,
options.name,
'Python',
new ShellExecution(executable, allArgs, { cwd: options.cwd, env: options.env }),
'$python',
);
task.presentationOptions = {
reveal: extra?.reveal ?? TaskRevealKind.Silent,
echo: true,
panel: TaskPanelKind.Shared,
close: false,
showReuseMessage: true,
};
return executeTask(task);
}