使用“preLaunchTasks"并在 Visual Studio Code 中命名任务 [英] Using "preLaunchTasks" and Naming a Task in Visual Studio Code

查看:105
本文介绍了使用“preLaunchTasks"并在 Visual Studio Code 中命名任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据文档,可以在调试前启动程序:

According to the documentation, it is possible to launch a program before debugging:

要在每个调试会话开始之前启动任务,请将 preLaunchTask 设置为 tasks.json 中指定的任务之一的名称.

To launch a task before the start of each debug session, set the preLaunchTask to the name of one of the tasks specified in tasks.json.

我还没有看到命名"任务的示例语法,但是模式文档 显示了一个名为 taskName 的属性.我尝试使用它来将我的 launch.json preLaunchTasks 链接到任务,但它不起作用.当我启动我的程序时,Visual Studio Code 报告了这个错误:

I've not seen example syntax of a "named" task, but the schema documentation reveals a property called taskName. I tried using that to link my launch.json preLaunchTasks to the task, but it didn't work. When I launched my program, Visual Studio Code reported this error:

找不到独特的任务启动核心".确保该任务存在且具有唯一名称.

Could not find a unique task 'launch-core'. Make sure the task exists and that it has a unique name.

我的自定义命名"任务如下所示:

My custom "named" task looked something like this:

{
    "taskName": "launch-core",
    "version": "0.1.0",
    "command": "C:\\utils\\mystuff.exe",
    // The command is a shell script
    "isShellCommand": true,
    // Show the output window only if unrecognized errors occur.
    "showOutput": "silent",
}

然后我尝试将属性名称从 taskName 更改为 name基于此链接.这也不起作用.

I then tried changing the property name from taskName to just name, based on this link. That also didn't work.

Intellisense 没有给出如何命名任务的建议.

Intellisense gives no suggestions of how to name a task.

有人知道如何在 tasks.json 文件中唯一地命名任务吗?语法是什么?物业名称是什么?

Does anybody know how to uniquely name a task in the tasks.json file? What is the syntax? What is the property name?

最终我想在我自己的 node.js 应用程序启动之前执行两个或三个 node.js 进程.例如,我希望在我的应用程序启动到调试器之前启动以下三个应用程序:

Ultimately I'd like to execute two or three node.js processes before my own node.js app is launched. For example, I'd like to have the following three apps launched before my app is launched into the debugger:

sh -c 'cd ./manager/ && node manager.js'
sh -c 'cd ./adapter/ && node adapter.js'
sh -c 'cd ./core/ && node core.js'

如果我在 Windows 机器上工作,我的任务可能如下所示:

If I'm working on a Windows box, my task might look something like this:

{
    "taskName": "core-launch",
    "version": "0.1.0",

    // The command is tsc. Assumes that tsc has been installed using npm install -g typescript
    "command": "start",

    // The command is a shell script
    "isShellCommand": true,

    // Show the output window only if unrecognized errors occur.
    "showOutput": "silent",

    // args is the HelloWorld program to compile.
    "args": [
        "ACD-Manager",
        "/B",
        "/D",
        "./manager/",
        "node",
        "manager.js"
        ]
}

上述任务使用cmd 开始能力.我还不确定如何启动多个节点任务而不是一个,但由于此任务命名问题,我什至无法启动一项任务.

The above task using using the cmd start capability. I'm not sure yet how to make several node tasks launch instead of one, but I can't even get one task to launch because of this task-naming issue.

如何在 tasks.json 文件中命名任务?

How do I name a task in the tasks.json file?

推荐答案

所以,如果它仍然相关,或者如果有人发现这个线程有同样的问题,我刚刚弄清楚它是如何工作的:

So, if it's still relevant, or if someone finds this thread with the same problem, I've just figured it out how it works:

tasks.json 中,您需要创建一个 'tasks' 数组 - 代码提示将帮助您实现这一点 - 它包含一个对象数组.在对象内部,您可以拥有 'taskName' 键值对.

In the tasks.json, you need to create a 'tasks' array - code hint will help you with that - which holds an array of objects. Inside an object, you can have the 'taskName' key-value pair.

示例:

{
    "version": "0.1.0",
    "command": "npm",
    "isShellCommand": true,
    "args": ["run-script", "webpack"],
    "showOutput": "always",
    "tasks": [
        { 
            "taskName": "runwebpack",
            "suppressTaskName": true
        }
    ]
}

就我而言,我必须在运行我的项目之前运行 npm run-script webpack 命令.在 launch.json 文件中,"preLaunchTask": "runwebpack" 现在可以工作了.

In my case, I had to run the npm run-script webpack command before running my project. In the launch.json file, the "preLaunchTask": "runwebpack" will work now.

注意:suppressTaskName 在我的例子中是正确的.省略它或将其设置为 false 将导致 VS Code 在命令后附加 taskName.

Note: the suppressTaskName is true in my example. Omitting it, or setting it to false will result in VS Code appending the taskName after the command.

更通用的方法是这样的:

A more general approach would be something like this:

{
    "version": "0.1.0",
    "command": "npm",
    "isShellCommand": true,
    "args": ["run-script"],
    "showOutput": "always",
    "tasks": [
        { "taskName": "webpack" }
    ]
}

对于后一个示例,您可以使用其他要运行的脚本扩展 tasks 数组.

With the latter example, you can extend the tasks array with other scripts to be run also.

我的用法提示:npm run-script 从 package.json 文件的 scripts 对象中获取要执行的操作.

Hint for my usage: npm run-script fetches what to do from the package.json file's scripts object.

这适用于 VS Code 1.3.1

这篇关于使用“preLaunchTasks"并在 Visual Studio Code 中命名任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆