From 7ccfcf6ec731c9375b0955edc63c912ccab1dca2 Mon Sep 17 00:00:00 2001 From: Shivam Sourav Jha <60891544+shivamsouravjha@users.noreply.github.com> Date: Wed, 25 Sep 2024 17:48:25 +0530 Subject: [PATCH] add: autodetect project language (#61) Signed-off-by: shivamsouravjha <2019145@iiitdmj.ac.in> Co-authored-by: Aditya Sharma <70089590+Aditya-eddy@users.noreply.github.com> --- src/SidebarProvider.ts | 91 +++++++++++++++------- webviews/components/IntegrationTest.svelte | 72 ++++++++++++----- 2 files changed, 116 insertions(+), 47 deletions(-) diff --git a/src/SidebarProvider.ts b/src/SidebarProvider.ts index 89353df..71cec65 100644 --- a/src/SidebarProvider.ts +++ b/src/SidebarProvider.ts @@ -6,9 +6,41 @@ import { startRecording, stopRecording } from "./Record"; import { startTesting, stopTesting, displayTestCases, displayPreviousTestResults } from "./Test"; import { existsSync } from "fs"; import { handleInitializeKeployConfigFile, handleOpenKeployConfigFile } from "./Config"; -import SignIn from "./SignIn"; import SignInWithGitHub from "./SignIn"; import oneClickInstall from './OneClickInstall'; +import * as path from 'path'; +import * as fs from 'fs'; +import { workspace } from 'vscode'; + +function precheckFunction(): Promise { + const workspacePath = workspace.workspaceFolders ? workspace.workspaceFolders[0].uri.fsPath : ''; + + return new Promise((resolve, reject) => { + try { + if (!workspacePath) { + return reject('Workspace path not found.'); + } + + const pomFilePath = path.join(workspacePath, 'pom.xml'); + const goModFilePath = path.join(workspacePath, 'go.mod'); + const packageJsonFilePath = path.join(workspacePath, 'package.json'); + + let projectType: string = 'python'; // Default project type is python + if (fs.existsSync(pomFilePath)) { + projectType = 'java'; + } else if (fs.existsSync(goModFilePath)) { + projectType = 'go'; + } else if (fs.existsSync(packageJsonFilePath)) { + projectType = 'javascript'; + } + resolve(projectType); + } catch (error) { + reject(`Error checking project files: ${(error as Error).message}`); + } + }); +} + + const recordOptions: vscode.OpenDialogOptions = { canSelectFolders: true, @@ -30,7 +62,7 @@ export class SidebarProvider implements vscode.WebviewViewProvider { _doc?: vscode.TextDocument; _interval?: NodeJS.Timeout; // Store the interval reference - constructor(private readonly _extensionUri: vscode.Uri , private readonly _context: vscode.ExtensionContext) { + constructor(private readonly _extensionUri: vscode.Uri, private readonly _context: vscode.ExtensionContext) { } public postMessage(type: any, value: any) { @@ -58,7 +90,7 @@ export class SidebarProvider implements vscode.WebviewViewProvider { const apiResponse = this._context.globalState.get('apiResponse') || "No response"; const signedIn = this._context.globalState.get('SignedOthers') || "false"; - console.log("signedIn others value" , signedIn); + console.log("signedIn others value", signedIn); let scriptUri = webviewView.webview.asWebviewUri( @@ -70,13 +102,13 @@ export class SidebarProvider implements vscode.WebviewViewProvider { webviewView.webview.html = this._getHtmlForWebview(webviewView.webview, compiledCSSUri, scriptUri); - - this._sendApiResponseToWebview(apiResponse,signedIn); + + this._sendApiResponseToWebview(apiResponse, signedIn); // Start sending the updated `apiResponse` to the webview every 3 seconds this._startApiResponseUpdates(); -; + ; @@ -307,16 +339,16 @@ export class SidebarProvider implements vscode.WebviewViewProvider { case "signinwithstate": { try { - await vscode.commands.executeCommand('keploy.SignInWithOthers'); + await vscode.commands.executeCommand('keploy.SignInWithOthers'); } catch (error) { - console.error('Error while signing in:', error); - vscode.window.showErrorMessage('Failed to sign in. Please try again.'); + console.error('Error while signing in:', error); + vscode.window.showErrorMessage('Failed to sign in. Please try again.'); } break; - } + } + + case "openLink": { - case "openLink":{ - try { console.log("Opening external link: " + data.url); vscode.env.openExternal(vscode.Uri.parse(data.url)); @@ -427,30 +459,33 @@ export class SidebarProvider implements vscode.WebviewViewProvider { } break; } - // case "signIn": { - // if (!data.value) { - // return; - // } - // try { - // console.log('Signing in...'); - // const response: any = await SignIn(); - // console.log('Response from SignIn', response); - // } catch (error) { - // this._view?.webview.postMessage({ type: 'error', value: `Failed to sign in ${error}` }); - // } - // break; - // } + case "detectProjectType": { + try { + console.log('Detecting Project Type...'); + precheckFunction() + .then(projectType => { + console.log("Project type detected:", projectType); + this._view?.webview.postMessage({ type: 'projectDetected', projectType: projectType }); + }) + .catch(error => { + console.error("Error detecting project type:", error); + }); + } catch (error) { + console.log('Error in detecting project type', error); + } + break; + } } }); } - private _startApiResponseUpdates() { + private _startApiResponseUpdates() { this._interval = setInterval(() => { const apiResponse = this._context.globalState.get('apiResponse') || "No response"; const signedIn = this._context.globalState.get('SignedOthers') || "false"; - this._sendApiResponseToWebview(apiResponse , signedIn); + this._sendApiResponseToWebview(apiResponse, signedIn); }, 3000); // 3 seconds } @@ -462,7 +497,7 @@ export class SidebarProvider implements vscode.WebviewViewProvider { } // Helper function to send `apiResponse` to the webview - private _sendApiResponseToWebview(apiResponse: string , signedIn:string) { + private _sendApiResponseToWebview(apiResponse: string, signedIn: string) { if (this._view) { // console.log("api response withing 3 seconds" , apiResponse); this._view.webview.postMessage({ diff --git a/webviews/components/IntegrationTest.svelte b/webviews/components/IntegrationTest.svelte index e31c90e..df8dbd9 100644 --- a/webviews/components/IntegrationTest.svelte +++ b/webviews/components/IntegrationTest.svelte @@ -9,12 +9,34 @@ let initialiseConfigButton; const vscode = acquireVsCodeApi(); + let isPrecheckDone = false; + let projectType = ""; + function handleProjectTypeDetection(event) { + if (event.data && event.data.type === 'projectDetected') { + projectType = event.data.projectType; + console.log("Project type detected:", projectType); + isPrecheckDone = true; + } + } + onMount(() => { + vscode.postMessage({ + type: 'detectProjectType', + }); vscode.postMessage({ type: "openConfigFile", value: `/keploy.yml`, }); - + if (!window.hasProjectTypeListener) { + window.addEventListener('message', handleProjectTypeDetection); + window.hasProjectTypeListener = true; // Mark that the listener has been added + } + return () => { + if (window.hasProjectTypeListener) { + window.removeEventListener('message', handleProjectTypeDetection); + window.hasProjectTypeListener = false; + } + }; }); @@ -74,27 +96,39 @@ id="configCommand" />
-
-
- -

go run main.go

-
-
- -

npm run start

-
-
-
-
- -

Python3 main.py

+
+ {#if projectType === 'go'} +
+ +

go run main.go

+
+ {/if} + + {#if projectType === 'javascript'} +
+ +

npm run start

+
+ {/if}
-
- -

java xyz.jar

+ +
+ {#if projectType === 'python'} +
+ +

Python3 main.py

+
+ {/if} + + {#if projectType === 'java'} +
+ +

java xyz.jar

+
+ {/if}
-
+