7

I'm new to Visual Studio Code and trying to write tasks.json to perform my custom build task so that I can build my project by "Terminal / Run Build Task ...".

However, my build task is composed of multiple commands, like (windows script for example):

preBuildTask1.bat
preBuildTask2.bat
...
build.bat
...
postBuildTask1.bat
postBuildTask2.bat
...

I looks like task.json only allows me to put a single command for "command" property, like:

    "tasks": [
        {
            "label": "emcc",
            "type": "shell",
            "command": "buildTask.ps1",
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },
    ]
}

My current work around is put all sub-tasks in a powershell script then invoke that script in tasks.json, like "buildTask.ps1" showed in the code above. It will be more convenient if I can directly write multiple tasks for a single command property. Anyone knows how to do that? Thanks.

4
  • code.visualstudio.com/docs/editor/tasks#_compound-tasks Commented Mar 15, 2020 at 16:18
  • Thanks for your suggestion, compound task is one of options. It also requires some extra works. I may stay with the "all-in-one" script solution unless there is more direct way. For example, it will be very nice if "command" property accept array literal like "['task1', 'task2', ..., 'taskN']". Commented Mar 15, 2020 at 16:52
  • 2
    Does `"command": "preBuildTask1.bat; preBuildTask2.bat; etc.", not work for you - just separated by semicolons? Commented Mar 16, 2020 at 6:03
  • Have not done this yet and never thought about this way, I hope it will work! Commented Apr 5, 2020 at 19:19

1 Answer 1

6

Thanks for the suggestions from Mark, I find the solution to define a command with multiple sub-tasks in tasks.json of VS code:

Solution 1: Simply put all tasks in the command value, separated by ";":

"command": "task1;task2;task3"

Solution 2: Define sub-task as environment variables, then invoke them one by one. For example, on Windows, we can use powershell's command Invoke-Expression:

First, define all sub-tasks as environment variables:

"options": {
        "env": {
                "TASK_1": "emsdk_env",
                "TASK_2": "emcc canvas.cpp -o canvas.html --shell-file canvas_shell.html -s NO_EXIT_RUNTIME=1 -s EXTRA_EXPORTED_RUNTIME_METHODS='[\"ccall\", \"cwrap\"]'",
                "TASK_3": "http-server -p 8000"
            }
    },    

Then, invoke them:

  "tasks": [
        {
            "label": "emcc",
            "type": "shell",
            "command": "Invoke-Expression $env:TASK_1; Invoke-Expression $env:TASK_2; Invoke-Expression $env:TASK_3",
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "problemMatcher": []
        }
    ]

The second solution is useful when sub-task command lines are very long and do not want write a super long string when defining command.

Of course, we can also create a single script then invoke it, which is my original solution.

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.