Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 43 additions & 44 deletions src/utils/workspaceUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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 (!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 environment variable file(s), log the count
if (globPattern === envFileGlobPattern || globPattern === `**/${envFileGlobPattern}`) {
context.telemetry.properties.environmentVariableFileCount = String(files.length);
}
const pattern: RelativePattern = new RelativePattern(
context.rootFolder ?? workspace.workspaceFolders[0],
globPattern ?? '**/*'
);
const files: Uri[] = await workspace.findFiles(pattern);

if (options.autoSelectIfOne && files.length === 1) {
return files[0].fsPath;
}
// If dockerfile(s), log the count
if (globPattern === dockerfileGlobPattern || globPattern === `**/${dockerfileGlobPattern}`) {
context.telemetry.properties.dockerfileCount = String(files.length);
}

quickPicks.push(...files.map((uri: Uri) => {
return {
label: basename(uri.path),
description: uri.path,
data: uri.fsPath
};
}));
// If environment variable file(s), log the count
if (globPattern === envFileGlobPattern || globPattern === `**/${envFileGlobPattern}`) {
context.telemetry.properties.environmentVariableFileCount = String(files.length);
}

quickPicks.push(browseItem);
if (options.autoSelectIfOne && files.length === 1) {
return files[0].fsPath;
}

const label = options.skipLabel ?? localize('skipForNow', '$(clock) Skip for now');
if (options.allowSkip) {
quickPicks.push({
label,
description: '',
data: skipForNow
});
}
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);

input = await context.ui.showQuickPick(quickPicks, { placeHolder });
const skipForNow: string = 'skipForNow';
if (options.allowSkip) {
quickPicks.push({
label: options.skipLabel ?? localize('skipForNow', '$(clock) Skip for now'),
description: '',
data: skipForNow
});
}

if (input?.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;
}
}

Expand Down