Skip to content

Commit

Permalink
chore: conflict fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mohandast52 committed Jul 29, 2024
2 parents e20b014 + d503451 commit 5135af9
Show file tree
Hide file tree
Showing 20 changed files with 670 additions and 663 deletions.
2 changes: 1 addition & 1 deletion electron/constants/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const isProd = !isDev;
// Paths
const dotOperateDirectory = isProd
? path.join(os.homedir(), '.operate')
: '.operate';
: path.join(process.cwd(), '.operate');

const paths = {
dotOperateDirectory,
Expand Down
208 changes: 27 additions & 181 deletions electron/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const sudo = require('sudo-prompt');
const process = require('process');
const axios = require('axios');
const { spawnSync } = require('child_process');
const { logger } = require('./logger');

const { paths } = require('./constants');

Expand All @@ -13,17 +14,19 @@ const { paths } = require('./constants');
* - use "" (nothing as a suffix) for latest release candidate, for example "0.1.0rc26"
* - use "alpha" for alpha release, for example "0.1.0rc26-alpha"
*/
const OlasMiddlewareVersion = '0.1.0rc86';
const OlasMiddlewareVersion = '0.1.0rc93';

const Env = {
...process.env,
PATH: `${process.env.PATH}:/opt/homebrew/bin:/usr/local/bin`,
HOMEBREW_NO_AUTO_UPDATE: '1',
};

const SudoOptions = {
name: 'Pearl',
env: Env,
};

const TendermintUrls = {
darwin: {
x64: 'https://github.com/tendermint/tendermint/releases/download/v0.34.19/tendermint_0.34.19_darwin_amd64.tar.gz',
Expand All @@ -48,19 +51,8 @@ function getBinPath(command) {
.trim();
}

function appendInstallationLog(log) {
fs.appendFileSync(paths.OperateInstallationLog, `${log}\n`, {
encoding: 'utf-8',
});
return log;
}

function runCmdUnix(command, options) {
console.log(
appendInstallationLog(
`Running ${command} with options ${JSON.stringify(options)}`,
),
);
logger.electron(`Running ${command} with options ${JSON.stringify(options)}`);
let bin = getBinPath(command);
if (!bin) {
throw new Error(`Command ${command} not found; Path : ${Env.PATH}`);
Expand All @@ -72,9 +64,9 @@ function runCmdUnix(command, options) {
Error: ${output.error}; Stdout: ${output.stdout}; Stderr: ${output.stderr}`,
);
}
console.log(appendInstallationLog(`Executed ${command} ${options} with`));
console.log(appendInstallationLog(`===== stdout ===== \n${output.stdout}`));
console.log(appendInstallationLog(`===== stderr ===== \n${output.stderr}`));
logger.electron(`Executed ${command} ${options} with`);
logger.electron(`===== stdout ===== \n${output.stdout}`);
logger.electron(`===== stderr ===== \n${output.stderr}`);
}

function runSudoUnix(command, options) {
Expand All @@ -98,15 +90,9 @@ function runSudoUnix(command, options) {
Error: ${output.error}; Stdout: ${output.stdout}; Stderr: ${output.stderr}`,
);
}
console.log(
appendInstallationLog(`Executed ${command} ${options} with`),
);
console.log(
appendInstallationLog(`===== stdout ===== \n${output.stdout}`),
);
console.log(
appendInstallationLog(`===== stderr ===== \n${output.stderr}`),
);
logger.electron(`Executed ${command} ${options} with`);
logger.electron(`===== stdout ===== \n${output.stdout}`);
logger.electron(`===== stderr ===== \n${output.stderr}`);
resolve();
},
);
Expand Down Expand Up @@ -137,90 +123,33 @@ async function downloadFile(url, dest) {
}

async function installTendermintUnix() {
logger.electron(`Installing tendermint for ${os.platform()}-${process.arch}`);
const cwd = process.cwd();
process.chdir(paths.tempDir);

console.log(
appendInstallationLog(
`Installing tendermint for ${os.platform()}-${process.arch}`,
),
);
const url = TendermintUrls[os.platform()][process.arch];

console.log(
appendInstallationLog(`Downloading ${url}, might take a while...`),
logger.electron(
`Downloading ${url} to ${paths.tempDir}. This might take a while...`,
);
await downloadFile(url, `${paths.tempDir}/tendermint.tar.gz`);

console.log(appendInstallationLog(`Installing tendermint binary`));
logger.electron(`Installing tendermint binary`);
runCmdUnix('tar', ['-xvf', 'tendermint.tar.gz']);

// TOFIX: Install tendermint in .operate instead of globally
if (!Env.CI) {
if (!fs.existsSync('/usr/local/bin')) {
await runSudoUnix('mkdir', '/usr/local/bin');
}
await runSudoUnix('install', 'tendermint /usr/local/bin/tendermint');
await runSudoUnix(
'install',
`${paths.tempDir}/tendermint /usr/local/bin/tendermint`,
);
}
process.chdir(cwd);
}

function createVirtualEnvUnix(path) {
runCmdUnix('python3.10', ['-m', 'venv', path]);
}

function isPythonInstalledUbuntu() {
return Boolean(getBinPath('python3.10'));
}

function isGitInstalledUbuntu() {
return Boolean(getBinPath('git'));
}

function installPythonUbuntu() {
return runSudoUnix('apt', 'install -y python3.10 python3.10-dev python3-pip');
}

function installGitUbuntu() {
return runSudoUnix('apt', 'install -y git');
}

function installOperatePackageUnix(path) {
runCmdUnix(`${path}/venv/bin/python3.10`, [
'-m',
'pip',
'install',
`olas-operate-middleware==${OlasMiddlewareVersion}`,
]);
}

function reInstallOperatePackageUnix(path) {
console.log(appendInstallationLog('Reinstalling pearl CLI'));
runCmdUnix(`${path}/venv/bin/python3.10`, [
'-m',
'pip',
'install',
`olas-operate-middleware==${OlasMiddlewareVersion}`,
'--force-reinstall',
]);
}

function installOperateCli(path) {
let installPath = `${path}/operate`;
if (fs.existsSync(installPath)) {
fs.rmSync(installPath);
}
return new Promise((resolve, _reject) => {
fs.copyFile(
`${paths.dotOperateDirectory}/venv/bin/operate`,
installPath,
function (error, _stdout, _stderr) {
resolve(!error);
},
);
});
}

function createDirectory(path) {
if (fs.existsSync(path)) {
return;
Expand All @@ -232,118 +161,35 @@ function createDirectory(path) {
});
}

function writeVersion() {
fs.writeFileSync(paths.versionFile, OlasMiddlewareVersion);
}

function versionBumpRequired() {
if (!fs.existsSync(paths.versionFile)) {
return true;
}
const olasMiddlewareVersionInFile = fs
.readFileSync(paths.versionFile)
.toString();
return olasMiddlewareVersionInFile != OlasMiddlewareVersion;
}

function removeLogFile() {
if (fs.existsSync(paths.LogFile)) {
fs.rmSync(paths.LogFile);
}
}

function removeInstallationLogFile() {
if (fs.existsSync(paths.OperateInstallationLog)) {
fs.rmSync(paths.OperateInstallationLog);
}
}

/*******************************/
// NOTE: "Installing" is string matched in loading.html to detect installation
/*******************************/

async function setupDarwin(ipcChannel) {
removeInstallationLogFile();
console.log(appendInstallationLog('Creating required directories'));
logger.electron('Creating required directories');
await createDirectory(`${paths.dotOperateDirectory}`);
await createDirectory(`${paths.dotOperateDirectory}/temp`);
await createDirectory(`${paths.tempDir}`);

console.log(appendInstallationLog('Checking tendermint installation'));
logger.electron('Checking tendermint installation');
if (!isTendermintInstalledUnix()) {
ipcChannel.send('response', 'Installing Pearl Daemon');
console.log(appendInstallationLog('Installing tendermint'));
logger.electron('Installing tendermint');
await installTendermintUnix();
}

console.log(appendInstallationLog('Checking if upgrade is required'));
if (versionBumpRequired()) {
console.log(
appendInstallationLog(
`Upgrading pearl daemon to ${OlasMiddlewareVersion}`,
),
);
writeVersion();
removeLogFile();
// reInstallOperatePackageUnix(OperateDirectory);
}
}

// TODO: Add Tendermint installation
async function setupUbuntu(ipcChannel) {
removeInstallationLogFile();

console.log(appendInstallationLog('Checking python installation'));
if (!isPythonInstalledUbuntu()) {
ipcChannel.send('response', 'Installing Pearl Daemon');
console.log(appendInstallationLog('Installing Python'));
await installPythonUbuntu(paths.dotOperateDirectory);
}

console.log(appendInstallationLog('Checking git installation'));
if (!isGitInstalledUbuntu()) {
ipcChannel.send('response', 'Installing Pearl Daemon');
console.log(appendInstallationLog('Installing git'));
await installGitUbuntu(paths.dotOperateDirectory);
}

console.log(appendInstallationLog('Creating required directories'));
logger.electron('Creating required directories');
await createDirectory(`${paths.dotOperateDirectory}`);
await createDirectory(`${paths.dotOperateDirectory}/temp`);
await createDirectory(`${paths.tempDir}`);

console.log(appendInstallationLog('Checking tendermint installation'));
logger.electron('Checking tendermint installation');
if (!isTendermintInstalledUnix()) {
ipcChannel.send('response', 'Installing Pearl Daemon');
console.log(appendInstallationLog('Installing tendermint'));
logger.electron('Installing tendermint');
await installTendermintUnix();
}

if (!fs.existsSync(paths.venvDir)) {
ipcChannel.send('response', 'Installing Pearl Daemon');
console.log(appendInstallationLog('Creating virtual environment'));
createVirtualEnvUnix(paths.venvDir);

console.log(appendInstallationLog('Installing pearl backend'));
installOperatePackageUnix(paths.dotOperateDirectory);
}

console.log(appendInstallationLog('Checking if upgrade is required'));
if (versionBumpRequired()) {
console.log(
appendInstallationLog(
`Upgrading pearl daemon to ${OlasMiddlewareVersion}`,
),
);
reInstallOperatePackageUnix(paths.dotOperateDirectory);
writeVersion();
removeLogFile();
}

if (!fs.existsSync(`${paths.dotOperateDirectory}/venv/bin/operate`)) {
reInstallOperatePackageUnix(paths.dotOperateDirectory);
}

console.log(appendInstallationLog('Installing pearl CLI'));
await installOperateCli('/usr/local/bin');
}

module.exports = {
Expand Down
17 changes: 8 additions & 9 deletions electron/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const http = require('http');
const AdmZip = require('adm-zip');
const { TRAY_ICONS, TRAY_ICONS_PATHS } = require('./icons');

const { Env } = require('./install');
const { setupDarwin, setupUbuntu, Env } = require('./install');

const { paths } = require('./constants');
const { killProcesses } = require('./processes');
Expand Down Expand Up @@ -427,14 +427,13 @@ ipcMain.on('check', async function (event, _argument) {
// Setup
try {
event.sender.send('response', 'Checking installation');
if (!isDev) {
if (platform === 'darwin') {
//await setupDarwin(event.sender);
} else if (platform === 'win32') {
// TODO
} else {
//await setupUbuntu(event.sender);
}

if (platform === 'darwin') {
await setupDarwin(event.sender);
} else if (platform === 'win32') {
// TODO
} else {
await setupUbuntu(event.sender);
}

if (isDev) {
Expand Down
Loading

0 comments on commit 5135af9

Please sign in to comment.