Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
bpasero committed Sep 21, 2017
1 parent 871369d commit e46610d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 50 deletions.
39 changes: 18 additions & 21 deletions commands/build-image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,20 @@ import { DOCKERFILE_GLOB_PATTERN, dockerExplorerProvider } from '../dockerExtens

const teleCmdId: string = 'vscode-docker.image.build';

function hasWorkspaceFolder(): boolean {
return vscode.workspace.rootPath ? true : false;
}

async function getDockerFileUris(): Promise<vscode.Uri[]> {
if (!hasWorkspaceFolder()) {
async function getDockerFileUris(folder?: vscode.WorkspaceFolder): Promise<vscode.Uri[]> {
if (!folder) {
return;
}
return await vscode.workspace.findFiles(DOCKERFILE_GLOB_PATTERN, null, 1000, null);
return await vscode.workspace.findFiles(DOCKERFILE_GLOB_PATTERN, null, 1000, null); // TODO@Ben leverage relative pattern support
}

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

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

return <Item>{
description: null,
Expand All @@ -32,35 +28,36 @@ function createItem(uri: vscode.Uri): Item {
};
}

function computeItems(uris: vscode.Uri[]): vscode.QuickPickItem[] {
function computeItems(uris: vscode.Uri[], folder?: vscode.WorkspaceFolder): vscode.QuickPickItem[] {
let items: vscode.QuickPickItem[] = [];
for (let i = 0; i < uris.length; i++) {
items.push(createItem(uris[i]));
items.push(createItem(uris[i], folder));
}
return items;
}

async function resolveImageItem(dockerFileUri?: vscode.Uri): Promise<Item> {
async function resolveImageItem(dockerFileUri?: vscode.Uri, folder?: vscode.WorkspaceFolder): Promise<Item> {

if (dockerFileUri) {
return createItem(dockerFileUri);
return createItem(dockerFileUri, folder);
};

const uris: vscode.Uri[] = await getDockerFileUris();
const uris: vscode.Uri[] = await getDockerFileUris(folder);

if (!uris || uris.length == 0) {
vscode.window.showInformationMessage('Couldn\'t find a Dockerfile in your workspace.');
return;
} else {
const res: vscode.QuickPickItem = await vscode.window.showQuickPick(computeItems(uris), {placeHolder: 'Choose Dockerfile to build'});
const res: vscode.QuickPickItem = await vscode.window.showQuickPick(computeItems(uris, folder), { placeHolder: 'Choose Dockerfile to build' });
return <Item>res;
}

}

export async function buildImage(dockerFileUri?: vscode.Uri) {

const uri: Item = await resolveImageItem(dockerFileUri);
export async function buildImage(dockerFileUri?: vscode.Uri, ) {
// TODO need the workspace folder picker here
let folder: vscode.WorkspaceFolder = void 0;
const uri: Item = await resolveImageItem(dockerFileUri, folder);

if (!uri) return;

Expand All @@ -73,9 +70,9 @@ export async function buildImage(dockerFileUri?: vscode.Uri) {

if (imageName === '.') {
if (process.platform === 'win32') {
imageName = vscode.workspace.rootPath.split('\\').pop().toLowerCase();
imageName = folder.uri.fsPath.split('\\').pop().toLowerCase();
} else {
imageName = vscode.workspace.rootPath.split('/').pop().toLowerCase();
imageName = folder.uri.fsPath.split('/').pop().toLowerCase();
}
}

Expand All @@ -94,7 +91,7 @@ export async function buildImage(dockerFileUri?: vscode.Uri) {
terminal.show();

dockerExplorerProvider.refreshImages(true);

if (reporter) {
reporter.sendTelemetryEvent('command', {
command: teleCmdId
Expand Down
28 changes: 14 additions & 14 deletions commands/docker-compose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,8 @@ import { COMPOSE_FILE_GLOB_PATTERN, dockerExplorerProvider } from '../dockerExte
import { reporter } from '../telemetry/telemetry';
const teleCmdId: string = 'vscode-docker.compose.'; // we append up or down when reporting telemetry

function hasWorkspaceFolder(): boolean {
return vscode.workspace.rootPath ? true : false;
}

async function getDockerComposeFileUris(): Promise<vscode.Uri[]> {
if (!hasWorkspaceFolder()) {
return;
}
async function getDockerComposeFileUris(folder: vscode.WorkspaceFolder): Promise<vscode.Uri[]> {
// TODO@Ben use relative pattern support
return await vscode.workspace.findFiles(COMPOSE_FILE_GLOB_PATTERN, null, 9999, null);
}

Expand All @@ -20,8 +14,8 @@ interface Item extends vscode.QuickPickItem {
file: string
}

function createItem(uri: vscode.Uri): Item {
const filePath = hasWorkspaceFolder() ? path.join('.', uri.fsPath.substr(vscode.workspace.rootPath.length)) : uri.fsPath;
function createItem(uri: vscode.Uri, folder?: vscode.WorkspaceFolder): Item {
const filePath = folder ? path.join('.', uri.fsPath.substr(folder.uri.fsPath.length)) : uri.fsPath;

return <Item>{
description: null,
Expand All @@ -31,21 +25,27 @@ function createItem(uri: vscode.Uri): Item {
};
}

function computeItems(uris: vscode.Uri[]): vscode.QuickPickItem[] {
function computeItems(uris: vscode.Uri[], folder?: vscode.WorkspaceFolder): vscode.QuickPickItem[] {
const items: vscode.QuickPickItem[] = [];
for (let i = 0; i < uris.length; i++) {
items.push(createItem(uris[i]));
items.push(createItem(uris[i], folder));
}
return items;
}

export async function compose(command: string, message: string) {
// TODO@Ben need the workspace folder picker here
let folder: vscode.WorkspaceFolder = void 0;
if (!folder) {
vscode.window.showErrorMessage('Docker files can only be generated if VS Code is opened on a folder.');
return;
}

const uris: vscode.Uri[] = await getDockerComposeFileUris();
const uris: vscode.Uri[] = await getDockerComposeFileUris(folder);
if (!uris || uris.length == 0) {
vscode.window.showInformationMessage('Couldn\'t find any docker-compose file in your workspace.');
} else {
const items: vscode.QuickPickItem[] = computeItems(uris);
const items: vscode.QuickPickItem[] = computeItems(uris, folder);
const selectedItem: Item = <Item>await vscode.window.showQuickPick(items, { placeHolder: `Choose Docker Compose file ${message}` });
if (selectedItem) {
const terminal: vscode.Terminal = vscode.window.createTerminal('Docker Compose');
Expand Down
25 changes: 10 additions & 15 deletions configureWorkspace/configure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,21 +217,14 @@ interface PackageJson {
version: string
}

function hasWorkspaceFolder(): boolean {
return vscode.workspace.rootPath ? true : false;
}

async function getPackageJson(): Promise<vscode.Uri[]> {
if (!hasWorkspaceFolder()) {
return;
}

async function getPackageJson(folder: vscode.WorkspaceFolder): Promise<vscode.Uri[]> {
// TODO@Ben use relative pattern support
return vscode.workspace.findFiles('package.json', null, 1, null);
}

async function readPackageJson(): Promise<PackageJson> {
async function readPackageJson(folder: vscode.WorkspaceFolder): Promise<PackageJson> {
// open package.json and look for main, scripts start
const uris: vscode.Uri[] = await getPackageJson();
const uris: vscode.Uri[] = await getPackageJson(folder);
var pkg: PackageJson = {
npmStart: true,
fullCommand: 'npm start',
Expand Down Expand Up @@ -286,7 +279,9 @@ const YES_OR_NO_PROMPT: vscode.MessageItem[] = [
];

export async function configure(): Promise<void> {
if (!hasWorkspaceFolder()) {
// TODO need the workspace folder picker here
let folder: vscode.WorkspaceFolder = void 0;
if (!folder) {
vscode.window.showErrorMessage('Docker files can only be generated if VS Code is opened on a folder.');
return;
}
Expand All @@ -297,8 +292,8 @@ export async function configure(): Promise<void> {
const port = await promptForPort();
if (!port) return;

const serviceName = path.basename(vscode.workspace.rootPath).toLowerCase();
const pkg = await readPackageJson();
const serviceName = path.basename(folder.uri.fsPath).toLowerCase();
const pkg = await readPackageJson(folder);

await Promise.all(Object.keys(DOCKER_FILE_TYPES).map((fileName) => {
return createWorkspaceFileIfNotExists(fileName, DOCKER_FILE_TYPES[fileName]);
Expand All @@ -310,7 +305,7 @@ export async function configure(): Promise<void> {
});

async function createWorkspaceFileIfNotExists(fileName, writerFunction) {
const workspacePath = path.join(vscode.workspace.rootPath, fileName);
const workspacePath = path.join(folder.uri.fsPath, fileName);
if (fs.existsSync(workspacePath)) {
const item: vscode.MessageItem = await vscode.window.showErrorMessage(`A ${fileName} already exists. Would you like to override it?`, ...YES_OR_NO_PROMPT);
if (item.title.toLowerCase() === 'yes') {
Expand Down

0 comments on commit e46610d

Please sign in to comment.