Search code examples
visual-studio-codevscode-tasks

Is it possible to group tasks and specify global options for the group in VS Code?


I am doing development that gets pushed/built to multiple systems. To push/build to each system, I am using the same command but have to change an environment variable.

I am using tasks.json to push to multiple systems with one task and I am using task options to set the environment variable.

As you can see it, it is kinda messy.

{
    "version": "2.0.0",

    // global options
    "options": {
        "env": {
            "SOME_ENV_VAR": "~/globalFile"
        }
    },
    // global options for windows
    "windows": {
        "options": {
            "env": {
                "SOME_ENV_VAR": "${env:userprofile}\\globalFile",
            }
        }
    },
    "tasks": [
        {
            "label": "push to system A",
            "type": "shell",
            "command": "pushCommand"
        },
        {
            "label": "push to system B",
            "type": "shell",
            "command": "pushCommand",
            // special options for pushing to sytem B
            "options": {
                "env": {
                    "SOME_ENV_VAR": "~/systemBFile"
                }
            },
            // special windows options for pushing to system B
            "windows": {
                "options": {
                    "env": {
                        "SOME_ENV_VAR": "${env:userprofile}\\systemBFile",
                    }
                }
            }
        },
        // main task to run both 
        {
            "label": "push",
            "type": "shell",
            "dependsOrder": "parallel",
            "dependsOn": [
                "push to system A",
                "push to system B"
            ]
        },

        {
            "label": "build to system A",
            "type": "shell",
            "command": "buildCommand"
        },
        {
            "label": "build to system B",
            "type": "shell",
            "command": "buildCommand",
            // special options for pushing to sytem B
            "options": {
                "env": {
                    "SOME_ENV_VAR": "~/systemBFile"
                }
            },
            // special windows options for pushing to system B
            "windows": {
                "options": {
                    "env": {
                        "SOME_ENV_VAR": "${env:userprofile}\\systemBFile",
                    }
                }
            }
        },
        // main task to run both 
        {
            "label": "build",
            "type": "shell",
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "dependsOrder": "parallel",
            "dependsOn": [
                "build to system A",
                "build to system B"
            ]
        }
    ]
}

So I am wondering if I can create different groups, one for each system: system A, system B, system C, etc... and then assign options and windows to those groups to make it easier to manage. Something like this:

{
    "label": "build to system A",
    "type": "shell",
    "command": "buildCommand",
    "group": "system A"
},
{
    "label": "build to system B",
    "type": "shell",
    "command": "buildCommand",
    "group": "system B"
}

Solution

  • I am unsure about the shared variables, but it is possible to start multiple tasks and have them grouped

    On each task if you specify:

    "presentation": {
      "group": "local-dev", //Can be any name
      "panel": "shared",  //"shared" means they will appear in a "split" terminal window
    },
    

    Here it all is in context:

    {
      "version": "2.0.0",
      "tasks": [
       {
          "label": "Local Dev",
          "dependsOn": ["Angular UI", "NestJS Backend"],
          "dependsOrder": "parallel",
          "group": "build",
        },
        {
          "label": "NestJS Backend",
          "type": "npm",
          "script": "start-backend",
          "isBackground": true,
          "group": "build",
          "icon": { "color": "terminal.ansiRed", "id": "server" },
          "presentation": {
            "group": "local-dev",
            "panel": "shared",
          },
        },
        {
          "label": "Angular UI",
          "type": "npm",
          "script": "start",
          "isBackground": true,
          "group": "build",
          "icon": { "color": "terminal.ansiMagenta", "id": "browser" },
          "presentation": {
            "group": "local-dev",
            "panel": "shared",
          },
        }
      ]
    }
    

    So when I run the Local Dev task it starts up both of the sub tasks in a side-by-side split terminal window. Notice that each one also has specified colored icons as well!

    Here's how it appears: enter image description here

    Any new terminal windows you create will be separate and outside of the group

    enter image description here