Skip to content

Commit 39ddc3e

Browse files
committed
Add setting "preConfigureTask"
1 parent e47c89d commit 39ddc3e

7 files changed

Lines changed: 48 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Features:
66
- Add support for the FASTBuild generator (CMake 4.2+). [#4690](https://github.com/microsoft/vscode-cmake-tools/pull/4690)
77
- Add support for `${workspaceFolder}`, `${workspaceFolder:name}` variables and relative paths in `cmake.exclude` setting for multi-root workspaces. [#4689](https://github.com/microsoft/vscode-cmake-tools/pull/4689)
88
- Add `onConfigureResult` event to the CMake Tools API that fires after every configure attempt (success or failure), allowing dependent extensions to detect and react to configure failures. [#4021](https://github.com/microsoft/vscode-cmake-tools/issues/4021)
9+
- Add `cmake.preConfigureTask` setting to execute a named VS Code task before every CMake configure. [@erdemiru](https://github.com/erdemiru)
910

1011
Improvements:
1112
- Reduce CI pipeline time by parallelizing E2E test jobs and adding build artifact caching.

docs/cmake-settings.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Options that support substitution, in the table below, allow variable references
3131
| `cmake.cmakeProviderExtensions` | List of VS Code extension IDs that provide or install their own CMake binary. When CMake is not found during automatic configure-on-open and one of these extensions is installed, CMake Tools will briefly poll for CMake availability instead of showing an immediate error. Set to an empty array to disable this behavior. | `["stmicroelectronics.stm32-vscode-extension", "espressif.esp-idf-extension", "NXPSemiconductors.mcuxpresso", "nordic-semiconductor.nrf-connect"]` | no |
3232
| `cmake.configureSettings` | An object containing `key:value` pairs, which will be passed to CMake when configuring. The same as passing `-DVAR_NAME=ON` via `cmake.configureArgs`. NOTE: Semicolons (`;`) in string values are passed through to CMake verbatim, CMake itself decides whether to treat them as list separators (matching how CMake Presets `cacheVariables` and `cmake-kits.json` `cmakeSettings` behave). To pass a CMake list, you can either use array notation (e.g. `"MY_LIST": [ "a", "b" ]`) or write the list as a string with `;` separators (e.g. `"MY_LIST": "a;b"`). If you genuinely need a literal `;` inside a single list element, pre-escape it as `\;` in your JSON. | `{}` (no values) | yes |
3333
| `cmake.copyCompileCommands`| If not `null`, copies the `compile_commands.json` file generated by CMake to the path specified by this setting whenever CMake successfully configures. | `null` (do not copy the file) | yes |
34+
| `cmake.preConfigureTask`| If not `null`, the task with this name is executed before CMake configures. | `null` (do not run any task) | yes |
3435
| `cmake.postConfigureTask`| If not `null`, the task with this name is executed whenever CMake successfully configures. | `null` (do not run any task) | yes |
3536
| `cmake.coverageInfoFiles` | LCOV coverage info files to be processed after running tests with coverage using the test explorer. | `[]` | yes |
3637
| `cmake.cpackArgs` | An array of additional arguments to pass to cpack. | `[]` | yes |

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3108,6 +3108,12 @@
31083108
"description": "%cmake-tools.configuration.cmake.copyCompileCommands.description%",
31093109
"scope": "resource"
31103110
},
3111+
"cmake.preConfigureTask": {
3112+
"type": "string",
3113+
"default": null,
3114+
"description": "%cmake-tools.configuration.cmake.preConfigureTask.description%",
3115+
"scope": "resource"
3116+
},
31113117
"cmake.postConfigureTask": {
31123118
"type": "string",
31133119
"default": null,

package.nls.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@
272272
"cmake-tools.configuration.cmake.emscriptenSearchDirs.description": "Directories where Emscripten may be installed.",
273273
"cmake-tools.configuration.cmake.mergedCompileCommands.description": "Recursively collect and merge all compile_commands.json found in the cmake.buildDirectory.",
274274
"cmake-tools.configuration.cmake.copyCompileCommands.description": "Copy compile_commands.json to this location after a successful configure.",
275+
"cmake-tools.configuration.cmake.preConfigureTask.description": "If set, this named task will be executed before CMake configure.",
275276
"cmake-tools.configuration.cmake.postConfigureTask.description": "If set, this named task will be executed after a successful CMake configure.",
276277
"cmake-tools.configuration.cmake.configureOnOpen.description": "Automatically configure CMake project directories when they are opened.",
277278
"cmake-tools.configuration.cmake.configureOnEdit.description": "Automatically configure CMake project directories when cmake.sourceDirectory or CMakeLists.txt content are saved.",

src/cmakeProject.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1717,6 +1717,35 @@ export class CMakeProject {
17171717

17181718
}
17191719

1720+
/**
1721+
* Execute the preConfigureTask if configured
1722+
*/
1723+
private async executePreConfigureTask(): Promise<void> {
1724+
const preConfigureTask = this.workspaceContext.config.preConfigureTask;
1725+
if (preConfigureTask) {
1726+
try {
1727+
log.debug(localize('executing.pre.configure.task', 'Executing pre configure task: {0}', preConfigureTask));
1728+
1729+
// Fetch all available tasks
1730+
const tasks = await vscode.tasks.fetchTasks();
1731+
1732+
// Find the task by label
1733+
const task = tasks.find(t => t.name === preConfigureTask);
1734+
1735+
if (task) {
1736+
await vscode.tasks.executeTask(task);
1737+
} else {
1738+
const errorMsg = localize('task.not.found', 'Task "{0}" not found. Available tasks: {1}', preConfigureTask, tasks.map(t => t.name).join(', '));
1739+
void vscode.window.showErrorMessage(errorMsg);
1740+
log.error(errorMsg);
1741+
}
1742+
} catch (error: any) {
1743+
void vscode.window.showErrorMessage(localize('failed.to.execute.pre.configure.task', 'Failed to execute pre configure task: {0}', error.toString()));
1744+
log.error(localize('pre.configure.task.error', 'Error executing pre configure task'), error);
1745+
}
1746+
}
1747+
}
1748+
17201749
/**
17211750
* Execute the postConfigureTask if configured
17221751
*/
@@ -1764,6 +1793,8 @@ export class CMakeProject {
17641793
// Don't show a progress bar when the extension is using Cache for configuration.
17651794
// Using cache for configuration happens only one time.
17661795
if (drv && drv.shouldUseCachedConfiguration(trigger)) {
1796+
await this.executePreConfigureTask();
1797+
17671798
const result: ConfigureResult = await drv.configure(trigger, []);
17681799
if (result.exitCode === 0) {
17691800
await this.refreshCompileDatabase(drv.expansionOptions);
@@ -1838,6 +1869,8 @@ export class CMakeProject {
18381869
});
18391870
try {
18401871
progress.report({ message: this.folderName });
1872+
await this.executePreConfigureTask();
1873+
18411874
let result: ConfigureResult;
18421875
await setContextAndStore(isConfiguringKey, true);
18431876
if (type === ConfigureType.Cache) {

src/config.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ export interface ExtensionConfigurationSettings {
223223
emscriptenSearchDirs: string[];
224224
mergedCompileCommands: string | null;
225225
copyCompileCommands: string | null;
226+
preConfigureTask: string | null;
226227
postConfigureTask: string | null;
227228
loadCompileCommands: boolean;
228229
configureOnOpen: boolean;
@@ -604,6 +605,9 @@ export class ConfigurationReader implements vscode.Disposable {
604605
get copyCompileCommands(): string | null {
605606
return this.configData.copyCompileCommands;
606607
}
608+
get preConfigureTask(): string | null {
609+
return this.configData.preConfigureTask;
610+
}
607611
get postConfigureTask(): string | null {
608612
return this.configData.postConfigureTask;
609613
}
@@ -738,6 +742,7 @@ export class ConfigurationReader implements vscode.Disposable {
738742
emscriptenSearchDirs: new vscode.EventEmitter<string[]>(),
739743
mergedCompileCommands: new vscode.EventEmitter<string | null>(),
740744
copyCompileCommands: new vscode.EventEmitter<string | null>(),
745+
preConfigureTask: new vscode.EventEmitter<string | null>(),
741746
postConfigureTask: new vscode.EventEmitter<string | null>(),
742747
loadCompileCommands: new vscode.EventEmitter<boolean>(),
743748
configureOnOpen: new vscode.EventEmitter<boolean>(),

test/unit-tests/config.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ function createConfig(conf: Partial<ExtensionConfigurationSettings>): Configurat
9090
postRunCoverageTarget: null,
9191
coverageInfoFiles: [],
9292
useFolderPropertyInBuildTargetDropdown: true,
93+
preConfigureTask: null,
9394
postConfigureTask: null,
9495
additionalBuildProblemMatchers: [],
9596
shell: null,

0 commit comments

Comments
 (0)