Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try shell integration proposed API for Python extension #23769

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
"description": "%walkthrough.pythonWelcome.description%",
"when": "workspacePlatform != webworker",
"steps": [
{
{
"id": "python.createPythonFolder",
"title": "%walkthrough.step.python.createPythonFolder.title%",
"description": "%walkthrough.step.python.createPythonFolder.description%",
Expand Down Expand Up @@ -1514,6 +1514,7 @@
"dependencies": {
"@iarna/toml": "^2.2.5",
"@vscode/extension-telemetry": "^0.8.4",
"@xterm/headless": "^5.5.0",
"arch": "^2.1.0",
"fs-extra": "^10.0.1",
"glob": "^7.2.0",
Expand Down
70 changes: 69 additions & 1 deletion src/client/terminals/envCollectionActivation/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.

import * as path from 'path';
import { Terminal as HeadlessTerminal } from '@xterm/headless';
import { inject, injectable } from 'inversify';
import {
MarkdownString,
Expand All @@ -10,6 +11,9 @@ import {
EnvironmentVariableScope,
EnvironmentVariableMutatorOptions,
ProgressLocation,
Terminal,
window,
Disposable,
} from 'vscode';
import { pathExists } from 'fs-extra';
import { IExtensionActivationService } from '../../activation/types';
Expand Down Expand Up @@ -42,6 +46,8 @@ import { ProgressService } from '../../common/application/progressService';

@injectable()
export class TerminalEnvVarCollectionService implements IExtensionActivationService, ITerminalEnvVarCollectionService {
// extends Disposable
// implements IExtensionActivationService, ITerminalEnvVarCollectionService {
public readonly supportedWorkspaceTypes = {
untrustedWorkspace: false,
virtualWorkspace: false,
Expand All @@ -67,6 +73,9 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ

private separator: string;

// private _shellIntegrationDisposableMap = this.register(new DisposableMap());

// problem: when extension is disposed, we want terminal env collection service to be disposed. When that is disposed, we want the listeners disposed as well
constructor(
@inject(IPlatformService) private readonly platform: IPlatformService,
@inject(IInterpreterService) private interpreterService: IInterpreterService,
Expand All @@ -84,7 +93,8 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ
@inject(IEnvironmentVariablesProvider)
private readonly environmentVariablesProvider: IEnvironmentVariablesProvider,
) {
this.separator = platform.osType === OSType.Windows ? ';' : ':';
// super();
// this.this.separator = platform.osType === OSType.Windows ? ';' : ':';
this.progressService = new ProgressService(this.shell);
}

Expand Down Expand Up @@ -166,6 +176,8 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ
this.progressService.hideProgress();
}

private _trackedTerminals: Set<Terminal> = new Set();

private async _applyCollectionImpl(resource: Resource, shell = this.applicationEnvironment.shell): Promise<void> {
const workspaceFolder = this.getWorkspaceFolder(resource);
const settings = this.configurationService.getSettings(resource);
Expand All @@ -181,6 +193,30 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ
undefined,
shell,
);
/// ////////////////////////////////////////////////////////////////
// TODO: Try to get environment variable using shell integration API here -- using hidden terminal.
// But first, try some dummy commands to see if I can get any sort of exit code.
const myTerm = window.createTerminal();
window.onDidChangeTerminalShellIntegration(async ({ terminal, shellIntegration }) => {
if (terminal === myTerm) {
const execution = shellIntegration.executeCommand(
'python /Users/anthonykim/Desktop/vscode-python/python_files/printEnvVariables.py',
);

// const stream = execution.read();
// for await(const data of stream) {
// traceLog(`HERE ${data} HERE I AM WITH THE DATA`);
// }
window.onDidEndTerminalShellExecution((event) => {
if (event.execution === execution) {
console.log(`Command exited with code ${event.exitCode}`);
traceLog(`HERE ${event.exitCode} HERE I AM WITH THE EXIT CODE`);
// const temp = event.exitCode;
}
});
}
});
/// ///////////////////////////////////////////////////////////
const env = activatedEnv ? normCaseKeys(activatedEnv) : undefined;
traceVerbose(`Activated environment variables for ${resource?.fsPath}`, env);
if (!env) {
Expand All @@ -202,7 +238,39 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ
resource,
shell,
);
// // TODO: Try to get environment variable using shell integration API here -- using hidden terminal.
// // But first, try some dummy commands to see if I can get any sort of exit code.
// const myTerm = window.createTerminal();

// map.set(
// terminal,
// window.onDidChangeTerminalShellIntegration(async ({ terminal, shellIntegration }) => {
// // can fire multiple times for single terminal ---> ATM when shell integration status changes, when its activated or working directory changes.
// // listen to this once per terminal. Dispose once its done

// if (terminal === myTerm) {
// const dispoable = map.get(terminal);
// disposable.dispose();
// map.delete(terminal);
// const execution = shellIntegration.executeCommand('echo "Hello world"');

// // const stream = execution.read();
// // for await(const data of stream) {
// // traceLog(`HERE ${data} HERE I AM WITH THE DATA`);
// // }
// window.onDidEndTerminalShellExecution((event) => {
// if (event.execution === execution) {
// console.log(`Command exited with code ${event.exitCode}`); // Keep getting undefined... --- placing this above gets me exit code 0.
// traceLog(`HERE ${event.exitCode} HERE I AM WITH THE EXIT CODE`);
// // const temp = event.exitCode;
// }
// });
// }
// }),
// );
}

/// /////////////////////////
const processEnv = normCaseKeys(this.processEnvVars);

// PS1 in some cases is a shell variable (not an env variable) so "env" might not contain it, calculate it in that case.
Expand Down
Loading