vscode-docker/commands/build-image.ts

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

2016-11-29 01:26:33 +03:00
2016-09-14 08:20:20 +03:00
import vscode = require('vscode');
function hasWorkspaceFolder(): boolean {
2016-09-14 08:20:20 +03:00
return vscode.workspace.rootPath ? true : false;
}
function getDockerFileUris(): Thenable<vscode.Uri[]> {
2016-09-14 08:20:20 +03:00
if (!hasWorkspaceFolder()) {
return Promise.resolve(null);
}
return Promise.resolve(vscode.workspace.findFiles('**/[dD]ocker[fF]ile', null, 9999, null));
}
interface Item extends vscode.QuickPickItem {
path: string,
file: string
}
function createItem(uri: vscode.Uri): Item {
2016-09-14 08:20:20 +03:00
let length = vscode.workspace.rootPath.length;
2016-10-12 03:21:42 +03:00
let label = uri.fsPath.substr(length);
return <Item>{
2016-09-14 08:20:20 +03:00
label: label,
description: null,
2016-11-28 23:32:43 +03:00
path: '.' + label.substr(0, label.length - '/Dockerfile'.length),
2016-09-14 08:20:20 +03:00
file: '.' + label
};
}
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;
}
export function buildImage() {
getDockerFileUris().then(function (uris: vscode.Uri[]) {
if (!uris || uris.length == 0) {
2016-11-29 01:26:33 +03:00
vscode.window.showInformationMessage('Couldn\'t find a Dockerfile in your workspace.');
2016-09-14 08:20:20 +03:00
} else {
let items: vscode.QuickPickItem[] = computeItems(uris);
vscode.window.showQuickPick(items, { placeHolder: 'Choose Dockerfile to build' }).then(function (selectedItem: Item) {
2016-09-14 08:20:20 +03:00
if (selectedItem) {
// TODO: Prompt for name, prefill with generated name below...
2016-10-21 01:58:48 +03:00
var imageName: string;
if (process.platform === 'win32') {
2016-10-21 01:58:48 +03:00
imageName = selectedItem.path.split('\\').pop().toLowerCase();
} else {
2016-10-21 01:58:48 +03:00
imageName = selectedItem.path.split('/').pop().toLowerCase();
}
2016-09-16 20:04:46 +03:00
if (imageName === '.') {
if (process.platform === 'win32') {
imageName = vscode.workspace.rootPath.split('\\').pop().toLowerCase();
} else {
imageName = vscode.workspace.rootPath.split('/').pop().toLowerCase();
}
2016-09-16 20:04:46 +03:00
}
2016-10-25 02:40:34 +03:00
var opt: vscode.InputBoxOptions = {
prompt: 'Tag image as...',
value: imageName + ':latest',
placeHolder: imageName + ':latest'
2016-09-16 00:26:47 +03:00
}
2016-09-20 19:47:56 +03:00
2016-10-25 02:40:34 +03:00
vscode.window.showInputBox(opt).then((value: string) => {
if (!value) {
return;
}
let terminal: vscode.Terminal = vscode.window.createTerminal('Docker');
terminal.sendText(`docker build -f ${selectedItem.file} -t ${value} ${selectedItem.path}`);
terminal.show();
});
2016-09-20 19:47:56 +03:00
2016-09-14 08:20:20 +03:00
}
});
}
});
}