Skip to content

Commit 237dad3

Browse files
authored
Add support for choosing a source directory when deploying from a workspace project (#586)
1 parent a58a82c commit 237dad3

File tree

5 files changed

+68
-9
lines changed

5 files changed

+68
-9
lines changed

src/commands/image/imageSource/buildImageInAzure/BuildFromProjectListStep.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { ImageNameStep } from "./ImageNameStep";
1414
import { OSPickStep } from "./OSPickStep";
1515
import { RootFolderStep } from "./RootFolderStep";
1616
import { RunStep } from "./RunStep";
17+
import { SourcePathStep } from "./SourcePathStep";
1718
import { TarFileStep } from "./TarFileStep";
1819
import { UploadSourceCodeStep } from "./UploadSourceCodeStep";
1920

@@ -49,7 +50,7 @@ export class BuildFromProjectListStep extends AzureWizardPromptStep<ImageSourceC
4950

5051
switch (context.buildType) {
5152
case ImageSource.RemoteAcrBuild:
52-
promptSteps.push(new AcrListStep(), new RootFolderStep(), new DockerFileItemStep(), new ImageNameStep(), new OSPickStep());
53+
promptSteps.push(new AcrListStep(), new RootFolderStep(), new DockerFileItemStep(), new SourcePathStep(), new ImageNameStep(), new OSPickStep());
5354
executeSteps.push(new TarFileStep(), new UploadSourceCodeStep(), new RunStep(), new BuildImageStep());
5455
break;
5556
//TODO: case for 'Build from project locally using Docker'

src/commands/image/imageSource/buildImageInAzure/BuildImageInAzureImageSourceContext.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { type AcrBuildSupportedOS } from './OSPickStep';
1212

1313
export interface BuildImageInAzureImageSourceBaseContext extends ContainerRegistryImageSourceBaseContext {
1414
rootFolder: vscode.WorkspaceFolder;
15+
srcPath: string;
1516
dockerfilePath: string;
1617
imageName: string;
1718
os: AcrBuildSupportedOS;

src/commands/image/imageSource/buildImageInAzure/RunStep.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export class RunStep extends ExecuteActivityOutputStepBase<BuildImageInAzureImag
2626
isPushEnabled: true,
2727
sourceLocation: context.uploadedSourceLocation,
2828
platform: { os: context.os },
29-
dockerFilePath: path.basename(context.dockerfilePath) /* Assume the dockerfile is always in the root of the source */
29+
dockerFilePath: path.relative(context.srcPath, context.dockerfilePath)
3030
};
3131

3232
const retries = 3;
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
}

src/commands/image/imageSource/buildImageInAzure/UploadSourceCodeStep.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,22 @@ const vcsIgnoreList = ['.git', '.gitignore', '.bzr', 'bzrignore', '.hg', '.hgign
1919

2020
export class UploadSourceCodeStep extends ExecuteActivityOutputStepBase<BuildImageInAzureImageSourceContext> {
2121
public priority: number = 430;
22+
/** Relative path of src folder from rootFolder and what gets deployed */
2223
private _sourceFilePath: string;
2324

2425
protected async executeCore(context: BuildImageInAzureImageSourceContext, progress: Progress<{ message?: string | undefined; increment?: number | undefined }>): Promise<void> {
25-
/* relative path of src folder from rootFolder and what gets deployed */
26-
this._sourceFilePath = path.dirname(path.relative(context.rootFolder.uri.path, context.dockerfilePath));
26+
this._sourceFilePath = context.rootFolder.uri.fsPath === context.srcPath ? '.' : path.relative(context.rootFolder.uri.fsPath, context.srcPath);
2727
context.telemetry.properties.sourceDepth = this._sourceFilePath === '.' ? '0' : String(this._sourceFilePath.split(path.sep).length);
2828

2929
context.registryName = nonNullValue(context.registry?.name);
3030
context.resourceGroupName = getResourceGroupFromId(nonNullValue(context.registry?.id));
3131
context.client = await createContainerRegistryManagementClient(context);
3232

33-
const uploading: string = localize('uploadingSourceCode', 'Uploading source code...', this._sourceFilePath);
34-
progress.report({ message: uploading });
33+
progress.report({ message: localize('uploadingSourceCode', 'Uploading source code...') });
34+
3535
const source: string = path.join(context.rootFolder.uri.fsPath, this._sourceFilePath);
3636
let items = await AzExtFsExtra.readDirectory(source);
37-
items = items.filter(i => {
38-
return !vcsIgnoreList.includes(i.name)
39-
});
37+
items = items.filter(i => !vcsIgnoreList.includes(i.name));
4038

4139
tar.c({ cwd: source }, items.map(i => i.name)).pipe(fse.createWriteStream(context.tarFilePath));
4240

0 commit comments

Comments
 (0)