3

我的 tasks.json 看起来像这样:

{
    // 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. 现在如果我想添加一个新的构建任务(例如一个runspider带有scrapy的命令),那么它就会被添加到构建任务中。我将如何添加这个?

4

1 回答 1

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++Shift运行B默认的构建任务,因此您可能需要设置"workbench.action.tasks.runTask"键绑定。

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

完成后,您可以在使用workbench.action.tasks.runTask命令时选择任务,如下所示:

选择要运行的任务

您还可以通过设置任务的"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"
    }
]
...

您可以在此处阅读有关任务的更多信息:VSCode 中的任务

于 2017-08-13T10:41:18.927 回答