Skip to content

Commit

Permalink
Implement EnvironmentVariableMutatorOptions API. Fixes #12941 (#12984)
Browse files Browse the repository at this point in the history
Contributed on behalf of STMicroelectronics

Signed-off-by: Thomas Mäder <t.s.maeder@gmail.com>
  • Loading branch information
tsmaeder authored Oct 19, 2023
1 parent 30d166e commit d635d6f
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 20 deletions.
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

0 comments on commit d635d6f

Please sign in to comment.