6

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"
}
8
  • 1
    Does this answer your question? Configuring multiple commands in VS Code tasks (to compile and autoprefix Sass) Commented Jul 28, 2020 at 20:44
  • 2
    My somewhat hacky approach would be to write a script and have VS Code execute that. Commented Jul 28, 2020 at 21:10
  • @soulshined Thanks for your suggestion. It is related, however for my build chain to work i need to execute the commands in sequence. The solution you posted executes them at the same time. Commented Jul 29, 2020 at 6:46
  • 1
    I don’t understand what you mean by sequence then. Would you mind elaborating? The dependsOn property will only run a command after a command it depends on is run, ergo, a sequence...no?. Commented Jul 29, 2020 at 6:51
  • 3
    You need the option: "dependsOrder": "sequence", as well. Right where you have the dependsOn option. Without it the default is parallel. See stackoverflow.com/a/62899764/836330 for example. Commented Jul 29, 2020 at 15:06

1 Answer 1

11

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"
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.