Skip to content

Pre & Post Build Command Support #167

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 12 commits into from
Feb 22, 2025
Merged
Show file tree
Hide file tree
Changes from 8 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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
},
"require-dev": {
"laravel/pint": "^1.0",
"mockery/mockery": "^1.6",
"nunomaduro/collision": "^7.9",
"nunomaduro/larastan": "^2.0.1",
"orchestra/testbench": "^8.18",
Expand Down
6 changes: 6 additions & 0 deletions src/Commands/BuildCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
use Illuminate\Support\Str;
use Native\Electron\Concerns\LocatesPhpBinary;
use Native\Electron\Facades\Updater;
use Native\Electron\Traits\HasPreAndPostProcessing;
use Native\Electron\Traits\InstallsAppIcon;
use Native\Electron\Traits\OsAndArch;

class BuildCommand extends Command
{
use HasPreAndPostProcessing;
use InstallsAppIcon;
use LocatesPhpBinary;
use OsAndArch;
Expand All @@ -27,6 +29,8 @@ public function handle(): void
{
$this->info('Build NativePHP app…');

$this->preProcess();

Process::path(__DIR__.'/../../resources/js/')
->env($this->getEnvironmentVariables())
->forever()
Expand Down Expand Up @@ -64,6 +68,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
94 changes: 94 additions & 0 deletions src/Traits/HasPreAndPostProcessing.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace Native\Electron\Traits;

use Illuminate\Contracts\Process\ProcessResult;
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 = $this->getPrePostBuildConfig('prebuild');

if (! $config instanceof Collection) {
return;
}

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

$config->each($this->getProcessCallback());

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

public function postProcess(): void
{
$config = $this->getPrePostBuildConfig('postbuild');

if (! $config instanceof Collection) {
return;
}

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

$config->each($this->getProcessCallback());

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

private function formatConfigKey(string $configKey): string
{
return sprintf('nativephp.%s', $configKey);
}

private function executeCommand(mixed $command): ProcessResult
{
return Process::path(base_path())
->timeout(300)
->tty(\Symfony\Component\Process\Process::isTtySupported())
->run($command, function (string $type, string $output) {
echo $output;
});
}

protected function getPrePostBuildConfig(string $configKey): mixed
{
$config = config($this->formatConfigKey($configKey));

if (is_array($config)) {
// Filter out empty values
return collect($config)
->filter(fn ($value) => ! empty($value));
}

return $config;
}

private function getProcessCallback(): callable
{
return function ($command) {
note("Running command: {$command}");

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

$result = $this->executeCommand($command);

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

return;
}

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

namespace Native\Electron\Tests\Mocks;

use Illuminate\Console\Command;
use Native\Electron\Traits\HasPreAndPostProcessing;

#[\AllowDynamicProperties]
class HasPreAndPostProcessingMock extends Command
{
use HasPreAndPostProcessing;
}
49 changes: 49 additions & 0 deletions tests/Unit/Traits/HasPreAndPostProcessingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Native\Electron\Tests\Unit\Traits;

use Illuminate\Support\Facades\Config;
use Native\Electron\Tests\Mocks\HasPreAndPostProcessingMock;

it('can run pre and post processing from config', function () {
$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',
]);

$mock = \Mockery::mock(HasPreAndPostProcessingMock::class)
->makePartial();

$mock->shouldAllowMockingProtectedMethods();
$mock->shouldReceive('getPrePostBuildConfig')
->with('prebuild')
->andReturn(collect(Config::get('nativephp.prebuild')));

$mock->shouldReceive('getPrePostBuildConfig')
->with('postbuild')
->andReturn(collect(Config::get('nativephp.postbuild')));

// 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');
});
Loading