VSCode:如何使用JavaScript API执行任意任务?

时间:2017-10-16 10:25:40

标签: visual-studio-code vscode-extensions vscode-tasks

在较新版本的VSCode中,我可以创建对tasks.json中定义的任何任务的绑定。例如,下面有3个任务

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "Clean",
      "type": "shell",
      "command": "clean.cmd",
      "problemMatcher": []
    },
    {
      "label": "Build",
      "type": "shell",
      "command": "build.cmd",
      "problemMatcher": [],
      "group": { "kind": "build", "isDefault": true }
    },
    {
      "label": "Flash",
      "type": "shell",
      "command": "flash.cmd",
      "problemMatcher": []
    }
  ]
}

我可以使用

创建键绑定
[
  {
    "key": "alt+f9",
    "command": "workbench.action.tasks.runTask",
    "args": "Clean"
  },
  {
    "key": "ctrl+f9",
    "command": "workbench.action.tasks.build"
  },
  {
    "key": "f9",
    "command": "workbench.action.tasks.runTask",
    "args": "Flash"
  }
]  

一切都按预期工作。我正在尝试使用VSCode API在扩展中(例如使用Script Commands)执行相同的操作,但它仅适用于buildtest任务。

调用

vscode.commands.executeCommand("workbench.action.tasks.build")

有效但

vscode.commands.executeCommand("workbench.action.tasks.runTask", ["Clean"])

打开任务选择列表。

如何使用JavaScript代码直接启动buildtest之外的其他任务?

1 个答案:

答案 0 :(得分:0)

您的任务配置文件看起来很旧。如果您使用的是新版本的Visual Studio Code,则应升级。

我认为api期望任务有一个“标签”字段。

https://code.visualstudio.com/docs/editor/tasks#_convert-from-010-to-200

所以intsead:

{
   "taskName": "Clean",
   "type": "shell",
   "command": "clean.cmd",
   "problemMatcher": []
},

应该是:

{
  "label": "Clean",
  "type": "shell",
  "command": "clean.cmd",
  "problemMatcher": []
},

更新:尝试仅使用字符串调用它,而不是将其包装在数组中,如下所示:

vscode.commands.executeCommand("workbench.action.tasks.runTask", "Clean")