使用 shell 命令作为 VSCode 任务变量值 [英] Using a shell command as VSCode task variable value

查看:36
本文介绍了使用 shell 命令作为 VSCode 任务变量值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to define a VSCode task in tasks.json that would adapt to the specific architecture where VSCode runs. To do this, I want to get the architecture as uname --m (e.g. "aarch64" or "amd64"). My goal is to interpolate the output of uname into an environment variable like so

"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}"
            }
        },
    }
]

就我而言,我将在 mylib/aarch64mylib/amd64等下拥有特定于架构的 mylib.

In my case, I will have architecture-specific versions of mylib under mylib/aarch64, mylib/amd64, etc.

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

My attempt so far as been to define a second get_arch task used in the environment definition of MYLIB, that simply runs uname.

{
    "label": "get_arch",
    "type": "shell",
    "command": "uname --m"
}

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

Of course, this task is not a proper command and so it isn't detected by VSCode and my build task fails. I checked out the documentation on variable substition, but they don't mention if it's possible to substitute a shell command. I guess this would be possible from within an extension, but I want to keep things as simple as possible.

推荐答案

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

This extension provides a way to launch arbitrary shell commands as a VS Code command:

"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"
        }
    }
]

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

One disadvantage I discovered is that you have to press Enter once more when the result of the command is prompted in picker. Aside of this, this is the straightest way to implement what you want, and yet it can be utilized in many similar situations.

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

Alternatively, you can add pickString input with arch picker or create an extension that adds only single command GetArch.

这篇关于使用 shell 命令作为 VSCode 任务变量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆