Skip to content

Commit 258cbc3

Browse files
authored
Basic Build Command cleanup (#178)
1 parent fa21cfe commit 258cbc3

File tree

6 files changed

+113
-21
lines changed

6 files changed

+113
-21
lines changed

resources/js/php.js

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import unzip from "yauzl";
88
const isBuilding = Boolean(process.env.NATIVEPHP_BUILDING);
99
const phpBinaryPath = process.env.NATIVEPHP_PHP_BINARY_PATH;
1010
const phpVersion = process.env.NATIVEPHP_PHP_BINARY_VERSION;
11-
const certificatePath = process.env.NATIVEPHP_CERTIFICATE_FILE_PATH;
1211

1312
// Differentiates for Serving and Building
1413
const isArm64 = isBuilding ? process.argv.includes('--arm64') : process.arch.includes('arm64');
@@ -98,13 +97,3 @@ if (platform.phpBinary) {
9897
console.error('Error copying PHP binary', e);
9998
}
10099
}
101-
102-
if (certificatePath) {
103-
try {
104-
let certDest = join(import.meta.dirname, 'resources', 'cacert.pem');
105-
copySync(certificatePath, certDest);
106-
console.log('Copied certificate file from ' + certificatePath + ' to ' + certDest);
107-
} catch (e) {
108-
console.error('Error copying certificate file', e);
109-
}
110-
}

src/Commands/BuildCommand.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,22 @@
77
use Illuminate\Support\Str;
88
use Native\Electron\Facades\Updater;
99
use Native\Electron\Traits\CleansEnvFile;
10+
use Native\Electron\Traits\CopiesCertificateAuthority;
1011
use Native\Electron\Traits\CopiesToBuildDirectory;
1112
use Native\Electron\Traits\HasPreAndPostProcessing;
1213
use Native\Electron\Traits\InstallsAppIcon;
1314
use Native\Electron\Traits\LocatesPhpBinary;
1415
use Native\Electron\Traits\OsAndArch;
1516
use Native\Electron\Traits\PrunesVendorDirectory;
1617
use Native\Electron\Traits\SetsAppName;
17-
use Symfony\Component\Filesystem\Path;
1818
use Symfony\Component\Process\Process as SymfonyProcess;
1919

2020
use function Laravel\Prompts\intro;
2121

2222
class BuildCommand extends Command
2323
{
2424
use CleansEnvFile;
25+
use CopiesCertificateAuthority;
2526
use CopiesToBuildDirectory;
2627
use HasPreAndPostProcessing;
2728
use InstallsAppIcon;
@@ -81,11 +82,7 @@ public function handle(): void
8182
$this->copyToBuildDirectory();
8283

8384
$this->newLine();
84-
intro('Copying latest CA Certificate...');
85-
copy(
86-
Path::join($this->sourcePath(), 'vendor', 'nativephp', 'php-bin', 'cacert.pem'),
87-
Path::join($this->sourcePath(), 'vendor', 'nativephp', 'electron', 'resources', 'js', 'resources', 'cacert.pem')
88-
);
85+
$this->copyCertificateAuthorityCertificate();
8986

9087
$this->newLine();
9188
intro('Cleaning .env file...');
@@ -121,7 +118,6 @@ protected function getEnvironmentVariables(): array
121118
'NATIVEPHP_BUILDING' => true,
122119
'NATIVEPHP_PHP_BINARY_VERSION' => PHP_MAJOR_VERSION.'.'.PHP_MINOR_VERSION,
123120
'NATIVEPHP_PHP_BINARY_PATH' => $this->sourcePath($this->phpBinaryPath()),
124-
'NATIVEPHP_CERTIFICATE_FILE_PATH' => $this->sourcePath($this->binaryPackageDirectory().'cacert.pem'),
125121
'NATIVEPHP_APP_NAME' => config('app.name'),
126122
'NATIVEPHP_APP_ID' => config('nativephp.app_id'),
127123
'NATIVEPHP_APP_VERSION' => config('nativephp.version'),

src/Commands/DevelopCommand.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Native\Electron\Commands;
44

55
use Illuminate\Console\Command;
6+
use Native\Electron\Traits\CopiesCertificateAuthority;
67
use Native\Electron\Traits\Developer;
78
use Native\Electron\Traits\Installer;
89
use Native\Electron\Traits\InstallsAppIcon;
@@ -12,7 +13,10 @@
1213

1314
class DevelopCommand extends Command
1415
{
15-
use Developer, Installer, InstallsAppIcon;
16+
use CopiesCertificateAuthority;
17+
use Developer;
18+
use Installer;
19+
use InstallsAppIcon;
1620

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

@@ -40,6 +44,8 @@ public function handle(): void
4044

4145
$this->installIcon();
4246

47+
$this->copyCertificateAuthorityCertificate();
48+
4349
$this->runDeveloper(
4450
installer: $this->option('installer'),
4551
skip_queue: $this->option('no-queue'),
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace Native\Electron\Traits;
4+
5+
use Symfony\Component\Filesystem\Path;
6+
7+
use function Laravel\Prompts\error;
8+
use function Laravel\Prompts\intro;
9+
use function Laravel\Prompts\warning;
10+
11+
trait CopiesCertificateAuthority
12+
{
13+
protected function copyCertificateAuthorityCertificate(): void
14+
{
15+
try {
16+
intro('Copying latest CA Certificate...');
17+
18+
$phpBinaryDirectory = base_path('vendor/nativephp/php-bin/');
19+
20+
// Check if the class this trait is used in also uses LocatesPhpBinary
21+
if (method_exists($this, 'phpBinaryPath')) {
22+
// Get binary directory but up one level
23+
$phpBinaryDirectory = dirname(base_path($this->phpBinaryPath()));
24+
}
25+
26+
$certificateFileName = 'cacert.pem';
27+
$certFilePath = Path::join($phpBinaryDirectory, $certificateFileName);
28+
29+
if (! file_exists($certFilePath)) {
30+
warning('CA Certificate not found at '.$certFilePath.'. Skipping copy.');
31+
32+
return;
33+
}
34+
35+
$copied = copy(
36+
$certFilePath,
37+
Path::join(base_path('vendor/nativephp/electron/resources/js/resources'), $certificateFileName)
38+
);
39+
40+
if (! $copied) {
41+
// It returned false, but doesn't give a reason why.
42+
throw new \Exception('copy() failed for an unknown reason.');
43+
}
44+
} catch (\Throwable $e) {
45+
error('Failed to copy CA Certificate: '.$e->getMessage());
46+
}
47+
}
48+
}

src/Traits/ExecuteCommand.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,11 @@ protected function executeCommand(
2020
'install' => [
2121
'NATIVEPHP_PHP_BINARY_VERSION' => PHP_MAJOR_VERSION.'.'.PHP_MINOR_VERSION,
2222
'NATIVEPHP_PHP_BINARY_PATH' => base_path($this->phpBinaryPath()),
23-
'NATIVEPHP_CERTIFICATE_FILE_PATH' => base_path($this->binaryPackageDirectory().'cacert.pem'),
2423
],
2524
'serve' => [
2625
'APP_PATH' => base_path(),
2726
'NATIVEPHP_PHP_BINARY_VERSION' => PHP_MAJOR_VERSION.'.'.PHP_MINOR_VERSION,
2827
'NATIVEPHP_PHP_BINARY_PATH' => base_path($this->phpBinaryPath()),
29-
'NATIVEPHP_CERTIFICATE_FILE_PATH' => base_path($this->binaryPackageDirectory().'cacert.pem'),
3028
'NATIVE_PHP_SKIP_QUEUE' => $skip_queue,
3129
'NATIVEPHP_BUILDING' => false,
3230
],
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace Native\Electron\Tests\Unit\Traits;
4+
5+
use Native\Electron\Traits\CopiesCertificateAuthority;
6+
use RecursiveCallbackFilterIterator;
7+
use RecursiveDirectoryIterator;
8+
use RecursiveIteratorIterator;
9+
10+
it('can copy the default CA certificate from php-bin', function ($mock) {
11+
// Set up
12+
app()->setBasePath(realpath(__DIR__.'/../../../'));
13+
14+
// / Make directory temporarily
15+
mkdir(base_path('vendor/nativephp/electron/resources/js/resources'), 0777, true);
16+
17+
// Test
18+
expect(file_exists(base_path('vendor/nativephp/electron/resources/js/resources/cacert.pem')))->toBeFalse();
19+
20+
$mock->run();
21+
22+
expect(file_exists(base_path('vendor/nativephp/electron/resources/js/resources/cacert.pem')))->toBeTrue();
23+
24+
// Cleanup
25+
// Delete the vendor/nativephp/electron directory, recursively including directories and then files
26+
$files = new RecursiveIteratorIterator(
27+
new RecursiveCallbackFilterIterator(
28+
new RecursiveDirectoryIterator(base_path('vendor/nativephp/electron'), RecursiveDirectoryIterator::SKIP_DOTS),
29+
fn ($current, $key, $iterator) => $current->isDir() || $current->isFile()
30+
),
31+
RecursiveIteratorIterator::CHILD_FIRST
32+
);
33+
34+
foreach ($files as $file) {
35+
if ($file->isDir()) {
36+
rmdir($file->getRealPath());
37+
} else {
38+
unlink($file->getRealPath());
39+
}
40+
}
41+
42+
rmdir(base_path('vendor/nativephp/electron'));
43+
44+
})->with([
45+
// Empty class with the CopiesCertificateAuthority trait
46+
new class
47+
{
48+
use CopiesCertificateAuthority;
49+
50+
public function run()
51+
{
52+
$this->copyCertificateAuthorityCertificate();
53+
}
54+
},
55+
]);

0 commit comments

Comments
 (0)