Task/Launch Input Variables shows a way to do this with a command input variable
. That command can come from vscode's built-in commands or from an extension. In your case, there is no built-in command which returns a list of folders in a given workspace. But it is fairly straightforward to do it in an extension. The documentation does not give a full example of how to do this so I'll show one here. First the demo of it working to ls
a chosen folder in a task:
.
Here is the entire tasks.json
file:
{
"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
},
]
}
You can see in the input variables that a command is called from the folder-operations
extension. Since that command returns a QuickPick
element that is what you'll see when you run the task.
Here is the guts of the extension:
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
}
Here is a link to the folder-operations demo extension so you can look at the full extension code and its package.json. Once you get your extension publishing credentials set up it is really pretty easy to publish more.