From c0847cc42042239f0c782eaf66a7a3f73790ed41 Mon Sep 17 00:00:00 2001 From: Job Evers-Meltzer Date: Mon, 16 Jan 2017 14:06:53 -0500 Subject: [PATCH] initial electron app --- README.md | 44 +++++++++++++++++++++++ build.sh | 22 ++++++++++++ electron/main.js | 82 +++++++++++++++++++++++++++++++++++++++++++ electron/package.json | 8 +++++ lbrynet/lbry.py | 4 +++ 5 files changed, 160 insertions(+) create mode 100644 README.md create mode 100755 build.sh create mode 100644 electron/main.js create mode 100644 electron/package.json create mode 100644 lbrynet/lbry.py diff --git a/README.md b/README.md new file mode 100644 index 000000000..f0ca52556 --- /dev/null +++ b/README.md @@ -0,0 +1,44 @@ +# LBRY Electron + +An electron version of the LBRY application. + +## Setup + +Need to have the lbry-web-ui and lbry repos in a folder next to +lbry-electron. For example, my directory structure looks like + +``` +$HOME/projects/lbryio/lbry +$HOME/projects/lbryio/lbry-web-ui +$HOME/projects/lbryio/lbry-electron +``` + +Additionally, lbrynet needs to be installed along with pyinstaller, and you +need everything to be able to build the lbry-web-ui + +## Build + +run `./build.sh` to create a lbry executable, bundle the front-end and move +everything into the the electron repo + +## Run + +`electron electron` + +## Package + +To build a distributable package for OSX, run (on an OSX machine): + +`electron-packager --electron-version=1.4.14 --overwrite electron LBRY` + +This also probably works for windows and linux, but I haven't tested it + +## TODO + +This app works by launching the lbrynet daemon in a seperate process. Currently the +process management is very poor and the lbrynet process might not be shut-down when the app +is closed. Also, if the lbrynet daemon dies, there is no attempt to restart it. + + + + diff --git a/build.sh b/build.sh new file mode 100755 index 000000000..7b34afb62 --- /dev/null +++ b/build.sh @@ -0,0 +1,22 @@ +#! /bin/bash + +set -o xtrace +set -eu + +cd lbrynet +pyinstaller lbry.py -y --windowed --onefile --icon=../../lbry/packaging/osx/lbry-osx-app/app.icns + +cd ../../lbry-web-ui +git checkout master +git pull --rebase +git cherry-pick 06224b1d2cf4bf1f63d95031502260dd9c3ec5c1 +node_modules/.bin/node-sass --output dist/css --sourcemap=none scss/ +webpack +git reset --hard origin/master + +cd ../lbry-electron/ +cp -R ../lbry-web-ui/dist electron/ + +mv lbrynet/dist/lbry electron/dist + +echo 'Build complete. Run `electron electron` to launch the app' diff --git a/electron/main.js b/electron/main.js new file mode 100644 index 000000000..10d400359 --- /dev/null +++ b/electron/main.js @@ -0,0 +1,82 @@ +const {app, BrowserWindow} = require('electron') +var jayson = require('jayson'); +var client = jayson.client.http('http://localhost:5279/lbryapi'); + + +// Keep a global reference of the window object, if you don't, the window will +// be closed automatically when the JavaScript object is garbage collected. +let win +// Also keep the daemon subprocess alive +let subpy + +function createWindow () { + // Create the browser window. + win = new BrowserWindow({width: 800, height: 600}) + win.maximize() + + // and load the index.html of the app. + win.loadURL(`file://${__dirname}/dist/index.html`) + console.log('Loaded the index page') + + // Open the DevTools. + //win.webContents.openDevTools() + + // Emitted when the window is closed. + win.on('closed', () => { + // Dereference the window object, usually you would store windows + // in an array if your app supports multi windows, this is the time + // when you should delete the corresponding element. + win = null + console.log('Loaded the index page') + subpy.kill('SIGINT'); + }) +} + +// This method will be called when Electron has finished +// initialization and is ready to create browser windows. +// Some APIs can only be used after this event occurs. +app.on('ready', function(){ + // call python? + subpy = require('child_process').spawn(`${__dirname}/dist/lbry`, ['--no-launch', '--log-to-console'], {stdio: ['ignore', process.stdout, process.stderr]}) + console.log('lbrynet daemon has launched') + launchWindowWhenDaemonHasStarted(); +}) + +// TODO: incorporate this into the LBRY module +function launchWindowWhenDaemonHasStarted() { + client.request( + 'status', [], + function (err, res) { + // Did it all work ? + if (err) { + console.log(err); + console.log('Will try again in half a second'); + setTimeout(launchWindowWhenDaemonHasStarted, 500); + } + else { + console.log(res); + createWindow(); + } + } + ); +} + +// Quit when all windows are closed. +app.on('window-all-closed', () => { + // On macOS it is common for applications and their menu bar + // to stay active until the user quits explicitly with Cmd + Q + if (process.platform !== 'darwin') { + app.quit() + } +}) + +app.on('activate', () => { + // On macOS it's common to re-create a window in the app when the + // dock icon is clicked and there are no other windows open. + if (win === null) { + createWindow() + } +}) + +// In this file you can include the rest of your app's specific main process +// code. You can also put them in separate files and require them here. diff --git a/electron/package.json b/electron/package.json new file mode 100644 index 000000000..9d25db454 --- /dev/null +++ b/electron/package.json @@ -0,0 +1,8 @@ +{ + "name": "LBRY", + "version": "0.1.0", + "main": "main.js", + "dependencies": { + "jayson": "^2.0.2" + } +} diff --git a/lbrynet/lbry.py b/lbrynet/lbry.py new file mode 100644 index 000000000..2ed0360ab --- /dev/null +++ b/lbrynet/lbry.py @@ -0,0 +1,4 @@ +from lbrynet.lbrynet_daemon import DaemonControl + +if __name__ == '__main__': + DaemonControl.start()