vscode-docker/commands/build-image.ts

126 строки
4.4 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 { DOCKERFILE_GLOB_PATTERN } from '../dockerExtension';
import { ext } from "../extensionVariables";
import { reporter } from '../telemetry/telemetry';
2017-03-28 03:54:16 +03:00
2017-03-04 04:13:14 +03:00
const teleCmdId: string = 'vscode-docker.image.build';
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), null, 1000, null);
2016-09-14 08:20:20 +03:00
}
interface Item extends vscode.QuickPickItem {
2016-12-08 22:05:00 +03:00
file: string,
path: string
2016-09-14 08:20:20 +03:00
}
function createItem(folder: vscode.WorkspaceFolder, uri: vscode.Uri): Item {
let filePath = path.join(".", uri.fsPath.substr(folder.uri.fsPath.length));
2016-12-08 22:05:00 +03:00
return <Item>{
2016-09-14 08:20:20 +03:00
description: null,
2016-12-08 22:05:00 +03:00
file: filePath,
label: filePath,
2017-01-03 02:10:55 +03:00
path: path.dirname(filePath)
2016-09-14 08:20:20 +03:00
};
}
function computeItems(folder: vscode.WorkspaceFolder, uris: vscode.Uri[]): vscode.QuickPickItem[] {
let items: vscode.QuickPickItem[] = [];
// tslint:disable-next-line:prefer-for-of // Grandfathered in
2016-09-14 08:20:20 +03:00
for (let i = 0; i < uris.length; i++) {
items.push(createItem(folder, uris[i]));
2016-09-14 08:20:20 +03:00
}
return items;
}
async function resolveImageItem(folder: vscode.WorkspaceFolder, dockerFileUri?: vscode.Uri): Promise<Item> {
2017-06-06 03:55:05 +03:00
if (dockerFileUri) {
return createItem(folder, dockerFileUri);
}
2017-06-06 03:55:05 +03:00
const uris: vscode.Uri[] = await getDockerFileUris(folder);
2017-06-06 03:55:05 +03:00
2018-07-18 00:12:15 +03:00
if (!uris || uris.length === 0) {
2017-06-06 03:55:05 +03:00
vscode.window.showInformationMessage('Couldn\'t find a Dockerfile in your workspace.');
return;
} else {
const res: vscode.QuickPickItem = await vscode.window.showQuickPick(computeItems(folder, uris), { placeHolder: 'Choose Dockerfile to build' });
2017-06-06 03:55:05 +03:00
return <Item>res;
}
2016-12-08 22:05:00 +03:00
}
export async function buildImage(dockerFileUri?: vscode.Uri): Promise<void> {
const configOptions: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration('docker');
const defaultContextPath = configOptions.get('imageBuildContextPath', '');
let folder: vscode.WorkspaceFolder;
if (vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders.length === 1) {
folder = vscode.workspace.workspaceFolders[0];
} else {
folder = await (<any>vscode).window.showWorkspaceFolderPick();
}
if (!folder) {
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
const uri: Item = await resolveImageItem(folder, dockerFileUri);
2018-07-18 00:12:15 +03:00
if (!uri) { return; }
2017-12-04 13:42:15 +03:00
let contextPath: string = uri.path;
2018-07-18 00:12:15 +03:00
if (defaultContextPath && defaultContextPath !== '') {
contextPath = defaultContextPath;
}
2017-06-06 03:55:05 +03:00
let imageName: string;
if (process.platform === 'win32') {
imageName = uri.path.split('\\').pop().toLowerCase();
} else {
imageName = uri.path.split('/').pop().toLowerCase();
}
if (imageName === '.') {
2016-12-08 22:05:00 +03:00
if (process.platform === 'win32') {
imageName = folder.uri.fsPath.split('\\').pop().toLowerCase();
2016-12-08 22:05:00 +03:00
} else {
imageName = folder.uri.fsPath.split('/').pop().toLowerCase();
2016-12-08 22:05:00 +03:00
}
2017-06-06 03:55:05 +03:00
}
2016-10-25 02:40:34 +03:00
2017-06-06 03:55:05 +03:00
const opt: vscode.InputBoxOptions = {
placeHolder: imageName + ':latest',
prompt: 'Tag image as...',
value: imageName + ':latest'
};
const value: string = await ext.ui.showInputBox(opt);
2016-10-25 02:40:34 +03:00
const terminal: vscode.Terminal = ext.terminalProvider.createTerminal('Docker');
terminal.sendText(`docker build --rm -f "${uri.file}" -t ${value} ${contextPath}`);
2017-06-06 03:55:05 +03:00
terminal.show();
2017-09-15 04:25:19 +03:00
2017-06-06 03:55:05 +03:00
if (reporter) {
/* __GDPR__
"command" : {
"command" : { "classification": "SystemMetaData", "purpose": "FeatureInsight" }
}
*/
2017-06-06 03:55:05 +03:00
reporter.sendTelemetryEvent('command', {
command: teleCmdId
2016-12-08 22:05:00 +03:00
});
2017-06-06 03:55:05 +03:00
}
2018-01-18 02:32:20 +03:00
}