forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathexec.ts
More file actions
71 lines (67 loc) · 2.08 KB
/
exec.ts
File metadata and controls
71 lines (67 loc) · 2.08 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
/**
* A representation of the information needed to run a Python executable.
*
* @prop command - the executable to execute in a new OS process
* @prop args - the full list of arguments with which to invoke the command
* @prop python - the command + the arguments needed just to invoke Python
* @prop pythonExecutable - the path the the Python executable
*/
export type PythonExecInfo = {
command: string;
args: string[];
python: string[];
pythonExecutable: string;
};
/**
* Compose Python execution info for the given executable.
*
* @param python - the path (or command + arguments) to use to invoke Python
* @param pythonArgs - any extra arguments to use when running Python
*/
export function buildPythonExecInfo(
python: string | string[],
pythonArgs?: string[],
pythonExecutable?: string,
): PythonExecInfo {
if (Array.isArray(python)) {
const args = python.slice(1);
if (pythonArgs) {
args.push(...pythonArgs);
}
return {
args,
command: python[0],
python: [...python],
pythonExecutable: pythonExecutable ?? python[python.length - 1],
};
}
return {
command: python,
args: pythonArgs || [],
python: [python],
pythonExecutable: python,
};
}
/**
* Create a copy, optionally adding to the args to pass to Python.
*
* @param orig - the object to copy
* @param extraPythonArgs - any arguments to add to the end of `orig.args`
*/
export function copyPythonExecInfo(orig: PythonExecInfo, extraPythonArgs?: string[]): PythonExecInfo {
const info = {
command: orig.command,
args: [...orig.args],
python: [...orig.python],
pythonExecutable: orig.pythonExecutable,
};
if (extraPythonArgs) {
info.args.push(...extraPythonArgs);
}
if (info.pythonExecutable === undefined) {
info.pythonExecutable = info.python[info.python.length - 1]; // Default case
}
return info;
}