【问题标题】:How to launch specific task from input variable in VS Code?如何从 VS Code 中的输入变量启动特定任务?
【发布时间】:2019-07-11 16:18:44
【问题描述】:

我正在尝试创建一个启动配置,其中环境变量由 shell 脚本动态确定。尽管command variable 可以通过workbench.action.tasks.runTask 启动任务,但似乎无法指定要运行的任务。 Input variables 在这方面似乎更灵活一些,但我似乎无法让它发挥作用。这是我得到的:

launch.json:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch",
            "type": "go",
            "request": "launch",
            "mode": "auto",
            "program": "${workspaceFolder}/main.go",
            "env": {
                "XXX": "${input:foo}"
            },
            "args": []
        }
    ],
    "inputs": [
        {
            "type": "command",
            "id": "foo",
            "command": "workbench.action.tasks.runTask",
            "args": {
                "args": "bar",
            }
        }
    ]
}

tasks.json:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "bar",
            "type": "shell",
            "command": "find /dev -name 'myspecialdevice*' -maxdepth 1"
        }
    ]
}

问题是用户仍然被询问要运行哪个任务。我对launch.json 的inputs.args 部分最不安全。我真的不知道关键值应该是什么。也许implementation 有助于解决这个问题?

【问题讨论】:

  • command 输入类型工作也难倒我。来自code.visualstudio.com/docs/editor/…pickString 示例就像一个魅力,但没有command 的示例,并且到目前为止所有逻辑尝试都失败了......这是否有效?在 Ubuntu 18.04 上使用 1.39.2 Visual Studio Code。

标签: visual-studio-code


【解决方案1】:

这个答案实际上与使用 vscode 任务无关,但你的介绍句子提供了动机/打算解决的问题。

我遇到了同样的问题,想知道vscode's input type:command。它提供了一种嵌入(自定义)vscode 命令的方法——这看起来像是在此处嵌入(自定义)扩展的强大机制。但是我没有找到一个简单地执行shell脚本并返回stdout的内置命令。因此,恕我直言,需要一个扩展来捕获 shell 命令的输出。

例如https://marketplace.visualstudio.com/items?itemName=augustocdias.tasks-shell-input 提供了一个命令shellCommand.execute 正是这样做的。

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch",
            "type": "go",
            "request": "launch",
            "mode": "auto",
            "program": "${workspaceFolder}/main.go",
            "env": {
                "XXX": "${input:foo}"
            },
            "args": []
        }
    ],
    "inputs": [
        {
            "id": "foo",
            "type": "command",
            "command": "shellCommand.execute",
            "args": {
                "command": "find /dev -name 'myspecialdevice*' -maxdepth 1",
                "cwd": "${workspaceFolder}",
                /* To prevent user from selecting output (if there is just
                   one line printed by command), un-comment next line */
                //"useSingleResult": true
            }
        }
    ]
}

(灵感来自https://stackoverflow.com/a/58930746/1903441

【讨论】:

    【解决方案2】:

    似乎在 vscode 中,您无法将返回值甚至环境变量从 task.json 传递给 launch.json。但是您可以使用文件作为中间体。

    例如在task.json:

    {
        "version": "2.0.0",
        "tasks": [
            {
                "label": "bar",
                "type": "shell",
                "command": "find /dev -name 'myspecialdevice*' -maxdepth 1 > ${workspaceFolder}/.vscode/temp"
            }
        ]
    }
    

    launch.json 中,您将bar 设置为preLaunchTask,然后使用inputs 访问该文件,如下所示:

    {
        "version": "0.2.0",
        "configurations": [
            {
                "name": "Launch",
                "type": "go",
                "request": "launch",
                "mode": "auto",
                "program": "${workspaceFolder}/main.go",
                "env": {
                    "XXX": "${input:foo}"
                },
                "args": [],
                "preLaunchTask": "bar",
            }
        ],
        "inputs": [
            {
                "id": "foo",
                "type": "command",
                "command": "extension.commandvariable.file.content",
                "args": {
                    "fileName": "${workspaceFolder}/.vscode/temp",
                }
            }
        ]
    }
    

    要让评论生效,只需安装此扩展程序:https://marketplace.visualstudio.com/items?itemName=rioj7.command-variable

    【讨论】:

    • 这很好用。尽管注意疯狂的fileName 大写。这让我很困惑,而且它没有很好的错误检查,所以你得到的唯一错误是ENOENT
    【解决方案3】:

    在你的launch.json,尝试替换

    "args": {
        "args": "bar",
    }
    

    "args": "bar"
    

    【讨论】:

    • 我认为 args 的值应该是一个数组,因为你可以有多个参数。但在这种情况下,如果它只是一个参数,那么省略 [] 应该仍然有效
    • 适用于我的字符串,不适用于我的列表。还有其他命令可以处理列表,但不是这个。您可以通过 github.com/microsoft/vscode/issues 请求更改 - 我同意一致性会很好。
    猜你喜欢
    • 2019-06-03
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 2021-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多