From b35d0fe3eb0be3aa8ba0740a5da87d05bf2e0195 Mon Sep 17 00:00:00 2001 From: msivasubramaniaan Date: Thu, 15 Dec 2022 19:21:25 +0530 Subject: [PATCH 1/6] show error message content if folder already exists and provide option to select new folder until valid Signed-off-by: msivasubramaniaan --- src/webview/git-import/gitImportLoader.ts | 32 +++++++++++++---------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/src/webview/git-import/gitImportLoader.ts b/src/webview/git-import/gitImportLoader.ts index 65e380147..3f7cc3ccb 100644 --- a/src/webview/git-import/gitImportLoader.ts +++ b/src/webview/git-import/gitImportLoader.ts @@ -26,19 +26,27 @@ let panel: vscode.WebviewPanel; export class Command { @vsCommand('openshift.component.importFromGit') static async createComponent(event: any) { - const workspacePath = await selectWorkspaceFolder(); - const appendedUri = Uri.joinPath(workspacePath, event.projectName); - //const wsFolderLength = workspace?.workspaceFolders?.length || 0; + let alreadyExisits: boolean; + let appendedUri: vscode.Uri; + do { + alreadyExisits = false; + const workspacePath = await selectWorkspaceFolder(); + appendedUri = Uri.joinPath(workspacePath, event.projectName); + if (fs.existsSync(appendedUri.fsPath) && fs.readdirSync(appendedUri.fsPath).length > 0) { + vscode.window.showErrorMessage(`Folder ${appendedUri.fsPath.substring(appendedUri.fsPath.lastIndexOf('\\') + 1)} already exists + on the selected location: ${appendedUri.fsPath.substring(0, appendedUri.fsPath.lastIndexOf('\\'))}`); + alreadyExisits = true; + } + } while (alreadyExisits); panel.webview.postMessage({ action: 'cloneStarted' - }) - //workspace.updateWorkspaceFolders(wsFolderLength, 0, { uri: appendedUri }); - workspace.updateWorkspaceFolders(workspace.workspaceFolders? workspace.workspaceFolders.length : 0 , null, { uri: appendedUri }); + }); + workspace.updateWorkspaceFolders(workspace.workspaceFolders ? workspace.workspaceFolders.length : 0, null, { uri: appendedUri }); await clone(event, appendedUri.fsPath); if (!event.isDevFile) { panel.webview.postMessage({ action: 'start_create_component' - }) + }); try { await Component.createFromRootWorkspaceFolder(appendedUri, undefined, { @@ -243,7 +251,7 @@ function clone(event: any, location: string): Promise { const git = gitExtension.getAPI(1).git.path; // run 'git clone url location' as external process and return location return new Promise((resolve, reject) => cp.exec(`${git} clone ${event.gitURL} ${location}`, (error: cp.ExecException) => error ? - showError(event, location, error.message) : resolve(true))); + showError(event) : resolve(true))); } function validateComponentName(event: any) { @@ -275,16 +283,12 @@ function validateDevFilePath(event: any) { }); } -function showError(event: any, location: string, message: string): void { +function showError(event: any): void { panel.webview.postMessage({ action: event.action, status: false }); - if (message.indexOf('already exists') !== -1) { - vscode.window.showErrorMessage(`Folder already exists on the selected ${location.substring(0, location.lastIndexOf('\\'))}`); - } else { - vscode.window.showErrorMessage('Error occurred while cloning the repository. Please try again.'); - } + vscode.window.showErrorMessage('Error occurred while cloning the repository. Please try again.'); } function isGitURL(host: string): boolean { From 9d18eed5df41ace2491e6c8251d37d569cff8499 Mon Sep 17 00:00:00 2001 From: msivasubramaniaan Date: Sat, 17 Dec 2022 05:24:11 +0530 Subject: [PATCH 2/6] typeo and error message changed Signed-off-by: msivasubramaniaan --- src/webview/git-import/gitImportLoader.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/webview/git-import/gitImportLoader.ts b/src/webview/git-import/gitImportLoader.ts index 3f7cc3ccb..ddac7ca5b 100644 --- a/src/webview/git-import/gitImportLoader.ts +++ b/src/webview/git-import/gitImportLoader.ts @@ -26,18 +26,18 @@ let panel: vscode.WebviewPanel; export class Command { @vsCommand('openshift.component.importFromGit') static async createComponent(event: any) { - let alreadyExisits: boolean; + let alreadyExist: boolean; let appendedUri: vscode.Uri; do { - alreadyExisits = false; + alreadyExist = false; const workspacePath = await selectWorkspaceFolder(); appendedUri = Uri.joinPath(workspacePath, event.projectName); if (fs.existsSync(appendedUri.fsPath) && fs.readdirSync(appendedUri.fsPath).length > 0) { vscode.window.showErrorMessage(`Folder ${appendedUri.fsPath.substring(appendedUri.fsPath.lastIndexOf('\\') + 1)} already exists - on the selected location: ${appendedUri.fsPath.substring(0, appendedUri.fsPath.lastIndexOf('\\'))}`); - alreadyExisits = true; + at the selected location: ${appendedUri.fsPath.substring(0, appendedUri.fsPath.lastIndexOf('\\'))}`); + alreadyExist = true; } - } while (alreadyExisits); + } while (alreadyExist); panel.webview.postMessage({ action: 'cloneStarted' }); @@ -288,7 +288,7 @@ function showError(event: any): void { action: event.action, status: false }); - vscode.window.showErrorMessage('Error occurred while cloning the repository. Please try again.'); + vscode.window.showErrorMessage('Error occurred while cloning the repository. Please click on Analyze Button and Try again.'); } function isGitURL(host: string): boolean { From 9014a3cc781c271052faeca4c02830fe6972284c Mon Sep 17 00:00:00 2001 From: msivasubramaniaan Date: Tue, 20 Dec 2022 18:36:45 +0530 Subject: [PATCH 3/6] restrict the selection of existing workspace folder for creating the component Signed-off-by: msivasubramaniaan --- src/webview/git-import/gitImportLoader.ts | 34 +++++++++++++++++------ 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/src/webview/git-import/gitImportLoader.ts b/src/webview/git-import/gitImportLoader.ts index ddac7ca5b..528816c2c 100644 --- a/src/webview/git-import/gitImportLoader.ts +++ b/src/webview/git-import/gitImportLoader.ts @@ -12,7 +12,6 @@ import { DetectedServiceData, DetectedStrategy, detectImportStrategies } from '. import { ComponentTypesView } from '../../registriesView'; import { ComponentTypeDescription } from '../../odo/componentType'; import { Response } from '../../git-import/types'; -import { Uri, workspace } from 'vscode'; import { Component } from '../../openshift/component'; import OpenShiftItem from '../../openshift/openshiftItem'; import { selectWorkspaceFolder } from '../../util/workspace'; @@ -27,13 +26,16 @@ export class Command { @vsCommand('openshift.component.importFromGit') static async createComponent(event: any) { let alreadyExist: boolean; - let appendedUri: vscode.Uri; + let workspacePath,appendedUri: vscode.Uri; do { alreadyExist = false; - const workspacePath = await selectWorkspaceFolder(); - appendedUri = Uri.joinPath(workspacePath, event.projectName); - if (fs.existsSync(appendedUri.fsPath) && fs.readdirSync(appendedUri.fsPath).length > 0) { - vscode.window.showErrorMessage(`Folder ${appendedUri.fsPath.substring(appendedUri.fsPath.lastIndexOf('\\') + 1)} already exists + workspacePath = await selectWorkspaceFolder(); + appendedUri = vscode.Uri.joinPath(workspacePath, event.projectName); + if (isWorkspaceFolder(workspacePath) && fs.readdirSync(workspacePath.fsPath).length > 0) { + vscode.window.showErrorMessage(`Unable to create Component on Workspace Folder: ${workspacePath}, Please select another folder`); + alreadyExist = true; + } else if (fs.existsSync(appendedUri.fsPath) && fs.readdirSync(appendedUri.fsPath).length > 0) { + vscode.window.showErrorMessage(`Folder ${appendedUri.fsPath.substring(appendedUri.fsPath.lastIndexOf('\\') + 1)} already exist at the selected location: ${appendedUri.fsPath.substring(0, appendedUri.fsPath.lastIndexOf('\\'))}`); alreadyExist = true; } @@ -41,7 +43,9 @@ export class Command { panel.webview.postMessage({ action: 'cloneStarted' }); - workspace.updateWorkspaceFolders(workspace.workspaceFolders ? workspace.workspaceFolders.length : 0, null, { uri: appendedUri }); + if (!isWorkspaceFolder(workspacePath)) { + vscode.workspace.updateWorkspaceFolders(vscode.workspace.workspaceFolders ? vscode.workspace.workspaceFolders.length : 0, null, { uri: appendedUri }); + } await clone(event, appendedUri.fsPath); if (!event.isDevFile) { panel.webview.postMessage({ @@ -80,6 +84,20 @@ export class Command { } } + +function isWorkspaceFolder(uri: vscode.Uri): boolean { + const workspaceFolders = vscode.workspace.workspaceFolders; + if(workspaceFolders && workspaceFolders.length > 0) { + const sameFolder = workspaceFolders.filter(workspaceFolder => workspaceFolder.uri.fsPath === uri.fsPath || uri.fsPath.indexOf(workspaceFolder.uri.fsPath) !== -1); + if (sameFolder && sameFolder.length > 0) { + return true; + } else { + return false; + } + } + return false; +} + async function gitImportMessageListener(event: any): Promise { switch (event?.action) { case 'validateGitURL': @@ -271,7 +289,7 @@ function validateDevFilePath(event: any) { let validationMessage = OpenShiftItem.emptyName(`Required ${event.param}`, event.param.trim()); if (!validationMessage) validationMessage = OpenShiftItem.validateFilePath(`Not matches ^[a-z]:((\/|\\\\)[a-zA-Z0-9_ \\-]+)+\\.yaml$`, event.param); if (!validationMessage && event.param !== 'devfile.yaml' && event.param !== 'devfile.yml') { - const uri = Uri.parse(event.param); + const uri = vscode.Uri.parse(event.param); const devFileLocation = path.join(uri.fsPath); validationMessage = fs.existsSync(devFileLocation) ? null : 'devfile not available on the given path'; } From e9503d673279be580626b777a647bdfebf3b578b Mon Sep 17 00:00:00 2001 From: msivasubramaniaan Date: Fri, 23 Dec 2022 16:37:28 +0530 Subject: [PATCH 4/6] fixed component creation on empty workspace Signed-off-by: msivasubramaniaan --- src/webview/git-import/gitImportLoader.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/webview/git-import/gitImportLoader.ts b/src/webview/git-import/gitImportLoader.ts index 528816c2c..744187f81 100644 --- a/src/webview/git-import/gitImportLoader.ts +++ b/src/webview/git-import/gitImportLoader.ts @@ -43,9 +43,6 @@ export class Command { panel.webview.postMessage({ action: 'cloneStarted' }); - if (!isWorkspaceFolder(workspacePath)) { - vscode.workspace.updateWorkspaceFolders(vscode.workspace.workspaceFolders ? vscode.workspace.workspaceFolders.length : 0, null, { uri: appendedUri }); - } await clone(event, appendedUri.fsPath); if (!event.isDevFile) { panel.webview.postMessage({ @@ -81,6 +78,9 @@ export class Command { }); vscode.window.showInformationMessage('Selected Component added to the workspace.'); } + if (!isWorkspaceFolder(workspacePath)) { + vscode.workspace.updateWorkspaceFolders(vscode.workspace.workspaceFolders ? vscode.workspace.workspaceFolders.length : 0, null, { uri: appendedUri }); + } } } From 9ea39e9b31a4beff5e3bb4a3a8a786518f4f8e09 Mon Sep 17 00:00:00 2001 From: msivasubramaniaan Date: Tue, 3 Jan 2023 13:33:15 +0530 Subject: [PATCH 5/6] used getWorkspaceFolder method and error message changed Signed-off-by: msivasubramaniaan --- src/webview/git-import/gitImportLoader.ts | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/src/webview/git-import/gitImportLoader.ts b/src/webview/git-import/gitImportLoader.ts index 744187f81..1db785f05 100644 --- a/src/webview/git-import/gitImportLoader.ts +++ b/src/webview/git-import/gitImportLoader.ts @@ -27,11 +27,13 @@ export class Command { static async createComponent(event: any) { let alreadyExist: boolean; let workspacePath,appendedUri: vscode.Uri; + let workspaceFolder: vscode.WorkspaceFolder; do { alreadyExist = false; workspacePath = await selectWorkspaceFolder(); appendedUri = vscode.Uri.joinPath(workspacePath, event.projectName); - if (isWorkspaceFolder(workspacePath) && fs.readdirSync(workspacePath.fsPath).length > 0) { + workspaceFolder = vscode.workspace.getWorkspaceFolder(workspacePath); + if (workspaceFolder && fs.readdirSync(workspacePath.fsPath).length > 0) { vscode.window.showErrorMessage(`Unable to create Component on Workspace Folder: ${workspacePath}, Please select another folder`); alreadyExist = true; } else if (fs.existsSync(appendedUri.fsPath) && fs.readdirSync(appendedUri.fsPath).length > 0) { @@ -78,26 +80,13 @@ export class Command { }); vscode.window.showInformationMessage('Selected Component added to the workspace.'); } - if (!isWorkspaceFolder(workspacePath)) { + if (!workspaceFolder) { vscode.workspace.updateWorkspaceFolders(vscode.workspace.workspaceFolders ? vscode.workspace.workspaceFolders.length : 0, null, { uri: appendedUri }); } } } -function isWorkspaceFolder(uri: vscode.Uri): boolean { - const workspaceFolders = vscode.workspace.workspaceFolders; - if(workspaceFolders && workspaceFolders.length > 0) { - const sameFolder = workspaceFolders.filter(workspaceFolder => workspaceFolder.uri.fsPath === uri.fsPath || uri.fsPath.indexOf(workspaceFolder.uri.fsPath) !== -1); - if (sameFolder && sameFolder.length > 0) { - return true; - } else { - return false; - } - } - return false; -} - async function gitImportMessageListener(event: any): Promise { switch (event?.action) { case 'validateGitURL': @@ -268,7 +257,7 @@ function clone(event: any, location: string): Promise { const gitExtension = vscode.extensions.getExtension('vscode.git').exports; const git = gitExtension.getAPI(1).git.path; // run 'git clone url location' as external process and return location - return new Promise((resolve, reject) => cp.exec(`${git} clone ${event.gitURL} ${location}`, (error: cp.ExecException) => error ? + return new Promise((resolve, _reject) => cp.exec(`${git} clone ${event.gitURL} ${location}`, (error: cp.ExecException) => error ? showError(event) : resolve(true))); } @@ -306,7 +295,7 @@ function showError(event: any): void { action: event.action, status: false }); - vscode.window.showErrorMessage('Error occurred while cloning the repository. Please click on Analyze Button and Try again.'); + vscode.window.showErrorMessage('An error occurred while cloning the repository. Please click \'Analyze\' button and try again'); } function isGitURL(host: string): boolean { From 9990ad44ffcc705f940a6b2925284a836920eb21 Mon Sep 17 00:00:00 2001 From: msivasubramaniaan Date: Thu, 5 Jan 2023 21:18:27 +0530 Subject: [PATCH 6/6] restrict component creation under the existing components Signed-off-by: msivasubramaniaan --- src/webview/git-import/gitImportLoader.ts | 52 ++++++++++++++++++++--- 1 file changed, 45 insertions(+), 7 deletions(-) diff --git a/src/webview/git-import/gitImportLoader.ts b/src/webview/git-import/gitImportLoader.ts index 1db785f05..8b77896aa 100644 --- a/src/webview/git-import/gitImportLoader.ts +++ b/src/webview/git-import/gitImportLoader.ts @@ -19,6 +19,9 @@ import jsYaml = require('js-yaml'); import GitUrlParse = require('git-url-parse'); import cp = require('child_process'); import { vsCommand } from '../../vscommand'; +import * as odo3 from '../../odo3'; +import { ComponentDescription } from '../../odo/componentTypeDescription'; +import { ComponentWorkspaceFolder } from '../../odo/workspace'; let panel: vscode.WebviewPanel; @@ -26,15 +29,19 @@ export class Command { @vsCommand('openshift.component.importFromGit') static async createComponent(event: any) { let alreadyExist: boolean; - let workspacePath,appendedUri: vscode.Uri; + let workspacePath: vscode.Uri, appendedUri: vscode.Uri; let workspaceFolder: vscode.WorkspaceFolder; do { alreadyExist = false; workspacePath = await selectWorkspaceFolder(); + if (!workspacePath) { + return null; + } appendedUri = vscode.Uri.joinPath(workspacePath, event.projectName); workspaceFolder = vscode.workspace.getWorkspaceFolder(workspacePath); - if (workspaceFolder && fs.readdirSync(workspacePath.fsPath).length > 0) { - vscode.window.showErrorMessage(`Unable to create Component on Workspace Folder: ${workspacePath}, Please select another folder`); + const isExistingComponent = await existingComponent(workspacePath); + if (isExistingComponent) { + vscode.window.showErrorMessage(`Unable to create Component inside an existing Component: ${workspacePath}, Please select another folder`); alreadyExist = true; } else if (fs.existsSync(appendedUri.fsPath) && fs.readdirSync(appendedUri.fsPath).length > 0) { vscode.window.showErrorMessage(`Folder ${appendedUri.fsPath.substring(appendedUri.fsPath.lastIndexOf('\\') + 1)} already exist @@ -86,7 +93,6 @@ export class Command { } } - async function gitImportMessageListener(event: any): Promise { switch (event?.action) { case 'validateGitURL': @@ -210,8 +216,8 @@ function validateGitURL(event: any) { try { const parse = GitUrlParse(event.param); const isGitRepo = isGitURL(parse.host); - if(!isGitRepo) { - throw 'Invalid Git URL'; + if (!isGitRepo) { + throw 'Invalid Git URL'; } if (parse.organization !== '' && parse.name !== '') { panel.webview.postMessage({ @@ -299,6 +305,38 @@ function showError(event: any): void { } function isGitURL(host: string): boolean { - return ['github.com','bitbucket.org','gitlab.com'].includes(host); + return ['github.com', 'bitbucket.org', 'gitlab.com'].includes(host); +} + +async function existingComponent(uri: vscode.Uri): Promise { + let worksapceFolder = vscode.workspace.getWorkspaceFolder(uri); + if (worksapceFolder?.uri) { + const workspaceSubDirs = fs.readdirSync(worksapceFolder.uri.fsPath, { withFileTypes: true }) + .filter(dirent => dirent.isDirectory()) + .map(dirent => dirent.name).map((subDir) => vscode.Uri.joinPath(worksapceFolder.uri, subDir)); + const components = await getComponents(workspaceSubDirs); + if (components?.length > 0) { + const sameFolder = components.filter(component => component.contextPath === uri.fsPath || uri.fsPath.indexOf(component.contextPath) !== -1); + if (sameFolder && sameFolder.length > 0) { + return true; + } else { + return false; + } + } + } + return false; } +async function getComponents(folders: vscode.Uri[]): Promise { + const descriptions = folders.map( + (folder) => odo3.newInstance().describeComponent(folder.fsPath) + .then((component: ComponentDescription) => { + return { + contextPath: folder.fsPath, + component + } + }) + ); + const results = await Promise.all(descriptions); + return results.filter((compFolder) => !!compFolder.component); +}