Skip to content

Commit

Permalink
Fixed version race condition and improved build script
Browse files Browse the repository at this point in the history
  • Loading branch information
CEbbinghaus committed Sep 1, 2024
1 parent 50cf20e commit e9f5cfe
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 27 deletions.
5 changes: 5 additions & 0 deletions mise.toml → .mise.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ run = "node --no-warnings=ExperimentalWarning 'util/build.mjs' -o collect"
sources = ['backend/target/release/backend', 'dist/index.js', 'main.py', 'package.json', 'plugin.json', 'README.md']
outputs = ['build/**/*.*']

[tasks."copy"]
depends = ["build"]
description = 'Copy MicroSDeck to the SteamDeck Plugins'
run = "node --no-warnings=ExperimentalWarning 'util/build.mjs' -o copy"

[tasks."upload"]
depends = ["build"]
description = 'Upload MicroSDeck to the SteamDeck'
Expand Down
2 changes: 1 addition & 1 deletion rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import deckyPlugin from "@decky/rollup";
import externalGlobals from 'rollup-plugin-external-globals';
import { merge } from 'merge-anything';

import plugin from "./plugin.json" assert {type: "json"};
import plugin from "./plugin.json" with { type: "json" };

export default defineConfig(merge(
deckyPlugin(),
Expand Down
44 changes: 31 additions & 13 deletions util/build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Basic Usage: ./build [flags]
process.exit(0);
}

const only = [];
const tasks = [];
if (process.argv.includes('-o') || process.argv.includes('--only')) {
let [opt0, opt1] = [process.argv.indexOf('-o'), process.argv.indexOf('--only')];

Expand All @@ -43,12 +43,29 @@ if (process.argv.includes('-o') || process.argv.includes('--only')) {
for (let i = index + 1; i < process.argv.length; i++) {
let arg = process.argv[i];
if(arg.startsWith('-')) break;
only.push(arg);
tasks.push(arg);
}
}

if (only.length == 0) {
only.push('backend', 'frontend', 'collect', 'copy', 'upload');
if (tasks.length == 0) {
tasks.push('backend', 'frontend', 'collect', 'copy');
}

const mapped = {
"--skip-backend": "backend",
"--skip-frontend": "frontend",
"--skip-collect": "collect",
"--skip-copy": "copy",
}

for(let arg of process.argv) {
if (mapped[arg]) {
tasks.splice(tasks.indexOf(mapped[arg]), 1);
}
}

if (process.argv.includes('--upload') && !tasks.includes('upload')) {
tasks.push('upload');
}

const basePath = resolve(process.cwd());
Expand All @@ -70,7 +87,7 @@ function runCommand(command, directory = "") {
}

async function importJson(file) {
return (await import(file, { assert: { type: "json" } })).default;
return (await import(file, { with: { type: "json" } })).default;
}

const { name: PluginName } = await importJson(join(basePath, "plugin.json"));
Expand All @@ -84,13 +101,13 @@ if (!existsSync('plugin.json')) {

UpdateVersion("package.json", "lib/package.json");

if (!process.argv.includes('--skip-backend') && only.includes('backend')) {
if (tasks.includes('backend')) {
Logger.Log('Building backend');

runCommand('cargo build --release', 'backend');
}

if (!process.argv.includes('--skip-frontend') && only.includes('frontend')) {
if (tasks.includes('frontend')) {
if (!process.argv.includes('--skip-dependencies')) {
Logger.Log('Installing dependencies');
runCommand('pnpm install');
Expand All @@ -100,7 +117,7 @@ if (!process.argv.includes('--skip-frontend') && only.includes('frontend')) {
runCommand('pnpm run bundle');
}

if (!process.argv.includes('--skip-collect') && only.includes('collect')) {
if (tasks.includes('collect')) {
Logger.Log('Collecting outputs into /build folder');
mkdirSync('build/dist', { recursive: true });
mkdirSync('build/bin', { recursive: true });
Expand All @@ -114,19 +131,20 @@ if (!process.argv.includes('--skip-collect') && only.includes('collect')) {

const is_local = existsSync('/home/deck/homebrew');

if (is_local && (!process.argv.includes('--skip-copy') && only.includes('copy'))) {
if (is_local && tasks.includes('copy')) {
Logger.Log('Copying build folder to local plugin directory');
execSync(`sudo rm -rf /home/deck/homebrew/plugins/${PluginName}`);
execSync(`sudo cp -r build/ /home/deck/homebrew/plugins/${PluginName}`);
execSync(`sudo chmod 555 /home/deck/homebrew/plugins/${PluginName}`);
} else {
if (!is_local) {
Logger.Info('Not running on steamdeck');
} else if (!process.argv.includes('--skip-copy') && only.includes('copy'))
if (!tasks.includes('copy')) {
Logger.Log('Skipping copying build folder to local plugin directory');
} else if (!is_local) {
Logger.Info('Not running on steamdeck');
}
}

if (process.argv.includes('--upload') || only.includes('upload')) {
if (tasks.includes('upload')) {
Logger.Log("Uploading plugin to SteamDeck");

try {
Expand Down
18 changes: 5 additions & 13 deletions util/versioning.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { readFileSync, writeFileSync, openSync, fstatSync, utimesSync, closeSync, ftruncateSync } from "fs";
import { readFileSync, writeFileSync, openSync, fstatSync, utimesSync, closeSync, ftruncateSync, stat, statSync } from "fs";
import { dirname, resolve } from "path";
import { fileURLToPath } from "url";

Expand All @@ -8,20 +8,12 @@ function ReadPackageVersion() {
}

function WriteVersionToPackage(file, version) {
const fd = openSync(file, 'a+');
// Get last modified time of file
const { atime, mtime } = fstatSync(fd);
const { atime, mtime } = statSync(file);

try {

var value = readFileSync(fd, { encoding: "utf-8", flag: "r" });
const pkg = JSON.parse(value);
pkg.version = version;
ftruncateSync(fd);
writeFileSync(fd, JSON.stringify(pkg, null, " "));
} finally {
closeSync(fd);
}
var value = readFileSync(file, { encoding: "utf-8" });
const pkg = {...JSON.parse(value), version };
writeFileSync(file, JSON.stringify(pkg, null, " "));

// update file so it doesn't get marked as changed
utimesSync(file, atime, mtime);
Expand Down

0 comments on commit e9f5cfe

Please sign in to comment.