Skip to content

Commit f8a935c

Browse files
authored
Code execution APIs (#15)
1 parent d59fadc commit f8a935c

File tree

9 files changed

+281
-169
lines changed

9 files changed

+281
-169
lines changed

src/api.ts

Lines changed: 151 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Uri, Disposable, MarkdownString, Event, LogOutputChannel, ThemeIcon, Terminal } from 'vscode';
1+
import { Uri, Disposable, MarkdownString, Event, LogOutputChannel, ThemeIcon, Terminal, TaskExecution } from 'vscode';
22

33
/**
44
* The path to an icon, or a theme-specific configuration of icons.
@@ -767,13 +767,11 @@ export interface Installable {
767767
readonly uri?: Uri;
768768
}
769769

770-
export interface PythonTaskResult {}
771-
772770
export interface PythonProcess {
773771
/**
774772
* The process ID of the Python process.
775773
*/
776-
readonly pid: number;
774+
readonly pid?: number;
777775

778776
/**
779777
* The standard input of the Python process.
@@ -798,10 +796,10 @@ export interface PythonProcess {
798796
/**
799797
* Event that is fired when the Python process exits.
800798
*/
801-
onExit: Event<number>;
799+
onExit(listener: (code: number | null, signal: NodeJS.Signals | null) => void): void;
802800
}
803801

804-
export interface PythonEnvironmentManagerApi {
802+
export interface PythonEnvironmentManagerRegistrationApi {
805803
/**
806804
* Register an environment manager implementation.
807805
*
@@ -810,7 +808,9 @@ export interface PythonEnvironmentManagerApi {
810808
* @see {@link EnvironmentManager}
811809
*/
812810
registerEnvironmentManager(manager: EnvironmentManager): Disposable;
811+
}
813812

813+
export interface PythonEnvironmentItemApi {
814814
/**
815815
* Create a Python environment item from the provided environment info. This item is used to interact
816816
* with the environment.
@@ -820,7 +820,9 @@ export interface PythonEnvironmentManagerApi {
820820
* @returns The Python environment.
821821
*/
822822
createPythonEnvironmentItem(info: PythonEnvironmentInfo, manager: EnvironmentManager): PythonEnvironment;
823+
}
823824

825+
export interface PythonEnvironmentManagementApi {
824826
/**
825827
* Create a Python environment using environment manager associated with the scope.
826828
*
@@ -836,7 +838,9 @@ export interface PythonEnvironmentManagerApi {
836838
* @returns A promise that resolves when the environment has been removed.
837839
*/
838840
removeEnvironment(environment: PythonEnvironment): Promise<void>;
841+
}
839842

843+
export interface PythonEnvironmentsApi {
840844
/**
841845
* Initiates a refresh of Python environments within the specified scope.
842846
* @param scope - The scope within which to search for environments.
@@ -857,6 +861,16 @@ export interface PythonEnvironmentManagerApi {
857861
*/
858862
onDidChangeEnvironments: Event<DidChangeEnvironmentsEventArgs>;
859863

864+
/**
865+
* This method is used to get the details missing from a PythonEnvironment. Like
866+
* {@link PythonEnvironment.execInfo} and other details.
867+
*
868+
* @param context : The PythonEnvironment or Uri for which details are required.
869+
*/
870+
resolveEnvironment(context: ResolveEnvironmentContext): Promise<PythonEnvironment | undefined>;
871+
}
872+
873+
export interface PythonProjectEnvironmentApi {
860874
/**
861875
* Sets the current Python environment within the specified scope.
862876
* @param scope - The scope within which to set the environment.
@@ -876,17 +890,16 @@ export interface PythonEnvironmentManagerApi {
876890
* @see {@link DidChangeEnvironmentEventArgs}
877891
*/
878892
onDidChangeEnvironment: Event<DidChangeEnvironmentEventArgs>;
879-
880-
/**
881-
* This method is used to get the details missing from a PythonEnvironment. Like
882-
* {@link PythonEnvironment.execInfo} and other details.
883-
*
884-
* @param context : The PythonEnvironment or Uri for which details are required.
885-
*/
886-
resolveEnvironment(context: ResolveEnvironmentContext): Promise<PythonEnvironment | undefined>;
887893
}
888894

889-
export interface PythonPackageManagerApi {
895+
export interface PythonEnvironmentManagerApi
896+
extends PythonEnvironmentManagerRegistrationApi,
897+
PythonEnvironmentItemApi,
898+
PythonEnvironmentManagementApi,
899+
PythonEnvironmentsApi,
900+
PythonProjectEnvironmentApi {}
901+
902+
export interface PythonPackageManagerRegistrationApi {
890903
/**
891904
* Register a package manager implementation.
892905
*
@@ -895,24 +908,9 @@ export interface PythonPackageManagerApi {
895908
* @see {@link PackageManager}
896909
*/
897910
registerPackageManager(manager: PackageManager): Disposable;
911+
}
898912

899-
/**
900-
* Install packages into a Python Environment.
901-
*
902-
* @param environment The Python Environment into which packages are to be installed.
903-
* @param packages The packages to install.
904-
* @param options Options for installing packages.
905-
*/
906-
installPackages(environment: PythonEnvironment, packages: string[], options: PackageInstallOptions): Promise<void>;
907-
908-
/**
909-
* Uninstall packages from a Python Environment.
910-
*
911-
* @param environment The Python Environment from which packages are to be uninstalled.
912-
* @param packages The packages to uninstall.
913-
*/
914-
uninstallPackages(environment: PythonEnvironment, packages: PackageInfo[] | string[]): Promise<void>;
915-
913+
export interface PythonPackageGetterApi {
916914
/**
917915
* Refresh the list of packages in a Python Environment.
918916
*
@@ -934,7 +932,9 @@ export interface PythonPackageManagerApi {
934932
* @see {@link DidChangePackagesEventArgs}
935933
*/
936934
onDidChangePackages: Event<DidChangePackagesEventArgs>;
935+
}
937936

937+
export interface PythonPackageItemApi {
938938
/**
939939
* Create a package item from the provided package info.
940940
*
@@ -946,38 +946,46 @@ export interface PythonPackageManagerApi {
946946
createPackageItem(info: PackageInfo, environment: PythonEnvironment, manager: PackageManager): Package;
947947
}
948948

949-
/**
950-
* The API for interacting with Python projects. A project in python is any folder or file that is a contained
951-
* in some manner. For example, a PEP-723 compliant file can be treated as a project. A folder with a `pyproject.toml`,
952-
* or just python files can be treated as a project. All this allows you to do is set a python environment for that project.
953-
*
954-
* By default all `vscode.workspace.workspaceFolders` are treated as projects.
955-
*/
956-
export interface PythonProjectApi {
949+
export interface PythonPackageManagementApi {
957950
/**
958-
* Add a python project or projects to the list of projects.
951+
* Install packages into a Python Environment.
959952
*
960-
* @param projects The project or projects to add.
953+
* @param environment The Python Environment into which packages are to be installed.
954+
* @param packages The packages to install.
955+
* @param options Options for installing packages.
961956
*/
962-
addPythonProject(projects: PythonProject | PythonProject[]): void;
957+
installPackages(environment: PythonEnvironment, packages: string[], options: PackageInstallOptions): Promise<void>;
963958

964959
/**
965-
* Remove a python project from the list of projects.
960+
* Uninstall packages from a Python Environment.
966961
*
967-
* @param project The project to remove.
962+
* @param environment The Python Environment from which packages are to be uninstalled.
963+
* @param packages The packages to uninstall.
968964
*/
969-
removePythonProject(project: PythonProject): void;
965+
uninstallPackages(environment: PythonEnvironment, packages: PackageInfo[] | string[]): Promise<void>;
966+
}
970967

968+
export interface PythonPackageManagerApi
969+
extends PythonPackageManagerRegistrationApi,
970+
PythonPackageGetterApi,
971+
PythonEnvironmentManagerApi,
972+
PythonPackageItemApi {}
973+
974+
export interface PythonProjectCreationApi {
971975
/**
972-
* Get all python projects.
976+
* Register a Python project creator.
977+
*
978+
* @param creator The project creator to register.
979+
* @returns A disposable that can be used to unregister the project creator.
980+
* @see {@link PythonProjectCreator}
973981
*/
974-
getPythonProjects(): readonly PythonProject[];
975-
982+
registerPythonProjectCreator(creator: PythonProjectCreator): Disposable;
983+
}
984+
export interface PythonProjectGetterApi {
976985
/**
977-
* Event raised when python projects are added or removed.
978-
* @see {@link DidChangePythonProjectsEventArgs}
986+
* Get all python projects.
979987
*/
980-
onDidChangePythonProjects: Event<DidChangePythonProjectsEventArgs>;
988+
getPythonProjects(): readonly PythonProject[];
981989

982990
/**
983991
* Get the python project for a given URI.
@@ -986,52 +994,110 @@ export interface PythonProjectApi {
986994
* @returns The project or `undefined` if not found.
987995
*/
988996
getPythonProject(uri: Uri): PythonProject | undefined;
997+
}
989998

999+
export interface PythonProjectModifyApi {
9901000
/**
991-
* Register a Python project creator.
1001+
* Add a python project or projects to the list of projects.
9921002
*
993-
* @param creator The project creator to register.
994-
* @returns A disposable that can be used to unregister the project creator.
995-
* @see {@link PythonProjectCreator}
1003+
* @param projects The project or projects to add.
9961004
*/
997-
registerPythonProjectCreator(creator: PythonProjectCreator): Disposable;
1005+
addPythonProject(projects: PythonProject | PythonProject[]): void;
1006+
1007+
/**
1008+
* Remove a python project from the list of projects.
1009+
*
1010+
* @param project The project to remove.
1011+
*/
1012+
removePythonProject(project: PythonProject): void;
1013+
1014+
/**
1015+
* Event raised when python projects are added or removed.
1016+
* @see {@link DidChangePythonProjectsEventArgs}
1017+
*/
1018+
onDidChangePythonProjects: Event<DidChangePythonProjectsEventArgs>;
9981019
}
9991020

1000-
export interface PythonExecutionApi {
1021+
/**
1022+
* The API for interacting with Python projects. A project in python is any folder or file that is a contained
1023+
* in some manner. For example, a PEP-723 compliant file can be treated as a project. A folder with a `pyproject.toml`,
1024+
* or just python files can be treated as a project. All this allows you to do is set a python environment for that project.
1025+
*
1026+
* By default all `vscode.workspace.workspaceFolders` are treated as projects.
1027+
*/
1028+
export interface PythonProjectApi extends PythonProjectCreationApi, PythonProjectGetterApi, PythonProjectModifyApi {}
1029+
1030+
export interface PythonTerminalCreateApi {
10011031
createTerminal(
1002-
cwd: string | Uri | PythonProject,
1003-
environment?: PythonEnvironment,
1004-
envVars?: { [key: string]: string },
1005-
): Promise<Terminal>;
1006-
runInTerminal(
10071032
environment: PythonEnvironment,
1008-
cwd: string | Uri | PythonProject,
1009-
command: string,
1010-
args?: string[],
1033+
cwd: string | Uri,
1034+
envVars?: { [key: string]: string },
10111035
): Promise<Terminal>;
1036+
}
1037+
1038+
export interface PythonTerminalExecutionOptions {
1039+
cwd: string | Uri;
1040+
args?: string[];
1041+
1042+
show?: boolean;
1043+
}
1044+
1045+
export interface PythonTerminalRunApi {
1046+
runInTerminal(environment: PythonEnvironment, options: PythonTerminalExecutionOptions): Promise<Terminal>;
10121047
runInDedicatedTerminal(
1013-
terminalKey: string | Uri,
1048+
terminalKey: Uri,
10141049
environment: PythonEnvironment,
1015-
cwd: string | Uri | PythonProject,
1016-
command: string,
1017-
args?: string[],
1050+
options: PythonTerminalExecutionOptions,
10181051
): Promise<Terminal>;
1019-
runAsTask(
1020-
environment: PythonEnvironment,
1021-
cwd: string | Uri | PythonProject,
1022-
command: string,
1023-
args?: string[],
1024-
envVars?: { [key: string]: string },
1025-
): Promise<PythonTaskResult>;
1026-
runInBackground(
1027-
environment: PythonEnvironment,
1028-
cwd: string | Uri | PythonProject,
1029-
command: string,
1030-
args?: string[],
1031-
envVars?: { [key: string]: string },
1032-
): Promise<PythonProcess>;
10331052
}
1053+
1054+
/**
1055+
*
1056+
* Example:
1057+
* * Running Script: `python myscript.py --arg1`
1058+
* ```typescript
1059+
* {
1060+
* args: ["myscript.py", "--arg1"]
1061+
* }
1062+
* ```
1063+
* * Running a module: `python -m my_module --arg1`
1064+
* ```typescript
1065+
* {
1066+
* args: ["-m", "my_module", "--arg1"]
1067+
* }
1068+
* ```
1069+
*/
1070+
export interface PythonTaskExecutionOptions {
1071+
project?: PythonProject;
1072+
args: string[];
1073+
cwd?: string;
1074+
env?: { [key: string]: string };
1075+
name: string;
1076+
}
1077+
1078+
export interface PythonTaskRunApi {
1079+
runAsTask(environment: PythonEnvironment, options: PythonTaskExecutionOptions): Promise<TaskExecution>;
1080+
}
1081+
1082+
export interface PythonBackgroundRunOptions {
1083+
args: string[];
1084+
cwd?: string;
1085+
env?: { [key: string]: string };
1086+
}
1087+
export interface PythonBackgroundRunApi {
1088+
runInBackground(environment: PythonEnvironment, options: PythonBackgroundRunOptions): Promise<PythonProcess>;
1089+
}
1090+
1091+
export interface PythonExecutionApi
1092+
extends PythonTerminalCreateApi,
1093+
PythonTerminalRunApi,
1094+
PythonTaskRunApi,
1095+
PythonBackgroundRunApi {}
10341096
/**
10351097
* The API for interacting with Python environments, package managers, and projects.
10361098
*/
1037-
export interface PythonEnvironmentApi extends PythonEnvironmentManagerApi, PythonPackageManagerApi, PythonProjectApi {}
1099+
export interface PythonEnvironmentApi
1100+
extends PythonEnvironmentManagerApi,
1101+
PythonPackageManagerApi,
1102+
PythonProjectApi,
1103+
PythonExecutionApi {}

src/extension.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export async function activate(context: ExtensionContext): Promise<PythonEnviron
7070
registerAutoProjectProvider(projectCreators),
7171
);
7272

73-
setPythonApi(envManagers, projectManager, projectCreators);
73+
setPythonApi(envManagers, projectManager, projectCreators, terminalManager);
7474

7575
const managerView = new EnvManagerView(envManagers);
7676
context.subscriptions.push(managerView);

0 commit comments

Comments
 (0)