Skip to content
6 changes: 6 additions & 0 deletions src/Commands/BuildCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Native\Electron\Facades\Updater;
use Native\Electron\Traits\CleansEnvFile;
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;
Expand All @@ -21,6 +22,7 @@ class BuildCommand extends Command
{
use CleansEnvFile;
use CopiesToBuildDirectory;
use HasPreAndPostProcessing;
use InstallsAppIcon;
use LocatesPhpBinary;
use OsAndArch;
Expand Down Expand Up @@ -60,6 +62,8 @@ public function handle(): void
}
}

$this->preProcess();

$this->setAppName(slugify: true);

$this->newLine();
Expand Down Expand Up @@ -96,6 +100,8 @@ public function handle(): void
->run("npm run {$buildCommand}:{$os}", function (string $type, string $output) {
echo $output;
});

$this->postProcess();
}

protected function getEnvironmentVariables(): array
Expand Down
70 changes: 70 additions & 0 deletions src/Traits/HasPreAndPostProcessing.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace Native\Electron\Traits;

use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Process;

use function Laravel\Prompts\error;
use function Laravel\Prompts\intro;
use function Laravel\Prompts\note;
use function Laravel\Prompts\outro;

trait HasPreAndPostProcessing
{
public function preProcess(): void
{
$config = collect(config('nativephp.prebuild'));

if ($config->isEmpty()) {
return;
}

intro('Running pre-process commands...');

$this->runProcess($config);

outro('Pre-process commands completed.');
}

public function postProcess(): void
{
$config = collect(config('nativephp.postbuild'));

if ($config->isEmpty()) {
return;
}

intro('Running post-process commands...');

$this->runProcess($config);

outro('Post-process commands completed.');
}

private function runProcess(Collection $configCommands): void
{
$configCommands->each(function ($command) {
note("Running command: {$command}");

if (is_array($command)) {
$command = implode(' && ', $command);
}

$result = Process::path(base_path())
->timeout(300)
->tty(\Symfony\Component\Process\Process::isTtySupported())
->run($command, function (string $type, string $output) {
echo $output;
});

if (! $result->successful()) {
error("Command failed: {$command}");

return;
}

note("Command successful: {$command}");
});
}
}
44 changes: 44 additions & 0 deletions tests/Unit/Traits/HasPreAndPostProcessingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Native\Electron\Tests\Unit\Traits;

use Illuminate\Support\Facades\Config;
use Native\Electron\Traits\HasPreAndPostProcessing;

it('can run pre and post processing from config', function (object $mock) {
$tmpDir = sys_get_temp_dir();

Config::set('nativephp.prebuild', [
'touch '.$tmpDir.'/prebuild1',
'touch '.$tmpDir.'/prebuild2',
]);

Config::set('nativephp.postbuild', [
'touch '.$tmpDir.'/postbuild1',
'touch '.$tmpDir.'/postbuild2',
]);

// Verify those files were created in preProcess
$mock->preProcess();

expect(file_exists($tmpDir.'/prebuild1'))->toBeTrue();
expect(file_exists($tmpDir.'/prebuild2'))->toBeTrue();

// Verify those files were created in postProcess
$mock->postProcess();
expect(file_exists($tmpDir.'/postbuild1'))->toBeTrue();
expect(file_exists($tmpDir.'/postbuild2'))->toBeTrue();

// Cleanup
unlink($tmpDir.'/prebuild1');
unlink($tmpDir.'/prebuild2');
unlink($tmpDir.'/postbuild1');
unlink($tmpDir.'/postbuild2');
})
->with([
// Empty class with the HasPreAndPostProcessing trait
new class
{
use HasPreAndPostProcessing;
},
]);
Comment on lines +38 to +44
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

beautiful!