2

I have a build script that supports ~40 different commands. I rather not write out 40 task even if its a copy/paste with an argument or two changed. Is there some way I have can one or two task and pass the arguments in from launch.json? or have a template that all my task use and I can change the args there?

rioV8
  • 24,506
  • 3
  • 32
  • 49
Eric Stotch
  • 141
  • 4
  • 19

1 Answers1

2

You can use the extension Command Variable

It has a few commands that can remember what you have picked from a list and use that information in launch.json and tasks.json.

You can choose to store the argument variations of the build script in launch.json and use extension.commandvariable.pickStringRemember to convert the choice to a number of stored variables that can be used in launch.json and tasks.json with the command extension.commandvariable.remember.

You can also store all the configuration options in a json file:

{
  "build": {
    "foo": {
      "arg1": 10,
      "arg2": "-v"
    },
    "bar": {
      "arg1": 50,
      "arg2": "-b"
    }
  }
}

In launch.json use extension.commandvariable.pickStringRemember to pick a build variant (foo or bar) and use this picked string in the json property of command extension.commandvariable.file.content in launch.json and tasks.json.

I will add the possibility that key and json property can contain variables so you can use the remember variable (${remember:arg1}) in v1.26 of the extension.

launch.json

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "node",
      "request": "launch",
      "name": "Launch Program",
      "program": "${workspaceFolder}/app.js${input:pickBuild}",
      "preLaunchTask": "Build"
    }
  ],
  "inputs": [
    {
      "id": "pickBuild",
      "type": "command",
      "command": "extension.commandvariable.pickStringRemember",
      "args": {
        "key": "empty",
        "options": [
          ["foo", {"buildvariant":"foo"}],
          ["bar", {"buildvariant":"bar"}],
        "description": "Pick a build variant"
      }
    }
  ]
}

This trick returns an empty string but stores the buildvariant to be used in tasks.json. If you have to configure the launch based on the build you can return a string with content. I will add the key empty to the default remember store in v1.26.

tasks.json

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "Build",
      "type": "shell",
      "command": "python",
      "args": [ "build.py", "${input:buildArg1}", "${input:buildArg2}" ],
    },
  ],
  "inputs": [
    {
      "id": "buildArg1",
      "type": "command",
      "command": "extension.commandvariable.file.content",
      "args": {
        "fileName": "${workspaceFolder}/build-options.json",
        "json": "content.${remember:buildvariant}.arg1",
        "default": "--dummy",
      }
    },
    {
      "id": "buildArg2",
      "type": "command",
      "command": "extension.commandvariable.file.content",
      "args": {
        "fileName": "${workspaceFolder}/build-options.json",
        "json": "content.${remember:buildvariant}.arg2",
        "default": "--dummy",
      }
    }
  ]
}

If you use the extension.commandvariable.file.pickFile in launch.json you can use this file path in the command extension.commandvariable.file.content

Edit

In v1.26 I have added the possibility to add key-value pairs to the remember storage and return an empty string. This makes it possible for a launch configuration to set key-values to be used in a task. Just use ${input:name} anywhere in a launch property string that supports variables.

rioV8
  • 24,506
  • 3
  • 32
  • 49