我想通过定义一些输入变量来使用 Visual Studio Code 定义可配置任务。
在我的特定情况下,我希望一项任务取决于带有子句
${input:package_manager}
的 "dependsOn": ["${input:package_manager}"]
变量的值。
但是,这样的设置会输出错误:
Activating task providers all
Couldn't resolve dependent task '${input:package_manager}' in workspace folder 'vscode-remote://
从文档可以清楚地看出,并非所有字段都支持输入变量:
并非tasks.json中的所有值都支持变量替换。具体来说,只有 command、args 和 options 支持变量替换。输入部分中的输入变量将不会被解析,因为不支持输入变量的嵌套
但是根据用户输入选择任务的替代方法是什么?
{
"version": "2.0.0",
"inputs": [
{
"id": "std",
"description": "std version to be used",
"type": "pickString",
"options": ["20","23"],
"default": "23",
},
{
"id": "build_type",
"description": "Build type to be used: Debug or Release",
"type": "pickString",
"options": ["Debug","Release"],
"default": "Debug",
},
{
"id": "package_manager",
"description": "Package Manager",
"type": "pickString",
"options": ["vcpkg","conan"],
"default": "vcpkg",
},
],
"tasks": [
{
"label": "vcpkg",
"type": "shell",
"command": "vcpkg --version",
"group": "test",
"presentation": {
"reveal": "always",
"panel": "shared"
},
"hide": true,
},
{
"label": "conan",
"type": "shell",
"command": "conan install . -b missing -of build/conan -s compiler=gcc -s compiler.version=14 -s build_type=${input:build_type} -s compiler.cppstd=${input:std}",
"group": "test",
"presentation": {
"reveal": "always",
"panel": "shared"
},
"hide": true,
},
{
"label": "Configure CMake",
"type": "shell",
"command": "cmake --preset ${input:package_manager} -D CMAKE_BUILD_TYPE=${input:build_type} -DCMAKE_CXX_STANDARD=${input:std}",
"group": "test",
"presentation": {
"reveal": "always",
"panel": "shared"
},
"dependsOn": ["${input:package_manager}"],
},
{
"label": "Build Unit Tests",
"type": "shell",
"command": "cmake --build --preset ${input:package_manager}-${input:build_type}",
"group": "test",
"presentation": {
"reveal": "always",
"panel": "shared"
},
"dependsOn": ["Configure CMake"],
},
{
"label": "Run Unit Tests",
"type": "shell",
"command": "cd ${workspaceFolder}/build && ctest -C ${input:build_type} --rerun-failed --output-on-failure",
"group": "test",
"presentation": {
"reveal": "always",
"panel": "shared"
},
"problemMatcher": [],
"dependsOn": ["Build Unit Tests"],
},
{
"label": "Make Docs",
"type": "shell",
"command": "cd ${workspaceFolder}/build && make docs ",
"group": "test",
"presentation": {
"reveal": "always",
"panel": "shared"
},
"dependsOn": ["Run Unit Tests"],
},
]
}
使用我编写的扩展,命令变量和命令
extension.commandvariable.pickStringRemember
,您可以在选择一个选项时设置多个字符串。使用这些字符串之一作为任务的命令。
{
"version": "2.0.0",
"inputs": [
{
"id": "std",
"key": "std",
"description": "std version to be used",
"type": "extension.commandvariable.pickStringRemember",
"options": ["20","23"],
"default": "23",
},
{
"id": "build_type",
"key": "build_type",
"description": "Build type to be used: Debug or Release",
"type": "extension.commandvariable.pickStringRemember",
"options": ["Debug","Release"],
"default": "Debug",
},
{
"id": "pmcommand",
"description": "Package Manager Command",
"type": "command",
"command": "extension.commandvariable.remember",
"args": { "key": "pmcommand" }
},
{
"id": "rmpackage_manager",
"description": "Remember Package Manager",
"type": "command",
"command": "extension.commandvariable.remember",
"args": { "key": "package_manager" }
},
{
"id": "rmbuild_type",
"description": "Remember Build type",
"type": "command",
"command": "extension.commandvariable.remember",
"args": { "key": "build_type" }
},
{
"id": "package_manager",
"description": "Package Manager",
"type": "command",
"command": "extension.commandvariable.pickStringRemember",
"args": {
"key": "package_manager",
"options": [
["vcpkg", {
"package_manager":"vcpkg",
"pmcommand":"vcpkg --version"
}],
["conan", {
"package_manager":"conan",
"pmcommand":"conan install . -b missing -of build/conan -s compiler=gcc -s compiler.version=14 -s build_type=${remember:build_type} -s compiler.cppstd=${remember:std}"
}]
],
"description": "Pick a Package Manager",
"default": "vcpkg"
}
},
],
"tasks": [
{
"label": "package_manager",
"type": "shell",
"command": "${input:pmcommand}",
"group": "test",
"presentation": {
"reveal": "always",
"panel": "shared"
},
"hide": true,
},
{
"label": "Configure CMake",
"type": "shell",
"command": "cmake --preset ${input:rmpackage_manager} -D CMAKE_BUILD_TYPE=${input:rmbuild_type} -DCMAKE_CXX_STANDARD=${input:std}",
"group": "test",
"presentation": {
"reveal": "always",
"panel": "shared"
},
"dependsOn": ["package_manager"],
},
{
"label": "Build Unit Tests",
"type": "shell",
"command": "cmake --build --preset ${input:package_manager}-${input:rmbuild_type}",
"group": "test",
"presentation": {
"reveal": "always",
"panel": "shared"
},
"dependsOn": ["Configure CMake"],
},
{
"label": "Run Unit Tests",
"type": "shell",
"command": "cd ${workspaceFolder}/build && ctest -C ${input:build_type} --rerun-failed --output-on-failure",
"group": "test",
"presentation": {
"reveal": "always",
"panel": "shared"
},
"problemMatcher": [],
"dependsOn": ["Build Unit Tests"],
},
{
"label": "Make Docs",
"type": "shell",
"command": "cd ${workspaceFolder}/build && make docs ",
"group": "test",
"presentation": {
"reveal": "always",
"panel": "shared"
},
"dependsOn": ["Run Unit Tests"],
},
]
}
我还没有测试过它,但你可以了解如何实现它。
您也许可以使用 extension.commandvariable.pickStringRemember
的
Multi Pick选项并记住某些选项的多个字符串,并使用
__key
属性选择特定的字符串。