|
3 | 3 | * Licensed under the MIT License. See License.md in the project root for license information. |
4 | 4 | *--------------------------------------------------------------------------------------------*/ |
5 | 5 |
|
6 | | -import { AzureWizardPromptStep, nonNullValue } from '@microsoft/vscode-azext-utils'; |
| 6 | +import { AzExtFsExtra, AzureWizardPromptStep, nonNullValue, type FileActivityAttributes } from '@microsoft/vscode-azext-utils'; |
7 | 7 | import { dockerFilePick, dockerfileGlobPattern } from "../../../../constants"; |
8 | 8 | import { selectWorkspaceFile } from "../../../../utils/workspaceUtils"; |
9 | 9 | import { type BuildImageInAzureImageSourceContext } from './BuildImageInAzureImageSourceContext'; |
10 | 10 |
|
11 | | -export class DockerfileItemStep extends AzureWizardPromptStep<BuildImageInAzureImageSourceContext> { |
12 | | - public async prompt(context: BuildImageInAzureImageSourceContext): Promise<void> { |
| 11 | +const dockerfileAttributeName: string = 'Dockerfile'; |
| 12 | + |
| 13 | +export class DockerfileItemStep<T extends BuildImageInAzureImageSourceContext> extends AzureWizardPromptStep<T> { |
| 14 | + public async configureBeforePrompt(context: T): Promise<void> { |
| 15 | + if (context.dockerfilePath) { |
| 16 | + await this.addDockerfileAttributes(context, context.dockerfilePath); |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + public async prompt(context: T): Promise<void> { |
13 | 21 | context.dockerfilePath = nonNullValue(await selectWorkspaceFile(context, dockerFilePick, { filters: {}, autoSelectIfOne: true }, `**/${dockerfileGlobPattern}`)); |
| 22 | + await this.addDockerfileAttributes(context, context.dockerfilePath); |
14 | 23 | } |
15 | 24 |
|
16 | | - public shouldPrompt(context: BuildImageInAzureImageSourceContext): boolean { |
| 25 | + public shouldPrompt(context: T): boolean { |
17 | 26 | return !context.dockerfilePath; |
18 | 27 | } |
| 28 | + |
| 29 | + private async addDockerfileAttributes(context: T, dockerfilePath: string): Promise<void> { |
| 30 | + if (context.activityAttributes?.files?.some(f => f.path === dockerfilePath)) { |
| 31 | + // If this dockerfile already exists in activity attributes, don't add it again |
| 32 | + return; |
| 33 | + } |
| 34 | + |
| 35 | + const dockerfile: FileActivityAttributes = { |
| 36 | + name: dockerfileAttributeName, |
| 37 | + description: 'A Dockerfile from the user\'s VS Code workspace that was used to build the project.', |
| 38 | + path: dockerfilePath, |
| 39 | + content: await AzExtFsExtra.readFile(dockerfilePath), |
| 40 | + }; |
| 41 | + |
| 42 | + context.activityAttributes ??= {}; |
| 43 | + context.activityAttributes.files ??= []; |
| 44 | + context.activityAttributes.files.push(dockerfile); |
| 45 | + } |
| 46 | + |
| 47 | + public undo(context: T): void { |
| 48 | + const files: FileActivityAttributes[] = context.activityAttributes?.files ?? []; |
| 49 | + if (files[files.length - 1]) { |
| 50 | + const lastFile = files[files.length - 1]; |
| 51 | + if (lastFile.name === dockerfileAttributeName) { |
| 52 | + context.activityAttributes?.files?.pop(); |
| 53 | + } |
| 54 | + } |
| 55 | + } |
19 | 56 | } |
0 commit comments