Skip to content

Commit

Permalink
Refactored PoAInstaller class to handle different platforms and added…
Browse files Browse the repository at this point in the history
… new properties and methods to PoAViewContent component.
  • Loading branch information
nathansenn committed Aug 21, 2023
1 parent 5fd976a commit 91a5330
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 24 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ build/

.env

temp
temp
.aider*
13 changes: 10 additions & 3 deletions src/main/AutoUpdaterPoA.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class PoAInstaller extends EventEmitter {
return null;
}
}
async install() {
async install() {
const installDir = Path.join(os.homedir(), (await this.getDefaultPath()) || '');
console.log(`Installing PoA to ${installDir}`);
if (!fs.existsSync(installDir)) {
Expand All @@ -69,7 +69,14 @@ class PoAInstaller extends EventEmitter {
if (compareVersions.compare(tag_name, currentVersion, '>')) {
console.log('Update available');
this.emit('update-available', tag_name);
const asset = assets.find((a) => a.name.includes('win-main') && a.name.includes('exe') && isWin);

let asset;

if (isWin) {
asset = assets.find((a) => a.name.includes('win-main') && a.name.includes('exe'));
} else if (process.platform === 'linux') {
asset = assets.find((a) => a.name.includes(`linux-main-${tag_name}`)); // Modified this line
}

if (!asset) {
console.error('Could not find PoA asset for this platform');
Expand All @@ -84,7 +91,7 @@ class PoAInstaller extends EventEmitter {
responseType: 'arraybuffer',
});

const installPath = Path.join(installDir, 'PoA.exe');
const installPath = isWin ? Path.join(installDir, 'PoA.exe') : Path.join(installDir, 'PoA');

fs.writeFileSync(installPath, Buffer.from(response.data));

Expand Down
25 changes: 13 additions & 12 deletions src/main/core/components/ProgramRunner.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// ProgramRunner.ts
import { exec, ChildProcess } from 'child_process';
import { spawn, SpawnOptions, ChildProcess } from 'child_process';
import treeKill from 'tree-kill';

class ProgramRunner {
Expand All @@ -15,23 +15,24 @@ class ProgramRunner {
public setupProgram(onExit: () => void): void {
console.log(`Setting up command: ${this.command}`);

this.process = exec(this.command, (error, stdout, stderr) => {
if (error && this.process && !this.process.killed) {
console.error(`Error running program: ${error.message}`);
console.error('Error details:', error);
return;
}
const commandParts = this.command.split(' ');
const cmd = commandParts[0];
const args = commandParts.slice(1);

console.log(`stdout: ${stdout}`);
console.error(`stderr: ${stderr}`);
});
const options: SpawnOptions = {
stdio: 'pipe',
detached: true, // This might help in some scenarios to open the program in a new process group.
shell: true // Running in a shell can sometimes help with GUI apps.
};

this.process = spawn(cmd, args, options);

this.process.stdout.on('data', (data) => {
this.outputHandler(data);
this.outputHandler(data.toString());
});

this.process.stderr.on('data', (data) => {
this.outputHandler(data);
this.outputHandler(data.toString());
});

this.process.on('exit', () => {
Expand Down
8 changes: 6 additions & 2 deletions src/renderer/views/PoAView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function PoAView() {
const { programRunner, setProgramRunner, terminalRef } = usePoAProgramRunnerContext();

const updater = usePoAInstaller();
const { terminal, setTerminal, isPoARunning, runPoA, contextValue } = usePoAProgramRunner();
const { terminal, setTerminal, isPoARunning, runPoA, contextValue, storageSize, autoPin, setAutoPin, setStorageSize } = usePoAProgramRunner();
const { stopPoA } = contextValue;
useEffect(() => {
if (terminalRef.current && !terminal) {
Expand All @@ -40,8 +40,12 @@ export function PoAView() {
isPoARunning={isPoARunning}
updatePoA={updater.updatePoA}
runPoA={runPoA}
stopPoA={stopPoA} // Pass the stopPoA function as a prop
stopPoA={stopPoA}
terminalRef={terminalRef}
storageSize={storageSize}
autoPin={autoPin}
setStorageSize={setStorageSize}
setAutoPin={setAutoPin}
/>
);
}
40 changes: 38 additions & 2 deletions src/renderer/views/PoAView/PoAViewContent.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
//Path: src\renderer\views\PoAView\PoAViewContent.tsx
import React, { useEffect, useRef, useState, RefObject } from 'react';
import { Button, OverlayTrigger, Tooltip, Card } from 'react-bootstrap';
import { Form, Button, OverlayTrigger, Tooltip, Card } from 'react-bootstrap';
import { IoIosRadioButtonOn } from 'react-icons/io';
import { usePoAState } from './PoAStateContext';
import { usePoAProgramRunner } from './usePoAProgramRunner';

interface PoAViewContentProps {
isPoARunning: boolean;
updatePoA: () => Promise<void>;
runPoA: () => void;
runPoA: () => Promise<void>;
stopPoA: () => void;
terminalRef: RefObject<HTMLDivElement>;

// Add the new properties
storageSize: number;
autoPin: boolean;
setStorageSize: React.Dispatch<React.SetStateAction<number>>;
setAutoPin: React.Dispatch<React.SetStateAction<boolean>>;
}

export const PoAViewContent: React.FC<PoAViewContentProps> = ({
isPoARunning,
updatePoA,
runPoA,
terminalRef,
autoPin,
storageSize,
setAutoPin,
setStorageSize,
}) => {
const { logs, validatorOnline, setValidatorOnline } = usePoAState();
const [isDataReceived, setIsDataReceived] = useState(false);
Expand Down Expand Up @@ -97,6 +108,14 @@ export const PoAViewContent: React.FC<PoAViewContentProps> = ({
}
}, [logs, terminalRef]);


const handleAutoPinChange = (e: React.ChangeEvent<HTMLInputElement>) => {
setAutoPin(e.target.checked);
};

const handleStorageSizeChange = (e: React.ChangeEvent<HTMLInputElement>) => {
setStorageSize(Number(e.target.value));
};
const startPoA = async () => {
if (!isPoARunning && !isDataReceived && !isUpdating) {
console.log('starting PoA');
Expand Down Expand Up @@ -149,6 +168,23 @@ export const PoAViewContent: React.FC<PoAViewContentProps> = ({
</Button>
</OverlayTrigger>
<IoIosRadioButtonOn style={{ color: validatorOnline ? 'green' : 'red' }} />
<Form.Check
type="checkbox"
label="Auto Pin"
checked={autoPin}
onChange={handleAutoPinChange}
disabled={isPoARunning}
/>
<p>Enter storage size in GB to auto pin</p>
<Form.Control
type="number"
title="Enter storage size in GB to auto pin"
value={storageSize}
onChange={handleStorageSizeChange}
min={0}
placeholder="Enter storage size in GB"
disabled={isPoARunning}
/>
</div>
</p>
<h1>SPK Network Stats</h1>
Expand Down
21 changes: 17 additions & 4 deletions src/renderer/views/PoAView/usePoAProgramRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import os from 'os';
import ArraySearch from 'arraysearch';
import PromiseIPC from 'electron-promise-ipc';

const isWin = process.platform === 'win32';
export const usePoAProgramRunner = () => {
const [terminal, setTerminal] = useState<Terminal | null>(null);
const [isPoARunning, setIsPoARunning] = useState(false);
Expand All @@ -17,6 +18,8 @@ export const usePoAProgramRunner = () => {
const Finder = ArraySearch.Finder;
const { logs, setLogs } = usePoAState();
const isMountedRef = useRef(true);
const [autoPin, setAutoPin] = useState(false);
const [storageSize, setStorageSize] = useState(0);

const runPoA = useCallback(async () => {
let command = ''; // Define command here
Expand All @@ -25,8 +28,18 @@ export const usePoAProgramRunner = () => {
const getAccount = (await PromiseIPC.send('accounts.get', profileID as any)) as any;
const hiveInfo = Finder.one.in(getAccount.keyring).with({ type: 'hive' });
const installDir = Path.join(os.homedir(), (await poaInstaller.current.getDefaultPath()) || '');
const executablePath = Path.join(installDir, 'PoA.exe');
command = `"${executablePath}" -node=2 -username=${hiveInfo.username} -useWS=true -IPFS_PORT=5004`; // Assign command here
const executablePath = isWin ? Path.join(installDir, 'PoA.exe') : Path.join(installDir, 'PoA');
command = `"${executablePath}" -node=2 -username=${hiveInfo.username} -useWS=true -IPFS_PORT=5004`;
console.log(command);
console.log(autoPin);
console.log(storageSize);
if (autoPin) {
command += " -getVids=true -pinVideos=true";
if (storageSize > 0) {
command += ` -storageLimit=${storageSize}`;
}
}
console.log(command);
if (!runner.current) {
runner.current = new ProgramRunner(command, (data: string) => {
if (!isMountedRef.current) return;
Expand All @@ -45,7 +58,7 @@ export const usePoAProgramRunner = () => {
runner.current.stopProgram();
setIsPoARunning(false);
}
}, [terminal, isPoARunning]);
}, [terminal, isPoARunning, autoPin, storageSize]);

const contextValue = {
isPoARunning,
Expand All @@ -54,5 +67,5 @@ export const usePoAProgramRunner = () => {
stopPoA: () => runner.current?.stopProgram(),
};

return { terminal, setTerminal, isPoARunning, runPoA, contextValue };
return { terminal, setTerminal, isPoARunning, runPoA, contextValue, setAutoPin, setStorageSize, storageSize, autoPin };
}

0 comments on commit 91a5330

Please sign in to comment.