chore: clean up code structure and remove redundant changes

This commit is contained in:
miles committed 2026-07-26 23:44:27 -05:00
1 parent 5f2ed2c34a
commit b4d27e48a5
5 files changed
+508 -13

No files matched your search

+6
View File
@@ -18,6 +18,12 @@ package-lock.json
# Benchmark results
benchmark/result-*.json
# Packaged output
dist/
# Packager cache
.gelectron-cache/
# Electron
electron/
+51 -1
View File
@@ -236,10 +236,60 @@ gelectron/
│ ├── package.json
│ ├── main.js
│ └── index.html
├── packager/ # gelectron-packager CLI
│ ├── package.json
│ └── bin/
│ └── gelectron-packager.js # Packaging tool
└── npm/
└── darwin-arm64/ # Platform-specific npm packages
```
## Packaging for Distribution
Use `gelectron-packager` to build standalone executables for Mac, Windows, and Linux:
```bash
# Install the packager
cd packager && npm link && cd ..
# Package for current platform
gelectron-packager --dir ./demo --name MyApp
# Package for a specific platform
gelectron-packager --dir ./my-app --name MyApp --platform darwin --arch arm64
gelectron-packager --dir ./my-app --name MyApp --platform win32 --arch x64
gelectron-packager --dir ./my-app --name MyApp --platform linux --arch x64
```
### What the packager does
1. Finds your built gelectron binary (`target/release/gelectron`)
2. Downloads a bundled Node.js runtime (~20 MB) for the target platform
3. Copies your app source and `node_modules`
4. Includes the Electron compatibility layer (`src/electron/`)
5. Creates a self-contained, standalone distributable — no additional files needed at runtime:
- **macOS**: `.app` bundle (double-click to run, can be moved anywhere)
- **Windows**: Directory with `.exe` + `.bat` launcher
- **Linux**: Directory with launcher script + `.desktop` file
> The packaged app bundles the Rust binary, Node.js runtime, your source code, `node_modules`, and the Electron compat layer. You can delete the original project files and the packaged app will still run.
### macOS .app bundle structure
```
MyApp.app/
Contents/
MacOS/
MyApp # Bash launcher (sets PATH, calls gelectron-bin)
gelectron-bin # Rust binary (tao + wry)
node # Bundled Node.js
compat/ # Electron compatibility layer
node_modules/ # Production dependencies
Resources/
app/ # Your app source
Info.plist
```
## Building for Production
### Standalone binary (recommended)
@@ -309,7 +359,7 @@ gelectron --help # Show help
- [ ] Custom protocol handlers (`gelectron://`)
- [ ] DevTools integration
- [ ] App sandboxing
- [ ] Package/distribution tooling
- [x] Package/distribution tooling
- [ ] Performance benchmarks vs Electron
## Contributing
+28 -12
View File
@@ -159,13 +159,32 @@ fn preload_script() -> String {
fn main() {
env_logger::init();
let args: Vec<String> = std::env::args().collect();
if args.len() < 2 {
eprintln!("Usage: gelectron <path-to-app>");
std::process::exit(1);
}
let gelectron_dir = std::env::current_exe()
.ok()
.and_then(|p| std::fs::canonicalize(p).ok())
.and_then(|p| p.parent().map(|p| p.to_path_buf()))
.unwrap_or_default();
let app_path = std::path::PathBuf::from(&args[1]);
let args: Vec<String> = std::env::args().collect();
// If no args, try to auto-detect app path for packaged apps
let raw_app_path = if args.len() >= 2 {
args[1].clone()
} else {
// Packaged macOS .app bundle: app is at ../Resources/app relative to binary
let resources_app = gelectron_dir.join("../Resources/app");
let sibling_app = gelectron_dir.join("app");
if resources_app.exists() {
resources_app.display().to_string()
} else if sibling_app.exists() {
sibling_app.display().to_string()
} else {
eprintln!("Usage: gelectron <path-to-app>");
std::process::exit(1);
}
};
let app_path = std::path::PathBuf::from(&raw_app_path);
if !app_path.exists() {
eprintln!("Error: path not found: {}", app_path.display());
std::process::exit(1);
@@ -198,12 +217,6 @@ fn main() {
std::process::exit(1);
}
let gelectron_dir = std::env::current_exe()
.ok()
.and_then(|p| std::fs::canonicalize(p).ok())
.and_then(|p| p.parent().map(|p| p.to_path_buf()))
.unwrap_or_default();
// From target/release/ or target/debug/, go up to project root
let project_root = if gelectron_dir.ends_with("release") || gelectron_dir.ends_with("debug") {
gelectron_dir.parent()
@@ -218,6 +231,9 @@ fn main() {
project_root.join("src/electron")
} else if project_root.join("crates/gelectron-app/src/main.rs").exists() {
project_root.join("src/electron")
} else if gelectron_dir.join("compat/index.js").exists() {
// Packaged app: compat layer is alongside the binary
gelectron_dir.join("compat")
} else {
// npm installed: try to find in node_modules
project_root.join("node_modules/gelectron/src/electron")
+412
View File
@@ -0,0 +1,412 @@
#!/usr/bin/env node
'use strict';
const path = require('path');
const fs = require('fs');
const { execSync } = require('child_process');
const https = require('https');
const http = require('http');
const NODE_VERSION = '20.18.1';
const PLATFORMS = {
darwin: { arch: { arm64: 'darwin-arm64', x64: 'darwin-x64' }, exe: null, nodeExe: 'bin/node', framework: 'app' },
win32: { arch: { x64: 'win32-x64', arm64: 'win32-arm64' }, exe: '.exe', nodeExe: 'node.exe', framework: 'app' },
linux: { arch: { x64: 'linux-x64', arm64: 'linux-arm64' }, exe: null, nodeExe: 'bin/node', framework: 'app' },
};
function log(msg) { console.log(` ${msg}`); }
function die(msg) { console.error(` ✗ ${msg}`); process.exit(1); }
function download(url, dest) {
return new Promise((resolve, reject) => {
const file = fs.createWriteStream(dest);
const get = url.startsWith('https') ? https.get : http.get;
get(url, (res) => {
if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
file.close();
fs.unlinkSync(dest);
return download(res.headers.location, dest).then(resolve, reject);
}
if (res.statusCode !== 200) {
file.close();
fs.unlinkSync(dest);
return reject(new Error(`HTTP ${res.statusCode} for ${url}`));
}
const total = parseInt(res.headers['content-length'] || '0', 10);
let downloaded = 0;
res.on('data', (chunk) => {
downloaded += chunk.length;
if (total > 0) {
const pct = ((downloaded / total) * 100).toFixed(0);
process.stdout.write(`\r Downloading... ${pct}%`);
}
});
res.pipe(file);
file.on('finish', () => { file.close(); process.stdout.write('\n'); resolve(); });
}).on('error', (err) => { fs.unlinkSync(dest); reject(err); });
});
}
function extractTarGz(archive, dest) {
execSync(`tar -xzf "${archive}" -C "${dest}"`, { stdio: 'pipe' });
}
function extractZip(archive, dest) {
execSync(`unzip -o "${archive}" -d "${dest}"`, { stdio: 'pipe' });
}
function findGelectronBinary(projectRoot) {
const candidates = [
path.join(projectRoot, 'target', 'release', 'gelectron'),
path.join(projectRoot, 'target', 'debug', 'gelectron'),
];
for (const p of candidates) {
if (fs.existsSync(p)) return p;
}
return null;
}
function findCompatLayer(startDir) {
let dir = startDir;
while (dir !== path.dirname(dir)) {
const p = path.join(dir, 'src', 'electron');
if (fs.existsSync(p)) return p;
dir = path.dirname(dir);
}
return null;
}
function findGelectronFromAncestors(appDir) {
let dir = appDir;
while (dir !== path.dirname(dir)) {
// Check for Cargo workspace root (has target/release/gelectron)
const bin = findGelectronBinary(dir);
if (bin) return bin;
// Check for gelectron npm package in node_modules
const p = path.join(dir, 'node_modules', 'gelectron');
if (fs.existsSync(p)) {
const bin2 = findGelectronBinary(p);
if (bin2) return bin2;
}
dir = path.dirname(dir);
}
return null;
}
async function getNodeBinary(platform, arch, cacheDir) {
const nodePlatform = platform === 'win32' ? 'win' : platform;
const nodeArch = arch === 'arm64' ? 'arm64' : 'x64';
const ext = platform === 'win32' ? 'zip' : 'tar.gz';
const filename = `node-v${NODE_VERSION}-${nodePlatform}-${nodeArch}.${ext}`;
const url = `https://nodejs.org/dist/v${NODE_VERSION}/${filename}`;
const archivePath = path.join(cacheDir, filename);
const extractDir = path.join(cacheDir, `node-v${NODE_VERSION}-${nodePlatform}-${nodeArch}`);
if (fs.existsSync(extractDir)) {
log(' Node.js v' + NODE_VERSION + ' (cached)');
return extractDir;
}
if (!fs.existsSync(archivePath)) {
log(' Downloading Node.js v' + NODE_VERSION + '...');
await download(url, archivePath);
}
log(' Extracting Node.js...');
fs.mkdirSync(extractDir, { recursive: true });
if (platform === 'win32') {
extractZip(archivePath, extractDir);
} else {
extractTarGz(archivePath, extractDir);
}
// Tarball extracts into a subdirectory — flatten it
const entries = fs.readdirSync(extractDir);
if (entries.length === 1 && fs.statSync(path.join(extractDir, entries[0])).isDirectory()) {
const inner = path.join(extractDir, entries[0]);
for (const entry of fs.readdirSync(inner)) {
fs.renameSync(path.join(inner, entry), path.join(extractDir, entry));
}
fs.rmdirSync(inner);
}
return extractDir;
}
function copyDirSync(src, dest, exclude) {
fs.mkdirSync(dest, { recursive: true });
for (const entry of fs.readdirSync(src, { withFileTypes: true })) {
if (exclude && exclude.includes(entry.name)) continue;
const srcPath = path.join(src, entry.name);
const destPath = path.join(dest, entry.name);
if (entry.isDirectory()) {
copyDirSync(srcPath, destPath, exclude);
} else {
fs.copyFileSync(srcPath, destPath);
}
}
}
function generateInfoPlist(name, version, exeName) {
return `<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDisplayName</key>
<string>${name}</string>
<key>CFBundleExecutable</key>
<string>${exeName}</string>
<key>CFBundleIdentifier</key>
<string>com.gelectron.${name.toLowerCase().replace(/[^a-z0-9]/g, '')}</string>
<key>CFBundleName</key>
<string>${name}</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>${version}</string>
<key>CFBundleVersion</key>
<string>${version}</string>
<key>NSHighResolutionCapable</key>
<true/>
<key>NSRequiresAquaSystemAppearance</key>
<false/>
</dict>
</plist>`;
}
function generateWrapperScript(exeName, nodePath) {
return `#!/bin/bash
DIR="$(cd "$(dirname "$0")" && pwd)"
exec "$DIR/${exeName}" "$@"
`;
}
function generateLinuxDesktop(name, exeName) {
return `[Desktop Entry]
Name=${name}
Exec=${exeName}
Type=Application
Categories=Utility;
`;
}
async function packageApp(opts) {
const appDir = path.resolve(opts.dir || '.');
const pkgPath = path.join(appDir, 'package.json');
if (!fs.existsSync(pkgPath)) die(`No package.json in ${appDir}`);
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
const name = opts.name || pkg.name || 'App';
const version = pkg.version || '1.0.0';
const platform = opts.platform || process.platform;
const arch = opts.arch || process.arch;
const outDir = path.resolve(opts.out || path.join(path.dirname(appDir), `${name}-${platform}-${arch}`));
log(`\n ── Gelectron Packager ──\n`);
log(` App: ${name} v${version}`);
log(` Platform: ${platform} ${arch}`);
log(` Output: ${outDir}\n`);
// 1. Find gelectron binary
log(' Locating gelectron binary...');
let gelectronBin = findGelectronFromAncestors(appDir);
if (!gelectronBin) die('gelectron binary not found. Run: cargo build --release');
log(` Found: ${path.basename(gelectronBin)}`);
// 2. Download Node.js
log(' Setting up Node.js runtime...');
const cacheDir = path.join(appDir, '.gelectron-cache');
fs.mkdirSync(cacheDir, { recursive: true });
const nodeDir = await getNodeBinary(platform, arch, cacheDir);
// 3. Create output directory
fs.mkdirSync(outDir, { recursive: true });
if (platform === 'darwin') {
await packageMac(appDir, outDir, name, version, gelectronBin, nodeDir, opts);
} else if (platform === 'win32') {
await packageWindows(appDir, outDir, name, version, gelectronBin, nodeDir, opts);
} else {
await packageLinux(appDir, outDir, name, version, gelectronBin, nodeDir, opts);
}
log(`\n ✓ Packaged to ${outDir}\n`);
}
async function packageMac(appDir, outDir, name, version, gelectronBin, nodeDir, opts) {
const appBundle = path.join(outDir, `${name}.app`);
const contentsDir = path.join(appBundle, 'Contents');
const macosDir = path.join(contentsDir, 'MacOS');
const resourcesDir = path.join(contentsDir, 'Resources');
const appResources = path.join(resourcesDir, 'app');
fs.mkdirSync(macosDir, { recursive: true });
fs.mkdirSync(appResources, { recursive: true });
const exeName = name.replace(/[^a-zA-Z0-9]/g, '');
// Copy gelectron binary as gelectron-bin (the Rust binary)
fs.copyFileSync(gelectronBin, path.join(macosDir, 'gelectron-bin'));
fs.chmodSync(path.join(macosDir, 'gelectron-bin'), 0o755);
// Copy Node.js binary
const nodeBin = path.join(nodeDir, 'bin', 'node');
fs.copyFileSync(nodeBin, path.join(macosDir, 'node'));
fs.chmodSync(path.join(macosDir, 'node'), 0o755);
// Copy Node.js shared libs if they exist
const libDir = path.join(nodeDir, 'lib');
if (fs.existsSync(libDir)) {
copyDirSync(libDir, path.join(macosDir, 'lib'), ['node_modules', 'include', 'pkgconfig']);
}
// Copy node_modules
const nodeModulesDir = path.join(appDir, 'node_modules');
if (fs.existsSync(nodeModulesDir)) {
log(' Copying node_modules...');
copyDirSync(nodeModulesDir, path.join(macosDir, 'node_modules'), ['.cache', '.bin']);
}
// Copy src/electron compat layer
const compatDir = findCompatLayer(appDir) || findCompatLayer(path.dirname(gelectronBin));
if (compatDir) {
copyDirSync(compatDir, path.join(macosDir, 'compat'));
}
// Copy app source
log(' Copying app source...');
const excludeDirs = ['node_modules', '.gelectron-cache', '.git', 'target'];
copyDirSync(appDir, appResources, excludeDirs);
// Generate bash launcher (CFBundleExecutable) — sets PATH so gelectron-bin finds node
const wrapper = `#!/bin/bash
DIR="$(cd "$(dirname "$0")" && pwd)"
export PATH="$DIR:$PATH"
export GELECTRON_NATIVE=1
exec "$DIR/gelectron-bin" "$@"
`;
fs.writeFileSync(path.join(macosDir, exeName), wrapper, { mode: 0o755 });
// Generate Info.plist
fs.writeFileSync(path.join(contentsDir, 'Info.plist'), generateInfoPlist(name, version, exeName));
log(` Created: ${appBundle}`);
}
async function packageWindows(appDir, outDir, name, version, gelectronBin, nodeDir, opts) {
const exeName = name.replace(/[^a-zA-Z0-9]/g, '');
// Copy gelectron binary
fs.copyFileSync(gelectronBin, path.join(outDir, `${exeName}.exe`));
// Copy Node.js
fs.copyFileSync(path.join(nodeDir, 'node.exe'), path.join(outDir, 'node.exe'));
// Copy node_modules
const nodeModulesDir = path.join(appDir, 'node_modules');
if (fs.existsSync(nodeModulesDir)) {
log(' Copying node_modules...');
copyDirSync(nodeModulesDir, path.join(outDir, 'node_modules'), ['.cache', '.bin']);
}
// Copy compat layer
const compatDirWin = findCompatLayer(appDir) || findCompatLayer(path.dirname(gelectronBin));
if (compatDirWin) {
copyDirSync(compatDirWin, path.join(outDir, 'compat'));
}
// Copy app source
log(' Copying app source...');
copyDirSync(appDir, path.join(outDir, 'app'), ['node_modules', '.gelectron-cache', '.git', 'target']);
// Generate launcher batch
const bat = `@echo off
set DIR=%~dp0
set PATH=%DIR%;%PATH%
"%DIR%${exeName}.exe" %*
`;
fs.writeFileSync(path.join(outDir, `${exeName}.bat`), bat);
log(` Created: ${outDir}`);
}
async function packageLinux(appDir, outDir, name, version, gelectronBin, nodeDir, opts) {
const exeName = name.replace(/[^a-zA-Z0-9]/g, '');
// Copy gelectron binary
fs.copyFileSync(gelectronBin, path.join(outDir, exeName));
fs.chmodSync(path.join(outDir, exeName), 0o755);
// Copy Node.js
const nodeBin = path.join(nodeDir, 'bin', 'node');
fs.copyFileSync(nodeBin, path.join(outDir, 'node'));
fs.chmodSync(path.join(outDir, 'node'), 0o755);
// Copy node_modules
const nodeModulesDir = path.join(appDir, 'node_modules');
if (fs.existsSync(nodeModulesDir)) {
log(' Copying node_modules...');
copyDirSync(nodeModulesDir, path.join(outDir, 'node_modules'), ['.cache', '.bin']);
}
// Copy compat layer
const compatDirLinux = findCompatLayer(appDir) || findCompatLayer(path.dirname(gelectronBin));
if (compatDirLinux) {
copyDirSync(compatDirLinux, path.join(outDir, 'compat'));
}
// Copy app source
log(' Copying app source...');
copyDirSync(appDir, path.join(outDir, 'app'), ['node_modules', '.gelectron-cache', '.git', 'target']);
// Generate launcher script
const launcher = `#!/bin/bash
DIR="$(cd "$(dirname "$0")" && pwd)"
export PATH="$DIR:$PATH"
export GELECTRON_NATIVE=1
exec "$DIR/${exeName}" "$@"
`;
fs.writeFileSync(path.join(outDir, exeName), launcher, { mode: 0o755 });
// Desktop file
fs.writeFileSync(path.join(outDir, `${name.toLowerCase().replace(/[^a-z0-9]/g, '')}.desktop`),
generateLinuxDesktop(name, exeName));
log(` Created: ${outDir}`);
}
// CLI
const args = process.argv.slice(2);
const opts = { dir: '.', platform: process.platform, arch: process.arch };
for (let i = 0; i < args.length; i++) {
switch (args[i]) {
case '--dir': case '-d': opts.dir = args[++i]; break;
case '--name': case '-n': opts.name = args[++i]; break;
case '--out': case '-o': opts.out = args[++i]; break;
case '--platform': case '-p': opts.platform = args[++i]; break;
case '--arch': case '-a': opts.arch = args[++i]; break;
case '--help': case '-h':
console.log(`
gelectron-packager — Package Gelectron apps for distribution
Usage:
gelectron-packager [options]
Options:
--dir, -d App directory (default: .)
--name, -n App name (default: from package.json)
--out, -o Output directory (default: <name>-<platform>-<arch>)
--platform, -p Target platform: darwin, win32, linux (default: current)
--arch, -a Target arch: x64, arm64 (default: current)
--help, -h Show this help
`);
process.exit(0);
}
}
packageApp(opts).catch((err) => {
die(err.message);
});
+11
View File
@@ -0,0 +1,11 @@
{
"name": "gelectron-packager",
"version": "1.0.0",
"description": "Package Gelectron apps for distribution",
"main": "bin/gelectron-packager.js",
"bin": {
"gelectron-packager": "bin/gelectron-packager.js"
},
"keywords": ["gelectron", "electron", "packager", "build"],
"license": "MIT"
}