23

我一直在研究Visual Studio Code的文档,以弄清楚如何将多个连续任务添加到tasks.json文件中。

tasks数组仅允许为同一命令创建不同的参数。在此示例中,命令是echo.

{
    "version": "0.1.0",
    "command": "echo",
    "isShellCommand": true,
    "args": [],
    "showOutput": "always",
    "echoCommand": true,
    "suppressTaskName": true,
    "tasks": [
        {
            "taskName": "hello",
            "args": ["Hello World"]
        },
        {
            "taskName": "bye",
            "args": ["Good Bye"]
        }
    ]
}

tasks.json 是否允许连续执行多个任务?例如,tsc后跟uglify?

4

2 回答 2

28

dependsOn功能在1.10.0 版本中发布。例如,我使用它在 TypeScript 中编译和运行单个文件脚本:

{
    "version": "2.0.0",
    "tasks": [
        {
            "command": "tsc -p ${cwd}/2017-play",
            "label": "tsc-compile",
            "type": "shell"
        },
        {
            "command": "node ${cwd}/2017-play/build/${fileBasenameNoExtension}.js",
            "label": "node-exec",
            "type": "shell",
            "dependsOn": [
                "tsc-compile"
            ],
            "problemMatcher": []
        }
    ]
}
于 2017-09-04T21:21:53.140 回答
4

这是一个运行 tcs 构建并使用 shell 脚本将源代码复制到另一个文件夹的工作示例。这是基于 StackOverflow 上的各种帖子和此处找到的文档:

https://code.visualstudio.com/updates/v1_10#_more-work-on-terminal-runner

也可以创建一个包含两个任务的 tasks.json,其中第二个任务依赖于第一个任务,如 Ben Creasy 帖子所示,这两个任务将在第二个任务被调用时执行。我需要能够执行一个,另一个或两者。非常感谢 Ben,在发布这篇文章之前,我很难找到解决方案。

顺便说一句,当包含 shell 文件时,命令是参考项目文件夹运行的,而不是脚本所在的文件夹。

{
 // See https://go.microsoft.com/fwlink/?LinkId=733558
 // for the documentation about the tasks.json format
 "version": "2.0.0",
 "tasks": [
  {
   "type": "typescript",
   "tsconfig": "tsconfig.json",
   "problemMatcher": [
    "$tsc"
   ],
   "group": "build",
   "identifier": "build"
  },
  {
   "label": "Copy files",
   "type": "shell",
   "command": "./scripts/copysrc.sh",
   "windows": {
    "command": ".\\scripts\\copysrc.cmd"
   },
   "group": "build",
   "presentation": {
    "reveal": "always"
   },
   "problemMatcher": [],
   "dependsOn": "build"
  },
  {
   "label": "Build and copy",
   "dependsOn": [
    "build",
    "Copy files"
   ],
   "group": "build",
   "problemMatcher": []
  }
 ]
}
于 2018-05-08T20:42:25.897 回答