Skip to content

Commit 5e01130

Browse files
authored
Merge pull request #21 from urgetolearn/main
Made changes for extracting functions
2 parents 8cd3d46 + 0e6ffb9 commit 5e01130

File tree

3 files changed

+167
-155
lines changed

3 files changed

+167
-155
lines changed

extension.js

Lines changed: 149 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,54 +24,49 @@ let loadedConnectionProfile = null;
2424
const fabricsamples = require("./src/fabricsamples");
2525

2626
function activate(context) {
27-
const fabricDebuggerPath = 'C:\\Users\\Public\\fabric-debugger';
28-
function runupBashScript() {
27+
const fabricDebuggerPath = 'C:\\Users\\chinm\\fabric-debugger';
28+
29+
let greenButton = vscode.commands.registerCommand('myview.button1', () => {
2930
const platform = process.platform;
30-
const changeDirCommand = `cd "${fabricDebuggerPath}"`;
31-
let runScriptCommand;
31+
let command;
3232
if (platform === 'win32') {
33-
runScriptCommand = `wsl bash local-networkup.sh`;
33+
command = `cd "${fabricDebuggerPath}" && wsl bash local-networkup.sh`;
3434
} else {
35-
runScriptCommand = `bash local-networkup.sh`;
35+
command = `cd "${fabricDebuggerPath}" && bash local-networkup.sh`;
3636
}
37-
const fullCommand = `${changeDirCommand} && ${runScriptCommand}`;
38-
exec(fullCommand, (err, stdout, stderr) => {
37+
38+
exec(command, (err, stdout, stderr) => {
3939
if (err) {
4040
vscode.window.showErrorMessage(`Error: ${stderr}`);
41-
console.error(`Error: ${stderr}`);
4241
return;
4342
}
44-
vscode.window.showInformationMessage(`Output: ${stdout}`);
4543
console.log(`Output: ${stdout}`);
44+
vscode.window.showInformationMessage("network is up and running");
4645
});
47-
}
48-
let greenButton = vscode.commands.registerCommand('myview.button1', () => {
49-
runupBashScript();
5046
});
51-
context.subscriptions.push(greenButton);
52-
function rundownBashScript() {
47+
48+
let redButton = vscode.commands.registerCommand('myview.button2', () => {
5349
const platform = process.platform;
54-
const changeDirCommand = `cd "${fabricDebuggerPath}"`;
55-
let runScriptCommand;
50+
51+
let command;
5652
if (platform === 'win32') {
57-
runScriptCommand = `wsl bash local-networkdown.sh`;
53+
command = `cd "${fabricDebuggerPath}" && wsl bash local-networkdown.sh`;
5854
} else {
59-
runScriptCommand = `bash local-networkdown.sh`;
55+
command = `cd "${fabricDebuggerPath}" && bash local-networkdown.sh`;
6056
}
61-
const fullCommand = `${changeDirCommand} && ${runScriptCommand}`;
62-
exec(fullCommand, (err, stdout, stderr) => {
57+
58+
// Execute the command
59+
exec(command, (err, stdout, stderr) => {
6360
if (err) {
6461
vscode.window.showErrorMessage(`Error: ${stderr}`);
65-
console.error(`Error: ${stderr}`);
6662
return;
6763
}
68-
vscode.window.showInformationMessage(`Output: ${stdout}`);
6964
console.log(`Output: ${stdout}`);
65+
vscode.window.showInformationMessage("network is down");
7066
});
71-
}
72-
let redButton = vscode.commands.registerCommand('myview.button2', () => {
73-
rundownBashScript();
7467
});
68+
69+
context.subscriptions.push(greenButton);
7570
context.subscriptions.push(redButton);
7671

7772

@@ -96,7 +91,6 @@ function activate(context) {
9691
vscode.window.createTreeView("wallets", {
9792
treeDataProvider: treeViewProviderWallet,
9893
});
99-
10094
const loadProfilesAndWallets = async () => {
10195
try {
10296
const savedProfiles = await loadConnectionProfilesFromStorage(context);
@@ -240,7 +234,135 @@ function activate(context) {
240234
}
241235
)
242236
);
237+
const outputChannel = vscode.window.createOutputChannel("Chaincode Invocation");
238+
243239

240+
let disposableExtractFunctions = vscode.commands.registerCommand('extension.extractFunctions', function () {
241+
const editor = vscode.window.activeTextEditor;
242+
if (!editor) {
243+
vscode.window.showInformationMessage('No active editor. Open a chaincode file.');
244+
return;
245+
}
246+
const filePath = editor.document.fileName;
247+
const text = editor.document.getText();
248+
let functions = [];
249+
250+
if (isGoChaincodeFile(filePath)) {
251+
functions = extractGoFunctions(text);
252+
}
253+
254+
const filteredFunctions = filterIntAndStringFunctions(functions);
255+
const uniqueFunctions = [...new Set(filteredFunctions)];
256+
storeFunctions(uniqueFunctions, context);
257+
258+
vscode.window.showInformationMessage(`Extracted and stored ${uniqueFunctions.length} unique functions with int or string parameters.`);
259+
260+
showStoredFunctions(context, outputChannel);
261+
});
262+
263+
context.subscriptions.push(disposableExtractFunctions);
264+
265+
266+
function isGoChaincodeFile(filePath) {
267+
return filePath.toLowerCase().endsWith('.go');
268+
}
269+
270+
function extractGoFunctions(code) {
271+
const functionDetails = [];
272+
const regex = /func\s*\((\w+)\s+\*SmartContract\)\s*(\w+)\s*\((.*?)\)\s*(\w*)/g;
273+
let match;
274+
275+
while ((match = regex.exec(code)) !== null) {
276+
const functionName = match[2];
277+
const params = match[3];
278+
functionDetails.push({ name: functionName, params });
279+
}
280+
281+
return functionDetails;
282+
}
283+
284+
function filterIntAndStringFunctions(functions) {
285+
return functions.filter(func => /int|string/.test(func.params)).map(func => `${func.name}(${func.params})`);
286+
}
287+
288+
function storeFunctions(functions, context) {
289+
let storedFunctions = context.workspaceState.get('storedFunctions', []);
290+
storedFunctions = [...new Set([...storedFunctions, ...functions])];
291+
context.workspaceState.update('storedFunctions', storedFunctions);
292+
}
293+
294+
function showStoredFunctions(context, outputChannel) {
295+
const storedFunctions = context.workspaceState.get('storedFunctions', []);
296+
297+
vscode.window.showQuickPick(storedFunctions, {
298+
placeHolder: 'Select a function to invoke',
299+
canPickMany: false
300+
}).then(selectedFunction => {
301+
if (selectedFunction) {
302+
vscode.window.showInformationMessage(`Selected: ${selectedFunction}`);
303+
promptForArgumentsSequentially(selectedFunction, outputChannel);
304+
}
305+
});
306+
}
307+
308+
async function promptForArgumentsSequentially(selectedFunction, outputChannel) {
309+
const functionPattern = /(\w+)\((.*)\)/;
310+
const match = functionPattern.exec(selectedFunction);
311+
312+
if (!match) {
313+
vscode.window.showErrorMessage("Invalid function format.");
314+
return;
315+
}
316+
317+
const functionName = match[1];
318+
const paramList = match[2].split(',').map(param => param.trim());
319+
320+
let argumentValues = [];
321+
322+
for (let param of paramList) {
323+
if (/int/.test(param)) {
324+
const input = await vscode.window.showInputBox({ prompt: `Enter an integer value for ${param}` });
325+
const intValue = parseInt(input, 10);
326+
if (isNaN(intValue)) {
327+
vscode.window.showErrorMessage(`Invalid integer value for ${param}.`);
328+
return;
329+
}
330+
argumentValues.push(intValue);
331+
} else if (/string/.test(param)) {
332+
const input = await vscode.window.showInputBox({ prompt: `Enter a string value for ${param}` });
333+
if (!input) {
334+
vscode.window.showErrorMessage(`Invalid string value for ${param}.`);
335+
return;
336+
}
337+
argumentValues.push(`"${input}"`);
338+
}
339+
}
340+
341+
const finalArgs = argumentValues.join(', ');
342+
outputChannel.show();
343+
outputChannel.appendLine(`Function: ${functionName}`);
344+
outputChannel.appendLine(`Arguments: ${finalArgs}`);
345+
346+
vscode.window.showInformationMessage(`Arguments captured. Press "Invoke" to execute the command.`, "Invoke").then(selection => {
347+
if (selection === "Invoke") {
348+
invokeCommand(functionName, argumentValues);
349+
}
350+
});
351+
}
352+
353+
354+
async function invokeCommand(functionName, argumentValues) {
355+
outputChannel.appendLine(`Invoking function ${functionName} with arguments: ${argumentValues.join(', ')}`);
356+
357+
try {
358+
359+
outputChannel.appendLine(`Simulated invocation of ${functionName}(${argumentValues.join(', ')})`);
360+
} catch (error) {
361+
outputChannel.appendLine(`Error during invocation: ${error.message}`);
362+
}
363+
364+
outputChannel.show();
365+
}
244366
context.subscriptions.push(
245367
vscode.commands.registerCommand(
246368
"fabric-network.switchNetwork",

package.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,16 @@
103103
"command": "wallets.uploadWallet",
104104
"when": "view == wallets",
105105
"group": "navigation"
106+
},
107+
{
108+
"command": "myview.button1",
109+
"when": "view == start-local-network",
110+
"group": "navigation"
111+
},
112+
{
113+
"command": "myview.button2",
114+
"when": "view == start-local-network",
115+
"group": "navigation"
106116
}
107117
],
108118
"view/item/context": [
@@ -134,6 +144,14 @@
134144
"command": "extension.extractFunctions",
135145
"title": "Debug-Chaincode ▶"
136146
},
147+
{
148+
"command": "myview.button1",
149+
"title": "🟢"
150+
},
151+
{
152+
"command": "myview.button2",
153+
"title": "🔴"
154+
},
137155
{
138156
"command": "fabric-network.start",
139157
"title": "Connection profile form",

src/Chaincode/invokechaincode.js

Lines changed: 0 additions & 128 deletions
This file was deleted.

0 commit comments

Comments
 (0)