diff --git a/src/Commands/BuildCommand.php b/src/Commands/BuildCommand.php index d77016ee..cba960d6 100644 --- a/src/Commands/BuildCommand.php +++ b/src/Commands/BuildCommand.php @@ -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; @@ -21,6 +22,7 @@ class BuildCommand extends Command { use CleansEnvFile; use CopiesToBuildDirectory; + use HasPreAndPostProcessing; use InstallsAppIcon; use LocatesPhpBinary; use OsAndArch; @@ -60,6 +62,8 @@ public function handle(): void } } + $this->preProcess(); + $this->setAppName(slugify: true); $this->newLine(); @@ -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 diff --git a/src/Traits/HasPreAndPostProcessing.php b/src/Traits/HasPreAndPostProcessing.php new file mode 100644 index 00000000..61c81f4d --- /dev/null +++ b/src/Traits/HasPreAndPostProcessing.php @@ -0,0 +1,70 @@ +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}"); + }); + } +} diff --git a/tests/Unit/Traits/HasPreAndPostProcessingTest.php b/tests/Unit/Traits/HasPreAndPostProcessingTest.php new file mode 100644 index 00000000..e9ec01b0 --- /dev/null +++ b/tests/Unit/Traits/HasPreAndPostProcessingTest.php @@ -0,0 +1,44 @@ +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; + }, + ]);