2017-08-12 85 views
3

我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命令),那麼它會添加到構建任務中。我將如何去添加這個?

回答

2

可以通過分配任務對象的數組的任務屬性,像這樣在你的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