Skip to content

Commit

Permalink
start a local-network
Browse files Browse the repository at this point in the history
Signed-off-by: urgetolearn <[email protected]>
  • Loading branch information
urgetolearn committed Oct 5, 2024
1 parent f41d750 commit 90dbef3
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 52 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
*.vsix
.DS_Store
.hidden/
101 changes: 51 additions & 50 deletions extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,58 @@ const { exec } = require('child_process');
const path = require('path');
const os = require('os');
const fabricsamples = require('./src/fabricsamples');
const { Console } = require("console");
const outputChannel = vscode.window.createOutputChannel("Function Arguments Logger");

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

let greenButton = vscode.commands.registerCommand('myview.button1', () => {
const platform = process.platform;
const scriptUpPath = path.join(fabricDebuggerPath, 'local-networkup.sh');
// Command to execute based on the platform
let command;
if (platform === 'win32') {
command = `wsl bash "${scriptUpPath}"`;
} else {
command = `bash "${scriptUpPath}"`;
}

exec(command, (err, stdout, stderr) => {
if (err) {
vscode.window.showErrorMessage(`Error: ${stderr}`);
return;
}
vscode.window.showInformationMessage(`Output: ${stdout}`);
console.log("network is up and running");
});
});

// Command for the red button
let redButton = vscode.commands.registerCommand('myview.button2', () => {
const platform = process.platform;

const scriptDownPath = path.join(fabricDebuggerPath, 'local-networkdown.sh');
let command;
if (platform === 'win32') {
command = `wsl bash "${scriptDownPath}"`;
} else {
command = `bash "${scriptDownPath}"`;
}

// Execute the command
exec(command, (err, stdout, stderr) => {
if (err) {
vscode.window.showErrorMessage(`Error: ${stderr}`);
return;
}
vscode.window.showInformationMessage(`Output: ${stdout}`);
console.log("network is down");
});
});

context.subscriptions.push(greenButton);
context.subscriptions.push(redButton);
let disposable5 = vscode.commands.registerCommand('extension.extractFunctions', function () {
const editor = vscode.window.activeTextEditor;
if (!editor) {
Expand Down Expand Up @@ -57,58 +106,10 @@ function activate(context) {
"fabric-network",
context
);

const treeViewProviderDesc = new TreeViewProvider("network-desc", context);
const treeViewProviderWallet = new TreeViewProvider("wallets", context);
function runCommand(command, callback) {
const platform = os.platform(); // Detect the OS platform

if (platform === 'win32') {
command = `bash -c "${command}"`;
} else if (platform === 'darwin' || platform === 'linux') {
// For macOS no need to modify the command
}

exec(command, (err, stdout, stderr) => {
if (err) {
console.error(`Error: ${err.message}`);
return;
}
console.log(stdout);
if (stderr) {
console.error(`Stderr: ${stderr}`);
}
if (callback) callback();
});
}

function ensureRepoCloned(commandToRun) {
if (!fs.existsSync('fabric-samples')) {
console.log('Cloning the repository...');
runCommand('git clone https://github.com/urgetolearn/fabric-samples.git', () => {
console.log('Repository cloned. Executing the next command...');
runCommand(commandToRun);
});
} else {
console.log('Repository already cloned. Executing the command...');
runCommand(commandToRun);
}
}

const setupNetwork = () => {
const command = 'cd fabric-samples/test-network && ./network.sh up';
ensureRepoCloned(command);
};

const shutDownNetwork = () => {
const command = 'cd fabric-samples/test-network && ./network.sh down';
ensureRepoCloned(command);
};

const disposableUp = vscode.commands.registerCommand('myview.button1', setupNetwork);
const disposableDown = vscode.commands.registerCommand('myview.button2', shutDownNetwork);

context.subscriptions.push(disposableUp);
context.subscriptions.push(disposableDown);


vscode.window.createTreeView("fabric-network", {
treeDataProvider: treeViewProviderFabric,
Expand Down
11 changes: 11 additions & 0 deletions local-networkdown.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash
if [ ! -d ".hidden/fabric-samples" ]; then
curl -sSLO https://raw.githubusercontent.com/hyperledger/fabric/main/scripts/install-fabric.sh -o .hidden/install-fabric.sh && chmod +x .hidden/install-fabric.sh
# Clone into the hidden directory
git clone https://github.com/hyperledger/fabric-samples.git ".hidden/fabric-samples"

# Run the install script
.hidden/install-fabric.sh docker samples binary
fi
cd .hidden/fabric-samples/test-network
./network.sh down
12 changes: 12 additions & 0 deletions local-networkup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash
if [ ! -d ".hidden/fabric-samples" ]; then
curl -sSLO https://raw.githubusercontent.com/hyperledger/fabric/main/scripts/install-fabric.sh -o .hidden/install-fabric.sh && chmod +x .hidden/install-fabric.sh
# Clone into the hidden directory
git clone https://github.com/hyperledger/fabric-samples.git ".hidden/fabric-samples"

# Run the install script
.hidden/install-fabric.sh docker samples binary
fi
cd .hidden/fabric-samples/test-network
./network.sh up

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,11 @@
},
{
"command": "myview.button1",
"title": "🔴"
"title": "🟢"
},
{
"command": "myview.button2",
"title": "🟢"
"title": "🔴"
}
]
},
Expand Down

0 comments on commit 90dbef3

Please sign in to comment.