Skip to content

Commit

Permalink
Handle build image command
Browse files Browse the repository at this point in the history
  • Loading branch information
danegsta committed Dec 19, 2024
1 parent 2a2c08f commit c443855
Showing 1 changed file with 31 additions and 5 deletions.
36 changes: 31 additions & 5 deletions src/commands/images/buildImage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*--------------------------------------------------------------------------------------------*/

import { IActionContext, UserCancelledError } from "@microsoft/vscode-azext-utils";
import { quoted } from "@microsoft/vscode-container-client";
import * as path from "path";
import * as vscode from "vscode";
import { ext } from "../../extensionVariables";
Expand Down Expand Up @@ -48,10 +49,10 @@ export async function buildImage(context: IActionContext, dockerFileUri: vscode.
contextPath
);

// Replace '${tag}' if needed. Tag is a special case because we don't want to prompt unless necessary, so must manually replace it.
if (tagRegex.test(terminalCommand.command)) {
const absFilePath: string = path.join(rootFolder.uri.fsPath, dockerFileItem.relativeFilePath);
const dockerFileKey = `buildTag_${absFilePath}`;
const absFilePath: string = path.join(rootFolder.uri.fsPath, dockerFileItem.relativeFilePath);
const dockerFileKey = `buildTag_${absFilePath}`;

const getImageName = async (): Promise<string> => {
const prevImageName: string | undefined = ext.context.workspaceState.get(dockerFileKey);

// Get imageName based previous entries, else on name of subfolder containing the Dockerfile
Expand All @@ -64,8 +65,33 @@ export async function buildImage(context: IActionContext, dockerFileUri: vscode.
const imageName: string = await getTagFromUserInput(context, suggestedImageName);
addImageTaggingTelemetry(context, imageName, '.after');

return imageName;
};

// Replace '${tag}' if needed. Tag is a special case because we don't want to prompt unless necessary, so must manually replace it.
if (!terminalCommand.args || terminalCommand.args.length === 0) {
// This is a customized command, so parse the tag from the command
if (tagRegex.test(terminalCommand.command)) {
const imageName = await getImageName();
await ext.context.workspaceState.update(dockerFileKey, imageName);
terminalCommand.command = terminalCommand.command.replace(tagRegex, imageName);
}
} else if (terminalCommand.args.some(arg => tagRegex.test(typeof (arg) === 'string' ? arg : arg.value))) {
// This is a default command, so look for ${tag} in the args
const imageName = await getImageName();
await ext.context.workspaceState.update(dockerFileKey, imageName);
terminalCommand.command = terminalCommand.command.replace(tagRegex, imageName);

terminalCommand.args = terminalCommand.args.map(arg => {
if (typeof (arg) === 'string') {
if (tagRegex.test(arg)) {
arg = quoted(arg.replace(tagRegex, imageName));
}
} else if (tagRegex.test(arg.value)) {
arg = quoted(arg.value.replace(tagRegex, imageName));
}

return arg;
});
}

const client = await ext.runtimeManager.getClient();
Expand Down

0 comments on commit c443855

Please sign in to comment.