/*--------------------------------------------------------------------------------------------- * 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 * as Path from 'path' import * as Types from '../utils/sysTypes' import { IStringDictionary, ISystemVariables } from './types' /* tslint:disable:rule1 no-any no-unnecessary-callback-wrapper jsdoc-format no-for-in prefer-const no-increment-decrement */ export abstract class AbstractSystemVariables implements ISystemVariables { public resolve(value: string): string public resolve(value: string[]): string[] public resolve(value: IStringDictionary): IStringDictionary public resolve(value: IStringDictionary): IStringDictionary public resolve(value: IStringDictionary>): IStringDictionary> // tslint:disable-next-line:no-any public resolve(value: any): any { if (Types.isString(value)) { return this.__resolveString(value) } else if (Types.isArray(value)) { return this.__resolveArray(value) } else if (Types.isObject(value)) { return this.__resolveLiteral(value) } return value } public resolveAny(value: T): T // tslint:disable-next-line:no-any public resolveAny(value: any): any { if (Types.isString(value)) { return this.__resolveString(value) } else if (Types.isArray(value)) { return this.__resolveAnyArray(value) } else if (Types.isObject(value)) { return this.__resolveAnyLiteral(value) } return value } private __resolveString(value: string): string { const regexp = /\$\{(.*?)\}/g return value.replace(regexp, (match: string, name: string) => { // tslint:disable-next-line:no-any const newValue = (this)[name] if (Types.isString(newValue)) { return newValue } else { return match && (match.indexOf('env.') > 0 || match.indexOf('env:') > 0) ? '' : match } }) } private __resolveLiteral(values: IStringDictionary | string[]>): IStringDictionary | string[]> { const result: IStringDictionary | string[]> = Object.create(null) Object.keys(values).forEach(key => { const value = values[key] // tslint:disable-next-line:no-any result[key] = this.resolve(value) }) return result } private __resolveAnyLiteral(values: T): T // tslint:disable-next-line:no-any private __resolveAnyLiteral(values: any): any { const result: IStringDictionary | string[]> = Object.create(null) Object.keys(values).forEach(key => { const value = values[key] // tslint:disable-next-line:no-any result[key] = this.resolveAny(value) }) return result } private __resolveArray(value: string[]): string[] { return value.map(s => this.__resolveString(s)) } private __resolveAnyArray(value: T[]): T[] // tslint:disable-next-line:no-any private __resolveAnyArray(value: any[]): any[] { return value.map(s => this.resolveAny(s)) } } export class SystemVariables extends AbstractSystemVariables { private _workspaceFolder: string private _workspaceFolderName: string constructor(workspaceFolder?: string) { super() this._workspaceFolder = typeof workspaceFolder === 'string' ? workspaceFolder : __dirname this._workspaceFolderName = Path.basename(this._workspaceFolder) Object.keys(process.env).forEach(key => { (this as any as Record)[`env:${key}`] = (this as any as Record)[`env.${key}`] = process.env[key] }) } public get cwd(): string { return this.workspaceFolder } public get workspaceRoot(): string { return this._workspaceFolder } public get workspaceFolder(): string { return this._workspaceFolder } public get workspaceRootFolderName(): string { return this._workspaceFolderName } public get workspaceFolderBasename(): string { return this._workspaceFolderName } }