src/vs/workbench/contrib/terminal/common/environmentVariableService.ts
126 LOC · 114 covered · 12 uncovered · 10 ranges · 5 concepts · 2 introducers · 4 tests
File neighbourhood
The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.
Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file
In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.
Graph controls are ready.
Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.
/*---------------------------------------------------------------------------------------------
environmentVariableService.ts ×6
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { Event, Emitter } from '../../../../base/common/event.js';
import { debounce, throttle } from '../../../../base/common/decorators.js';
import { IStorageService, StorageScope, StorageTarget } from '../../../../platform/storage/common/storage.js';
import { IExtensionService } from '../../../services/extensions/common/extensions.js';
import { MergedEnvironmentVariableCollection } from '../../../../platform/terminal/common/environmentVariableCollection.js';
import { deserializeEnvironmentDescriptionMap, deserializeEnvironmentVariableCollection, serializeEnvironmentDescriptionMap, serializeEnvironmentVariableCollection } from '../../../../platform/terminal/common/environmentVariableShared.js';
import { IEnvironmentVariableCollectionWithPersistence, IEnvironmentVariableService } from './environmentVariable.js';
import { TerminalStorageKeys } from './terminalStorageKeys.js';
import { IMergedEnvironmentVariableCollection, ISerializableEnvironmentDescriptionMap, ISerializableEnvironmentVariableCollection } from '../../../../platform/terminal/common/environmentVariable.js';
import { Disposable } from '../../../../base/common/lifecycle.js';
interface ISerializableExtensionEnvironmentVariableCollection {
extensionIdentifier: string;
collection: ISerializableEnvironmentVariableCollection;
description?: ISerializableEnvironmentDescriptionMap;
}
/**
* Tracks and persists environment variable collections as defined by extensions.
*/
export class EnvironmentVariableService extends Disposable implements IEnvironmentVariableService {
declare readonly _serviceBrand: undefined;
collections: Map<string, IEnvironmentVariableCollectionWithPersistence> = new Map();
mergedCollection: IMergedEnvironmentVariableCollection;
private readonly _onDidChangeCollections = this._register(new Emitter<IMergedEnvironmentVariableCollection>());
get onDidChangeCollections(): Event<IMergedEnvironmentVariableCollection> { return this._onDidChangeCollections.event; }
constructor(
@IExtensionService private readonly _extensionService: IExtensionService,
@IStorageService private readonly _storageService: IStorageService
) {
super();
this._storageService.remove(TerminalStorageKeys.DeprecatedEnvironmentVariableCollections, StorageScope.WORKSPACE);
const serializedPersistedCollections = this._storageService.get(TerminalStorageKeys.EnvironmentVariableCollections, StorageScope.WORKSPACE);
if (serializedPersistedCollections) {
const collectionsJson: ISerializableExtensionEnvironmentVariableCollection[] = JSON.parse(serializedPersistedCollections);
environmentVariableService.ts ×4
collectionsJson.forEach(c => this.collections.set(c.extensionIdentifier, {
persistent: true,
map: deserializeEnvironmentVariableCollection(c.collection),
descriptionMap: deserializeEnvironmentDescriptionMap(c.description)
}));
// Asynchronously invalidate collections where extensions have been uninstalled, this is
// async to avoid making all functions on the service synchronous and because extensions
// being uninstalled is rare.
this._invalidateExtensionCollections();
}
// Listen for uninstalled/disabled extensions
this._register(this._extensionService.onDidChangeExtensions(() => this._invalidateExtensionCollections()));
}
set(extensionIdentifier: string, collection: IEnvironmentVariableCollectionWithPersistence): void {
this.collections.set(extensionIdentifier, collection);
this._updateCollections();
}
delete(extensionIdentifier: string): void {
this.collections.delete(extensionIdentifier);
this._updateCollections();
}
private _updateCollections(): void {
this._persistCollectionsEventually();
this.mergedCollection = this._resolveMergedCollection();
this._notifyCollectionUpdatesEventually();
}
@throttle(1000)
private _persistCollectionsEventually(): void {
this._persistCollections();
}
protected _persistCollections(): void {
const collectionsJson: ISerializableExtensionEnvironmentVariableCollection[] = [];
this.collections.forEach((collection, extensionIdentifier) => {
if (collection.persistent) {
collectionsJson.push({
extensionIdentifier,
collection: serializeEnvironmentVariableCollection(this.collections.get(extensionIdentifier)!.map),
description: serializeEnvironmentDescriptionMap(collection.descriptionMap)
});
}
});
const stringifiedJson = JSON.stringify(collectionsJson);
this._storageService.store(TerminalStorageKeys.EnvironmentVariableCollections, stringifiedJson, StorageScope.WORKSPACE, StorageTarget.MACHINE);
}
@debounce(1000)
private _notifyCollectionUpdatesEventually(): void {
this._notifyCollectionUpdates();
}
protected _notifyCollectionUpdates(): void {
this._onDidChangeCollections.fire(this.mergedCollection);
}
private _resolveMergedCollection(): IMergedEnvironmentVariableCollection {
return new MergedEnvironmentVariableCollection(this.collections);
}
private async _invalidateExtensionCollections(): Promise<void> {
await this._extensionService.whenInstalledExtensionsRegistered();
environmentVariableService.ts ×4
const registeredExtensions = this._extensionService.extensions;
let changes = false;
this.collections.forEach((_, extensionIdentifier) => {
const isExtensionRegistered = registeredExtensions.some(r => r.identifier.value === extensionIdentifier);
if (!isExtensionRegistered) {
this.collections.delete(extensionIdentifier);
changes = true;
}
if (changes) {
this._updateCollections();
}