Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas Utzinger committed Jun 14, 2024
1 parent c1d1981 commit 1a90b99
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 29 deletions.
57 changes: 33 additions & 24 deletions resources/js/php.js
Original file line number Diff line number Diff line change
@@ -1,59 +1,68 @@
const { log } = require("console");
const fs = require("fs");
const {copySync, removeSync, existsSync, ensureDirSync} = require("fs-extra");
const {join} = require("path");
const { exit } = require("process");
const unzip = require("yauzl");

const isBuilding = process.env.NATIVEPHP_BUILDING;
const isBuilding = Boolean(process.env.NATIVEPHP_BUILDING);
const phpBinaryPath = process.env.NATIVEPHP_PHP_BINARY_PATH;
const phpVersion = process.env.NATIVEPHP_PHP_BINARY_VERSION;
const certificatePath = process.env.NATIVEPHP_CERTIFICATE_FILE_PATH;

// Differentiates for Serving and Building
const isArm64 = isBuilding ? process.argv.includes('--arm64') : process.platform.includes('arm64') ;
const isArm64 = isBuilding ? process.argv.includes('--arm64') : process.arch.includes('arm64');
const isWindows = isBuilding ? process.argv.includes('--win') : process.platform.includes('win32');
const isLinux = isBuilding ? process.argv.includes('--linux') : process.platform.includes('linux');
const isDarwin = isBuilding ? process.argv.includes('--mac') : process.platform.includes('darwin');

let buildArch = false;
if (isBuilding) {
// Only one will be used by the configured build commands in package.json
buildArch = process.argv.includes('--x64') ? 'x64' : buildArch;
buildArch = process.argv.includes('--x86') ? 'x86' : buildArch;
buildArch = process.argv.includes('--arm64') ? 'arm64' : buildArch;
}

let targetOs;
let serveArch = 'x64';
let phpBinaryFilename = 'php';
const platform = {
os: false,
arch: false,
phpBinary: 'php'
};

if (isWindows) {
targetOs = 'win';
phpBinaryFilename += '.exe';
platform.os = 'win';
platform.php += '.exe';
}

if (isLinux) {
targetOs = 'linux';
platform.os = 'linux';
}
// Use of isDarwin
if (isDarwin) {
targetOs = 'mac';
serveArch = 'x86';
platform.os = 'mac';
platform.arch = 'x86';
}

if (isArm64) {
serveArch = 'arm64';
platform.arch = 'arm64';
}

if (isBuilding) {
// Only one will be used by the configured build commands in package.json
platform.arch = process.argv.includes('--x64') ? 'x64' : false;
platform.arch = process.argv.includes('--x86') ? 'x86' : false;
platform.arch = process.argv.includes('--arm64') ? 'arm64' : false;
}

if (!platform.arch) {
console.log('Platform Architecture not set', isBuilding, isWindows, platform);
exit(1);
}

// select correct arch
let arch = isBuilding ? buildArch : serveArch;

const phpVersionZip = 'php-' + phpVersion + '.zip';
const binarySrcDir = join(phpBinaryPath, targetOs, arch, phpVersionZip);
const binarySrcDir = join(phpBinaryPath, platform.os, platform.arch, phpVersionZip);
const binaryDestDir = join(__dirname, 'resources/php');

console.log('Binary Source: ', binarySrcDir);
console.log('Binary Filename: ', phpBinaryFilename);
console.log('Binary Filename: ', platform.phpBinary);
console.log('PHP version: ' + phpVersion);

if (phpBinaryPath) {
if (platform.phpBinary) {
try {
console.log('Unzipping PHP binary from ' + binarySrcDir + ' to ' + binaryDestDir);
removeSync(binaryDestDir);
Expand All @@ -68,7 +77,7 @@ if (phpBinaryPath) {
zipfile.openReadStream(entry, function (err, readStream) {
if (err) throw err;

const binaryPath = join(binaryDestDir, phpBinaryFilename);
const binaryPath = join(binaryDestDir, platform.phpBinary);
const writeStream = fs.createWriteStream(binaryPath);

readStream.pipe(writeStream);
Expand Down
3 changes: 2 additions & 1 deletion src/Commands/BuildCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class BuildCommand extends Command

protected $signature = 'native:build
{os? : The operating system to build for (all, linux, mac, win)}
{arch? : The Processor Architecture to build for (-x64, -x86, -arm64)}
{arch? : The Processor Architecture to build for (x64, x86, arm64)}
{pub? : Publish the app (false, true)}';

public function handle(): void
Expand Down Expand Up @@ -61,6 +61,7 @@ public function handle(): void
$arch = '';
}
}
$arch = !empty($arch) ? "-{$arch}": $arch;

// Wether to publish the app or not
if (! $publish = $this->argument('pub')) {
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/PublishCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class PublishCommand extends Command

protected $signature = 'native:publish
{os? : The operating system to build for (linux, mac, win)}
{arch? : The Processor Architecture to build for (-x64, -x86, -arm64)}';
{arch? : The Processor Architecture to build for (x64, x86, arm64)}';

public function handle(): void
{
Expand Down
8 changes: 5 additions & 3 deletions src/Traits/OsAndArch.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace Native\Electron\Traits;

use Illuminate\Support\Facades\Storage;

trait OsAndArch {
protected function getDefaultOs(): string
{
Expand All @@ -14,9 +16,9 @@ protected function getDefaultOs(): string

protected function getArchForOs(string $os): array {
return match ($os) {
'win' => ['-x64'],
'mac' => ['-x86', '-arm', 'all'],
'linux' => ['-x64']
'win' => ['x64'],
'mac' => ['x86', 'arm64', 'all'],
'linux' => ['x64']
};
}
}

0 comments on commit 1a90b99

Please sign in to comment.