vscode-docker/commands/build-image.ts

101 строка
2.8 KiB
TypeScript
Исходник Обычный вид История

2016-12-08 22:05:00 +03:00
import * as path from "path";
import * as vscode from "vscode";
2017-03-02 04:49:54 +03:00
import { reporter } from '../telemetry/telemetry';
import { DOCKERFILE_GLOB_PATTERN } from '../dockerExtension';
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
function hasWorkspaceFolder(): boolean {
2016-09-14 08:20:20 +03:00
return vscode.workspace.rootPath ? true : false;
}
2017-06-06 03:55:05 +03:00
async function getDockerFileUris(): Promise<vscode.Uri[]> {
2016-09-14 08:20:20 +03:00
if (!hasWorkspaceFolder()) {
2017-06-06 03:55:05 +03:00
return;
2016-09-14 08:20:20 +03:00
}
2017-06-06 03:55:05 +03:00
return await vscode.workspace.findFiles(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(uri: vscode.Uri): Item {
2017-01-03 02:10:55 +03:00
let filePath = hasWorkspaceFolder() ? path.join(".", uri.fsPath.substr(vscode.workspace.rootPath.length)) : uri.fsPath;
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(uris: vscode.Uri[]): vscode.QuickPickItem[] {
let items: vscode.QuickPickItem[] = [];
2016-09-14 08:20:20 +03:00
for (let i = 0; i < uris.length; i++) {
items.push(createItem(uris[i]));
}
return items;
}
2017-06-06 03:55:05 +03:00
async function resolveImageItem(dockerFileUri?: vscode.Uri): Promise<Item> {
if (dockerFileUri) {
return createItem(dockerFileUri);
};
const uris: vscode.Uri[] = await getDockerFileUris();
if (!uris || uris.length == 0) {
vscode.window.showInformationMessage('Couldn\'t find a Dockerfile in your workspace.');
return;
} else {
const res: vscode.QuickPickItem = await vscode.window.showQuickPick(computeItems(uris), {placeHolder: 'Choose Dockerfile to build'});
return <Item>res;
}
2016-12-08 22:05:00 +03:00
}
2017-06-06 03:55:05 +03:00
export async function buildImage(dockerFileUri?: vscode.Uri) {
const uri: Item = await resolveImageItem(dockerFileUri);
if (!uri) return;
2016-09-20 19:47:56 +03:00
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') {
2017-06-06 03:55:05 +03:00
imageName = vscode.workspace.rootPath.split('\\').pop().toLowerCase();
2016-12-08 22:05:00 +03:00
} else {
2017-06-06 03:55:05 +03:00
imageName = vscode.workspace.rootPath.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 vscode.window.showInputBox(opt);
2016-10-25 02:40:34 +03:00
2017-06-06 03:55:05 +03:00
if (!value) return;
const terminal: vscode.Terminal = vscode.window.createTerminal('Docker');
2017-09-15 04:34:35 +03:00
terminal.sendText(`docker build --rm -f ${uri.file} -t ${value} ${uri.path}`);
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) {
reporter.sendTelemetryEvent('command', {
command: teleCmdId
2016-12-08 22:05:00 +03:00
});
2017-06-06 03:55:05 +03:00
}
2016-09-14 08:20:20 +03:00
}