Skip to content

Commit df970bc

Browse files
authored
Pre & Post Build Command Support (#167)
1 parent bda3296 commit df970bc

File tree

3 files changed

+120
-0
lines changed

3 files changed

+120
-0
lines changed

src/Commands/BuildCommand.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Native\Electron\Facades\Updater;
99
use Native\Electron\Traits\CleansEnvFile;
1010
use Native\Electron\Traits\CopiesToBuildDirectory;
11+
use Native\Electron\Traits\HasPreAndPostProcessing;
1112
use Native\Electron\Traits\InstallsAppIcon;
1213
use Native\Electron\Traits\LocatesPhpBinary;
1314
use Native\Electron\Traits\OsAndArch;
@@ -21,6 +22,7 @@ class BuildCommand extends Command
2122
{
2223
use CleansEnvFile;
2324
use CopiesToBuildDirectory;
25+
use HasPreAndPostProcessing;
2426
use InstallsAppIcon;
2527
use LocatesPhpBinary;
2628
use OsAndArch;
@@ -60,6 +62,8 @@ public function handle(): void
6062
}
6163
}
6264

65+
$this->preProcess();
66+
6367
$this->setAppName(slugify: true);
6468

6569
$this->newLine();
@@ -96,6 +100,8 @@ public function handle(): void
96100
->run("npm run {$buildCommand}:{$os}", function (string $type, string $output) {
97101
echo $output;
98102
});
103+
104+
$this->postProcess();
99105
}
100106

101107
protected function getEnvironmentVariables(): array
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace Native\Electron\Traits;
4+
5+
use Illuminate\Support\Collection;
6+
use Illuminate\Support\Facades\Process;
7+
8+
use function Laravel\Prompts\error;
9+
use function Laravel\Prompts\intro;
10+
use function Laravel\Prompts\note;
11+
use function Laravel\Prompts\outro;
12+
13+
trait HasPreAndPostProcessing
14+
{
15+
public function preProcess(): void
16+
{
17+
$config = collect(config('nativephp.prebuild'));
18+
19+
if ($config->isEmpty()) {
20+
return;
21+
}
22+
23+
intro('Running pre-process commands...');
24+
25+
$this->runProcess($config);
26+
27+
outro('Pre-process commands completed.');
28+
}
29+
30+
public function postProcess(): void
31+
{
32+
$config = collect(config('nativephp.postbuild'));
33+
34+
if ($config->isEmpty()) {
35+
return;
36+
}
37+
38+
intro('Running post-process commands...');
39+
40+
$this->runProcess($config);
41+
42+
outro('Post-process commands completed.');
43+
}
44+
45+
private function runProcess(Collection $configCommands): void
46+
{
47+
$configCommands->each(function ($command) {
48+
note("Running command: {$command}");
49+
50+
if (is_array($command)) {
51+
$command = implode(' && ', $command);
52+
}
53+
54+
$result = Process::path(base_path())
55+
->timeout(300)
56+
->tty(\Symfony\Component\Process\Process::isTtySupported())
57+
->run($command, function (string $type, string $output) {
58+
echo $output;
59+
});
60+
61+
if (! $result->successful()) {
62+
error("Command failed: {$command}");
63+
64+
return;
65+
}
66+
67+
note("Command successful: {$command}");
68+
});
69+
}
70+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Native\Electron\Tests\Unit\Traits;
4+
5+
use Illuminate\Support\Facades\Config;
6+
use Native\Electron\Traits\HasPreAndPostProcessing;
7+
8+
it('can run pre and post processing from config', function (object $mock) {
9+
$tmpDir = sys_get_temp_dir();
10+
11+
Config::set('nativephp.prebuild', [
12+
'touch '.$tmpDir.'/prebuild1',
13+
'touch '.$tmpDir.'/prebuild2',
14+
]);
15+
16+
Config::set('nativephp.postbuild', [
17+
'touch '.$tmpDir.'/postbuild1',
18+
'touch '.$tmpDir.'/postbuild2',
19+
]);
20+
21+
// Verify those files were created in preProcess
22+
$mock->preProcess();
23+
24+
expect(file_exists($tmpDir.'/prebuild1'))->toBeTrue();
25+
expect(file_exists($tmpDir.'/prebuild2'))->toBeTrue();
26+
27+
// Verify those files were created in postProcess
28+
$mock->postProcess();
29+
expect(file_exists($tmpDir.'/postbuild1'))->toBeTrue();
30+
expect(file_exists($tmpDir.'/postbuild2'))->toBeTrue();
31+
32+
// Cleanup
33+
unlink($tmpDir.'/prebuild1');
34+
unlink($tmpDir.'/prebuild2');
35+
unlink($tmpDir.'/postbuild1');
36+
unlink($tmpDir.'/postbuild2');
37+
})
38+
->with([
39+
// Empty class with the HasPreAndPostProcessing trait
40+
new class
41+
{
42+
use HasPreAndPostProcessing;
43+
},
44+
]);

0 commit comments

Comments
 (0)