VScode:在tasks.json 中定义自己的变量 [英] VScode: Defining own variables in tasks.json

查看:161
本文介绍了VScode:在tasks.json 中定义自己的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了一个 tasks.json 文件来在多个平台上构建一个项目.所有平台都看到项目存储库的相同内容.这是通过磁盘共享完成的,因为在 VM 中运行另一个平台,或者通过与 Git 存储库同步.到目前为止一切顺利,他们都看到了相同的 task.json.然而,一些命令行相当长,而这些长行在大多数情况下是相同的.例如:

I've set up a tasks.json file for building a project on multiple platforms. All platforms see the same content of the project repository. This is done either via disk sharing, because of running another platform in a VM, or via sync with the Git repository. So far so good, they all see the same task.json. However some command lines are rather long and those long lines are identical for most part. for example:

"rm -rf build; mkdir build; cd build; ../configure --with-bash-malloc=no CFLAGS=\"-O3 -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-free\" LDFLAGS=-L/usr/local/lib LIBS=\"-ltcmalloc -lcurl\" CC=clang

不同平台有类似的线路.不同平台的配置部分总是相同的,因此最好将这个公共部分分解出来.因此,问题是是否可以定义您自己的变量,以便您可以像 ${workspaceRoot} 一样使用它们.

Similar lines are there for the different platforms. The configure part is always the same for the different platforms, so it would be nice to factor out this common part. Thus the question is if it is possible to define your own variables, so you can use them similar to ${workspaceRoot}.

因此定义某处

"win_dir": "build_windows",
"linux_dir": "build",
"osx_dir": "build_osx",
"configure": "../configure --with-bash-malloc=no CFLAGS=\"-O3 -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-free\" LDFLAGS=-L/usr/local/lib LIBS=\"-ltcmalloc -lcurl\" CC=clang"

然后写

"tasks": [
    {
        "taskName": "configure",
        "command": "bash",
        "windows": {
            "args": ["-c", "rm -rf ${win_dir}; mkdir ${win_dir}; cd ${win_dir}; ${configure}"]
        },
        "linux": {
            "args": ["-c", "rm -rf ${linux_dir}; mkdir ${linux_dir}; cd ${linux_dir}; ${configure}"]
        },
        "osx": {
            "args": ["-c", "rm -rf ${osx_dir}; mkdir ${osx_dir}; cd ${osx_dir}; ${configure}"]
        },
        "isBuildCommand": true,
        "problemMatcher": "$make-compile"
    },
    ... others tasks using the variables

当对构建目录或传递给配置等的参数进行更改时,tasks.json 文件只需要在一处进行编辑,而不是多处.

When making changes to the build directory or arguments passed to configure etc, then the tasks.json file needs only editing at one place, instead of many.

也许已经有可能了,但我不知道怎么做.我试图用声明块做一些事情,但这似乎与 problemMatcher 有很大关系.您可以找到一些示例,但我找不到关于 tasks.json 文件元素及其交互方式的明确文档.

Perhaps it is already possible but I'm unable to find out how. I tried to do something with the declares block, but that seems to be hard tied to problemMatcher. You can find some examples, but I could not find clear documentation of of the elements of the tasks.json file and how they interact.

也许我遗漏了一些东西,请教育我!

Perhaps I'm missing something, please educate me!

推荐答案

另一个答案不起作用,因为至少在 Windows 上,shell 不会替换作为参数给出的环境变量.${env:...} 对该答案的评论中建议的变量不会使用 tasks.json 本身中设置的环境变量替换,只有预先存在的变量.但是,您可以在 settings.json 中添加自定义设置,并使用 ${config:...}tasks.json 中引用这些设置.

The other answer won't work because, at least on windows, the shell will not substitute environment variables given as arguments. ${env:...} variables as suggested in a comment on that answer won't be substituted using environment variables set in tasks.json itself, only preexisting ones. You can however add custom settings in settings.json, and reference those in tasks.json using ${config:...}.

例如settings.json:

{
"win_dir": "build_windows",
"linux_dir": "build",
"osx_dir": "build_osx",
"configure": "../configure --with-bash-malloc=no CFLAGS=\"-O3 -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-free\" LDFLAGS=-L/usr/local/lib LIBS=\"-ltcmalloc -lcurl\" CC=clang"
}

tasks.json 中:

"tasks": [
    {
        "taskName": "configure",
        "command": "bash",
        "windows": {
            "args": ["-c", "rm -rf ${config:win_dir}; mkdir ${config:win_dir}; cd ${config:win_dir}; ${config:configure}"]
        },
        "linux": {
            "args": ["-c", "rm -rf ${config:linux_dir}; mkdir ${config:linux_dir}; cd ${config:linux_dir}; ${config:configure}"]
        },
        "osx": {
            "args": ["-c", "rm -rf ${config:osx_dir}; mkdir ${config:osx_dir}; cd ${config:osx_dir}; ${config:configure}"]
        },
        "isBuildCommand": true,
        "problemMatcher": "$make-compile"
    },
    ... others tasks using the variables

这篇关于VScode:在tasks.json 中定义自己的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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