Search code examples
jsonshellvisual-studio-codecmakevscode-tasks

Configure Visual Studio Code task to execute multiple shell commands in sequence


Is there a possibility, to add multiple shell commands in sequence to a Visual Studio Code task with separate arguments and options? I managed to execute multiple commands using && to chain then together to a single command, as you could in any Linux shell. But i guess, there has to be a better way to do this.

I use Visual Studio Code on Ubuntu 18.04 LTS.

Here is the example of how i currently chained the commands for a build task in a task.json file to build a c++ project using cmake:

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

Update: -------------------------------------------------------------------

I tried to use the dependsOn: property to start the 4 separatly defined tasks. However, this resulted in all 4 commands being executed at the same time in different shell instances instead of in sequence as needed:

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

Solution

  • Thanks to the many comments i found a solution which works well by setting "dependsOrder" to "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"
    }