Skip to content
54 changes: 38 additions & 16 deletions src/webview/git-import/gitImportLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -26,19 +25,32 @@ 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 alreadyExist: boolean;
let workspacePath,appendedUri: vscode.Uri;
do {
alreadyExist = false;
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`);
Comment thread
dgolovin marked this conversation as resolved.
Outdated
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;
}
} while (alreadyExist);
panel.webview.postMessage({
action: 'cloneStarted'
})
//workspace.updateWorkspaceFolders(wsFolderLength, 0, { uri: appendedUri });
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 });
Comment thread
msivasubramaniaan marked this conversation as resolved.
Outdated
}
await clone(event, appendedUri.fsPath);
if (!event.isDevFile) {
panel.webview.postMessage({
action: 'start_create_component'
})
});
try {
await Component.createFromRootWorkspaceFolder(appendedUri, undefined,
{
Expand Down Expand Up @@ -72,6 +84,20 @@ export class Command {
}
}


function isWorkspaceFolder(uri: vscode.Uri): boolean {
Comment thread
msivasubramaniaan marked this conversation as resolved.
Outdated
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<any> {
switch (event?.action) {
case 'validateGitURL':
Expand Down Expand Up @@ -243,7 +269,7 @@ function clone(event: any, location: string): Promise<any> {
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) {
Expand All @@ -263,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';
}
Expand All @@ -275,16 +301,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 click on Analyze Button and Try again.');
Comment thread
msivasubramaniaan marked this conversation as resolved.
Outdated
}

function isGitURL(host: string): boolean {
Expand Down