Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
176 changes: 149 additions & 27 deletions extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,54 +24,49 @@ let loadedConnectionProfile = null;
const fabricsamples = require("./src/fabricsamples");

function activate(context) {
const fabricDebuggerPath = 'C:\\Users\\Public\\fabric-debugger';
function runupBashScript() {
const fabricDebuggerPath = 'C:\\Users\\chinm\\fabric-debugger';

let greenButton = vscode.commands.registerCommand('myview.button1', () => {
const platform = process.platform;
const changeDirCommand = `cd "${fabricDebuggerPath}"`;
let runScriptCommand;
let command;
if (platform === 'win32') {
runScriptCommand = `wsl bash local-networkup.sh`;
command = `cd "${fabricDebuggerPath}" && wsl bash local-networkup.sh`;
} else {
runScriptCommand = `bash local-networkup.sh`;
command = `cd "${fabricDebuggerPath}" && bash local-networkup.sh`;
}
const fullCommand = `${changeDirCommand} && ${runScriptCommand}`;
exec(fullCommand, (err, stdout, stderr) => {

exec(command, (err, stdout, stderr) => {
if (err) {
vscode.window.showErrorMessage(`Error: ${stderr}`);
console.error(`Error: ${stderr}`);
return;
}
vscode.window.showInformationMessage(`Output: ${stdout}`);
console.log(`Output: ${stdout}`);
vscode.window.showInformationMessage("network is up and running");
});
}
let greenButton = vscode.commands.registerCommand('myview.button1', () => {
runupBashScript();
});
context.subscriptions.push(greenButton);
function rundownBashScript() {

let redButton = vscode.commands.registerCommand('myview.button2', () => {
const platform = process.platform;
const changeDirCommand = `cd "${fabricDebuggerPath}"`;
let runScriptCommand;

let command;
if (platform === 'win32') {
runScriptCommand = `wsl bash local-networkdown.sh`;
command = `cd "${fabricDebuggerPath}" && wsl bash local-networkdown.sh`;
} else {
runScriptCommand = `bash local-networkdown.sh`;
command = `cd "${fabricDebuggerPath}" && bash local-networkdown.sh`;
}
const fullCommand = `${changeDirCommand} && ${runScriptCommand}`;
exec(fullCommand, (err, stdout, stderr) => {

// Execute the command
exec(command, (err, stdout, stderr) => {
if (err) {
vscode.window.showErrorMessage(`Error: ${stderr}`);
console.error(`Error: ${stderr}`);
return;
}
vscode.window.showInformationMessage(`Output: ${stdout}`);
console.log(`Output: ${stdout}`);
vscode.window.showInformationMessage("network is down");
});
}
let redButton = vscode.commands.registerCommand('myview.button2', () => {
rundownBashScript();
});

context.subscriptions.push(greenButton);
context.subscriptions.push(redButton);


Expand All @@ -96,7 +91,6 @@ function activate(context) {
vscode.window.createTreeView("wallets", {
treeDataProvider: treeViewProviderWallet,
});

const loadProfilesAndWallets = async () => {
try {
const savedProfiles = await loadConnectionProfilesFromStorage(context);
Expand Down Expand Up @@ -240,7 +234,135 @@ function activate(context) {
}
)
);
const outputChannel = vscode.window.createOutputChannel("Chaincode Invocation");


let disposableExtractFunctions = vscode.commands.registerCommand('extension.extractFunctions', function () {
const editor = vscode.window.activeTextEditor;
if (!editor) {
vscode.window.showInformationMessage('No active editor. Open a chaincode file.');
return;
}
const filePath = editor.document.fileName;
const text = editor.document.getText();
let functions = [];

if (isGoChaincodeFile(filePath)) {
functions = extractGoFunctions(text);
}

const filteredFunctions = filterIntAndStringFunctions(functions);
const uniqueFunctions = [...new Set(filteredFunctions)];
storeFunctions(uniqueFunctions, context);

vscode.window.showInformationMessage(`Extracted and stored ${uniqueFunctions.length} unique functions with int or string parameters.`);

showStoredFunctions(context, outputChannel);
});

context.subscriptions.push(disposableExtractFunctions);


function isGoChaincodeFile(filePath) {
return filePath.toLowerCase().endsWith('.go');
}

function extractGoFunctions(code) {
const functionDetails = [];
const regex = /func\s*\((\w+)\s+\*SmartContract\)\s*(\w+)\s*\((.*?)\)\s*(\w*)/g;
let match;

while ((match = regex.exec(code)) !== null) {
const functionName = match[2];
const params = match[3];
functionDetails.push({ name: functionName, params });
}

return functionDetails;
}

function filterIntAndStringFunctions(functions) {
return functions.filter(func => /int|string/.test(func.params)).map(func => `${func.name}(${func.params})`);
}

function storeFunctions(functions, context) {
let storedFunctions = context.workspaceState.get('storedFunctions', []);
storedFunctions = [...new Set([...storedFunctions, ...functions])];
context.workspaceState.update('storedFunctions', storedFunctions);
}

function showStoredFunctions(context, outputChannel) {
const storedFunctions = context.workspaceState.get('storedFunctions', []);

vscode.window.showQuickPick(storedFunctions, {
placeHolder: 'Select a function to invoke',
canPickMany: false
}).then(selectedFunction => {
if (selectedFunction) {
vscode.window.showInformationMessage(`Selected: ${selectedFunction}`);
promptForArgumentsSequentially(selectedFunction, outputChannel);
}
});
}

async function promptForArgumentsSequentially(selectedFunction, outputChannel) {
const functionPattern = /(\w+)\((.*)\)/;
const match = functionPattern.exec(selectedFunction);

if (!match) {
vscode.window.showErrorMessage("Invalid function format.");
return;
}

const functionName = match[1];
const paramList = match[2].split(',').map(param => param.trim());

let argumentValues = [];

for (let param of paramList) {
if (/int/.test(param)) {
const input = await vscode.window.showInputBox({ prompt: `Enter an integer value for ${param}` });
const intValue = parseInt(input, 10);
if (isNaN(intValue)) {
vscode.window.showErrorMessage(`Invalid integer value for ${param}.`);
return;
}
argumentValues.push(intValue);
} else if (/string/.test(param)) {
const input = await vscode.window.showInputBox({ prompt: `Enter a string value for ${param}` });
if (!input) {
vscode.window.showErrorMessage(`Invalid string value for ${param}.`);
return;
}
argumentValues.push(`"${input}"`);
}
}

const finalArgs = argumentValues.join(', ');
outputChannel.show();
outputChannel.appendLine(`Function: ${functionName}`);
outputChannel.appendLine(`Arguments: ${finalArgs}`);

vscode.window.showInformationMessage(`Arguments captured. Press "Invoke" to execute the command.`, "Invoke").then(selection => {
if (selection === "Invoke") {
invokeCommand(functionName, argumentValues);
}
});
}


async function invokeCommand(functionName, argumentValues) {
outputChannel.appendLine(`Invoking function ${functionName} with arguments: ${argumentValues.join(', ')}`);

try {

outputChannel.appendLine(`Simulated invocation of ${functionName}(${argumentValues.join(', ')})`);
} catch (error) {
outputChannel.appendLine(`Error during invocation: ${error.message}`);
}

outputChannel.show();
}
context.subscriptions.push(
vscode.commands.registerCommand(
"fabric-network.switchNetwork",
Expand Down
18 changes: 18 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,16 @@
"command": "wallets.uploadWallet",
"when": "view == wallets",
"group": "navigation"
},
{
"command": "myview.button1",
"when": "view == start-local-network",
"group": "navigation"
},
{
"command": "myview.button2",
"when": "view == start-local-network",
"group": "navigation"
}
],
"view/item/context": [
Expand Down Expand Up @@ -134,6 +144,14 @@
"command": "extension.extractFunctions",
"title": "Debug-Chaincode ▶"
},
{
"command": "myview.button1",
"title": "🟢"
},
{
"command": "myview.button2",
"title": "🔴"
},
{
"command": "fabric-network.start",
"title": "Connection profile form",
Expand Down
128 changes: 0 additions & 128 deletions src/Chaincode/invokechaincode.js

This file was deleted.

Loading