vscode-docker/commands/build-image.ts

123 строки
5.2 KiB
TypeScript
Исходник Обычный вид История

/*---------------------------------------------------------------------------------------------
* 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";
import { DialogResponses, IActionContext, UserCancelledError } from "vscode-azureextensionui";
import { DOCKERFILE_GLOB_PATTERN } from '../dockerExtension';
import { ext } from "../extensionVariables";
import { addImageTaggingTelemetry, getTagFromUserInput } from "./tag-image";
2016-09-14 08:20:20 +03:00
async function getDockerFileUris(folder: vscode.WorkspaceFolder): Promise<vscode.Uri[]> {
return await vscode.workspace.findFiles(new vscode.RelativePattern(folder, DOCKERFILE_GLOB_PATTERN), undefined, 1000, undefined);
2016-09-14 08:20:20 +03:00
}
interface Item extends vscode.QuickPickItem {
relativeFilePath: string;
relativeFolderPath: string;
2016-09-14 08:20:20 +03:00
}
function createDockerfileItem(rootFolder: vscode.WorkspaceFolder, uri: vscode.Uri): Item {
let relativeFilePath = path.join(".", uri.fsPath.substr(rootFolder.uri.fsPath.length));
2016-12-08 22:05:00 +03:00
return <Item>{
description: undefined,
relativeFilePath: relativeFilePath,
label: relativeFilePath,
relativeFolderPath: path.dirname(relativeFilePath)
2016-09-14 08:20:20 +03:00
};
}
async function resolveDockerFileItem(rootFolder: vscode.WorkspaceFolder, dockerFileUri: vscode.Uri | undefined): Promise<Item | undefined> {
2017-06-06 03:55:05 +03:00
if (dockerFileUri) {
return createDockerfileItem(rootFolder, dockerFileUri);
}
2017-06-06 03:55:05 +03:00
const uris: vscode.Uri[] = await getDockerFileUris(rootFolder);
2017-06-06 03:55:05 +03:00
2018-07-18 00:12:15 +03:00
if (!uris || uris.length === 0) {
return undefined;
2017-06-06 03:55:05 +03:00
} else {
let items: Item[] = uris.map(uri => createDockerfileItem(rootFolder, uri));
if (items.length === 1) {
return items[0];
} else {
const res: vscode.QuickPickItem = await ext.ui.showQuickPick(items, { placeHolder: 'Choose Dockerfile to build' });
return <Item>res;
}
2017-06-06 03:55:05 +03:00
}
2016-12-08 22:05:00 +03:00
}
export async function buildImage(actionContext: IActionContext, dockerFileUri: vscode.Uri | undefined): Promise<void> {
const configOptions: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration('docker');
const defaultContextPath = configOptions.get('imageBuildContextPath', '');
let dockerFileItem: Item | undefined;
let rootFolder: vscode.WorkspaceFolder;
if (vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders.length === 1) {
rootFolder = vscode.workspace.workspaceFolders[0];
} else {
let selected = await vscode.window.showWorkspaceFolderPick();
if (!selected) {
throw new UserCancelledError();
}
rootFolder = selected;
}
if (!rootFolder) {
if (!vscode.workspace.workspaceFolders) {
2018-01-18 02:32:20 +03:00
vscode.window.showErrorMessage('Docker files can only be built if VS Code is opened on a folder.');
} else {
2018-01-18 02:32:20 +03:00
vscode.window.showErrorMessage('Docker files can only be built if a workspace folder is picked in VS Code.');
}
return;
}
2017-06-06 03:55:05 +03:00
while (!dockerFileItem) {
let resolvedItem: Item | undefined = await resolveDockerFileItem(rootFolder, dockerFileUri);
if (resolvedItem) {
dockerFileItem = resolvedItem;
} else {
let msg = "Couldn't find a Dockerfile in your workspace. Would you like to add Docker files to the workspace?";
actionContext.properties.cancelStep = msg;
await ext.ui.showWarningMessage(msg, DialogResponses.yes, DialogResponses.cancel);
actionContext.properties.cancelStep = undefined;
await vscode.commands.executeCommand('vscode-docker.configure');
// Try again
}
}
2017-12-04 13:42:15 +03:00
let contextPath: string = dockerFileItem.relativeFolderPath;
2018-07-18 00:12:15 +03:00
if (defaultContextPath && defaultContextPath !== '') {
contextPath = defaultContextPath;
}
let absFilePath: string = path.join(rootFolder.uri.fsPath, dockerFileItem.relativeFilePath);
let dockerFileKey = `buildTag_${absFilePath}`;
let prevImageName: string | undefined = ext.context.globalState.get(dockerFileKey);
let suggestedImageName: string;
2016-10-25 02:40:34 +03:00
if (!prevImageName) {
// Get imageName based on name of subfolder containing the Dockerfile, or else workspacefolder
suggestedImageName = path.basename(dockerFileItem.relativeFolderPath).toLowerCase();
if (suggestedImageName === '.') {
suggestedImageName = path.basename(rootFolder.uri.fsPath).toLowerCase();
}
suggestedImageName += ":latest"
} else {
suggestedImageName = prevImageName;
}
addImageTaggingTelemetry(actionContext, suggestedImageName, '.before');
const imageName: string = await getTagFromUserInput(suggestedImageName, !prevImageName);
addImageTaggingTelemetry(actionContext, imageName, '.after');
await ext.context.globalState.update(dockerFileKey, imageName);
const terminal: vscode.Terminal = ext.terminalProvider.createTerminal('Docker');
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
}