Skip to content

Basic Build Command cleanup #178

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions resources/js/php.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import unzip from "yauzl";
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.arch.includes('arm64');
Expand Down Expand Up @@ -98,13 +97,3 @@ if (platform.phpBinary) {
console.error('Error copying PHP binary', e);
}
}

if (certificatePath) {
try {
let certDest = join(import.meta.dirname, 'resources', 'cacert.pem');
copySync(certificatePath, certDest);
console.log('Copied certificate file from ' + certificatePath + ' to ' + certDest);
} catch (e) {
console.error('Error copying certificate file', e);
}
}
10 changes: 3 additions & 7 deletions src/Commands/BuildCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,22 @@
use Illuminate\Support\Str;
use Native\Electron\Facades\Updater;
use Native\Electron\Traits\CleansEnvFile;
use Native\Electron\Traits\CopiesCertificateAuthority;
use Native\Electron\Traits\CopiesToBuildDirectory;
use Native\Electron\Traits\HasPreAndPostProcessing;
use Native\Electron\Traits\InstallsAppIcon;
use Native\Electron\Traits\LocatesPhpBinary;
use Native\Electron\Traits\OsAndArch;
use Native\Electron\Traits\PrunesVendorDirectory;
use Native\Electron\Traits\SetsAppName;
use Symfony\Component\Filesystem\Path;
use Symfony\Component\Process\Process as SymfonyProcess;

use function Laravel\Prompts\intro;

class BuildCommand extends Command
{
use CleansEnvFile;
use CopiesCertificateAuthority;
use CopiesToBuildDirectory;
use HasPreAndPostProcessing;
use InstallsAppIcon;
Expand Down Expand Up @@ -81,11 +82,7 @@ public function handle(): void
$this->copyToBuildDirectory();

$this->newLine();
intro('Copying latest CA Certificate...');
copy(
Path::join($this->sourcePath(), 'vendor', 'nativephp', 'php-bin', 'cacert.pem'),
Path::join($this->sourcePath(), 'vendor', 'nativephp', 'electron', 'resources', 'js', 'resources', 'cacert.pem')
);
$this->copyCertificateAuthorityCertificate();

$this->newLine();
intro('Cleaning .env file...');
Expand Down Expand Up @@ -121,7 +118,6 @@ protected function getEnvironmentVariables(): array
'NATIVEPHP_BUILDING' => true,
'NATIVEPHP_PHP_BINARY_VERSION' => PHP_MAJOR_VERSION.'.'.PHP_MINOR_VERSION,
'NATIVEPHP_PHP_BINARY_PATH' => $this->sourcePath($this->phpBinaryPath()),
'NATIVEPHP_CERTIFICATE_FILE_PATH' => $this->sourcePath($this->binaryPackageDirectory().'cacert.pem'),
'NATIVEPHP_APP_NAME' => config('app.name'),
'NATIVEPHP_APP_ID' => config('nativephp.app_id'),
'NATIVEPHP_APP_VERSION' => config('nativephp.version'),
Expand Down
8 changes: 7 additions & 1 deletion src/Commands/DevelopCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Native\Electron\Commands;

use Illuminate\Console\Command;
use Native\Electron\Traits\CopiesCertificateAuthority;
use Native\Electron\Traits\Developer;
use Native\Electron\Traits\Installer;
use Native\Electron\Traits\InstallsAppIcon;
Expand All @@ -12,7 +13,10 @@

class DevelopCommand extends Command
{
use Developer, Installer, InstallsAppIcon;
use CopiesCertificateAuthority;
use Developer;
use Installer;
use InstallsAppIcon;

protected $signature = 'native:serve {--no-queue} {--D|no-dependencies} {--installer=npm}';

Expand Down Expand Up @@ -40,6 +44,8 @@ public function handle(): void

$this->installIcon();

$this->copyCertificateAuthorityCertificate();

$this->runDeveloper(
installer: $this->option('installer'),
skip_queue: $this->option('no-queue'),
Expand Down
48 changes: 48 additions & 0 deletions src/Traits/CopiesCertificateAuthority.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Native\Electron\Traits;

use Symfony\Component\Filesystem\Path;

use function Laravel\Prompts\error;
use function Laravel\Prompts\intro;
use function Laravel\Prompts\warning;

trait CopiesCertificateAuthority
{
protected function copyCertificateAuthorityCertificate(): void
{
try {
intro('Copying latest CA Certificate...');

$phpBinaryDirectory = base_path('vendor/nativephp/php-bin/');

// Check if the class this trait is used in also uses LocatesPhpBinary
if (method_exists($this, 'phpBinaryPath')) {
// Get binary directory but up one level
$phpBinaryDirectory = dirname(base_path($this->phpBinaryPath()));
}

$certificateFileName = 'cacert.pem';
$certFilePath = Path::join($phpBinaryDirectory, $certificateFileName);

if (! file_exists($certFilePath)) {
warning('CA Certificate not found at '.$certFilePath.'. Skipping copy.');

return;
}

$copied = copy(
$certFilePath,
Path::join(base_path('vendor/nativephp/electron/resources/js/resources'), $certificateFileName)
);

if (! $copied) {
// It returned false, but doesn't give a reason why.
throw new \Exception('copy() failed for an unknown reason.');
}
} catch (\Throwable $e) {
error('Failed to copy CA Certificate: '.$e->getMessage());
}
}
}
2 changes: 0 additions & 2 deletions src/Traits/ExecuteCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,11 @@ protected function executeCommand(
'install' => [
'NATIVEPHP_PHP_BINARY_VERSION' => PHP_MAJOR_VERSION.'.'.PHP_MINOR_VERSION,
'NATIVEPHP_PHP_BINARY_PATH' => base_path($this->phpBinaryPath()),
'NATIVEPHP_CERTIFICATE_FILE_PATH' => base_path($this->binaryPackageDirectory().'cacert.pem'),
],
'serve' => [
'APP_PATH' => base_path(),
'NATIVEPHP_PHP_BINARY_VERSION' => PHP_MAJOR_VERSION.'.'.PHP_MINOR_VERSION,
'NATIVEPHP_PHP_BINARY_PATH' => base_path($this->phpBinaryPath()),
'NATIVEPHP_CERTIFICATE_FILE_PATH' => base_path($this->binaryPackageDirectory().'cacert.pem'),
'NATIVE_PHP_SKIP_QUEUE' => $skip_queue,
'NATIVEPHP_BUILDING' => false,
],
Expand Down
55 changes: 55 additions & 0 deletions tests/Unit/Traits/CopiesCertificateAuthorityTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Native\Electron\Tests\Unit\Traits;

use Native\Electron\Traits\CopiesCertificateAuthority;
use RecursiveCallbackFilterIterator;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;

it('can copy the default CA certificate from php-bin', function ($mock) {
// Set up
app()->setBasePath(realpath(__DIR__.'/../../../'));

// / Make directory temporarily
mkdir(base_path('vendor/nativephp/electron/resources/js/resources'), 0777, true);

// Test
expect(file_exists(base_path('vendor/nativephp/electron/resources/js/resources/cacert.pem')))->toBeFalse();

$mock->run();

expect(file_exists(base_path('vendor/nativephp/electron/resources/js/resources/cacert.pem')))->toBeTrue();

// Cleanup
// Delete the vendor/nativephp/electron directory, recursively including directories and then files
$files = new RecursiveIteratorIterator(
new RecursiveCallbackFilterIterator(
new RecursiveDirectoryIterator(base_path('vendor/nativephp/electron'), RecursiveDirectoryIterator::SKIP_DOTS),
fn ($current, $key, $iterator) => $current->isDir() || $current->isFile()
),
RecursiveIteratorIterator::CHILD_FIRST
);

foreach ($files as $file) {
if ($file->isDir()) {
rmdir($file->getRealPath());
} else {
unlink($file->getRealPath());
}
}

rmdir(base_path('vendor/nativephp/electron'));

})->with([
// Empty class with the CopiesCertificateAuthority trait
new class
{
use CopiesCertificateAuthority;

public function run()
{
$this->copyCertificateAuthorityCertificate();
}
},
]);