diff --git a/resources/js/php.js b/resources/js/php.js index 88f54743..bc6160d5 100644 --- a/resources/js/php.js +++ b/resources/js/php.js @@ -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); @@ -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); diff --git a/src/Commands/BuildCommand.php b/src/Commands/BuildCommand.php index 3026373a..fd28311a 100644 --- a/src/Commands/BuildCommand.php +++ b/src/Commands/BuildCommand.php @@ -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 @@ -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')) { diff --git a/src/Commands/PublishCommand.php b/src/Commands/PublishCommand.php index 5875b231..289a2e14 100644 --- a/src/Commands/PublishCommand.php +++ b/src/Commands/PublishCommand.php @@ -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 { diff --git a/src/Traits/OsAndArch.php b/src/Traits/OsAndArch.php index ee3b9082..583c7d89 100644 --- a/src/Traits/OsAndArch.php +++ b/src/Traits/OsAndArch.php @@ -1,6 +1,8 @@ ['-x64'], - 'mac' => ['-x86', '-arm', 'all'], - 'linux' => ['-x64'] + 'win' => ['x64'], + 'mac' => ['x86', 'arm64', 'all'], + 'linux' => ['x64'] }; } }