Skip to content

Commit

Permalink
new variable resolution for configs
Browse files Browse the repository at this point in the history
  • Loading branch information
matepek committed Jul 22, 2022
1 parent cbedeb8 commit fcbf673
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 11 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## [Unreleased]

## [4.2.3]

### Added

- new variable resolution rule: `${config:<config name>}` where `<config name>` can be anything like

## [4.2.2] - 2022-05-22

### Added
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 15 additions & 1 deletion src/WorkspaceManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,33 @@ export class WorkspaceManager implements vscode.Disposable {
}),
);

const wsConfig = vscode.workspace.getConfiguration();

const variableToValue = [
createPythonIndexerForPathVariable('workspaceFolder', this.workspaceFolder.uri.fsPath),
createPythonIndexerForPathVariable('workspaceDirectory', this.workspaceFolder.uri.fsPath),
workspaceNameRes,
{
resolve: /\$\{assert(?::([^}]+))?\}/,
rule: async (m: RegExpMatchArray): Promise<never> => {
rule: (m: RegExpMatchArray): never => {
const msg = m[1] ? ': ' + m[1] : '';
throw Error('Assertion while resolving variable' + msg);
},
},
{ resolve: '${osPathSep}', rule: osPathSeparator },
{ resolve: '${osPathEnvSep}', rule: process.platform === 'win32' ? ';' : ':' },
{
resolve: /\$\{config:([^}]+)\}/,
rule: (m: RegExpMatchArray): string => {
try {
const ruleV = wsConfig.get<string>(m[1])?.toString();
if (ruleV !== undefined) return ruleV;
} catch (reason) {
log.warnS("couldn't resolve config", m[0]);
}
return m[0];
},
},
{
resolve: /\$\{command:([^}]+)\}/,
rule: async (m: RegExpMatchArray): Promise<string> => {
Expand Down
27 changes: 18 additions & 9 deletions src/util/ResolveRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,20 +121,30 @@ interface ResolveStrRuleStr {
isFlat?: boolean;
}

interface ResolveStrRuleAsync<R> {
interface ResolveStrRule<R> {
resolve: string;
rule: () => Promise<R>;
rule: () => R | Promise<R>;
isFlat?: boolean;
}

interface ResolveRegexRule {
resolve: RegExp;
rule: (m: RegExpMatchArray) => string;
isFlat?: never;
}

interface ResolveRegexRuleAsync {
resolve: RegExp;
rule: (m: RegExpMatchArray) => Promise<string>;
isFlat?: never;
}

// eslint-disable-next-line
export type ResolveRuleAsync<R = any> = ResolveStrRuleStr | ResolveStrRuleAsync<R> | ResolveRegexRuleAsync;
export type ResolveRuleAsync<R = any> =
| ResolveStrRuleStr
| ResolveStrRule<R>
| ResolveRegexRule
| ResolveRegexRuleAsync;

// eslint-disable-next-line
export function resolveVariablesAsync<T>(value: T, varValue: readonly ResolveRuleAsync<any>[]): Promise<T> {
Expand Down Expand Up @@ -260,17 +270,17 @@ export function createPythonIndexerForStringVariable(
value: string,
separator: string | RegExp,
join: string,
): ResolveRegexRuleAsync {
): ResolveRegexRule {
const resolve = new RegExp('\\$\\{' + varName + PythonIndexerRegexStr + '?\\}');
const array = value.split(separator);

return {
resolve,
rule: async (m: RegExpMatchArray): Promise<string> => processArrayWithPythonIndexer(array, m).join(join),
rule: (m: RegExpMatchArray): string => processArrayWithPythonIndexer(array, m).join(join),
};
}

export function createPythonIndexerForPathVariable(valName: string, pathStr: string): ResolveRegexRuleAsync {
export function createPythonIndexerForPathVariable(valName: string, pathStr: string): ResolveRegexRule {
const { resolve, rule } = createPythonIndexerForStringVariable(
valName,
pathlib.normalize(pathStr),
Expand All @@ -280,10 +290,9 @@ export function createPythonIndexerForPathVariable(valName: string, pathStr: str

return {
resolve,
rule: async (m: RegExpMatchArray): Promise<string> => {
rule: (m: RegExpMatchArray): string => {
try {
const ruleV = await rule(m);
return pathlib.normalize(ruleV);
return pathlib.normalize(rule(m));
} catch (e) {
return m[0];
}
Expand Down

0 comments on commit fcbf673

Please sign in to comment.