Skip to content

Commit 0f3dcf4

Browse files
authored
Refactor selectWorkspaceFile to automatically detect and incorporate any known rootFolder (#685)
1 parent 5af9c98 commit 0f3dcf4

File tree

1 file changed

+43
-44
lines changed

1 file changed

+43
-44
lines changed

src/utils/workspaceUtils.ts

Lines changed: 43 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
*--------------------------------------------------------------------------------------------*/
55

66
import { UserCancelledError, type IActionContext, type IAzureQuickPickItem } from "@microsoft/vscode-azext-utils";
7-
import { basename } from "path";
8-
import { Uri, window, workspace, type OpenDialogOptions, type WorkspaceFolder } from "vscode";
7+
import { basename, relative } from "path";
8+
import { RelativePattern, Uri, window, workspace, type OpenDialogOptions, type WorkspaceFolder } from "vscode";
99
import { browseItem, dockerfileGlobPattern, envFileGlobPattern } from "../constants";
1010
import { type SetTelemetryProps } from "../telemetry/SetTelemetryProps";
1111
import { type WorkspaceFileTelemetryProps as TelemetryProps } from "../telemetry/WorkspaceFileTelemetryProps";
@@ -32,62 +32,61 @@ interface SelectWorkspaceFileOptions extends OpenDialogOptions {
3232
* @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.
3333
*/
3434
export async function selectWorkspaceFile(
35-
context: IActionContext & SetTelemetryProps<TelemetryProps>,
35+
context: IActionContext & SetTelemetryProps<TelemetryProps> & { rootFolder?: WorkspaceFolder },
3636
placeHolder: string,
3737
options: SelectWorkspaceFileOptions,
3838
globPattern?: string
3939
): Promise<string | undefined> {
40-
let input: IAzureQuickPickItem<string | undefined> | undefined;
41-
const quickPicks: IAzureQuickPickItem<string | undefined>[] = [];
42-
const skipForNow: string = 'skipForNow';
43-
44-
if (workspace.workspaceFolders?.length === 1) {
45-
// if there's a fileExtension, then only populate the quickPick menu with that, otherwise show the current folders in the workspace
46-
const files = globPattern ? await workspace.findFiles(globPattern) : await workspace.findFiles('**/*');
47-
48-
context.telemetry.properties.dockerfileCount = '0';
49-
context.telemetry.properties.environmentVariableFileCount = '0';
50-
51-
// If dockerfile(s), log the count
52-
if (globPattern === dockerfileGlobPattern || globPattern === `**/${dockerfileGlobPattern}`) {
53-
context.telemetry.properties.dockerfileCount = String(files.length);
54-
}
40+
if (!workspace.workspaceFolders?.length) {
41+
throw new Error(localize('noWorkspaceOpen', 'No workspace is open to search through.'));
42+
} else if (workspace.workspaceFolders.length > 1 && !context.rootFolder) {
43+
throw new Error(localize('couldNotDetermineWorkspaceFolder', 'Could not determine which workspace folder to search through.'));
44+
}
5545

56-
// If environment variable file(s), log the count
57-
if (globPattern === envFileGlobPattern || globPattern === `**/${envFileGlobPattern}`) {
58-
context.telemetry.properties.environmentVariableFileCount = String(files.length);
59-
}
46+
const pattern: RelativePattern = new RelativePattern(
47+
context.rootFolder ?? workspace.workspaceFolders[0],
48+
globPattern ?? '**/*'
49+
);
50+
const files: Uri[] = await workspace.findFiles(pattern);
6051

61-
if (options.autoSelectIfOne && files.length === 1) {
62-
return files[0].fsPath;
63-
}
52+
// If dockerfile(s), log the count
53+
if (globPattern === dockerfileGlobPattern || globPattern === `**/${dockerfileGlobPattern}`) {
54+
context.telemetry.properties.dockerfileCount = String(files.length);
55+
}
6456

65-
quickPicks.push(...files.map((uri: Uri) => {
66-
return {
67-
label: basename(uri.path),
68-
description: uri.path,
69-
data: uri.fsPath
70-
};
71-
}));
57+
// If environment variable file(s), log the count
58+
if (globPattern === envFileGlobPattern || globPattern === `**/${envFileGlobPattern}`) {
59+
context.telemetry.properties.environmentVariableFileCount = String(files.length);
60+
}
7261

73-
quickPicks.push(browseItem);
62+
if (options.autoSelectIfOne && files.length === 1) {
63+
return files[0].fsPath;
64+
}
7465

75-
const label = options.skipLabel ?? localize('skipForNow', '$(clock) Skip for now');
76-
if (options.allowSkip) {
77-
quickPicks.push({
78-
label,
79-
description: '',
80-
data: skipForNow
81-
});
82-
}
66+
const quickPicks: IAzureQuickPickItem<string | undefined>[] = [];
67+
quickPicks.push(...files.map((uri: Uri) => {
68+
return {
69+
label: basename(uri.path),
70+
description: relative(pattern.baseUri.path, uri.path),
71+
data: uri.fsPath
72+
};
73+
}));
74+
quickPicks.push(browseItem);
8375

84-
input = await context.ui.showQuickPick(quickPicks, { placeHolder });
76+
const skipForNow: string = 'skipForNow';
77+
if (options.allowSkip) {
78+
quickPicks.push({
79+
label: options.skipLabel ?? localize('skipForNow', '$(clock) Skip for now'),
80+
description: '',
81+
data: skipForNow
82+
});
8583
}
8684

87-
if (input?.data === skipForNow) {
85+
const input: string | undefined = (await context.ui.showQuickPick(quickPicks, { placeHolder })).data;
86+
if (input === skipForNow) {
8887
return undefined;
8988
} else {
90-
return input?.data || (await context.ui.showOpenDialog(options))[0].fsPath;
89+
return input || (await context.ui.showOpenDialog(options))[0].fsPath;
9190
}
9291
}
9392

0 commit comments

Comments
 (0)