variables - VScode : Defining own variables in tasks. json

标签 variables task visual-studio-code

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

“rm -rf 构建;mkdir 构建;cd 构建;../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

不同平台有类似的线路。 对于不同的平台,配置部分始终是相同的,因此最好将这个公共(public)部分分解出来。因此,问题是是否可以定义自己的变量,以便您可以像 ${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 文件只需在一个位置进行编辑,而不是多个位置。

也许这已经是可能的,但我不知道如何实现。我尝试对声明 block 做一些事情,但这似乎与问题匹配器紧密相关。您可以找到一些示例,但我找不到关于tasks.json 文件的元素以及它们如何交互的清晰文档。

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

最佳答案

Adam Parkin's answer不起作用,因为至少在 Windows 上,shell 不会替换作为参数给出的环境变量。 ${env:...}该答案的评论中建议的变量不会使用 tasks.json 中设置的环境变量来替换本身,只是预先存在的。不过,您可以在 settings.json 中添加自定义设置,并引用 tasks.json 中的内容使用${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"
        },
        // ... other tasks using the variables
    ]
}

关于variables - VScode : Defining own variables in tasks. json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44303316/

相关文章:

c# - CPU 绑定(bind)任务的并行化继续与 IO 绑定(bind)

c++ - vscode-cpptools 错误 : "cl.exe build and debug is only usable when VS Code is run from the Developer Command Prompt for VS."

javascript - 在 jQuery 中获取变量名

c - 在C中将信息存储到变量中的问题

Java 长时间运行的计时器正在被 GC 使用

visual-studio-code - Visual Studio Code - 如何使用 [Alt]+[Left/Right] 逐字跳转

node.js - 可以将 Visual Studio Code 配置为使用 nodemon 启动吗

variables - 信号与变量

java - 布局更改时字符串值不会更改

ruby - 为什么 Rake 不能连续调用多个任务?