我想提供一个字符串列表作为任务的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>]
]
}
我希望用户在任务运行时看到文件夹列表。
发布于 2020-11-25 00:33:15
问题
实际上,VSCode tasks没有内置此功能。enhancement request被关闭可能是因为在不久的将来没有开发它的计划。
替代解决方案
您可以安装augustocdias.tasks-shell-input并配置一个command
类型的输入变量,调用这个扩展来填充选择列表。
请执行以下步骤:
Ctrl+Shift+X
)中搜索augustocdias.tasks-shell-input
和install或- `ext install augustocdias.tasks-shell-input`
task.json
:args
配置条目中,如使用以下命令在inputs
部分中的条目:id
:使用在任务d31中定义的相同变量名对于${input:my_variable}
type
:command
command
:shellCommand.execute
args
:,添加具有以下属性的配置:d45,d46,env
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}"
}
},
}
]
}
发布于 2020-11-02 05:46:50
Task/Launch Input Variables展示了一种使用command input variable
实现此目的的方法。该命令可以来自vscode的内置命令,也可以来自扩展。在您的示例中,没有返回给定工作区中的文件夹列表的内置命令。但是在扩展中做这件事是相当简单的。文档并没有给出如何做到这一点的完整示例,因此我将在这里展示一个示例。首先,演示如何在任务中ls
一个选定的文件夹:
。
下面是整个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。一旦设置了扩展发布凭证,就很容易发布更多扩展。
发布于 2021-11-12 08:40:38
如果没有安装扩展或writing your own,您应该首先确定您的工作区文件夹结构。
你本可以
具有主文件夹的(workspace=folder)
的
有一些内置的var允许您访问,例如
相对于${ ${workspaceFolder}
${relativeFileDirname}
的
假设您想为当前打开的文件的存储库运行git gui
-使用tasks.json
配置中的"cwd" : "${relativeFileDirname}"
,git应该会获取当前的存储库,而您不需要询问文件夹路径。
https://stackoverflow.com/questions/57977832
复制相似问题