2016-11-29 01:26:33 +03:00
|
|
|
|
2016-09-14 08:20:20 +03:00
|
|
|
import vscode = require('vscode');
|
|
|
|
|
|
|
|
|
2016-10-20 07:03:04 +03:00
|
|
|
function hasWorkspaceFolder(): boolean {
|
2016-09-14 08:20:20 +03:00
|
|
|
return vscode.workspace.rootPath ? true : false;
|
|
|
|
}
|
|
|
|
|
2016-10-20 07:03:04 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-10-20 07:03:04 +03:00
|
|
|
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);
|
2016-10-20 07:03:04 +03:00
|
|
|
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
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-10-20 07:03:04 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2016-10-20 07:03:04 +03:00
|
|
|
vscode.window.showQuickPick(items, { placeHolder: 'Choose Dockerfile to build' }).then(function (selectedItem: Item) {
|
2016-09-14 08:20:20 +03:00
|
|
|
if (selectedItem) {
|
2016-10-20 07:03:04 +03:00
|
|
|
|
|
|
|
// TODO: Prompt for name, prefill with generated name below...
|
2016-10-21 01:58:48 +03:00
|
|
|
|
2016-10-20 07:03:04 +03:00
|
|
|
var imageName: string;
|
|
|
|
|
|
|
|
if (process.platform === 'win32') {
|
2016-10-21 01:58:48 +03:00
|
|
|
imageName = selectedItem.path.split('\\').pop().toLowerCase();
|
2016-10-20 07:03:04 +03:00
|
|
|
} else {
|
2016-10-21 01:58:48 +03:00
|
|
|
imageName = selectedItem.path.split('/').pop().toLowerCase();
|
2016-10-20 07:03:04 +03:00
|
|
|
}
|
|
|
|
|
2016-09-16 20:04:46 +03:00
|
|
|
if (imageName === '.') {
|
2016-10-20 07:03:04 +03:00
|
|
|
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-20 07:03:04 +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
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|