From b39c24c52b54b0787e627b9caa634b178e9a6311 Mon Sep 17 00:00:00 2001 From: piomis Date: Tue, 7 Feb 2023 22:53:22 +0100 Subject: [PATCH 1/6] fix(CMakeTaskProvider): expand targets names while searching for matching tasks --- CHANGELOG.md | 3 ++- src/cmakeTaskProvider.ts | 18 +++++++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d1a03f2c3..1ce0f9807 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ Bug Fixes: - Check if "CMakeLists.txt" exists after renaming. [#2986](https://github.com/microsoft/vscode-cmake-tools/issues/2986) - CMake kits fails when parsing exported functions after running environmentSetupScript. [#2676](https://github.com/microsoft/vscode-cmake-tools/issues/2686) - Implement cmake.parseBuildDiagnostics. [#1932](https://github.com/microsoft/vscode-cmake-tools/issues/1932) +- Expand variables in task's targets while searching matching taks. [#2970](https://github.com/microsoft/vscode-cmake-tools/issues/2970) [@piomis](https://github.com/piomis) ## 1.13.45 Bug Fixes: @@ -104,7 +105,7 @@ Bug Fixes: - Revert back to the previous CMake language server extension dependency. [PR #2599](https://github.com/microsoft/vscode-cmake-tools/pull/2599) Bug Fixes: -- Ninja is used as a default generator. [#2598](https://github.com/microsoft/vscode-cmake-tools/issues/2598) +- Ninja is used as a default generator. [#2598](https://github.com/microsoft/vscode-cmake-tools/issues/2598) ## 1.11.25 Improvements: diff --git a/src/cmakeTaskProvider.ts b/src/cmakeTaskProvider.ts index b282a1104..d36e8a603 100644 --- a/src/cmakeTaskProvider.ts +++ b/src/cmakeTaskProvider.ts @@ -14,6 +14,7 @@ import * as preset from '@cmt/preset'; import { UseCMakePresets } from './config'; import * as telemetry from '@cmt/telemetry'; import * as util from '@cmt/util'; +import * as expand from '@cmt/expand'; nls.config({ messageFormat: nls.MessageFormat.bundle, bundleFormat: nls.BundleFormat.standalone })(); const localize: nls.LocalizeFunc = nls.loadMessageBundle(); @@ -189,15 +190,26 @@ export class CMakeTaskProvider implements vscode.TaskProvider { public static async findBuildTask(presetName?: string, targets?: string[]): Promise { // Fetch all CMake task from `tasks.json` files. const allTasks: vscode.Task[] = await vscode.tasks.fetchTasks({ type: CMakeTaskProvider.CMakeScriptType }); - const tasks: (CMakeTask | undefined)[] = allTasks.map((task: any) => { + + const project: CMakeProject | undefined = getActiveProject(); + const cmakeDriver: CMakeDriver | undefined = (await project?.getCMakeDriverInstance()) || undefined; + const tasks: (CMakeTask | undefined)[] = await Promise.all(allTasks.map(async (task: any) => { if (!task.definition.label || !task.group || (task.group && task.group.id !== vscode.TaskGroup.Build.id)) { return undefined; } + + let taskTargets: string[]; + if (cmakeDriver) { + taskTargets = await expand.expandStrings(task.definition.targets, cmakeDriver.expansionOptions); + } else { + taskTargets = task.definition.targets; + } + const definition: CMakeTaskDefinition = { type: task.definition.type, label: task.definition.label, command: task.definition.command, - targets: task.definition.targets || targets, + targets: taskTargets || targets, preset: task.definition.preset, options: task.definition.options }; @@ -208,7 +220,7 @@ export class CMakeTaskProvider implements vscode.TaskProvider { buildTask.isDefault = true; } return buildTask; - }); + })); const buildTasks: CMakeTask[] = tasks.filter((task) => task !== undefined) as CMakeTask[]; From aa138458d7d5c16d1bd6d4be42788f0bcd7115f7 Mon Sep 17 00:00:00 2001 From: piomis Date: Fri, 10 Feb 2023 17:14:06 +0100 Subject: [PATCH 2/6] feat(cmakeTaskProvider): Pass expansionOptions as argument instead of querying cmakeProject --- src/cmakeTaskProvider.ts | 8 +++----- src/drivers/cmakeDriver.ts | 2 +- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/cmakeTaskProvider.ts b/src/cmakeTaskProvider.ts index d36e8a603..382fd9ba4 100644 --- a/src/cmakeTaskProvider.ts +++ b/src/cmakeTaskProvider.ts @@ -187,20 +187,18 @@ export class CMakeTaskProvider implements vscode.TaskProvider { return task; } - public static async findBuildTask(presetName?: string, targets?: string[]): Promise { + public static async findBuildTask(presetName?: string, targets?: string[], expansionOptions?: expand.ExpansionOptions): Promise { // Fetch all CMake task from `tasks.json` files. const allTasks: vscode.Task[] = await vscode.tasks.fetchTasks({ type: CMakeTaskProvider.CMakeScriptType }); - const project: CMakeProject | undefined = getActiveProject(); - const cmakeDriver: CMakeDriver | undefined = (await project?.getCMakeDriverInstance()) || undefined; const tasks: (CMakeTask | undefined)[] = await Promise.all(allTasks.map(async (task: any) => { if (!task.definition.label || !task.group || (task.group && task.group.id !== vscode.TaskGroup.Build.id)) { return undefined; } let taskTargets: string[]; - if (cmakeDriver) { - taskTargets = await expand.expandStrings(task.definition.targets, cmakeDriver.expansionOptions); + if (expansionOptions) { + taskTargets = await expand.expandStrings(task.definition.targets, expansionOptions); } else { taskTargets = task.definition.targets; } diff --git a/src/drivers/cmakeDriver.ts b/src/drivers/cmakeDriver.ts index 253c2b851..569a9c9ff 100644 --- a/src/drivers/cmakeDriver.ts +++ b/src/drivers/cmakeDriver.ts @@ -1761,7 +1761,7 @@ export abstract class CMakeDriver implements vscode.Disposable { } const useBuildTask: boolean = this.config.buildTask && isBuildCommand === true; if (useBuildTask) { - const task: CMakeTask | undefined = await CMakeTaskProvider.findBuildTask(this._buildPreset?.name, targets); + const task: CMakeTask | undefined = await CMakeTaskProvider.findBuildTask(this._buildPreset?.name, targets, this.expansionOptions); if (task) { const resolvedTask: CMakeTask | undefined = await CMakeTaskProvider.resolveInternalTask(task); if (resolvedTask) { From a89030adc9024d97322b45cb26bc0a8eb16629f7 Mon Sep 17 00:00:00 2001 From: elrashed Date: Mon, 13 Feb 2023 07:51:05 -0800 Subject: [PATCH 3/6] expand the options cwd --- src/cmakeTaskProvider.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/cmakeTaskProvider.ts b/src/cmakeTaskProvider.ts index 382fd9ba4..c17305818 100644 --- a/src/cmakeTaskProvider.ts +++ b/src/cmakeTaskProvider.ts @@ -199,6 +199,9 @@ export class CMakeTaskProvider implements vscode.TaskProvider { let taskTargets: string[]; if (expansionOptions) { taskTargets = await expand.expandStrings(task.definition.targets, expansionOptions); + if (task.definition.options?.cwd){ + task.definition.options.cwd = await expand.expandString(task.definition.options.cwd, expansionOptions); + } } else { taskTargets = task.definition.targets; } @@ -247,12 +250,12 @@ export class CMakeTaskProvider implements vscode.TaskProvider { } else { // Search for the matching default task. const defaultTask: CMakeTask[] = matchingTargetTasks.filter(task => task.isDefault); - if (defaultTask.length === 1) { + if (defaultTask.length >= 1) { return defaultTask[0]; } else { // Search for the matching existing task. const existingTask: CMakeTask[] = matchingTargetTasks.filter(task => !task.isTemplate); - if (existingTask.length === 1) { + if (existingTask.length >= 1) { return existingTask[0]; } } From 407217f85f23b4a2fc003529835cfc274fc0496e Mon Sep 17 00:00:00 2001 From: elrashed Date: Thu, 16 Mar 2023 10:01:25 -0700 Subject: [PATCH 4/6] some final changes --- src/cmakeTaskProvider.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/cmakeTaskProvider.ts b/src/cmakeTaskProvider.ts index c17305818..d2596214c 100644 --- a/src/cmakeTaskProvider.ts +++ b/src/cmakeTaskProvider.ts @@ -253,10 +253,12 @@ export class CMakeTaskProvider implements vscode.TaskProvider { if (defaultTask.length >= 1) { return defaultTask[0]; } else { - // Search for the matching existing task. - const existingTask: CMakeTask[] = matchingTargetTasks.filter(task => !task.isTemplate); - if (existingTask.length >= 1) { - return existingTask[0]; + // If there is no default task, matchingTargetTasks is a mixture of template and defined tasks. + // If there is only one task, that task is a template, so return the template. + // If there are only two tasks, the first one is always a template, and the second one is the defined task that we are searching for. + // But if there are more than two tasks, it means that there are multiple defiend tasks and none are set as default. So ask the user to choose one later. + if (matchingTargetTasks.length == 1 || matchingTargetTasks.length == 2) { + return matchingTargetTasks[matchingTargetTasks.length]; } } } From b4141919c5997a208e6ff85235429289547d397f Mon Sep 17 00:00:00 2001 From: elrashed Date: Thu, 16 Mar 2023 12:34:04 -0700 Subject: [PATCH 5/6] lint --- src/cmakeTaskProvider.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cmakeTaskProvider.ts b/src/cmakeTaskProvider.ts index d2596214c..a0d27c932 100644 --- a/src/cmakeTaskProvider.ts +++ b/src/cmakeTaskProvider.ts @@ -199,7 +199,7 @@ export class CMakeTaskProvider implements vscode.TaskProvider { let taskTargets: string[]; if (expansionOptions) { taskTargets = await expand.expandStrings(task.definition.targets, expansionOptions); - if (task.definition.options?.cwd){ + if (task.definition.options?.cwd) { task.definition.options.cwd = await expand.expandString(task.definition.options.cwd, expansionOptions); } } else { @@ -257,7 +257,7 @@ export class CMakeTaskProvider implements vscode.TaskProvider { // If there is only one task, that task is a template, so return the template. // If there are only two tasks, the first one is always a template, and the second one is the defined task that we are searching for. // But if there are more than two tasks, it means that there are multiple defiend tasks and none are set as default. So ask the user to choose one later. - if (matchingTargetTasks.length == 1 || matchingTargetTasks.length == 2) { + if (matchingTargetTasks.length === 1 || matchingTargetTasks.length === 2) { return matchingTargetTasks[matchingTargetTasks.length]; } } From d6091f85b0822db4570c216f4c9dcc426ff654c1 Mon Sep 17 00:00:00 2001 From: elrashed Date: Thu, 16 Mar 2023 13:40:47 -0700 Subject: [PATCH 6/6] fix --- src/cmakeTaskProvider.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cmakeTaskProvider.ts b/src/cmakeTaskProvider.ts index a0d27c932..7c2c21003 100644 --- a/src/cmakeTaskProvider.ts +++ b/src/cmakeTaskProvider.ts @@ -256,9 +256,9 @@ export class CMakeTaskProvider implements vscode.TaskProvider { // If there is no default task, matchingTargetTasks is a mixture of template and defined tasks. // If there is only one task, that task is a template, so return the template. // If there are only two tasks, the first one is always a template, and the second one is the defined task that we are searching for. - // But if there are more than two tasks, it means that there are multiple defiend tasks and none are set as default. So ask the user to choose one later. + // But if there are more than two tasks, it means that there are multiple defiend tasks and none are set as default. So ask the user to choose one later. if (matchingTargetTasks.length === 1 || matchingTargetTasks.length === 2) { - return matchingTargetTasks[matchingTargetTasks.length]; + return matchingTargetTasks[matchingTargetTasks.length - 1]; } } }