提交 bfb105b6 编写于 作者: D Daniel Imms

Pull collection into its own file

上级 540232e3
......@@ -34,11 +34,6 @@ export interface IEnvironmentVariableCollection {
* Applies this collection to a process environment.
*/
applyToProcessEnvironment(env: IProcessEnvironment): void;
/**
* Gets a serializable view of the collection.
*/
// serialize(): [string, IEnvironmentVariableMutator][];
}
/**
......
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { IEnvironmentVariableCollection, IEnvironmentVariableMutator, EnvironmentVariableMutatorType } from 'vs/workbench/contrib/terminal/common/environmentVariable';
import { IProcessEnvironment } from 'vs/base/common/platform';
export class EnvironmentVariableCollection implements IEnvironmentVariableCollection {
readonly entries: Map<string, IEnvironmentVariableMutator>;
constructor(
variables?: string[],
values?: string[],
types?: EnvironmentVariableMutatorType[]
) {
this.entries = new Map();
if (variables && values && types) {
if (variables.length !== values.length || variables.length !== types.length) {
throw new Error('Cannot create environment collection from arrays of differing length');
}
for (let i = 0; i < variables.length; i++) {
this.entries.set(variables[i], { value: values[i], type: types[i] });
}
}
}
// TODO: Consider doing a full diff, just marking the environment as stale with no action available?
getNewAdditions(other: IEnvironmentVariableCollection): ReadonlyMap<string, IEnvironmentVariableMutator> | undefined {
const result = new Map<string, IEnvironmentVariableMutator>();
other.entries.forEach((newMutator, variable) => {
const currentMutator = this.entries.get(variable);
if (currentMutator?.type !== newMutator.type || currentMutator.value !== newMutator.value) {
result.set(variable, newMutator);
}
});
return result.size === 0 ? undefined : result;
}
applyToProcessEnvironment(env: IProcessEnvironment): void {
this.entries.forEach((mutator, variable) => {
switch (mutator.type) {
case EnvironmentVariableMutatorType.Append:
env[variable] = (env[variable] || '') + mutator.value;
break;
case EnvironmentVariableMutatorType.Prepend:
env[variable] = mutator.value + (env[variable] || '');
break;
case EnvironmentVariableMutatorType.Replace:
env[variable] = mutator.value;
break;
}
});
}
}
......@@ -3,12 +3,12 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { IEnvironmentVariableService, IEnvironmentVariableCollection, IEnvironmentVariableMutator, EnvironmentVariableMutatorType } from 'vs/workbench/contrib/terminal/common/environmentVariable';
import { IEnvironmentVariableService, IEnvironmentVariableCollection } from 'vs/workbench/contrib/terminal/common/environmentVariable';
import { Event, Emitter } from 'vs/base/common/event';
import { debounce } from 'vs/base/common/decorators';
import { IProcessEnvironment } from 'vs/base/common/platform';
import { IStorageService, StorageScope } from 'vs/platform/storage/common/storage';
import { IExtensionService } from 'vs/workbench/services/extensions/common/extensions';
import { EnvironmentVariableCollection } from 'vs/workbench/contrib/terminal/common/environmentVariableCollection';
const ENVIRONMENT_VARIABLE_COLLECTIONS_KEY = 'terminal.integrated.environmentVariableCollections';
......@@ -22,54 +22,6 @@ interface ISerializableExtensionEnvironmentVariableCollection {
collection: ISerializableEnvironmentVariableCollection
}
export class EnvironmentVariableCollection implements IEnvironmentVariableCollection {
readonly entries: Map<string, IEnvironmentVariableMutator>;
constructor(
variables?: string[],
values?: string[],
types?: EnvironmentVariableMutatorType[]
) {
this.entries = new Map();
if (variables && values && types) {
if (variables.length !== values.length || variables.length !== types.length) {
throw new Error('Cannot create environment collection from arrays of differing length');
}
for (let i = 0; i < variables.length; i++) {
this.entries.set(variables[i], { value: values[i], type: types[i] });
}
}
}
// TODO: Consider doing a full diff, just marking the environment as stale with no action available?
getNewAdditions(other: IEnvironmentVariableCollection): ReadonlyMap<string, IEnvironmentVariableMutator> | undefined {
const result = new Map<string, IEnvironmentVariableMutator>();
other.entries.forEach((newMutator, variable) => {
const currentMutator = this.entries.get(variable);
if (currentMutator?.type !== newMutator.type || currentMutator.value !== newMutator.value) {
result.set(variable, newMutator);
}
});
return result.size === 0 ? undefined : result;
}
applyToProcessEnvironment(env: IProcessEnvironment): void {
this.entries.forEach((mutator, variable) => {
switch (mutator.type) {
case EnvironmentVariableMutatorType.Append:
env[variable] = (env[variable] || '') + mutator.value;
break;
case EnvironmentVariableMutatorType.Prepend:
env[variable] = mutator.value + (env[variable] || '');
break;
case EnvironmentVariableMutatorType.Replace:
env[variable] = mutator.value;
break;
}
});
}
}
/**
* Tracks and persists environment variable collections as defined by extensions.
*/
......@@ -88,10 +40,7 @@ export class EnvironmentVariableService implements IEnvironmentVariableService {
) {
const serializedPersistedCollections = this._storageService.get(ENVIRONMENT_VARIABLE_COLLECTIONS_KEY, StorageScope.WORKSPACE);
if (serializedPersistedCollections) {
// TODO: Load in persisted collections
const collectionsJson: ISerializableExtensionEnvironmentVariableCollection[] = JSON.parse(serializedPersistedCollections);
collectionsJson.forEach(c => {
const extCollection = new EnvironmentVariableCollection(c.collection.variables, c.collection.values, c.collection.types);
this._collections.set(c.extensionIdentifier, extCollection);
......@@ -180,7 +129,6 @@ export class EnvironmentVariableService implements IEnvironmentVariableService {
}
}
function serializeEnvironmentVariableCollection(collection: IEnvironmentVariableCollection): ISerializableEnvironmentVariableCollection {
const entries = [...collection.entries.entries()];
const result: ISerializableEnvironmentVariableCollection = {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册