几个“构建任务”为Visual Studio代码(蟒蛇)

几个“构建任务”为Visual Studio代码(蟒蛇)

问题描述:

我tasks.json看起来是这样的:几个“构建任务”为Visual Studio代码(蟒蛇)

{ 
    // See https://go.microsoft.com/fwlink/?LinkId=733558 
    // for the documentation about the tasks.json format 
    // A task runner that runs a python program 
    "command": "python3", 
    "presentation": { 
     "echo": true, 
     "reveal": "always", 
     "focus": true 
    }, 
    "args": [ 
     "${file}" 
    ] 
} 

当我运行ctrl+shift+B顶部面板询问“选择构建任务运行”,并有一个替代方案:python3。现在,如果我想添加一个新的构建任务(例如使用scrapy的runspider命令),那么它会添加到构建任务中。我将如何去添加这个?

可以通过分配任务对象的数组的任务属性,像这样在你的tasks.json定义多个任务:

{ 
    // See https://go.microsoft.com/fwlink/?LinkId=733558 
    // for the documentation about the tasks.json format 
    "version": "2.0.0", 
    "tasks": [ 
     { 
      "taskName": "python3", 
      "type": "shell", 
      "command": "python3", 
      "args": [ 
       "${file}" 
      ], 
      "presentation": { 
       "echo": true, 
       "reveal": "always", 
       "focus": true 
      } 
     }, 
     { 
      "taskName": "runspider", 
      "type": "shell", 
      "command": "runspider" 
     } 
    ] 
} 

此外,按Ctrl + + 运行默认建立任务,所以你可能想要设置你的"workbench.action.tasks.runTask"键。

{ 
    "key": "ctrl+shift+b", 
    "command": "workbench.action.tasks.runTask" 
} 

一旦做到这一点,您可以选择任务时使用workbench.action.tasks.runTask命令,如下图所示:

Choose which task to run

您也可以通过设置在"group"属性选择默认的构建任务任务。在下面的代码片段中,您的"python3"任务将作为默认构建任务运行。

... 
"tasks": [ 
    { 
     "taskName": "python3", 
     "type": "shell", 
     "command": "python3", 
     "args": [ 
      "${file}" 
     ], 
     "presentation": { 
      "echo": true, 
      "reveal": "always", 
      "focus": true 
     }, 
     "group": { 
      "kind": "build", 
      "isDefault": true 
     } 
    }, 
    { 
     "taskName": "runspider", 
     "type": "shell", 
     "command": "runspider" 
    } 
] 
... 

你可以阅读更多关于任务的位置:Tasks in VSCode

+0

谢谢!两种选择都起作用,这真的写得很好,很棒! – MrJalapeno

+1

VSCode现在在tasks.json和launch.json中有IntelliSense。 – sauravsahu