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,
|
|
|
|
path: '.' + label.substr(0, label.length - '/dockerfile'.length),
|
|
|
|
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) {
|
|
|
|
vscode.window.showInformationMessage('Couldn\'t find any dockerfile in your workspace.');
|
|
|
|
} 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-09-16 00:26:47 +03:00
|
|
|
let configOptions: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration('docker');
|
2016-10-20 07:03:04 +03:00
|
|
|
|
2016-09-20 19:47:56 +03:00
|
|
|
let defaultRegistryPath = configOptions.get('defaultRegistryPath', '');
|
2016-09-16 00:26:47 +03:00
|
|
|
if (defaultRegistryPath.length > 0) {
|
|
|
|
imageName = defaultRegistryPath + '/' + imageName;
|
|
|
|
}
|
2016-09-20 19:47:56 +03:00
|
|
|
|
|
|
|
let defaultRegistry = configOptions.get('defaultRegistry', '');
|
2016-09-16 00:26:47 +03:00
|
|
|
if (defaultRegistry.length > 0) {
|
|
|
|
imageName = defaultRegistry + '/' + imageName;
|
|
|
|
}
|
2016-09-20 19:47:56 +03:00
|
|
|
|
|
|
|
let terminal: vscode.Terminal = vscode.window.createTerminal('Docker');
|
2016-09-15 19:24:49 +03:00
|
|
|
terminal.sendText(`docker build -f ${selectedItem.file} -t ${imageName} ${selectedItem.path}`);
|
2016-09-14 08:20:20 +03:00
|
|
|
terminal.show();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|