使用 shell 命令作为 VSCode 任务变量值

fra*_*isr 1 visual-studio-code vscode-settings vscode-tasks

我正在尝试定义一个 VSCode 任务tasks.json,以适应 VSCode 运行的特定架构。为此,我想将架构设为uname --m例如“aarch64”或“amd64”)。我的目标是将 的输出uname插入到这样的环境变量中

"version": "2.0.0",
"tasks": [
    {
        "label": "build",
        "type": "shell",
        "command": "cmake",
        "args": [
            "-DMYLIB_INCLUDE_DIR=$MYLIB/include",
            "-DMYLIB_LIBRARY=$MYLIB/lib"
        ],
        "options": {
            "env": {
                "MYLIB": "${workspaceFolder}/mylib/${command:get_arch}"
            }
        },
    }
]
Run Code Online (Sandbox Code Playgroud)

就我而言,我将有架构的特定版本mylibmylib/aarch64mylib/amd64

到目前为止,我尝试定义get_arch在 的环境定义中使用的第二个任务MYLIB,它只是运行uname

{
    "label": "get_arch",
    "type": "shell",
    "command": "uname --m"
}
Run Code Online (Sandbox Code Playgroud)

当然,这个任务不是一个正确的命令,所以它没有被 VSCode 检测到,我的构建任务失败了。我查看了有关变量替换的文档,但他们没有提到是否可以替换shell 命令。我想这在扩展中是可能的,但我想让事情尽可能简单。

Dmi*_*hev 5

扩展提供了一种将任意 shell 命令作为 VS Code 命令启动的方法:

"tasks": [
    {
        "label": "test_arch",
        "type": "shell",
        "command": "echo",
        "args": [
            "${MYARCH}"
        ],
        "options": {
            "env": {
                "MYARCH": "${input:get_arch}"
            }
        },
        "problemMatcher": []
    },
],
"inputs": [
    {
        "id": "get_arch",
        "type": "command",
        "command": "shellCommand.execute",
        "args": {
            "command": "uname -m"
        }
    }
]
Run Code Online (Sandbox Code Playgroud)

我发现的一个缺点是,当在选择器中提示命令的结果时,您必须再次按Enter键。除此之外,这是实现您想要的最直接的方法,但它可以在许多类似的情况下使用。

或者,您可以使用 arch 选择器添加pickString 输入或创建仅添加单个命令 GetArch 的扩展。