vscode-docker/commands/build-image.ts

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

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,
path: '.' + label.substr(0, label.length - '/dockerfile'.length),
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) {
vscode.window.showInformationMessage('Couldn\'t find any dockerfile in your workspace.');
} 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-09-16 00:26:47 +03:00
let configOptions: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration('docker');
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();
}
});
}
});
}