有没有一种方法可以在 VS Code 任务中动态填充 pickString?

20

我想提供一个字符串列表作为任务的pickString。这个字符串列表将是文件夹名称的列表,它可以通过PowerShell获取,但我不知道如何在任务中显示这个列表。

我该如何设置我的任务和输入,以便可以填充这个列表?

{
  "version": "2.0.0", 
  "tasks": [
    {
      "label": "Test Task", 
      "type":  "shell", 
      "windows": {
        "command":  "echo",
          "args": [
            "-opt",
            "${input:optionsList}"
          ]
      }
    }
  ], 
  "inputs": [
    "id": "optionsList", 
    "type": "pickString", 
    "options": [<insert something here>]
  ]
}

我希望用户在任务运行时能够看到文件夹列表。


1
你找到答案了吗? - Darshan L
在VSCode中已经提出了这个功能的增强请求。https://github.com/microsoft/vscode/issues/109789请通过上面的链接为此功能投票。如果有超过20个投票,那么这个功能将会在VSCode中得以实现。 - Darshan L
4个回答

7

问题

实际上,VSCode任务本身没有这个功能。增强的请求被关闭,可能是因为在不久的将来没有计划开发它。

替代解决方案

您可以安装augustocdias.tasks-shell-input并配置一个输入变量,类型为command,以调用此扩展程序以填充选择列表。

按照以下步骤进行操作:

  1. 使用以下命令安装扩展:
    1. 在“扩展”侧栏(Ctrl+Shift+X)中搜索 augustocdias.tasks-shell-input 并安装 或者
    2. 在 VS Code 快速打开(Ctrl+P)中粘贴以下命令,然后按 Enter 键:
    • ext install augustocdias.tasks-shell-input
  2. 使用以下配置的 task.json
    1. 具有要执行的命令的任务
    2. args配置条目添加到任务中,其中包含一个或多个对输入变量的引用,例如${input:my_variable}
    3. inputs部分中配置一个条目,其中包含:
      • id: 使用与任务 args 中定义的变量名称相同的变量名称,例如 my_variable 对应 ${input:my_variable}
      • type: command
      • command: shellCommand.execute
      • args: 添加一个具有属性:commandcwdenv 的配置项
  3. 查看下面的示例 task.json 文件。

示例

使用 augustocdias.tasks-shell-input 扩展程序的示例 task.json 文件:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Dynamically Populated Task",
            "type": "shell",
            "command": "echo",
            "args": [
                "'${input:my_dynamic_input_variable}'"
            ],
            "problemMatcher": []
        }
    ],
    "inputs": [
        {
            "id": "my_dynamic_input_variable",
            "type": "command",
            "command": "shellCommand.execute",
            "args": {
                "command": "ls -1 *.*",
                "cwd": "${workspaceFolder}",
                "env": {
                    "WORKSPACE": "${workspaceFolder[0]}",
                    "FILE": "${file}",
                    "PROJECT": "${workspaceFolderBasename}"
                }
            },
        }
    ]
}


6
任务/启动输入变量 提供了一种使用命令输入变量实现此操作的方法。该命令可以来自于VSCode内置命令或扩展程序。在您的情况下,没有内置命令返回给定工作区中文件夹列表。但是,在扩展程序中很容易实现它。文档没有给出如何实现这一点的完整示例,所以我将在这里展示一个。首先演示它工作的演示如下:Using an extension command to populate a pickString variable。以下是完整的tasks.json文件:

    {
      "version": "2.0.0",
      "tasks": [
    
        {
          "label": "List Folder Choice files",
          "type": "shell",
          "command": "ls",            // your command here
          "args": [
            "${input:pickTestDemo}"  // wait for the input by "id" below
          ],
          "problemMatcher": []
        }
      ],
      
      "inputs": [
        {
          "id": "pickTestDemo",
          "type": "command",
          "command": "folder-operations.getFoldersInWorkspace"  // returns a QuickPick element
        },
      ]
    } 

你可以在输入变量中看到一个命令是从“folder-operations”扩展中调用的。由于该命令返回一个“QuickPick”元素,所以你运行任务时会看到它。以下是扩展的核心代码:
    const vscode = require('vscode');
    const fs = require('fs');
    const path = require('path');
    
    /**
     * @param {vscode.ExtensionContext} context
     */
    function activate(context) {
    
        let disposable = vscode.commands.registerCommand('folder-operations.getFoldersInWorkspace', async function () {
    
        // get the workspaceFolder of the current file, check if multiple workspaceFolders
        // a file must be opened
        const wsFolders = await vscode.workspace.workspaceFolders;
        if (!wsFolders)  vscode.window.showErrorMessage('There is no workspacefolder open.')
        const currentWorkSpace = await vscode.workspace.getWorkspaceFolder(vscode.window.activeTextEditor.document.uri);
    
        // filter out files, keep folder names.  Returns an array of string.
        // no attempt made here to handle symbolic links for example - look at lstatSync if necessary
    
        const allFilesFolders = fs.readdirSync(currentWorkSpace.uri.fsPath);
        const onlyFolders = allFilesFolders.filter(f => fs.statSync(path.join(currentWorkSpace.uri.fsPath, f)).isDirectory());
    
        // showQuickPick() takes an array of strings
        return vscode.window.showQuickPick(onlyFolders);
        });
    
        context.subscriptions.push(disposable);
    }
    
    exports.activate = activate;
    
    // this method is called when your extension is deactivated
    function deactivate() {}
    
    module.exports = {
        activate,
        deactivate
    }

这里有一个链接,链接到了 folder-operations 演示扩展程序,您可以查看完整的扩展程序代码和其 package.json 文件。一旦您设置好扩展程序发布凭据,发布更多扩展程序就非常容易了。


但是,当您在控制台中添加新终端时,它会给您提供完全相同的列表;那么命令在哪里呢? - Ate Somebits

0

除非安装扩展或编写自己的扩展,否则您应该首先确定您的工作区文件夹结构

您可以有

  1. 单根工作区(workspace=folder)
  2. 带主文件夹的多根工作区(workspace=folder)
  3. 没有主文件夹的多根工作区(workspace=folder/../)

有内置变量可让您访问,例如

  • 文件夹路径(如果为 1、2、3 则为 workspace=folder)${workspaceFolder}
  • 当前文件路径 相对于 ${workspaceFolder} ${relativeFileDirname}

假设您想要为当前打开的文件的 repo 运行 git gui - 在 tasks.json 配置中使用 "cwd" : "${relativeFileDirname}",git 应该会选择当前 repo,并且您无需请求文件夹路径。


0
我遇到了一个类似的问题,是关于自定义任务的。我想利用@Mark提出的扩展点子,并创建一个更通用的扩展。Populate-quickpick可以根据文本文件中提供的列表创建选项。我将其用作Python脚本的简单输入,脚本会将所选择的选项作为参数接收。
以下是如何使用它:
    "tasks": {
        "version": "2.0.0",
        "tasks": [
            {
                "label": "My Task",
                "type": "shell",
                "command": "echo", // your command here
                "args": [
                    "${input:pickDemo}"
                ],
                "problemMatcher": []
            }
        ],
        "inputs": [
            {
                "id": "pickDemo",
                "type": "command",
                "command": "populate-quickpick.quickPickFromFile"
            }
        ]

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接