Skip to content

Commit a8b0a72

Browse files
authored
Merge pull request #507 from NativePHP/feat/bundle-builds
Feat: bundle builds
2 parents b8a753a + b20e3d4 commit a8b0a72

12 files changed

+84
-27
lines changed

config/nativephp-internal.php

+9
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@
3030
*/
3131
'api_url' => env('NATIVEPHP_API_URL', 'http://localhost:4000/api/'),
3232

33+
/**
34+
* Configuration for the Zephpyr API.
35+
*/
36+
'zephpyr' => [
37+
'host' => env('ZEPHPYR_HOST', 'https://zephpyr.com'),
38+
'token' => env('ZEPHPYR_TOKEN'),
39+
'key' => env('ZEPHPYR_KEY'),
40+
],
41+
3342
/**
3443
* The credentials to use Apples Notarization service.
3544
*/

config/nativephp.php

+8
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
'GITHUB_*',
4949
'DO_SPACES_*',
5050
'*_SECRET',
51+
'ZEPHPYR_*',
5152
'NATIVEPHP_UPDATER_PATH',
5253
'NATIVEPHP_APPLE_ID',
5354
'NATIVEPHP_APPLE_ID_PASS',
@@ -60,6 +61,8 @@
6061
* You may use glob / wildcard patterns here.
6162
*/
6263
'cleanup_exclude_files' => [
64+
'build',
65+
'temp',
6366
'content',
6467
'node_modules',
6568
'*/tests',
@@ -136,4 +139,9 @@
136139
'postbuild' => [
137140
// 'rm -rf public/build',
138141
],
142+
143+
/**
144+
* Custom PHP binary path.
145+
*/
146+
'binary_path' => env('NATIVEPHP_PHP_BINARY_PATH', null),
139147
];

src/App.php

+7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Native\Laravel;
44

55
use Native\Laravel\Client\Client;
6+
use Phar;
67

78
class App
89
{
@@ -63,6 +64,12 @@ public function clearRecentDocuments(): void
6364
$this->client->delete('app/recent-documents');
6465
}
6566

67+
public function isRunningBundled(): bool
68+
{
69+
return Phar::running() !== '';
70+
71+
}
72+
6673
public function openAtLogin(?bool $open = null): bool
6774
{
6875
if ($open === null) {

src/ChildProcess.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public function start(
8080
* @param string|string[] $cmd
8181
* @return $this
8282
*/
83-
public function php(string|array $cmd, string $alias, ?array $env = null, ?bool $persistent = false): self
83+
public function php(string|array $cmd, string $alias, ?array $env = null, ?bool $persistent = false, ?array $iniSettings = null): self
8484
{
8585
$cmd = is_array($cmd) ? array_values($cmd) : [$cmd];
8686

@@ -90,6 +90,7 @@ public function php(string|array $cmd, string $alias, ?array $env = null, ?bool
9090
'cwd' => base_path(),
9191
'env' => $env,
9292
'persistent' => $persistent,
93+
'iniSettings' => $iniSettings,
9394
])->json();
9495

9596
return $this->fromRuntimeProcess($process);
@@ -99,13 +100,13 @@ public function php(string|array $cmd, string $alias, ?array $env = null, ?bool
99100
* @param string|string[] $cmd
100101
* @return $this
101102
*/
102-
public function artisan(string|array $cmd, string $alias, ?array $env = null, ?bool $persistent = false): self
103+
public function artisan(string|array $cmd, string $alias, ?array $env = null, ?bool $persistent = false, ?array $iniSettings = null): self
103104
{
104105
$cmd = is_array($cmd) ? array_values($cmd) : [$cmd];
105106

106107
$cmd = ['artisan', ...$cmd];
107108

108-
return $this->php($cmd, $alias, env: $env, persistent: $persistent);
109+
return $this->php($cmd, $alias, env: $env, persistent: $persistent, iniSettings: $iniSettings);
109110
}
110111

111112
public function stop(?string $alias = null): void

src/Contracts/ChildProcess.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ public function start(
1616
bool $persistent = false
1717
): self;
1818

19-
public function php(string|array $cmd, string $alias, ?array $env = null, ?bool $persistent = false): self;
19+
public function php(string|array $cmd, string $alias, ?array $env = null, ?bool $persistent = false, ?array $iniSettings = null): self;
2020

21-
public function artisan(string|array $cmd, string $alias, ?array $env = null, ?bool $persistent = false): self;
21+
public function artisan(string|array $cmd, string $alias, ?array $env = null, ?bool $persistent = false, ?array $iniSettings = null): self;
2222

2323
public function stop(?string $alias = null): void;
2424

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Native\Laravel\Events\ChildProcess;
4+
5+
use Illuminate\Broadcasting\Channel;
6+
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
7+
use Illuminate\Foundation\Events\Dispatchable;
8+
use Illuminate\Queue\SerializesModels;
9+
10+
class StartupError implements ShouldBroadcastNow
11+
{
12+
use Dispatchable, SerializesModels;
13+
14+
public function __construct(public string $alias, public string $error) {}
15+
16+
public function broadcastOn()
17+
{
18+
return [
19+
new Channel('nativephp'),
20+
];
21+
}
22+
}

src/Facades/App.php

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* @method static void addRecentDocument(string $path)
1515
* @method static array recentDocuments()
1616
* @method static void clearRecentDocuments()
17+
* @method static bool isRunningBundled()
1718
* @method static bool openAtLogin(?bool $open = null)
1819
*/
1920
class App extends Facade

src/Fakes/ChildProcessFake.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,15 @@ public function php(
7777
array|string $cmd,
7878
string $alias,
7979
?array $env = null,
80-
?bool $persistent = false
80+
?bool $persistent = false,
81+
?array $iniSettings = null
8182
): self {
8283
$this->phps[] = [
8384
'cmd' => $cmd,
8485
'alias' => $alias,
8586
'env' => $env,
8687
'persistent' => $persistent,
88+
'iniSettings' => $iniSettings,
8789
];
8890

8991
return $this;
@@ -93,13 +95,15 @@ public function artisan(
9395
array|string $cmd,
9496
string $alias,
9597
?array $env = null,
96-
?bool $persistent = false
98+
?bool $persistent = false,
99+
?array $iniSettings = null
97100
): self {
98101
$this->artisans[] = [
99102
'cmd' => $cmd,
100103
'alias' => $alias,
101104
'env' => $env,
102105
'persistent' => $persistent,
106+
'iniSettings' => $iniSettings,
103107
];
104108

105109
return $this;

src/NativeServiceProvider.php

+1
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ public function rewriteDatabase()
150150
{
151151
$databasePath = config('nativephp-internal.database_path');
152152

153+
// Automatically create the database in development mode
153154
if (config('app.debug')) {
154155
$databasePath = database_path('nativephp.sqlite');
155156

src/QueueWorker.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,24 @@ public function up(string|QueueConfig $config): void
2424
throw new \InvalidArgumentException("Invalid queue configuration alias [$config]");
2525
}
2626

27-
$this->childProcess->php(
27+
$this->childProcess->artisan(
2828
[
29-
'-d',
30-
"memory_limit={$config->memoryLimit}M",
31-
'artisan',
3229
'queue:work',
3330
"--name={$config->alias}",
3431
'--queue='.implode(',', $config->queuesToConsume),
3532
"--memory={$config->memoryLimit}",
3633
"--timeout={$config->timeout}",
3734
],
38-
$config->alias,
35+
'queue_'.$config->alias,
3936
persistent: true,
37+
iniSettings: [
38+
'memory_limit' => "{$config->memoryLimit}M",
39+
]
4040
);
4141
}
4242

4343
public function down(string $alias): void
4444
{
45-
$this->childProcess->stop($alias);
45+
$this->childProcess->stop('queue_'.$alias);
4646
}
4747
}

tests/Fakes/FakeChildProcessTest.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -82,18 +82,18 @@
8282
$fake->php('cmdA', 'aliasA', ['envA'], true);
8383
$fake->php('cmdB', 'aliasB', ['envB'], false);
8484

85-
$fake->assertPhp(fn ($cmd, $alias, $env, $persistent) => $alias === 'aliasA' &&
85+
$fake->assertPhp(fn ($cmd, $alias, $env, $persistent, $iniSettings) => $alias === 'aliasA' &&
8686
$cmd === 'cmdA' &&
8787
$env === ['envA'] &&
8888
$persistent === true);
8989

90-
$fake->assertPhp(fn ($cmd, $alias, $env, $persistent) => $alias === 'aliasB' &&
90+
$fake->assertPhp(fn ($cmd, $alias, $env, $persistent, $iniSettings) => $alias === 'aliasB' &&
9191
$cmd === 'cmdB' &&
9292
$env === ['envB'] &&
9393
$persistent === false);
9494

9595
try {
96-
$fake->assertPhp(fn ($cmd, $alias, $env, $persistent) => $alias === 'aliasC');
96+
$fake->assertPhp(fn ($cmd, $alias, $env, $persistent, $iniSettings) => $alias === 'aliasC');
9797
} catch (AssertionFailedError) {
9898
return;
9999
}
@@ -107,18 +107,18 @@
107107
$fake->artisan('cmdA', 'aliasA', ['envA'], true);
108108
$fake->artisan('cmdB', 'aliasB', ['envB'], false);
109109

110-
$fake->assertArtisan(fn ($cmd, $alias, $env, $persistent) => $alias === 'aliasA' &&
110+
$fake->assertArtisan(fn ($cmd, $alias, $env, $persistent, $iniSettings) => $alias === 'aliasA' &&
111111
$cmd === 'cmdA' &&
112112
$env === ['envA'] &&
113113
$persistent === true);
114114

115-
$fake->assertArtisan(fn ($cmd, $alias, $env, $persistent) => $alias === 'aliasB' &&
115+
$fake->assertArtisan(fn ($cmd, $alias, $env, $persistent, $iniSettings) => $alias === 'aliasB' &&
116116
$cmd === 'cmdB' &&
117117
$env === ['envB'] &&
118118
$persistent === false);
119119

120120
try {
121-
$fake->assertArtisan(fn ($cmd, $alias, $env, $persistent) => $alias === 'aliasC');
121+
$fake->assertArtisan(fn ($cmd, $alias, $env, $persistent, $iniSettings) => $alias === 'aliasC');
122122
} catch (AssertionFailedError) {
123123
return;
124124
}

tests/QueueWorker/QueueWorkerTest.php

+12-8
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,27 @@
66

77
it('hits the child process with relevant queue config to spin up a new queue worker', function () {
88
ChildProcess::fake();
9-
$config = new QueueConfig('some_worker', ['default'], 128, 61);
9+
10+
$workerName = 'some_worker';
11+
12+
$config = new QueueConfig($workerName, ['default'], 128, 61);
1013

1114
QueueWorker::up($config);
1215

13-
ChildProcess::assertPhp(function (array $cmd, string $alias, $env, $persistent) {
16+
ChildProcess::assertArtisan(function (array $cmd, string $alias, $env, $persistent, $iniSettings) use ($workerName) {
1417
expect($cmd)->toBe([
15-
'-d',
16-
'memory_limit=128M',
17-
'artisan',
1818
'queue:work',
19-
"--name={$alias}",
19+
"--name={$workerName}",
2020
'--queue=default',
2121
'--memory=128',
2222
'--timeout=61',
2323
]);
2424

25-
expect($alias)->toBe('some_worker');
25+
expect($iniSettings)->toBe([
26+
'memory_limit' => '128M',
27+
]);
28+
29+
expect($alias)->toBe('queue_some_worker');
2630
expect($env)->toBeNull();
2731
expect($persistent)->toBeTrue();
2832

@@ -35,5 +39,5 @@
3539

3640
QueueWorker::down('some_worker');
3741

38-
ChildProcess::assertStop('some_worker');
42+
ChildProcess::assertStop('queue_some_worker');
3943
});

0 commit comments

Comments
 (0)