首页 > 解决方案 > Visual Studio 代码中的条件任务

问题描述

我正在尝试创建一个分层的任务结构。由于该项目目前有超过 100 个任务,我们需要通过使用“子任务”或inputs 作为其在 vs 代码中的调用来简化其结构,以获得对我们任务的更多可见性。

考虑这个例子(下面为此提供的代码):

Run Task -> option(s) --> sub-options 
            option    --> sub-options

我理想中想要的是:

Run "myTask" -> option(s) --> sub-options based on previous
                          |
                          |--> sub-options based on previous
                          | 
                          |--> sub-options based on previous

可以说我选择Run Task -> Option1 -> avaliable sub-options for option1

我想有条件地查看“父母”的选项。

一个真实世界的场景:

[Build Customer] Task -> CustomerName  -> Avaliable products for customer
                      -> CustomerName2 -> Avaliable products for customer2

任务.json

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "myTask",
      "type": "shell",
      "command": ".\\AutoBuild.bat",
      "options": {
        "shell": {
          "executable": "powershell.exe"
        },
        "cwd": "${workspaceFolder}",
      },
      "args": [
        "${input:myArg1}",
        "${input:myArg2}"
      ], 
      "group": "build",
      "problemMatcher": []
    }
  ],
  "inputs": [
    {
      "type": "pickString",
      "id":"myArg1",
      "options": [
        "option1",
        "option2",
        "option3",
        "option4"
      ],
      "description": "myArg1",
      "default": ""
    },
    {
      "type": "pickString",
      "id":"myArg2",
      "description": "myArg2",
      "options": [
        "sub-option1",
        "sub-option2",
        "sub-option3",
        "sub-option4"
      ],
      "default": ""
    },
  ]
}

这有可能以某种方式实现吗?

欢迎使用丑陋或概念验证解决方案!

标签: visual-studio-codevscode-tasks

解决方案


不幸的是,像键绑定'“何时”这样的扩展没有条件属性。解决方法是执行一个脚本文件,可以对输入进行操作。例如:

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "myTask",
      "type": "shell",
      "command": ".\\AutoBuild.bat",
      "args": [
        "${input:myArg1}",
        "${input:myArg2}"
      ]
    }
  ]
}

AutoBuild.bat然后将有逻辑来测试和采取行动myArg1myArg2


推荐阅读