Skip to content

Commit

Permalink
add: autodetect project language (#61)
Browse files Browse the repository at this point in the history
Signed-off-by: shivamsouravjha <[email protected]>
Co-authored-by: Aditya Sharma <[email protected]>
  • Loading branch information
shivamsouravjha and Aditya-eddy authored Sep 25, 2024
1 parent 3b37bab commit 7ccfcf6
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 47 deletions.
91 changes: 63 additions & 28 deletions src/SidebarProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> {
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,
Expand All @@ -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) {
Expand Down Expand Up @@ -58,7 +90,7 @@ export class SidebarProvider implements vscode.WebviewViewProvider {
const apiResponse = this._context.globalState.get<string>('apiResponse') || "No response";
const signedIn = this._context.globalState.get<string>('SignedOthers') || "false";

console.log("signedIn others value" , signedIn);
console.log("signedIn others value", signedIn);


let scriptUri = webviewView.webview.asWebviewUri(
Expand All @@ -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();
;
;



Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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<string>('apiResponse') || "No response";
const signedIn = this._context.globalState.get<string>('SignedOthers') || "false";

this._sendApiResponseToWebview(apiResponse , signedIn);
this._sendApiResponseToWebview(apiResponse, signedIn);
}, 3000); // 3 seconds
}

Expand All @@ -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({
Expand Down
72 changes: 53 additions & 19 deletions webviews/components/IntegrationTest.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
};
});
Expand Down Expand Up @@ -74,27 +96,39 @@
id="configCommand"
/>
<div class="language-icons">
<div class="language-icons-row">
<div class="icon-info">
<span class="golang-icon"></span>
<p>go run main.go</p>
</div>
<div class="icon-info">
<span class="node-icon"></span>
<p>npm run start</p>
</div>
</div>
<div class="language-icons-row">
<div class="icon-info">
<span class="python-icon"></span>
<p>Python3 main.py</p>
<div class="language-icons-row">
{#if projectType === 'go'}
<div class="icon-info">
<span class="golang-icon"></span>
<p>go run main.go</p>
</div>
{/if}

{#if projectType === 'javascript'}
<div class="icon-info">
<span class="node-icon"></span>
<p>npm run start</p>
</div>
{/if}
</div>
<div class="icon-info">
<span class="java-icon"></span>
<p>java xyz.jar</p>

<div class="language-icons-row">
{#if projectType === 'python'}
<div class="icon-info">
<span class="python-icon"></span>
<p>Python3 main.py</p>
</div>
{/if}

{#if projectType === 'java'}
<div class="icon-info">
<span class="java-icon"></span>
<p>java xyz.jar</p>
</div>
{/if}
</div>
</div>
</div>

</div>
</div>
<button class="buttonBlack" id="setupConfig" on:click={handleConfig}
Expand Down

0 comments on commit 7ccfcf6

Please sign in to comment.