2018-08-17 03:17:28 +03:00
|
|
|
/*---------------------------------------------------------------------------------------------
|
|
|
|
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
|
|
* Licensed under the MIT License. See LICENSE.md in the project root for license information.
|
|
|
|
*--------------------------------------------------------------------------------------------*/
|
|
|
|
|
2016-12-08 22:05:00 +03:00
|
|
|
import * as path from "path";
|
|
|
|
import * as vscode from "vscode";
|
2018-11-28 05:43:33 +03:00
|
|
|
import { IActionContext } from "vscode-azureextensionui";
|
2018-09-05 09:23:07 +03:00
|
|
|
import { delay } from "../explorer/utils/utils";
|
2018-08-10 01:26:31 +03:00
|
|
|
import { ext } from "../extensionVariables";
|
2018-09-01 04:57:32 +03:00
|
|
|
import { addImageTaggingTelemetry, getTagFromUserInput } from "./tag-image";
|
2018-11-28 05:43:33 +03:00
|
|
|
import { quickPickDockerFileItem } from "./utils/quick-pick-file";
|
2018-10-10 00:01:16 +03:00
|
|
|
import { quickPickWorkspaceFolder } from "./utils/quickPickWorkspaceFolder";
|
2016-09-14 08:20:20 +03:00
|
|
|
|
2018-09-01 04:27:55 +03:00
|
|
|
export async function buildImage(actionContext: IActionContext, dockerFileUri: vscode.Uri | undefined): Promise<void> {
|
2017-11-09 01:55:45 +03:00
|
|
|
const configOptions: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration('docker');
|
|
|
|
const defaultContextPath = configOptions.get('imageBuildContextPath', '');
|
|
|
|
|
2018-10-10 00:01:16 +03:00
|
|
|
let rootFolder: vscode.WorkspaceFolder = await quickPickWorkspaceFolder('To build Docker files you must first open a folder or workspace in VS Code.');
|
2017-06-06 03:55:05 +03:00
|
|
|
|
2018-11-28 05:43:33 +03:00
|
|
|
const dockerFileItem = await quickPickDockerFileItem(actionContext, dockerFileUri, rootFolder);
|
2017-12-04 13:42:15 +03:00
|
|
|
|
2018-08-18 02:10:10 +03:00
|
|
|
let contextPath: string = dockerFileItem.relativeFolderPath;
|
2018-07-18 00:12:15 +03:00
|
|
|
if (defaultContextPath && defaultContextPath !== '') {
|
2017-11-09 01:55:45 +03:00
|
|
|
contextPath = defaultContextPath;
|
|
|
|
}
|
2018-09-01 04:36:39 +03:00
|
|
|
let absFilePath: string = path.join(rootFolder.uri.fsPath, dockerFileItem.relativeFilePath);
|
|
|
|
let dockerFileKey = `buildTag_${absFilePath}`;
|
|
|
|
let prevImageName: string | undefined = ext.context.globalState.get(dockerFileKey);
|
2018-09-01 04:57:32 +03:00
|
|
|
let suggestedImageName: string;
|
2016-10-25 02:40:34 +03:00
|
|
|
|
2018-09-01 04:36:39 +03:00
|
|
|
if (!prevImageName) {
|
|
|
|
// Get imageName based on name of subfolder containing the Dockerfile, or else workspacefolder
|
2018-09-01 04:57:32 +03:00
|
|
|
suggestedImageName = path.basename(dockerFileItem.relativeFolderPath).toLowerCase();
|
|
|
|
if (suggestedImageName === '.') {
|
|
|
|
suggestedImageName = path.basename(rootFolder.uri.fsPath).toLowerCase();
|
2018-09-01 04:36:39 +03:00
|
|
|
}
|
|
|
|
|
2018-09-01 04:57:32 +03:00
|
|
|
suggestedImageName += ":latest"
|
2018-09-01 04:36:39 +03:00
|
|
|
} else {
|
2018-09-01 04:57:32 +03:00
|
|
|
suggestedImageName = prevImageName;
|
2018-09-01 02:19:53 +03:00
|
|
|
}
|
2018-09-01 04:36:39 +03:00
|
|
|
|
2018-09-05 09:23:07 +03:00
|
|
|
// Temporary work-around for vscode bug where valueSelection can be messed up if a quick pick is followed by a showInputBox
|
|
|
|
await delay(500);
|
|
|
|
|
2018-09-01 04:57:32 +03:00
|
|
|
addImageTaggingTelemetry(actionContext, suggestedImageName, '.before');
|
|
|
|
const imageName: string = await getTagFromUserInput(suggestedImageName, !prevImageName);
|
|
|
|
addImageTaggingTelemetry(actionContext, imageName, '.after');
|
|
|
|
|
|
|
|
await ext.context.globalState.update(dockerFileKey, imageName);
|
2018-09-01 04:36:39 +03:00
|
|
|
|
2018-08-10 01:26:31 +03:00
|
|
|
const terminal: vscode.Terminal = ext.terminalProvider.createTerminal('Docker');
|
2018-09-01 04:57:32 +03:00
|
|
|
terminal.sendText(`docker build --rm -f "${dockerFileItem.relativeFilePath}" -t ${imageName} ${contextPath}`);
|
2017-06-06 03:55:05 +03:00
|
|
|
terminal.show();
|
2018-01-18 02:32:20 +03:00
|
|
|
}
|