如何通过Visual Studio代码任务设置变量

在Visual Studio.net中,您有一个弹出菜单,可让您选择当前的构建配置,然后在脚本(makefile)中,可以使用$(Configuration)选择诸如构建g ++ -g与g ++ -O之类的内容。

我想在Visual Studio代码中也做类似的事情,但已经接近了一些,但没有雪茄。

我认为我的第一步是找到一种设置和读取配置变量的好方法。我发现我可以编写读写文件的任务(有效地产生所需的功能),但这有点慢。看起来使用预定义变量$ {config:xxxx}应该可以大致执行我想要的操作,但前提是可以设置它们(但我还没有发现这样做的能力)。

所以-我的问题是-从Visual Studio代码任务开始-如何在任务上下文中“设置”变量?

这是我尝试过的一个示例:

{
"version": "2.0.0",
"tasks": [
    {
        "isBackground": true,
        "label": "make CURRENT-CONFIGURATION",
        "type": "shell",
        "command": "make",
        "args": [
            "CONFIGURATION=${input:CURRENT-CONFIGURATION}",
            "${input:make-targets}",
            "-j4"
        ],
        "group": {
            "kind": "build",
            "isDefault": true
        },
        "presentation": {
            "clear": true,
            "panel": "dedicated"
        },
        "problemMatcher": [
            "$msCompile"
        ]
    },
    {
        "label": "make rebuild CURRENT-CONFIGURATION",
        "type": "shell",
        "command": "make CONFIGURATION=${input:CURRENT-CONFIGURATION} --directory ${input:DIRECTORY} clean && make CONFIGURATION=${input:CURRENT-CONFIGURATION} --directory ${input:DIRECTORY}  all -j4",
        "problemMatcher": [
            "$msCompile"
        ],
        "group": {
            "kind": "build",
            "isDefault": true
        },
    },
    {
        "isBackground": true,
        "label": "make CONFIGURATION",
        "type": "shell",
        "command": "make",
        "args": [
            "CONFIGURATION=${input:CONFIGURATION}",
            "--directory", "${input:DIRECTORY}",
            "${input:make-targets}",
            "-j4"
        ],
        "group": {
            "kind": "build",
            "isDefault": true
        },
        "presentation": {
            "clear": true,
            "panel": "dedicated"
        },
        "problemMatcher": [
            "$msCompile"
        ]
    },
    {
        "isBackground": true,
        "label": "Set CURRENT-CONFIGURATION",
        "type": "shell",
        "command": "echo ${input:CONFIGURATION} > ${workspaceFolder}/CURRENT-CONFIGURATION"
    },
    {
        "label": "make rebuild CONFIGURATION",
        "type": "shell",
        "command": "make CONFIGURATION=${input:CONFIGURATION} --no-print-directory --directory ${input:DIRECTORY} clean && make CONFIGURATION=${input:CONFIGURATION} --no-print-directory --directory ${input:DIRECTORY}  all -j4",
        "problemMatcher": [
            "$msCompile"
        ],
        "group": {
            "kind": "build",
            "isDefault": true
        },
        "presentation": {
            "clear": true,
            "panel": "dedicated"
        }
    },
    {
        "isBackground": true,
        "label": "make clean",
        "type": "shell",
        "command": "make",
        "args": [
            "CONFIGURATION=${input:CONFIGURATION}",
            "--directory", "${input:DIRECTORY}",
            "clean"
        ],
        "group": {
            "kind": "build",
            "isDefault": true
        },
        "presentation": {
            "clear": true,
            "panel": "dedicated"
        }
    },
    {
        "isBackground": true,
        "label": "make clobber",
        "type": "shell",
        "command": "make",
        "args": [
            "CONFIGURATION=${input:CONFIGURATION}",
            "--directory", "${input:DIRECTORY}",
            "clobber"
        ],
        "group": {
            "kind": "build",
            "isDefault": true
        },
        "presentation": {
            "clear": true,
            "panel": "dedicated"
        }
    }
],
"inputs": [
    {
        "id": "CONFIGURATION",
        "description": "configuration name?",
        "default": "Debug-U-32",
        "type": "pickString",
        "options": [
            "",
            "Debug-U-32",
            "Debug-U-64",
            "Release-U-32",
            "Release-U-64",
        ]
    },
    {
        "id": "DIRECTORY",
        "description": "directory?",
        "default": ".",
        "type": "pickString",
        "options": [
            "",
            ".",
            "libraries",
            "samples",
            "tests",
        ]
    },
    {
        "id": "make-targets",
        "description": "typically all, clean, clobber, libraries, tests, run-tests, etc.",
        "default": "all",
        "type": "promptString"
    },
    {
        "id": "CURRENT-CONFIGURATION",
        "type": "command",
        "command" : "cat ${workspaceFolder}/CURRENT-CONFIGURATION"
    }
]

}