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

Implement EnvironmentVariableMutatorOptions API. #12984

Merged
merged 1 commit into from
Oct 19, 2023
Merged
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
12 changes: 6 additions & 6 deletions packages/plugin-ext/src/plugin/terminal-ext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -372,16 +372,16 @@ export class EnvironmentVariableCollection implements theia.EnvironmentVariableC
return this.map.size;
}

replace(variable: string, value: string): void {
this._setIfDiffers(variable, { value, type: EnvironmentVariableMutatorType.Replace });
replace(variable: string, value: string, options?: theia.EnvironmentVariableMutatorOptions): void {
this._setIfDiffers(variable, { value, type: EnvironmentVariableMutatorType.Replace, options: options ?? { applyAtProcessCreation: true } });
}

append(variable: string, value: string): void {
this._setIfDiffers(variable, { value, type: EnvironmentVariableMutatorType.Append });
append(variable: string, value: string, options?: theia.EnvironmentVariableMutatorOptions): void {
this._setIfDiffers(variable, { value, type: EnvironmentVariableMutatorType.Append, options: options ?? { applyAtProcessCreation: true } });
}

prepend(variable: string, value: string): void {
this._setIfDiffers(variable, { value, type: EnvironmentVariableMutatorType.Prepend });
prepend(variable: string, value: string, options?: theia.EnvironmentVariableMutatorOptions): void {
this._setIfDiffers(variable, { value, type: EnvironmentVariableMutatorType.Prepend, options: options ?? { applyAtProcessCreation: true } });
}

private _setIfDiffers(variable: string, mutator: theia.EnvironmentVariableMutator): void {
Expand Down
29 changes: 26 additions & 3 deletions packages/plugin/src/theia.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3574,6 +3574,24 @@ export module '@theia/plugin' {
Prepend = 3
}

/**
* Options applied to the mutator.
*/
export interface EnvironmentVariableMutatorOptions {
/**
* Apply to the environment just before the process is created. Defaults to true
*/
applyAtProcessCreation?: boolean;

/**
* Apply to the environment in the shell integration script. Note that this _will not_ apply
* the mutator if shell integration is disabled or not working for some reason. Defaults to
* false.
* @stubbed
*/
applyAtShellIntegration?: boolean;
}

/**
* A type of mutation and its value to be applied to an environment variable.
*/
Expand All @@ -3587,6 +3605,11 @@ export module '@theia/plugin' {
* The value to use for the variable.
*/
readonly value: string;

/**
* Options applied to the mutator.
*/
readonly options: EnvironmentVariableMutatorOptions;
}

/**
Expand Down Expand Up @@ -3617,7 +3640,7 @@ export module '@theia/plugin' {
* @param variable The variable to replace.
* @param value The value to replace the variable with.
*/
replace(variable: string, value: string): void;
replace(variable: string, value: string, options?: EnvironmentVariableMutatorOptions): void;

/**
* Append a value to an environment variable.
Expand All @@ -3628,7 +3651,7 @@ export module '@theia/plugin' {
* @param variable The variable to append to.
* @param value The value to append to the variable.
*/
append(variable: string, value: string): void;
append(variable: string, value: string, options?: EnvironmentVariableMutatorOptions): void;

/**
* Prepend a value to an environment variable.
Expand All @@ -3639,7 +3662,7 @@ export module '@theia/plugin' {
* @param variable The variable to prepend.
* @param value The value to prepend to the variable.
*/
prepend(variable: string, value: string): void;
prepend(variable: string, value: string, options?: EnvironmentVariableMutatorOptions): void;

/**
* Gets the mutator that this collection applies to a variable, if any.
Expand Down
5 changes: 5 additions & 0 deletions packages/terminal/src/common/base-terminal-protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,14 @@ export enum EnvironmentVariableMutatorType {
Prepend = 3
}

export interface EnvironmentVariableMutatorOptions {
applyAtProcessCreation?: boolean;
}

export interface EnvironmentVariableMutator {
readonly value: string;
readonly type: EnvironmentVariableMutatorType;
readonly options: EnvironmentVariableMutatorOptions;
}

export interface ExtensionOwnedEnvironmentVariableMutator extends EnvironmentVariableMutator {
Expand Down
25 changes: 14 additions & 11 deletions packages/terminal/src/node/base-terminal-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,8 @@ export class MergedEnvironmentVariableCollectionImpl implements MergedEnvironmen
entry.unshift({
extensionIdentifier,
value: mutator.value,
type: mutator.type
type: mutator.type,
options: mutator.options
});

next = it.next();
Expand All @@ -291,16 +292,18 @@ export class MergedEnvironmentVariableCollectionImpl implements MergedEnvironmen
this.map.forEach((mutators, variable) => {
const actualVariable = isWindows ? lowerToActualVariableNames![variable.toLowerCase()] || variable : variable;
mutators.forEach(mutator => {
switch (mutator.type) {
case EnvironmentVariableMutatorType.Append:
env[actualVariable] = (env[actualVariable] || '') + mutator.value;
break;
case EnvironmentVariableMutatorType.Prepend:
env[actualVariable] = mutator.value + (env[actualVariable] || '');
break;
case EnvironmentVariableMutatorType.Replace:
env[actualVariable] = mutator.value;
break;
if (mutator.options?.applyAtProcessCreation ?? true) {
switch (mutator.type) {
case EnvironmentVariableMutatorType.Append:
env[actualVariable] = (env[actualVariable] || '') + mutator.value;
break;
case EnvironmentVariableMutatorType.Prepend:
env[actualVariable] = mutator.value + (env[actualVariable] || '');
break;
case EnvironmentVariableMutatorType.Replace:
env[actualVariable] = mutator.value;
break;
}
}
});
});
Expand Down