Skip to content

Commit

Permalink
fixed code issue while create component from root workspace (#2760)
Browse files Browse the repository at this point in the history
This fix removes the context folder selection step in case of
folder opened in workspace and use it as context.

If workspace has only one none component folder added to it
it is used as context.

If workspace has more than one folder added and only one is not
a component the context folder selection step is still
showed to the user.

Signed-off-by: msivasubramaniaan <msivasub@redhat.com>
Co-authored-by: Denis Golovin <dgolovin@redhat.com>
  • Loading branch information
msivasubramaniaan and dgolovin authored Feb 14, 2023
1 parent fb2d5b8 commit 9075b63
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 37 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ src/webview/create-service
src/webview/devfile-registry
src/webview/welcome
src/webview/git-import
test/sandbox-registration
18 changes: 8 additions & 10 deletions src/openshift/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -497,14 +497,12 @@ export class Component extends OpenShiftItem {
let createStarter: string;
let componentType: ComponentTypeAdapter;
let componentTypeCandidates: ComponentTypeAdapter[];
if (!useExistingDevfile && (!opts.devFilePath || opts.devFilePath.length === 0)) {
if (!useExistingDevfile && (!opts || !opts.devFilePath || opts.devFilePath.length === 0)) {
const componentTypes = await Component.odo.getComponentTypes();
if (!opts.componentTypeName && !opts.projectName) {
progressIndicator.busy = true;
progressIndicator.placeholder = opts.componentTypeName ? `Checking if '${opts.componentTypeName}' Component type is available` : 'Loading available Component types';
progressIndicator.show();
}
if (opts.componentTypeName) {
progressIndicator.busy = true;
progressIndicator.placeholder = opts?.componentTypeName ? `Checking if '${opts.componentTypeName}' Component type is available` : 'Loading available Component types';
progressIndicator.show();
if (opts?.componentTypeName) {
componentTypeCandidates = opts.registryName && opts.registryName.length > 0 ? componentTypes.filter(type => type.name === opts.componentTypeName && type.registryName === opts.registryName) : componentTypes.filter(type => type.name === opts.componentTypeName);
if (componentTypeCandidates?.length === 0) {
componentType = await window.showQuickPick(componentTypes.sort(ascDevfileFirst), { placeHolder: `Cannot find Component type '${opts.componentTypeName}', select one below to use instead`, ignoreFocusOut: true });
Expand All @@ -526,7 +524,7 @@ export class Component extends OpenShiftItem {
const paths = globby.sync(`${globbyPath}*`, { dot: true, onlyFiles: false });
progressIndicator.hide();
if (paths.length === 0 && !isGitImportCall) {
if (opts.projectName) {
if (opts?.projectName) {
createStarter = opts.projectName;
} else {
progressIndicator.placeholder = 'Loading Starter Projects for selected Component Type'
Expand Down Expand Up @@ -558,7 +556,7 @@ export class Component extends OpenShiftItem {
}
}

const componentName = opts.compName || await Component.getName(
const componentName = opts?.compName || await Component.getName(
'Name',
Promise.resolve([]),
initialNameValue?.trim().length > 0 ? initialNameValue : createStarter
Expand All @@ -583,7 +581,7 @@ export class Component extends OpenShiftItem {
folder,
createStarter,
useExistingDevfile,
opts.devFilePath,
opts?.devFilePath,
notification
)
);
Expand Down
66 changes: 39 additions & 27 deletions src/util/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Licensed under the MIT License. See LICENSE file in the project root for license information.
*-----------------------------------------------------------------------------------------------*/

import { workspace, QuickPickItem, window, Uri } from 'vscode';
import { workspace, QuickPickItem, window, Uri, WorkspaceFolder } from 'vscode';
import { Platform } from './platform';

import path = require('path');
Expand All @@ -18,35 +18,47 @@ export const AddWorkspaceFolder: QuickPickItem = {
description: 'Folder which does not have an OpenShift context',
};

function isFile(path: string) {
try {
return fs.statSync(path).isFile()
} catch (ignore) {
return false;
}
}

function isComponent(folder: WorkspaceFolder) {
return !isFile(path.join(folder.uri.fsPath, 'devfile.yaml'))
&& !isFile(path.join(folder.uri.fsPath, '.devfile.yaml'));
}

function isComponentFilter(wsFolder: WorkspaceFolder) {
return isComponent(wsFolder);
}

function createWorkspaceFolderItem(wsFolder: WorkspaceFolder) {
return {
label: `$(file-directory) ${wsFolder.uri.fsPath}`,
uri: wsFolder.uri,
};
}

export async function selectWorkspaceFolder(): Promise<Uri> {
let folder: WorkspaceFolderItem[] = [];
let folders: WorkspaceFolderItem[] = [];
if (workspace.workspaceFolders && workspace.workspaceFolders.length > 0) {
folder = workspace.workspaceFolders
.filter((value) => {
let odoDevfile = true;
try {
odoDevfile = !fs.statSync(path.join(value.uri.fsPath, 'devfile.yaml')).isFile()
} catch (ignore) {
// ignore errors if file does not exist
}
let odoDotDevfile = true;
try {
odoDotDevfile = !fs.statSync(path.join(value.uri.fsPath, '.devfile.yaml')).isFile();
} catch (ignore) {
// ignore errors if file does not exist
}
// if there is no devfile.yaml and no .devfile.yaml in the root of workspace folder
return !odoDevfile && !odoDotDevfile;
})
.map((wsFolder) => ({
label: `$(file-directory) ${wsFolder.uri.fsPath}`,
uri: wsFolder.uri,
}));
folders = workspace.workspaceFolders.filter(isComponentFilter).map(createWorkspaceFolderItem);
}

let choice:WorkspaceFolderItem | QuickPickItem;

if (folders.length === 1 && workspace.workspaceFolders.length === 1) {
choice = folders.pop()
} else {
choice = await window.showQuickPick(
[AddWorkspaceFolder, ...folders],
{placeHolder: 'Select context folder', ignoreFocusOut: true}
);
}
const choice = await window.showQuickPick([AddWorkspaceFolder, ...folder], {
placeHolder: 'Select context folder',
ignoreFocusOut: true,
});

if (!choice) return null;

let workspacePath: Uri;
Expand Down

0 comments on commit 9075b63

Please sign in to comment.