Refactor `selectWorkspaceFile` to automatically detect and incorporate any known `rootFolder` (#685)

This commit is contained in:
Matthew Fisher 2024-06-06 09:44:46 -07:00 коммит произвёл GitHub
Родитель 5af9c987bd
Коммит 0f3dcf4b6b
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
1 изменённых файлов: 49 добавлений и 50 удалений

Просмотреть файл

@ -4,8 +4,8 @@
*--------------------------------------------------------------------------------------------*/
import { UserCancelledError, type IActionContext, type IAzureQuickPickItem } from "@microsoft/vscode-azext-utils";
import { basename } from "path";
import { Uri, window, workspace, type OpenDialogOptions, type WorkspaceFolder } from "vscode";
import { basename, relative } from "path";
import { RelativePattern, Uri, window, workspace, type OpenDialogOptions, type WorkspaceFolder } from "vscode";
import { browseItem, dockerfileGlobPattern, envFileGlobPattern } from "../constants";
import { type SetTelemetryProps } from "../telemetry/SetTelemetryProps";
import { type WorkspaceFileTelemetryProps as TelemetryProps } from "../telemetry/WorkspaceFileTelemetryProps";
@ -32,62 +32,61 @@ interface SelectWorkspaceFileOptions extends OpenDialogOptions {
* @returns Returns a string representing the workspace file path chosen. A return of undefined is only possible when the `allowSkip` option is set to true.
*/
export async function selectWorkspaceFile(
context: IActionContext & SetTelemetryProps<TelemetryProps>,
context: IActionContext & SetTelemetryProps<TelemetryProps> & { rootFolder?: WorkspaceFolder },
placeHolder: string,
options: SelectWorkspaceFileOptions,
globPattern?: string
): Promise<string | undefined> {
let input: IAzureQuickPickItem<string | undefined> | undefined;
const quickPicks: IAzureQuickPickItem<string | undefined>[] = [];
const skipForNow: string = 'skipForNow';
if (workspace.workspaceFolders?.length === 1) {
// if there's a fileExtension, then only populate the quickPick menu with that, otherwise show the current folders in the workspace
const files = globPattern ? await workspace.findFiles(globPattern) : await workspace.findFiles('**/*');
context.telemetry.properties.dockerfileCount = '0';
context.telemetry.properties.environmentVariableFileCount = '0';
// If dockerfile(s), log the count
if (globPattern === dockerfileGlobPattern || globPattern === `**/${dockerfileGlobPattern}`) {
context.telemetry.properties.dockerfileCount = String(files.length);
}
// If environment variable file(s), log the count
if (globPattern === envFileGlobPattern || globPattern === `**/${envFileGlobPattern}`) {
context.telemetry.properties.environmentVariableFileCount = String(files.length);
}
if (options.autoSelectIfOne && files.length === 1) {
return files[0].fsPath;
}
quickPicks.push(...files.map((uri: Uri) => {
return {
label: basename(uri.path),
description: uri.path,
data: uri.fsPath
};
}));
quickPicks.push(browseItem);
const label = options.skipLabel ?? localize('skipForNow', '$(clock) Skip for now');
if (options.allowSkip) {
quickPicks.push({
label,
description: '',
data: skipForNow
});
}
input = await context.ui.showQuickPick(quickPicks, { placeHolder });
if (!workspace.workspaceFolders?.length) {
throw new Error(localize('noWorkspaceOpen', 'No workspace is open to search through.'));
} else if (workspace.workspaceFolders.length > 1 && !context.rootFolder) {
throw new Error(localize('couldNotDetermineWorkspaceFolder', 'Could not determine which workspace folder to search through.'));
}
if (input?.data === skipForNow) {
const pattern: RelativePattern = new RelativePattern(
context.rootFolder ?? workspace.workspaceFolders[0],
globPattern ?? '**/*'
);
const files: Uri[] = await workspace.findFiles(pattern);
// If dockerfile(s), log the count
if (globPattern === dockerfileGlobPattern || globPattern === `**/${dockerfileGlobPattern}`) {
context.telemetry.properties.dockerfileCount = String(files.length);
}
// If environment variable file(s), log the count
if (globPattern === envFileGlobPattern || globPattern === `**/${envFileGlobPattern}`) {
context.telemetry.properties.environmentVariableFileCount = String(files.length);
}
if (options.autoSelectIfOne && files.length === 1) {
return files[0].fsPath;
}
const quickPicks: IAzureQuickPickItem<string | undefined>[] = [];
quickPicks.push(...files.map((uri: Uri) => {
return {
label: basename(uri.path),
description: relative(pattern.baseUri.path, uri.path),
data: uri.fsPath
};
}));
quickPicks.push(browseItem);
const skipForNow: string = 'skipForNow';
if (options.allowSkip) {
quickPicks.push({
label: options.skipLabel ?? localize('skipForNow', '$(clock) Skip for now'),
description: '',
data: skipForNow
});
}
const input: string | undefined = (await context.ui.showQuickPick(quickPicks, { placeHolder })).data;
if (input === skipForNow) {
return undefined;
} else {
return input?.data || (await context.ui.showOpenDialog(options))[0].fsPath;
return input || (await context.ui.showOpenDialog(options))[0].fsPath;
}
}