Skip to content

Commit

Permalink
Mkae env collection simpler for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Kartik Raj committed Apr 13, 2023
1 parent 4f9a51e commit 460e5ba
Showing 1 changed file with 18 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { traceDecoratorVerbose, traceVerbose } from '../../logging';
import { IInterpreterService } from '../contracts';
import { defaultShells } from './service';
import { IEnvironmentActivationService } from './types';
import { EnvironmentVariables } from '../../common/variables/types';

@injectable()
export class TerminalEnvVarCollectionService implements IExtensionActivationService {
Expand All @@ -33,6 +34,8 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ

private previousEnvVars = _normCaseKeys(process.env);

public random = this._environmentActivationService;

constructor(
@inject(IPlatformService) private readonly platform: IPlatformService,
@inject(IInterpreterService) private interpreterService: IInterpreterService,
Expand All @@ -41,7 +44,7 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ
@inject(IExperimentService) private experimentService: IExperimentService,
@inject(IApplicationEnvironment) private applicationEnvironment: IApplicationEnvironment,
@inject(IDisposableRegistry) private disposables: IDisposableRegistry,
@inject(IEnvironmentActivationService) private environmentActivationService: IEnvironmentActivationService,
@inject(IEnvironmentActivationService) private _environmentActivationService: IEnvironmentActivationService,
@inject(IWorkspaceService) private workspaceService: IWorkspaceService,
@inject(IConfigurationService) private readonly configurationService: IConfigurationService,
) {
Expand All @@ -56,16 +59,17 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ
this.disposables,
);
this.applicationEnvironment.onDidChangeShell(
async (shell: string) => {
async (s: string) => {
this.showProgress();
// Pass in the shell where known instead of relying on the application environment, because of bug
// on VSCode: https://github.com/microsoft/vscode/issues/160694
await this._applyCollection(undefined, shell);
await this._applyCollection(undefined, s);
this.hideProgress();
},
this,
this.disposables,
);}
);
}

public async activate(resource: Resource): Promise<void> {
if (!inTerminalEnvVarExperiment(this.experimentService)) {
Expand All @@ -77,21 +81,20 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ

public async _applyCollection(resource: Resource, shell = this.applicationEnvironment.shell): Promise<void> {
let workspaceFolder = this.workspaceService.getWorkspaceFolder(resource);
if (!workspaceFolder && Array.isArray(this.workspaceService.workspaceFolders) && this.workspaceService.workspaceFolders.length > 0) {
workspaceFolder = this.workspaceService.workspaceFolders[0];
if (
!workspaceFolder &&
Array.isArray(this.workspaceService.workspaceFolders) &&
this.workspaceService.workspaceFolders.length > 0
) {
[workspaceFolder] = this.workspaceService.workspaceFolders;
}
console.log('Use workspace folder', workspaceFolder?.uri.fsPath);
const settings = this.configurationService.getSettings(resource);
if (!settings.terminal.activateEnvironment) {
traceVerbose('Activating environments in terminal is disabled for', resource?.fsPath);
return;
}
const env = await this.environmentActivationService.getActivatedEnvironmentVariables(
resource,
undefined,
undefined,
shell,
);
const env: EnvironmentVariables = { HELLO: workspaceFolder?.name };
if (!env) {
const shellType = identifyShellFromShellPath(shell);
const defaultShell = defaultShells[this.platform.osType];
Expand All @@ -113,18 +116,18 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ
if (prevValue !== value) {
if (value !== undefined) {
traceVerbose(`Setting environment variable ${key} in collection to ${value}`);
this.context.environmentVariableCollection.replace(key, value, {workspaceFolder});
this.context.environmentVariableCollection.replace(key, value, { workspaceFolder });
} else {
traceVerbose(`Clearing environment variable ${key} from collection`);
this.context.environmentVariableCollection.delete(key, {workspaceFolder});
this.context.environmentVariableCollection.delete(key, { workspaceFolder });
}
}
});
Object.keys(previousEnv).forEach((key) => {
// If the previous env var is not in the current env, clear it from collection.
if (!(key in env)) {
traceVerbose(`Clearing environment variable ${key} from collection`);
this.context.environmentVariableCollection.delete(key, {workspaceFolder});
this.context.environmentVariableCollection.delete(key, { workspaceFolder });
}
});
}
Expand Down

0 comments on commit 460e5ba

Please sign in to comment.