2018-08-17 03:17:28 +03:00
|
|
|
/*---------------------------------------------------------------------------------------------
|
|
|
|
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
|
|
* Licensed under the MIT License. See LICENSE.md in the project root for license information.
|
|
|
|
*--------------------------------------------------------------------------------------------*/
|
|
|
|
|
2016-12-08 22:05:00 +03:00
|
|
|
import * as path from "path";
|
|
|
|
import * as vscode from "vscode";
|
2017-09-28 05:46:01 +03:00
|
|
|
import { DOCKERFILE_GLOB_PATTERN } from '../dockerExtension';
|
2018-08-10 01:26:31 +03:00
|
|
|
import { ext } from "../extensionVariables";
|
2018-07-18 02:42:00 +03:00
|
|
|
import { reporter } from '../telemetry/telemetry';
|
2017-03-28 03:54:16 +03:00
|
|
|
|
2017-03-04 04:13:14 +03:00
|
|
|
const teleCmdId: string = 'vscode-docker.image.build';
|
2016-09-14 08:20:20 +03:00
|
|
|
|
2017-09-28 05:48:08 +03:00
|
|
|
async function getDockerFileUris(folder: vscode.WorkspaceFolder): Promise<vscode.Uri[]> {
|
|
|
|
return await vscode.workspace.findFiles(new vscode.RelativePattern(folder, DOCKERFILE_GLOB_PATTERN), null, 1000, null);
|
2016-09-14 08:20:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
interface Item extends vscode.QuickPickItem {
|
2016-12-08 22:05:00 +03:00
|
|
|
file: string,
|
|
|
|
path: string
|
2016-09-14 08:20:20 +03:00
|
|
|
}
|
|
|
|
|
2017-09-28 05:48:08 +03:00
|
|
|
function createItem(folder: vscode.WorkspaceFolder, uri: vscode.Uri): Item {
|
|
|
|
let filePath = path.join(".", uri.fsPath.substr(folder.uri.fsPath.length));
|
2016-12-08 22:05:00 +03:00
|
|
|
|
2016-10-20 07:03:04 +03:00
|
|
|
return <Item>{
|
2016-09-14 08:20:20 +03:00
|
|
|
description: null,
|
2016-12-08 22:05:00 +03:00
|
|
|
file: filePath,
|
|
|
|
label: filePath,
|
2017-01-03 02:10:55 +03:00
|
|
|
path: path.dirname(filePath)
|
2016-09-14 08:20:20 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-09-28 05:48:08 +03:00
|
|
|
function computeItems(folder: vscode.WorkspaceFolder, uris: vscode.Uri[]): vscode.QuickPickItem[] {
|
2016-10-20 07:03:04 +03:00
|
|
|
let items: vscode.QuickPickItem[] = [];
|
2018-07-19 19:15:12 +03:00
|
|
|
// tslint:disable-next-line:prefer-for-of // Grandfathered in
|
2016-09-14 08:20:20 +03:00
|
|
|
for (let i = 0; i < uris.length; i++) {
|
2017-09-28 05:48:08 +03:00
|
|
|
items.push(createItem(folder, uris[i]));
|
2016-09-14 08:20:20 +03:00
|
|
|
}
|
|
|
|
return items;
|
|
|
|
}
|
|
|
|
|
2017-09-28 05:48:08 +03:00
|
|
|
async function resolveImageItem(folder: vscode.WorkspaceFolder, dockerFileUri?: vscode.Uri): Promise<Item> {
|
2017-06-06 03:55:05 +03:00
|
|
|
if (dockerFileUri) {
|
2017-09-28 05:48:08 +03:00
|
|
|
return createItem(folder, dockerFileUri);
|
2018-07-14 00:16:56 +03:00
|
|
|
}
|
2017-06-06 03:55:05 +03:00
|
|
|
|
2017-09-28 05:48:08 +03:00
|
|
|
const uris: vscode.Uri[] = await getDockerFileUris(folder);
|
2017-06-06 03:55:05 +03:00
|
|
|
|
2018-07-18 00:12:15 +03:00
|
|
|
if (!uris || uris.length === 0) {
|
2017-06-06 03:55:05 +03:00
|
|
|
vscode.window.showInformationMessage('Couldn\'t find a Dockerfile in your workspace.');
|
|
|
|
return;
|
|
|
|
} else {
|
2017-09-28 05:48:08 +03:00
|
|
|
const res: vscode.QuickPickItem = await vscode.window.showQuickPick(computeItems(folder, uris), { placeHolder: 'Choose Dockerfile to build' });
|
2017-06-06 03:55:05 +03:00
|
|
|
return <Item>res;
|
|
|
|
}
|
|
|
|
|
2016-12-08 22:05:00 +03:00
|
|
|
}
|
2016-10-20 07:03:04 +03:00
|
|
|
|
2018-07-18 02:41:39 +03:00
|
|
|
export async function buildImage(dockerFileUri?: vscode.Uri): Promise<void> {
|
2017-11-09 01:55:45 +03:00
|
|
|
const configOptions: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration('docker');
|
|
|
|
const defaultContextPath = configOptions.get('imageBuildContextPath', '');
|
|
|
|
|
2017-09-28 05:48:08 +03:00
|
|
|
let folder: vscode.WorkspaceFolder;
|
|
|
|
if (vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders.length === 1) {
|
|
|
|
folder = vscode.workspace.workspaceFolders[0];
|
|
|
|
} else {
|
|
|
|
folder = await (<any>vscode).window.showWorkspaceFolderPick();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!folder) {
|
|
|
|
if (!vscode.workspace.workspaceFolders) {
|
2018-01-18 02:32:20 +03:00
|
|
|
vscode.window.showErrorMessage('Docker files can only be built if VS Code is opened on a folder.');
|
2017-09-28 05:48:08 +03:00
|
|
|
} else {
|
2018-01-18 02:32:20 +03:00
|
|
|
vscode.window.showErrorMessage('Docker files can only be built if a workspace folder is picked in VS Code.');
|
2017-09-28 05:48:08 +03:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2017-06-06 03:55:05 +03:00
|
|
|
|
2017-09-28 05:48:08 +03:00
|
|
|
const uri: Item = await resolveImageItem(folder, dockerFileUri);
|
2018-07-18 00:12:15 +03:00
|
|
|
if (!uri) { return; }
|
2017-12-04 13:42:15 +03:00
|
|
|
|
2017-11-09 01:55:45 +03:00
|
|
|
let contextPath: string = uri.path;
|
2018-07-18 00:12:15 +03:00
|
|
|
if (defaultContextPath && defaultContextPath !== '') {
|
2017-11-09 01:55:45 +03:00
|
|
|
contextPath = defaultContextPath;
|
|
|
|
}
|
2017-06-06 03:55:05 +03:00
|
|
|
|
|
|
|
let imageName: string;
|
|
|
|
if (process.platform === 'win32') {
|
|
|
|
imageName = uri.path.split('\\').pop().toLowerCase();
|
|
|
|
} else {
|
|
|
|
imageName = uri.path.split('/').pop().toLowerCase();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (imageName === '.') {
|
2016-12-08 22:05:00 +03:00
|
|
|
if (process.platform === 'win32') {
|
2017-09-28 05:48:08 +03:00
|
|
|
imageName = folder.uri.fsPath.split('\\').pop().toLowerCase();
|
2016-12-08 22:05:00 +03:00
|
|
|
} else {
|
2017-09-28 05:48:08 +03:00
|
|
|
imageName = folder.uri.fsPath.split('/').pop().toLowerCase();
|
2016-12-08 22:05:00 +03:00
|
|
|
}
|
2017-06-06 03:55:05 +03:00
|
|
|
}
|
2016-10-25 02:40:34 +03:00
|
|
|
|
2017-06-06 03:55:05 +03:00
|
|
|
const opt: vscode.InputBoxOptions = {
|
|
|
|
placeHolder: imageName + ':latest',
|
|
|
|
prompt: 'Tag image as...',
|
|
|
|
value: imageName + ':latest'
|
|
|
|
};
|
|
|
|
|
2018-08-10 01:26:31 +03:00
|
|
|
const value: string = await ext.ui.showInputBox(opt);
|
2016-10-25 02:40:34 +03:00
|
|
|
|
2018-08-10 01:26:31 +03:00
|
|
|
const terminal: vscode.Terminal = ext.terminalProvider.createTerminal('Docker');
|
2018-08-09 23:19:42 +03:00
|
|
|
terminal.sendText(`docker build --rm -f "${uri.file}" -t ${value} ${contextPath}`);
|
2017-06-06 03:55:05 +03:00
|
|
|
terminal.show();
|
2017-09-15 04:25:19 +03:00
|
|
|
|
2017-06-06 03:55:05 +03:00
|
|
|
if (reporter) {
|
2017-11-25 07:11:19 +03:00
|
|
|
/* __GDPR__
|
|
|
|
"command" : {
|
|
|
|
"command" : { "classification": "SystemMetaData", "purpose": "FeatureInsight" }
|
|
|
|
}
|
|
|
|
*/
|
2017-06-06 03:55:05 +03:00
|
|
|
reporter.sendTelemetryEvent('command', {
|
|
|
|
command: teleCmdId
|
2016-12-08 22:05:00 +03:00
|
|
|
});
|
2017-06-06 03:55:05 +03:00
|
|
|
}
|
2018-01-18 02:32:20 +03:00
|
|
|
}
|