配置Visual Studio Code任务以依次执行多个Shell命令

时间:2020-07-28 20:11:55

标签: json shell visual-studio-code cmake vscode-tasks

是否有可能向具有单独参数和选项的Visual Studio Code任务依次添加多个Shell命令?我设法使用&&来执行多个命令,然后将它们链接到一个命令,就像在任何Linux shell中一样。但是我想,必须有一种更好的方法来做到这一点。

我在Ubuntu 18.04 LTS上使用Visual Studio Code。

以下是我当前如何在task.json文件中链接构建任务命令以使用cmake构建c ++项目的示例:

{
    "tasks": [
        {
            "type": "shell",
            "label": "build",
            "command": "cd ${workspaceFolder}/build && cmake .. && make clean && make",
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ],
    "version": "2.0.0"
}

更新:------------------------------------------ -------------------------

我尝试使用dependsOn:属性来启动4个单独定义的任务。但是,这导致所有4条命令在不同的Shell实例中同时执行,而不是按需要依次执行:

{
    "tasks": [
        {
            "type": "shell",
            "label": "build project",
            "dependsOn": [
                "Go to build folder",
                "Cmake",
                "make clean",
                "make",
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },
        {
            "label": "Go to build folder",
            "type": "shell",
            "command": "cd ${workspaceFolder}/build",
            "presentation": {
                "group": "cmake-complile"
            }
        },
        {
            "label": "Cmake",
            "type": "shell",
            "command": "cmake ..",
            "presentation": {
                "group": "cmake-complile"
            }
        },
        {
            "label": "make clean",
            "type": "shell",
            "command": "make clean",
            "presentation": {
                "group": "cmake-complile"
            }
        },
        {
            "label": "make",
            "type": "shell",
            "command": "make",
            "presentation": {
                "group": "cmake-complile"
            }
        }
    ],
    "version": "2.0.0"
}

1 个答案:

答案 0 :(得分:0)

由于有很多评论,我找到了一种解决方案,该解决方案通过将“ dependsOrder”设置为“ sequence”可以很好地发挥作用:

{
    "tasks": [
        {
            "type": "shell",
            "label": "build project",
            "dependsOrder": "sequence",
            "dependsOn": [
                "Cmake",
                "make clean",
                "make",
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },
        {
            "label": "Cmake",
            "type": "shell",
            "command": "cmake ..",
            "options": {
                "cwd": "${workspaceFolder}/build",
            },
        },
        {
            "label": "make clean",
            "type": "shell",
            "command": "make clean",
            "options": {
                "cwd": "${workspaceFolder}/build",
            },
        },
        {
            "label": "make",
            "type": "shell",
            "command": "make",
            "options": {
                "cwd": "${workspaceFolder}/build",
            },
        }
    ],
    "version": "2.0.0"
}