提交 a8b0050c 编写于 作者: I isidor

configuration resolver service: also resolve configuration variables

上级 eae1b662
...@@ -391,7 +391,8 @@ export class Workbench implements IPartService { ...@@ -391,7 +391,8 @@ export class Workbench implements IPartService {
serviceCollection.set(IConfigurationEditingService, this.instantiationService.createInstance(ConfigurationEditingService)); serviceCollection.set(IConfigurationEditingService, this.instantiationService.createInstance(ConfigurationEditingService));
// Configuration Resolver // Configuration Resolver
const configurationResolverService = this.instantiationService.createInstance(ConfigurationResolverService); const workspace = this.contextService.getWorkspace();
const configurationResolverService = this.instantiationService.createInstance(ConfigurationResolverService, workspace ? workspace.resource : null, process.env);
serviceCollection.set(IConfigurationResolverService, configurationResolverService); serviceCollection.set(IConfigurationResolverService, configurationResolverService);
// Quick open service (quick open controller) // Quick open service (quick open controller)
......
...@@ -5,10 +5,11 @@ ...@@ -5,10 +5,11 @@
import * as paths from 'vs/base/common/paths'; import * as paths from 'vs/base/common/paths';
import * as types from 'vs/base/common/types'; import * as types from 'vs/base/common/types';
import uri from 'vs/base/common/uri';
import {IStringDictionary} from 'vs/base/common/collections'; import {IStringDictionary} from 'vs/base/common/collections';
import {IConfigurationResolverService} from 'vs/workbench/services/configurationResolver/common/configurationResolver'; import {IConfigurationResolverService} from 'vs/workbench/services/configurationResolver/common/configurationResolver';
import {IWorkspaceContextService} from 'vs/platform/workspace/common/workspace';
import {IEnvironmentService} from 'vs/platform/environment/common/environment'; import {IEnvironmentService} from 'vs/platform/environment/common/environment';
import {IConfigurationService} from 'vs/platform/configuration/common/configuration';
import {IWorkbenchEditorService} from 'vs/workbench/services/editor/common/editorService'; import {IWorkbenchEditorService} from 'vs/workbench/services/editor/common/editorService';
import {asFileEditorInput} from 'vs/workbench/common/editor'; import {asFileEditorInput} from 'vs/workbench/common/editor';
...@@ -18,16 +19,16 @@ export class ConfigurationResolverService implements IConfigurationResolverServi ...@@ -18,16 +19,16 @@ export class ConfigurationResolverService implements IConfigurationResolverServi
private _execPath: string; private _execPath: string;
constructor( constructor(
workspaceRoot: uri,
envVariables: { [key: string]: string },
@IWorkbenchEditorService private editorService: IWorkbenchEditorService, @IWorkbenchEditorService private editorService: IWorkbenchEditorService,
@IWorkspaceContextService private contextService: IWorkspaceContextService, @IEnvironmentService private environmentService: IEnvironmentService,
@IEnvironmentService private environmentService: IEnvironmentService @IConfigurationService private configurationService: IConfigurationService
) { ) {
const workspace = contextService.getWorkspace(); this._workspaceRoot = paths.normalize(workspaceRoot ? workspaceRoot.fsPath : '', true);
const fsPath = workspace ? workspace.resource.fsPath : '';
this._workspaceRoot = paths.normalize(fsPath, true);
this._execPath = environmentService.execPath; this._execPath = environmentService.execPath;
Object.keys(process.env).forEach(key => { Object.keys(envVariables).forEach(key => {
this[`env.${key}`] = process.env[key]; this[`env.${key}`] = envVariables[key];
}); });
} }
...@@ -85,9 +86,9 @@ export class ConfigurationResolverService implements IConfigurationResolverServi ...@@ -85,9 +86,9 @@ export class ConfigurationResolverService implements IConfigurationResolverServi
if (types.isString(value)) { if (types.isString(value)) {
return this.resolveString(value); return this.resolveString(value);
} else if (types.isArray(value)) { } else if (types.isArray(value)) {
return this.__resolveArray(value); return this.resolveArray(value);
} else if (types.isObject(value)) { } else if (types.isObject(value)) {
return this.__resolveLiteral(value); return this.resolveLiteral(value);
} }
return value; return value;
...@@ -98,17 +99,17 @@ export class ConfigurationResolverService implements IConfigurationResolverServi ...@@ -98,17 +99,17 @@ export class ConfigurationResolverService implements IConfigurationResolverServi
if (types.isString(value)) { if (types.isString(value)) {
return this.resolveString(value); return this.resolveString(value);
} else if (types.isArray(value)) { } else if (types.isArray(value)) {
return this.__resolveAnyArray(value); return this.resolveAnyArray(value);
} else if (types.isObject(value)) { } else if (types.isObject(value)) {
return this.__resolveAnyLiteral(value); return this.resolveAnyLiteral(value);
} }
return value; return value;
} }
protected resolveString(value: string): string { private resolveString(value: string): string {
let regexp = /\$\{(.*?)\}/g; let regexp = /\$\{(.*?)\}/g;
return value.replace(regexp, (match: string, name: string) => { const resolvedString = value.replace(regexp, (match: string, name: string) => {
let newValue = (<any>this)[name]; let newValue = (<any>this)[name];
if (types.isString(newValue)) { if (types.isString(newValue)) {
return newValue; return newValue;
...@@ -116,9 +117,25 @@ export class ConfigurationResolverService implements IConfigurationResolverServi ...@@ -116,9 +117,25 @@ export class ConfigurationResolverService implements IConfigurationResolverServi
return match && match.indexOf('env.') > 0 ? '' : match; return match && match.indexOf('env.') > 0 ? '' : match;
} }
}); });
return this.resolveConfigVariable(resolvedString);
}
private resolveConfigVariable(value: string): string {
let regexp = /\$\{config\.(.*?)\}/g;
return value.replace(regexp, (match: string, name: string) => {
let config = this.configurationService.getConfiguration();
let newValue = new Function('_', 'try {return _.' + name + ';} catch (ex) { return "";}')(config);
if (types.isString(newValue)) {
// Prevent infinite recursion and also support nested references (or tokens)
return newValue === value ? '' : this.resolveString(newValue);
} else {
return this.resolve(newValue) + '';
}
});
} }
private __resolveLiteral(values: IStringDictionary<string | IStringDictionary<string> | string[]>): IStringDictionary<string | IStringDictionary<string> | string[]> { private resolveLiteral(values: IStringDictionary<string | IStringDictionary<string> | string[]>): IStringDictionary<string | IStringDictionary<string> | string[]> {
let result: IStringDictionary<string | IStringDictionary<string> | string[]> = Object.create(null); let result: IStringDictionary<string | IStringDictionary<string> | string[]> = Object.create(null);
Object.keys(values).forEach(key => { Object.keys(values).forEach(key => {
let value = values[key]; let value = values[key];
...@@ -127,8 +144,8 @@ export class ConfigurationResolverService implements IConfigurationResolverServi ...@@ -127,8 +144,8 @@ export class ConfigurationResolverService implements IConfigurationResolverServi
return result; return result;
} }
private __resolveAnyLiteral<T>(values: T): T; private resolveAnyLiteral<T>(values: T): T;
private __resolveAnyLiteral<T>(values: any): any { private resolveAnyLiteral<T>(values: any): any {
let result: IStringDictionary<string | IStringDictionary<string> | string[]> = Object.create(null); let result: IStringDictionary<string | IStringDictionary<string> | string[]> = Object.create(null);
Object.keys(values).forEach(key => { Object.keys(values).forEach(key => {
let value = values[key]; let value = values[key];
...@@ -137,12 +154,12 @@ export class ConfigurationResolverService implements IConfigurationResolverServi ...@@ -137,12 +154,12 @@ export class ConfigurationResolverService implements IConfigurationResolverServi
return result; return result;
} }
private __resolveArray(value: string[]): string[] { private resolveArray(value: string[]): string[] {
return value.map(s => this.resolveString(s)); return value.map(s => this.resolveString(s));
} }
private __resolveAnyArray<T>(value: T[]): T[]; private resolveAnyArray<T>(value: T[]): T[];
private __resolveAnyArray(value: any[]): any[] { private resolveAnyArray(value: any[]): any[] {
return value.map(s => this.resolveAny(s)); return value.map(s => this.resolveAny(s));
} }
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册