|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | +* Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | +* Licensed under the MIT License. See License.md in the project root for license information. |
| 4 | +*--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import { AzureWizardPromptStep, type IAzureQuickPickItem } from '@microsoft/vscode-azext-utils'; |
| 7 | +import * as path from 'path'; |
| 8 | +import { browseItem } from '../../../../constants'; |
| 9 | +import { localize } from '../../../../utils/localize'; |
| 10 | +import { type BuildImageInAzureImageSourceContext } from './BuildImageInAzureImageSourceContext'; |
| 11 | + |
| 12 | +export class SourcePathStep extends AzureWizardPromptStep<BuildImageInAzureImageSourceContext> { |
| 13 | + public async prompt(context: BuildImageInAzureImageSourceContext): Promise<void> { |
| 14 | + const srcPath: string | undefined = (await context.ui.showQuickPick(this.getPicks(context), { |
| 15 | + placeHolder: localize('sourceDirectoryPick', 'Choose your source code directory'), |
| 16 | + suppressPersistence: true |
| 17 | + })).data; |
| 18 | + |
| 19 | + context.srcPath = srcPath ?? (await context.ui.showOpenDialog({ |
| 20 | + defaultUri: context.rootFolder?.uri, |
| 21 | + canSelectFiles: false, |
| 22 | + canSelectFolders: true |
| 23 | + }))[0].fsPath; |
| 24 | + } |
| 25 | + |
| 26 | + public async configureBeforePrompt(context: BuildImageInAzureImageSourceContext): Promise<void> { |
| 27 | + if (this.hasRootDockerfile(context)) { |
| 28 | + context.srcPath = context.rootFolder.uri.fsPath; |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + public shouldPrompt(context: BuildImageInAzureImageSourceContext): boolean { |
| 33 | + return !context.srcPath; |
| 34 | + } |
| 35 | + |
| 36 | + private getPicks(context: BuildImageInAzureImageSourceContext): IAzureQuickPickItem<string | undefined>[] { |
| 37 | + const rootPath: string = context.rootFolder.uri.fsPath; |
| 38 | + const directories: string[] = path.relative(rootPath, path.dirname(context.dockerfilePath)).split(path.sep); |
| 39 | + const picks: IAzureQuickPickItem<string | undefined>[] = [{ label: '.' + path.sep, data: rootPath }]; |
| 40 | + |
| 41 | + let p: string = ''; |
| 42 | + for (const directory of directories) { |
| 43 | + p += path.sep + directory; |
| 44 | + picks.push({ label: '.' + p, data: rootPath + p }); |
| 45 | + } |
| 46 | + |
| 47 | + picks.push(browseItem); |
| 48 | + return picks; |
| 49 | + } |
| 50 | + |
| 51 | + private hasRootDockerfile(context: BuildImageInAzureImageSourceContext): boolean { |
| 52 | + if (!context.rootFolder || !context.dockerfilePath) { |
| 53 | + return false; |
| 54 | + } |
| 55 | + |
| 56 | + const rootPath: string = context.rootFolder.uri.fsPath; |
| 57 | + return path.relative(rootPath, context.dockerfilePath) === path.basename(context.dockerfilePath); |
| 58 | + } |
| 59 | +} |
0 commit comments