#28538 Remove existing workspace configuration service. Update tests.

This commit is contained in:
Sandeep Somavarapu
2017-06-13 12:40:25 +02:00
parent 1e74972fe2
commit 2e2addebf7
7 changed files with 13 additions and 414 deletions
+6 -6
View File
@@ -19,7 +19,7 @@ import uri from 'vs/base/common/uri';
import strings = require('vs/base/common/strings');
import { IResourceInput } from 'vs/platform/editor/common/editor';
import { Workspace } from 'vs/platform/workspace/common/workspace';
import { WorkspaceService } from 'vs/workbench/services/configuration/node/configuration';
import { WorkspaceConfigurationService } from 'vs/workbench/services/configuration/node/configuration';
import { realpath, stat } from 'vs/base/node/pfs';
import { EnvironmentService } from 'vs/platform/environment/node/environmentService';
import path = require('path');
@@ -129,12 +129,12 @@ function getWorkspace(workspacePath: string): TPromise<Workspace> {
function openWorkbench(configuration: IWindowConfiguration, workspace: Workspace, options: IOptions): TPromise<void> {
const environmentService = new EnvironmentService(configuration, configuration.execPath);
const workspaceService = new WorkspaceService(environmentService, workspace);
const timerService = new TimerService((<any>window).MonacoEnvironment.timers as IInitData, !workspaceService.hasWorkspace());
const workspaceConfigurationService = new WorkspaceConfigurationService(environmentService, workspace);
const timerService = new TimerService((<any>window).MonacoEnvironment.timers as IInitData, !workspaceConfigurationService.hasWorkspace());
// Since the configuration service is one of the core services that is used in so many places, we initialize it
// right before startup of the workbench shell to have its data ready for consumers
return workspaceService.initialize().then(() => {
return workspaceConfigurationService.initialize().then(() => {
timerService.beforeDOMContentLoaded = Date.now();
return domContentLoaded().then(() => {
@@ -143,8 +143,8 @@ function openWorkbench(configuration: IWindowConfiguration, workspace: Workspace
// Open Shell
timerService.beforeWorkbenchOpen = Date.now();
const shell = new WorkbenchShell(document.body, {
contextService: workspaceService,
configurationService: workspaceService,
contextService: workspaceConfigurationService,
configurationService: workspaceConfigurationService,
environmentService,
timerService
}, configuration, options);
@@ -47,7 +47,7 @@ import { IStorageService, StorageScope } from 'vs/platform/storage/common/storag
import { ContextMenuService } from 'vs/workbench/services/contextview/electron-browser/contextmenuService';
import { WorkbenchKeybindingService } from 'vs/workbench/services/keybinding/electron-browser/keybindingService';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { WorkspaceConfigurationService } from 'vs/workbench/services/configuration/node/configurationService';
import { WorkspaceConfigurationService } from 'vs/workbench/services/configuration/node/configuration';
import { IConfigurationEditingService } from 'vs/workbench/services/configuration/common/configurationEditing';
import { ConfigurationEditingService } from 'vs/workbench/services/configuration/node/configurationEditingService';
import { ContextKeyService } from 'vs/platform/contextkey/browser/contextKeyService';
@@ -44,7 +44,7 @@ interface IWorkspaceConfiguration<T> {
type IWorkspaceFoldersConfiguration = { [rootFolder: string]: { folders: string[]; } };
export class WorkspaceService extends Disposable implements IWorkspaceContextService, IWorkspaceConfigurationService {
export class WorkspaceConfigurationService extends Disposable implements IWorkspaceContextService, IWorkspaceConfigurationService {
private static RELOAD_CONFIGURATION_DELAY = 50;
@@ -83,7 +83,7 @@ export class WorkspaceService extends Disposable implements IWorkspaceContextSer
source: ConfigurationSource.Workspace,
sourceConfig: config.workspace
}))
.done(null, errors.onUnexpectedError), WorkspaceService.RELOAD_CONFIGURATION_DELAY));
.done(null, errors.onUnexpectedError), WorkspaceConfigurationService.RELOAD_CONFIGURATION_DELAY));
this._register(this.baseConfigurationService.onDidUpdateConfiguration(e => this.onBaseConfigurationChanged(e)));
this._register(this.onDidUpdateConfiguration(e => this.resolveAdditionalFolders(true)));
@@ -1,357 +0,0 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import { TPromise } from 'vs/base/common/winjs.base';
import uri from 'vs/base/common/uri';
import paths = require('vs/base/common/paths');
import extfs = require('vs/base/node/extfs');
import objects = require('vs/base/common/objects');
import { RunOnceScheduler } from 'vs/base/common/async';
import collections = require('vs/base/common/collections');
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
import { IDisposable, Disposable } from 'vs/base/common/lifecycle';
import { readFile } from 'vs/base/node/pfs';
import errors = require('vs/base/common/errors');
import { ScopedConfigModel, WorkspaceConfigModel, WorkspaceSettingsConfigModel } from 'vs/workbench/services/configuration/common/configurationModels';
import { IConfigurationServiceEvent, ConfigurationSource, getConfigurationValue, IConfigModel, IConfigurationOptions } from 'vs/platform/configuration/common/configuration';
import { ConfigModel } from 'vs/platform/configuration/common/model';
import { ConfigurationService as BaseConfigurationService } from 'vs/platform/configuration/node/configurationService';
import { IWorkspaceConfigurationValues, IWorkspaceConfigurationService, IWorkspaceConfigurationValue, WORKSPACE_CONFIG_FOLDER_DEFAULT_NAME, WORKSPACE_STANDALONE_CONFIGURATIONS, WORKSPACE_CONFIG_DEFAULT_PATH } from 'vs/workbench/services/configuration/common/configuration';
import { FileChangeType, FileChangesEvent, isEqual } from 'vs/platform/files/common/files';
import Event, { Emitter } from 'vs/base/common/event';
import { Workspace } from "vs/platform/workspace/common/workspace";
interface IStat {
resource: uri;
isDirectory?: boolean;
children?: { resource: uri; }[];
}
interface IContent {
resource: uri;
value: string;
}
interface IWorkspaceConfiguration<T> {
workspace: T;
consolidated: any;
}
/**
* Wraps around the basic configuration service and adds knowledge about workspace settings.
*/
export class WorkspaceConfigurationService extends Disposable implements IWorkspaceConfigurationService, IDisposable {
public _serviceBrand: any;
private static RELOAD_CONFIGURATION_DELAY = 50;
private _onDidUpdateConfiguration: Emitter<IConfigurationServiceEvent>;
private baseConfigurationService: BaseConfigurationService<any>;
private cachedConfig: ConfigModel<any>;
private cachedWorkspaceConfig: WorkspaceConfigModel<any>;
private bulkFetchFromWorkspacePromise: TPromise<any>;
private workspaceFilePathToConfiguration: { [relativeWorkspacePath: string]: TPromise<IConfigModel<any>> };
private reloadConfigurationScheduler: RunOnceScheduler;
constructor(
private environmentService: IEnvironmentService,
private workspace?: Workspace,
private workspaceSettingsRootFolder: string = WORKSPACE_CONFIG_FOLDER_DEFAULT_NAME
) {
super();
this.workspaceFilePathToConfiguration = Object.create(null);
this.cachedConfig = new ConfigModel<any>(null);
this.cachedWorkspaceConfig = new WorkspaceConfigModel(new WorkspaceSettingsConfigModel(null), []);
this._onDidUpdateConfiguration = this._register(new Emitter<IConfigurationServiceEvent>());
this.baseConfigurationService = this._register(new BaseConfigurationService(environmentService));
this.reloadConfigurationScheduler = this._register(new RunOnceScheduler(() => this.doLoadConfiguration()
.then(config => this._onDidUpdateConfiguration.fire({
config: config.consolidated,
source: ConfigurationSource.Workspace,
sourceConfig: config.workspace
}))
.done(null, errors.onUnexpectedError), WorkspaceConfigurationService.RELOAD_CONFIGURATION_DELAY));
this._register(this.baseConfigurationService.onDidUpdateConfiguration(e => this.onBaseConfigurationChanged(e)));
}
get onDidUpdateConfiguration(): Event<IConfigurationServiceEvent> {
return this._onDidUpdateConfiguration.event;
}
private onBaseConfigurationChanged(event: IConfigurationServiceEvent): void {
if (event.source === ConfigurationSource.Default) {
this.cachedWorkspaceConfig.update();
}
// update cached config when base config changes
const configModel = <ConfigModel<any>>this.baseConfigurationService.getCache().consolidated // global/default values (do NOT modify)
.merge(this.cachedWorkspaceConfig); // workspace configured values
// emit this as update to listeners if changed
if (!objects.equals(this.cachedConfig.contents, configModel.contents)) {
this.cachedConfig = configModel;
this._onDidUpdateConfiguration.fire({
config: this.cachedConfig.contents,
source: event.source,
sourceConfig: event.sourceConfig
});
}
}
public initialize(): TPromise<void> {
return this.doLoadConfiguration().then(() => null);
}
public getConfiguration<C>(section?: string): C
public getConfiguration<C>(options?: IConfigurationOptions): C
public getConfiguration<C>(arg?: any): C {
const options = this.toOptions(arg);
const configModel = options.overrideIdentifier ? this.cachedConfig.configWithOverrides<C>(options.overrideIdentifier) : this.cachedConfig;
return options.section ? configModel.getContentsFor<C>(options.section) : configModel.contents;
}
public lookup<C>(key: string, overrideIdentifier?: string): IWorkspaceConfigurationValue<C> {
const configurationValue = this.baseConfigurationService.lookup<C>(key, overrideIdentifier);
return {
default: configurationValue.default,
user: configurationValue.user,
workspace: objects.clone(getConfigurationValue<C>(overrideIdentifier ? this.cachedWorkspaceConfig.configWithOverrides(overrideIdentifier).contents : this.cachedWorkspaceConfig.contents, key)),
value: objects.clone(getConfigurationValue<C>(overrideIdentifier ? this.cachedConfig.configWithOverrides(overrideIdentifier).contents : this.cachedConfig.contents, key))
};
}
public keys() {
const keys = this.baseConfigurationService.keys();
return {
default: keys.default,
user: keys.user,
workspace: this.cachedWorkspaceConfig.keys
};
}
public values(): IWorkspaceConfigurationValues {
const result: IWorkspaceConfigurationValues = Object.create(null);
const keyset = this.keys();
const keys = [...keyset.workspace, ...keyset.user, ...keyset.default].sort();
let lastKey: string;
for (const key of keys) {
if (key !== lastKey) {
lastKey = key;
result[key] = this.lookup(key);
}
}
return result;
}
public reloadConfiguration(section?: string): TPromise<any> {
// Reset caches to ensure we are hitting the disk
this.bulkFetchFromWorkspacePromise = null;
this.workspaceFilePathToConfiguration = Object.create(null);
// Load configuration
return this.baseConfigurationService.reloadConfiguration().then(() => {
const current = this.cachedConfig;
return this.doLoadConfiguration().then(configuration => {
// emit this as update to listeners if changed
if (!objects.equals(current, this.cachedConfig)) {
this._onDidUpdateConfiguration.fire({
config: configuration.consolidated,
source: ConfigurationSource.Workspace,
sourceConfig: configuration.workspace
});
}
return section ? configuration.consolidated[section] : configuration.consolidated;
});
});
}
private toOptions(arg: any): IConfigurationOptions {
if (typeof arg === 'string') {
return { section: arg };
}
if (typeof arg === 'object') {
return arg;
}
return {};
}
private doLoadConfiguration<T>(): TPromise<IWorkspaceConfiguration<T>> {
// Load workspace locals
return this.loadWorkspaceConfigFiles().then(workspaceConfigFiles => {
// Consolidate (support *.json files in the workspace settings folder)
const workspaceSettingsConfig = <WorkspaceSettingsConfigModel<T>>workspaceConfigFiles[WORKSPACE_CONFIG_DEFAULT_PATH] || new WorkspaceSettingsConfigModel<T>(null);
const otherConfigModels = Object.keys(workspaceConfigFiles).filter(key => key !== WORKSPACE_CONFIG_DEFAULT_PATH).map(key => <ScopedConfigModel<T>>workspaceConfigFiles[key]);
this.cachedWorkspaceConfig = new WorkspaceConfigModel<T>(workspaceSettingsConfig, otherConfigModels);
// Override base (global < user) with workspace locals (global < user < workspace)
this.cachedConfig = <ConfigModel<any>>this.baseConfigurationService.getCache().consolidated // global/default values (do NOT modify)
.merge(this.cachedWorkspaceConfig); // workspace configured values
return {
consolidated: this.cachedConfig.contents,
workspace: this.cachedWorkspaceConfig.contents
};
});
}
private loadWorkspaceConfigFiles<T>(): TPromise<{ [relativeWorkspacePath: string]: IConfigModel<T> }> {
// Return early if we don't have a workspace
if (!this.workspace) {
return TPromise.as(Object.create(null));
}
// once: when invoked for the first time we fetch json files that contribute settings
if (!this.bulkFetchFromWorkspacePromise) {
this.bulkFetchFromWorkspacePromise = resolveStat(this.workspace.toResource(this.workspaceSettingsRootFolder)).then(stat => {
if (!stat.isDirectory) {
return TPromise.as([]);
}
return resolveContents(stat.children.filter(stat => {
const isJson = paths.extname(stat.resource.fsPath) === '.json';
if (!isJson) {
return false; // only JSON files
}
return this.isWorkspaceConfigurationFile(this.workspace.toWorkspaceRelativePath(stat.resource)); // only workspace config files
}).map(stat => stat.resource));
}, err => [] /* never fail this call */)
.then((contents: IContent[]) => {
contents.forEach(content => this.workspaceFilePathToConfiguration[this.workspace.toWorkspaceRelativePath(content.resource)] = TPromise.as(this.createConfigModel(content)));
}, errors.onUnexpectedError);
}
// on change: join on *all* configuration file promises so that we can merge them into a single configuration object. this
// happens whenever a config file changes, is deleted, or added
return this.bulkFetchFromWorkspacePromise.then(() => TPromise.join(this.workspaceFilePathToConfiguration));
}
public handleWorkspaceFileEvents(event: FileChangesEvent): void {
if (!this.workspace) {
return; // only enabled when we have a known workspace
}
const events = event.changes;
let affectedByChanges = false;
// Find changes that affect workspace configuration files
for (let i = 0, len = events.length; i < len; i++) {
const resource = events[i].resource;
const isJson = paths.extname(resource.fsPath) === '.json';
const isDeletedSettingsFolder = (events[i].type === FileChangeType.DELETED && isEqual(paths.basename(resource.fsPath), this.workspaceSettingsRootFolder));
if (!isJson && !isDeletedSettingsFolder) {
continue; // only JSON files or the actual settings folder
}
const workspacePath = this.workspace.toWorkspaceRelativePath(resource);
if (!workspacePath) {
continue; // event is not inside workspace
}
// Handle case where ".vscode" got deleted
if (workspacePath === this.workspaceSettingsRootFolder && events[i].type === FileChangeType.DELETED) {
this.workspaceFilePathToConfiguration = Object.create(null);
affectedByChanges = true;
}
// only valid workspace config files
if (!this.isWorkspaceConfigurationFile(workspacePath)) {
continue;
}
// insert 'fetch-promises' for add and update events and
// remove promises for delete events
switch (events[i].type) {
case FileChangeType.DELETED:
affectedByChanges = collections.remove(this.workspaceFilePathToConfiguration, workspacePath);
break;
case FileChangeType.UPDATED:
case FileChangeType.ADDED:
this.workspaceFilePathToConfiguration[workspacePath] = resolveContent(resource).then(content => this.createConfigModel(content), errors.onUnexpectedError);
affectedByChanges = true;
}
}
// trigger reload of the configuration if we are affected by changes
if (affectedByChanges && !this.reloadConfigurationScheduler.isScheduled()) {
this.reloadConfigurationScheduler.schedule();
}
}
private createConfigModel<T>(content: IContent): IConfigModel<T> {
const path = this.workspace.toWorkspaceRelativePath(content.resource);
if (path === WORKSPACE_CONFIG_DEFAULT_PATH) {
return new WorkspaceSettingsConfigModel<T>(content.value, content.resource.toString());
} else {
const matches = /\/([^\.]*)*\.json/.exec(path);
if (matches && matches[1]) {
return new ScopedConfigModel<T>(content.value, content.resource.toString(), matches[1]);
}
}
return new ConfigModel<T>(null);
}
private isWorkspaceConfigurationFile(workspaceRelativePath: string): boolean {
return [WORKSPACE_CONFIG_DEFAULT_PATH, WORKSPACE_STANDALONE_CONFIGURATIONS.launch, WORKSPACE_STANDALONE_CONFIGURATIONS.tasks].some(p => p === workspaceRelativePath);
}
public getUnsupportedWorkspaceKeys(): string[] {
return this.cachedWorkspaceConfig.workspaceSettingsConfig.unsupportedKeys;
}
}
// node.hs helper functions
function resolveContents(resources: uri[]): TPromise<IContent[]> {
const contents: IContent[] = [];
return TPromise.join(resources.map(resource => {
return resolveContent(resource).then(content => {
contents.push(content);
});
})).then(() => contents);
}
function resolveContent(resource: uri): TPromise<IContent> {
return readFile(resource.fsPath).then(contents => ({ resource, value: contents.toString() }));
}
function resolveStat(resource: uri): TPromise<IStat> {
return new TPromise<IStat>((c, e) => {
extfs.readdir(resource.fsPath, (error, children) => {
if (error) {
if ((<any>error).code === 'ENOTDIR') {
c({ resource });
} else {
e(error);
}
} else {
c({
resource,
isDirectory: true,
children: children.map(child => { return { resource: uri.file(paths.join(resource.fsPath, child)) }; })
});
}
});
});
}
@@ -1,44 +0,0 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import { IConfigurationService, IConfigurationServiceEvent, IConfigurationValue, getConfigurationValue, IConfigurationKeys } from "vs/platform/configuration/common/configuration";
import Event, { Emitter } from 'vs/base/common/event';
import { TPromise } from "vs/base/common/winjs.base";
export class NullConfigurationService implements IConfigurationService {
_serviceBrand: any;
private _onDidUpdateConfiguration = new Emitter<IConfigurationServiceEvent>();
public onDidUpdateConfiguration: Event<IConfigurationServiceEvent> = this._onDidUpdateConfiguration.event;
private _config: any;
constructor() {
this._config = Object.create(null);
}
public getConfiguration<T>(section?: any): T {
return this._config;
}
public reloadConfiguration<T>(section?: string): TPromise<T> {
return TPromise.as<T>(this.getConfiguration<T>(section));
}
public lookup<C>(key: string): IConfigurationValue<C> {
return {
value: getConfigurationValue<C>(this.getConfiguration(), key),
default: getConfigurationValue<C>(this.getConfiguration(), key),
user: getConfigurationValue<C>(this.getConfiguration(), key)
};
}
public keys(): IConfigurationKeys {
return { default: [], user: [] };
}
}
@@ -19,7 +19,7 @@ import { parseArgs } from 'vs/platform/environment/node/argv';
import extfs = require('vs/base/node/extfs');
import uuid = require('vs/base/common/uuid');
import { IConfigurationRegistry, Extensions as ConfigurationExtensions } from 'vs/platform/configuration/common/configurationRegistry';
import { WorkspaceConfigurationService } from 'vs/workbench/services/configuration/node/configurationService';
import { WorkspaceConfigurationService } from 'vs/workbench/services/configuration/node/configuration';
import URI from 'vs/base/common/uri';
import { FileChangeType, FileChangesEvent } from 'vs/platform/files/common/files';
@@ -21,7 +21,7 @@ import extfs = require('vs/base/node/extfs');
import { TestTextFileService, TestEditorGroupService, TestLifecycleService, TestBackupFileService } from 'vs/workbench/test/workbenchTestServices';
import uuid = require('vs/base/common/uuid');
import { IConfigurationRegistry, Extensions as ConfigurationExtensions } from 'vs/platform/configuration/common/configurationRegistry';
import { WorkspaceService } from 'vs/workbench/services/workspace/node/workspace';
import { WorkspaceConfigurationService } from 'vs/workbench/services/configuration/node/configuration';
import URI from 'vs/base/common/uri';
import { FileService } from 'vs/workbench/services/files/node/fileService';
import { ConfigurationEditingService } from 'vs/workbench/services/configuration/node/configurationEditingService';
@@ -116,7 +116,7 @@ suite('ConfigurationEditingService', () => {
const environmentService = new SettingsTestEnvironmentService(parseArgs(process.argv), process.execPath, globalSettingsFile);
instantiationService.stub(IEnvironmentService, environmentService);
const workspace = noWorkspace ? null : new Workspace(URI.file(workspaceDir));
const workspaceService = new WorkspaceService(environmentService, workspace);
const workspaceService = new WorkspaceConfigurationService(environmentService, workspace);
instantiationService.stub(IWorkspaceContextService, workspaceService);
instantiationService.stub(IConfigurationService, workspaceService);
instantiationService.stub(ILifecycleService, new TestLifecycleService());
@@ -150,7 +150,7 @@ suite('ConfigurationEditingService', () => {
function clearServices(): void {
if (instantiationService) {
const configuraitonService = <WorkspaceService>instantiationService.get(IConfigurationService);
const configuraitonService = <WorkspaceConfigurationService>instantiationService.get(IConfigurationService);
if (configuraitonService) {
configuraitonService.dispose();
}