refactored part of the code while enhancing usability

This commit is contained in:
tcsenpai 2024-03-15 22:01:25 +01:00
parent c6860ea0d6
commit 87d31ef333
7 changed files with 137 additions and 25 deletions

8
.env
View File

@ -1,4 +1,4 @@
SERVER_URL="https://play.discus.sh"
START_SERVER="false"
GEOM_X="1280"
GEOM_Y="720"
SERVER_URL=""
START_SERVER="true"
GEOM_X="1600"
GEOM_Y="900"

3
.gitignore vendored
View File

@ -4,4 +4,5 @@ package-lock.json
yarn.lock
node_modules
build
dist
dist
.env

BIN
assets/icon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 94 KiB

4
build_app.sh Executable file
View File

@ -0,0 +1,4 @@
#!/bin/bash
# Building with electron-builder for all platforms
yarn start build

View File

@ -0,0 +1,23 @@
# Updating
cd ../swingmusic || exit -1
git pull || exit -2
cd ../swingmusic-client || exit -3
git pull || exit -4
# Cleanup
cd ../swingmusic || exit -5
rm -rf client/* || exit -6
rm -rf build dist || exit -7
# Build the client
cd ../swingmusic-client || exit -8
yarn build --outDir ../swingmusic/client/ || exit -9
# Build the server
cd ../swingmusic || exit -10
poetry run python manage.py --build || exit -11
# Copying the client to the appropriate location
echo "Done building. Moving to the next step"
cp -r dist/swingmusic ../swingmusic-desktop/swingmusic_bin || exit -12
echo "Done copying. Done."

49
main.js
View File

@ -1,17 +1,19 @@
// Modules to control application life and create native browser window
const { app, BrowserWindow, screen } = require('electron')
const path = require('node:path')
require('dotenv').config()
require('dotenv').config({path: path.join(__dirname, '.env')})
const { spawn } = require('child_process');
console.log(process.env)
const server_url = process.env.SERVER_URL
let factor = null
let server = null
let server_url = null
var server = null
// Debug line
console.log(process.env)
// Allow launching a local server quickly
function launchServer() {
server = spawn('swingmusic_bin/swingmusic');
server = spawn('swingmusic_bin/swingmusic', ["--host", "127.0.0.1", "--port", "1970"]); // Defaults to 1970 port
server.stdout.on('data', (data) => {
console.log(`[SERVER]: ${data}`);
});
@ -24,20 +26,33 @@ function launchServer() {
server.on('close', (code) => {
console.log(`[SERVER EXITED] child process exited with code ${code}`);
});
}
if (process.env.START_SERVER == "true") {
console.log("Launching server")
launchServer()
} else {
console.log("Server not launched")
function determineServer() {
// Settings for either local or remote server support
server_url = process.env.SERVER_URL
if (process.env.START_SERVER == "true") {
console.log("Launching server")
// Supports lazy syntax for local server
if (!server_url || server_url==="") {
server_url = "http://localhost:1970"
}
launchServer()
} else {
console.log("Server not launched")
}
// Mandatory: we need a server_url here
if (!server_url || server_url==="") {
console.log("[FATAL] Server URL is not defined")
process.exit(-1)
}
}
function createWindow () {
// Create the browser window.
const mainWindow = new BrowserWindow({
icon: "assets/favicon.ico",
icon: "assets/icon.png",
title: "SwingMusic",
center: true,
width: process.env.GEOM_X / factor,
@ -57,6 +72,10 @@ function createWindow () {
// Create the browser window
app.whenReady().then(() => {
// Determining the server
determineServer()
console.log("Server is: " + server_url)
factor = screen.getPrimaryDisplay().scaleFactor;
console.log("factor: ", factor)
createWindow()
@ -74,6 +93,4 @@ app.on("before-quit", function() {
app.on('window-all-closed', function () {
app.quit()
})
// TODO Avoid launching with the python script: use instead nodejs
})

View File

@ -1,10 +1,11 @@
{
"name": "electron-quick-start",
"name": "swingmusic-desktop",
"version": "1.0.0",
"description": "A minimal Electron application",
"description": "Let there be music",
"main": "main.js",
"scripts": {
"start": "electron ."
"start": "electron .",
"build": "electron-builder build"
},
"repository": "https://github.com/tcsenpai/swingmusic-desktop.git",
"keywords": [
@ -16,8 +17,74 @@
],
"author": "TheCookingSenpai",
"license": "CC0-1.0",
"build": {
"appId": "com.swingmusic.swingmusic-desktop",
"mac": {
"target": "zip",
"category": "io.music.swingmusic"
},
"win": {
"target": "portable"
},
"linux": {
"target": "AppImage",
"category": "Music",
"description": "Let there be music",
"desktop": {
"Name": "SwingMusicDesktop",
"Comment": "Let there be music",
"executableName": "swingmusic-desktop",
"Icon": "build/icons/icon.png"
}
},
"directories": {
"output": "dist/",
"buildResources": "assets/"
},
"files": [
"dist/main/**/*",
"node_modules/**/*",
"main.js",
"libs/**/*",
".env",
"package.json",
"assets/**/*",
"swingmusic_bin/**/*"
],
"extraFiles": [
{
"from": "swingmusic_bin/swingmusic",
"to": "swingmusic_bin/swingmusic",
"filter": [
"**/*"
]
},
{
"from": ".env",
"to": ".env",
"filter": [
"**/*"
]
},
{
"from": "assets",
"to": "assets",
"filter": [
"**/*"
]
},
{
"from": "swingmusic_bin/swingmusic.exe",
"to": "swingmusic_bin/swingmusic.exe",
"filter": [
"**/*"
]
}
]
},
"devDependencies": {
"electron": "^28.2.0"
"electron": "^28.2.0",
"electron-builder": "^24.9.1"
},
"dependencies": {
"dotenv": "^16.4.1"