Skip to content

Commit 9075b63

Browse files
fixed code issue while create component from root workspace (#2760)
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 <[email protected]> Co-authored-by: Denis Golovin <[email protected]>
1 parent fb2d5b8 commit 9075b63

File tree

3 files changed

+48
-37
lines changed

3 files changed

+48
-37
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ src/webview/create-service
1515
src/webview/devfile-registry
1616
src/webview/welcome
1717
src/webview/git-import
18+
test/sandbox-registration

src/openshift/component.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -497,14 +497,12 @@ export class Component extends OpenShiftItem {
497497
let createStarter: string;
498498
let componentType: ComponentTypeAdapter;
499499
let componentTypeCandidates: ComponentTypeAdapter[];
500-
if (!useExistingDevfile && (!opts.devFilePath || opts.devFilePath.length === 0)) {
500+
if (!useExistingDevfile && (!opts || !opts.devFilePath || opts.devFilePath.length === 0)) {
501501
const componentTypes = await Component.odo.getComponentTypes();
502-
if (!opts.componentTypeName && !opts.projectName) {
503-
progressIndicator.busy = true;
504-
progressIndicator.placeholder = opts.componentTypeName ? `Checking if '${opts.componentTypeName}' Component type is available` : 'Loading available Component types';
505-
progressIndicator.show();
506-
}
507-
if (opts.componentTypeName) {
502+
progressIndicator.busy = true;
503+
progressIndicator.placeholder = opts?.componentTypeName ? `Checking if '${opts.componentTypeName}' Component type is available` : 'Loading available Component types';
504+
progressIndicator.show();
505+
if (opts?.componentTypeName) {
508506
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);
509507
if (componentTypeCandidates?.length === 0) {
510508
componentType = await window.showQuickPick(componentTypes.sort(ascDevfileFirst), { placeHolder: `Cannot find Component type '${opts.componentTypeName}', select one below to use instead`, ignoreFocusOut: true });
@@ -526,7 +524,7 @@ export class Component extends OpenShiftItem {
526524
const paths = globby.sync(`${globbyPath}*`, { dot: true, onlyFiles: false });
527525
progressIndicator.hide();
528526
if (paths.length === 0 && !isGitImportCall) {
529-
if (opts.projectName) {
527+
if (opts?.projectName) {
530528
createStarter = opts.projectName;
531529
} else {
532530
progressIndicator.placeholder = 'Loading Starter Projects for selected Component Type'
@@ -558,7 +556,7 @@ export class Component extends OpenShiftItem {
558556
}
559557
}
560558

561-
const componentName = opts.compName || await Component.getName(
559+
const componentName = opts?.compName || await Component.getName(
562560
'Name',
563561
Promise.resolve([]),
564562
initialNameValue?.trim().length > 0 ? initialNameValue : createStarter
@@ -583,7 +581,7 @@ export class Component extends OpenShiftItem {
583581
folder,
584582
createStarter,
585583
useExistingDevfile,
586-
opts.devFilePath,
584+
opts?.devFilePath,
587585
notification
588586
)
589587
);

src/util/workspace.ts

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Licensed under the MIT License. See LICENSE file in the project root for license information.
44
*-----------------------------------------------------------------------------------------------*/
55

6-
import { workspace, QuickPickItem, window, Uri } from 'vscode';
6+
import { workspace, QuickPickItem, window, Uri, WorkspaceFolder } from 'vscode';
77
import { Platform } from './platform';
88

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

21+
function isFile(path: string) {
22+
try {
23+
return fs.statSync(path).isFile()
24+
} catch (ignore) {
25+
return false;
26+
}
27+
}
28+
29+
function isComponent(folder: WorkspaceFolder) {
30+
return !isFile(path.join(folder.uri.fsPath, 'devfile.yaml'))
31+
&& !isFile(path.join(folder.uri.fsPath, '.devfile.yaml'));
32+
}
33+
34+
function isComponentFilter(wsFolder: WorkspaceFolder) {
35+
return isComponent(wsFolder);
36+
}
37+
38+
function createWorkspaceFolderItem(wsFolder: WorkspaceFolder) {
39+
return {
40+
label: `$(file-directory) ${wsFolder.uri.fsPath}`,
41+
uri: wsFolder.uri,
42+
};
43+
}
44+
2145
export async function selectWorkspaceFolder(): Promise<Uri> {
22-
let folder: WorkspaceFolderItem[] = [];
46+
let folders: WorkspaceFolderItem[] = [];
2347
if (workspace.workspaceFolders && workspace.workspaceFolders.length > 0) {
24-
folder = workspace.workspaceFolders
25-
.filter((value) => {
26-
let odoDevfile = true;
27-
try {
28-
odoDevfile = !fs.statSync(path.join(value.uri.fsPath, 'devfile.yaml')).isFile()
29-
} catch (ignore) {
30-
// ignore errors if file does not exist
31-
}
32-
let odoDotDevfile = true;
33-
try {
34-
odoDotDevfile = !fs.statSync(path.join(value.uri.fsPath, '.devfile.yaml')).isFile();
35-
} catch (ignore) {
36-
// ignore errors if file does not exist
37-
}
38-
// if there is no devfile.yaml and no .devfile.yaml in the root of workspace folder
39-
return !odoDevfile && !odoDotDevfile;
40-
})
41-
.map((wsFolder) => ({
42-
label: `$(file-directory) ${wsFolder.uri.fsPath}`,
43-
uri: wsFolder.uri,
44-
}));
48+
folders = workspace.workspaceFolders.filter(isComponentFilter).map(createWorkspaceFolderItem);
49+
}
50+
51+
let choice:WorkspaceFolderItem | QuickPickItem;
52+
53+
if (folders.length === 1 && workspace.workspaceFolders.length === 1) {
54+
choice = folders.pop()
55+
} else {
56+
choice = await window.showQuickPick(
57+
[AddWorkspaceFolder, ...folders],
58+
{placeHolder: 'Select context folder', ignoreFocusOut: true}
59+
);
4560
}
46-
const choice = await window.showQuickPick([AddWorkspaceFolder, ...folder], {
47-
placeHolder: 'Select context folder',
48-
ignoreFocusOut: true,
49-
});
61+
5062
if (!choice) return null;
5163

5264
let workspacePath: Uri;

0 commit comments

Comments
 (0)