Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisdias committed Dec 13, 2016
2 parents 51f3dfa + 778be25 commit d166a15
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 53 deletions.
105 changes: 53 additions & 52 deletions commands/build-image.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

import vscode = require('vscode');

import * as path from "path";
import * as vscode from "vscode";

function hasWorkspaceFolder(): boolean {
return vscode.workspace.rootPath ? true : false;
Expand All @@ -14,18 +13,18 @@ function getDockerFileUris(): Thenable<vscode.Uri[]> {
}

interface Item extends vscode.QuickPickItem {
path: string,
file: string
file: string,
path: string
}

function createItem(uri: vscode.Uri): Item {
let length = vscode.workspace.rootPath.length;
let label = uri.fsPath.substr(length);
let filePath = hasWorkspaceFolder() ? path.join(".", uri.path.substr(vscode.workspace.rootPath.length)) : uri.path;

return <Item>{
label: label,
description: null,
path: '.' + label.substr(0, label.length - '/Dockerfile'.length),
file: '.' + label
file: filePath,
label: filePath,
path: path.dirname(filePath)
};
}

Expand All @@ -37,53 +36,55 @@ function computeItems(uris: vscode.Uri[]): vscode.QuickPickItem[] {
return items;
}

export function buildImage() {
getDockerFileUris().then(function (uris: vscode.Uri[]) {
if (!uris || uris.length == 0) {
vscode.window.showInformationMessage('Couldn\'t find a Dockerfile in your workspace.');
} else {
let items: vscode.QuickPickItem[] = computeItems(uris);
vscode.window.showQuickPick(items, { placeHolder: 'Choose Dockerfile to build' }).then(function (selectedItem: Item) {
if (selectedItem) {

// TODO: Prompt for name, prefill with generated name below...

var imageName: string;

if (process.platform === 'win32') {
imageName = selectedItem.path.split('\\').pop().toLowerCase();
} else {
imageName = selectedItem.path.split('/').pop().toLowerCase();
}

if (imageName === '.') {
if (process.platform === 'win32') {
imageName = vscode.workspace.rootPath.split('\\').pop().toLowerCase();
} else {
imageName = vscode.workspace.rootPath.split('/').pop().toLowerCase();
}
}
function resolveImageItem(dockerFileUri?: vscode.Uri): Promise<Item> {
return new Promise((resolve) => {
if (dockerFileUri) {
return resolve(createItem(dockerFileUri));
};

getDockerFileUris().then((uris: vscode.Uri[]) => {
if (!uris || uris.length == 0) {
vscode.window.showInformationMessage('Couldn\'t find a Dockerfile in your workspace.');
resolve();
} else {
let items: vscode.QuickPickItem[] = computeItems(uris);
vscode.window.showQuickPick(items, { placeHolder: 'Choose Dockerfile to build' }).then(resolve);
}
});
});
}

var opt: vscode.InputBoxOptions = {
prompt: 'Tag image as...',
value: imageName + ':latest',
placeHolder: imageName + ':latest'
}
export function buildImage(dockerFileUri?: vscode.Uri) {
resolveImageItem(dockerFileUri).then((uri: Item) => {
if (!uri) return;

vscode.window.showInputBox(opt).then((value: string) => {
let imageName: string;
if (process.platform === 'win32') {
imageName = uri.path.split('\\').pop().toLowerCase();
} else {
imageName = uri.path.split('/').pop().toLowerCase();
}

if (!value) {
return;
}
if (imageName === '.') {
if (process.platform === 'win32') {
imageName = vscode.workspace.rootPath.split('\\').pop().toLowerCase();
} else {
imageName = vscode.workspace.rootPath.split('/').pop().toLowerCase();
}
}

let terminal: vscode.Terminal = vscode.window.createTerminal('Docker');
terminal.sendText(`docker build -f ${selectedItem.file} -t ${value} ${selectedItem.path}`);
terminal.show();
const opt: vscode.InputBoxOptions = {
placeHolder: imageName + ':latest',
prompt: 'Tag image as...',
value: imageName + ':latest'
};

});
vscode.window.showInputBox(opt).then((value: string) => {
if (!value) return;

}
});
}
let terminal: vscode.Terminal = vscode.window.createTerminal('Docker');
terminal.sendText(`docker build -f ${uri.file} -t ${value} ${uri.path}`);
terminal.show();
});
});
}
18 changes: 17 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,22 @@
"path": "./snippets/dockerfile.json"
}
],
"menus": {
"editor/context": [
{
"when": "editorLangId == dockerfile",
"command": "vscode-docker.image.build",
"group": "docker"
}
],
"explorer/context": [
{
"when": "resourceLangId == dockerfile",
"command": "vscode-docker.image.build",
"group": "docker"
}
]
},
"debuggers": [
{
"type": "docker",
Expand Down Expand Up @@ -183,4 +199,4 @@
"dockerfile_lint": "^0.2.4",
"dockerode": "^2.3.0"
}
}
}

0 comments on commit d166a15

Please sign in to comment.