【问题标题】:How to chain tasks in Visual Studio Code using only tasks.json?如何仅使用 tasks.json 在 Visual Studio Code 中链接任务?
【发布时间】:2017-09-25 06:03:07
【问题描述】:

我一直在研究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?

【问题讨论】:

  • 在最新版本的 VS Code 中,我根本不再使用 tasks.json。您可以将您的命令放在package.json 中的scripts 标记下。如果您只需要两个或三个连续的命令,您可以使用prepost 标签。如果您的构建过程变得更加复杂,您可以使用 gulp 或 webpack。

标签: visual-studio-code vscode-tasks


【解决方案1】:

dependsOn 功能在 version 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": []
        }
    ]
}

【讨论】:

  • 这是一个巨大的进步!但我仍然认为 MS 的文档对于如何使用 tasks.json 非常不清楚。至此,我已经放弃,只使用npm scriptswebpack
【解决方案2】:

这是一个运行 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": []
  }
 ]
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-13
    • 2022-01-19
    • 2020-03-25
    • 2018-09-10
    • 2018-09-09
    • 1970-01-01
    • 2016-11-19
    • 1970-01-01
    相关资源
    最近更新 更多