Skip to content

Commit 1b23379

Browse files
committed
extract bundler related code to Support namespace
it used to live inside `Electron`.
1 parent f124836 commit 1b23379

21 files changed

+229
-165
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@
6363
"autoload": {
6464
"psr-4": {
6565
"Native\\Desktop\\": "src/Desktop",
66-
"Native\\Electron\\": "src/Electron"
66+
"Native\\Electron\\": "src/Electron",
67+
"Native\\Support\\": "src/Support"
6768
}
6869
},
6970
"autoload-dev": {

src/Electron/Commands/BuildCommand.php

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,10 @@
77
use Illuminate\Support\Str;
88
use Native\Electron\ElectronServiceProvider;
99
use Native\Electron\Facades\Updater;
10-
use Native\Electron\Traits\CleansEnvFile;
11-
use Native\Electron\Traits\CopiesBundleToBuildDirectory;
12-
use Native\Electron\Traits\CopiesCertificateAuthority;
13-
use Native\Electron\Traits\HasPreAndPostProcessing;
1410
use Native\Electron\Traits\InstallsAppIcon;
15-
use Native\Electron\Traits\LocatesPhpBinary;
1611
use Native\Electron\Traits\OsAndArch;
1712
use Native\Electron\Traits\PatchesPackagesJson;
18-
use Native\Electron\Traits\PrunesVendorDirectory;
13+
use Native\Support\Bundler;
1914
use Symfony\Component\Console\Attribute\AsCommand;
2015
use Symfony\Component\Process\Process as SymfonyProcess;
2116

@@ -27,15 +22,9 @@
2722
)]
2823
class BuildCommand extends Command
2924
{
30-
use CleansEnvFile;
31-
use CopiesBundleToBuildDirectory;
32-
use CopiesCertificateAuthority;
33-
use HasPreAndPostProcessing;
3425
use InstallsAppIcon;
35-
use LocatesPhpBinary;
3626
use OsAndArch;
3727
use PatchesPackagesJson;
38-
use PrunesVendorDirectory;
3928

4029
protected $signature = 'native:build
4130
{os? : The operating system to build for (all, linux, mac, win)}
@@ -48,14 +37,10 @@ class BuildCommand extends Command
4837

4938
private string $buildOS;
5039

51-
protected function buildPath(string $path = ''): string
52-
{
53-
return ElectronServiceProvider::ELECTRON_PATH.'/resources/app/'.$path;
54-
}
55-
56-
protected function sourcePath(string $path = ''): string
57-
{
58-
return base_path($path);
40+
public function __construct(
41+
protected Bundler $bundler
42+
) {
43+
parent::__construct();
5944
}
6045

6146
public function handle(): void
@@ -73,76 +58,81 @@ public function handle(): void
7358
$this->buildCommand = 'publish';
7459
}
7560

76-
if ($this->hasBundled()) {
61+
if ($this->bundler->hasBundled()) {
7762
$this->buildBundle();
7863
} else {
79-
$this->warnUnsecureBuild();
64+
$this->bundler->warnUnsecureBuild();
8065
$this->buildUnsecure();
8166
}
8267
}
8368

8469
private function buildBundle(): void
8570
{
71+
$this->bundler->preProcess();
72+
8673
$this->setAppNameAndVersion();
8774

8875
$this->updateElectronDependencies();
8976

9077
$this->newLine();
9178
intro('Copying Bundle to build directory...');
92-
$this->copyBundleToBuildDirectory();
93-
$this->keepRequiredDirectories();
79+
$this->bundler->copyBundleToBuildDirectory();
9480

9581
$this->newLine();
96-
$this->copyCertificateAuthorityCertificate();
82+
intro('Copying latest CA Certificate...');
83+
$this->bundler->copyCertificateAuthority(path: ElectronServiceProvider::ELECTRON_PATH.'/resources');
9784

9885
$this->newLine();
9986
intro('Copying app icons...');
10087
$this->installIcon();
10188

10289
$this->buildOrPublish();
90+
91+
$this->bundler->postProcess();
10392
}
10493

10594
private function buildUnsecure(): void
10695
{
107-
$this->preProcess();
96+
$this->bundler->preProcess();
10897

10998
$this->setAppNameAndVersion();
11099

111100
$this->updateElectronDependencies();
112101

113102
$this->newLine();
114103
intro('Copying App to build directory...');
115-
$this->copyToBuildDirectory();
104+
$this->bundler->copyToBuildDirectory();
116105

117106
$this->newLine();
118-
$this->copyCertificateAuthorityCertificate();
107+
intro('Copying latest CA Certificate...');
108+
$this->bundler->copyCertificateAuthority(path: ElectronServiceProvider::ELECTRON_PATH.'/resources');
119109

120110
$this->newLine();
121111
intro('Cleaning .env file...');
122-
$this->cleanEnvFile();
112+
$this->bundler->cleanEnvFile();
123113

124114
$this->newLine();
125115
intro('Copying app icons...');
126116
$this->installIcon();
127117

128118
$this->newLine();
129119
intro('Pruning vendor directory');
130-
$this->pruneVendorDirectory();
120+
$this->bundler->pruneVendorDirectory();
131121

132122
$this->buildOrPublish();
133123

134-
$this->postProcess();
124+
$this->bundler->postProcess();
135125
}
136126

137127
protected function getEnvironmentVariables(): array
138128
{
139129
return array_merge(
140130
[
141-
'APP_PATH' => $this->sourcePath(),
131+
'APP_PATH' => $this->bundler->sourcePath(),
142132
'APP_URL' => config('app.url'),
143133
'NATIVEPHP_BUILDING' => true,
144134
'NATIVEPHP_PHP_BINARY_VERSION' => PHP_MAJOR_VERSION.'.'.PHP_MINOR_VERSION,
145-
'NATIVEPHP_PHP_BINARY_PATH' => $this->sourcePath($this->phpBinaryPath()),
135+
'NATIVEPHP_PHP_BINARY_PATH' => $this->bundler->phpBinaryPath(),
146136
'NATIVEPHP_APP_NAME' => config('app.name'),
147137
'NATIVEPHP_APP_ID' => config('nativephp.app_id'),
148138
'NATIVEPHP_APP_VERSION' => config('nativephp.version'),

src/Electron/Commands/BundleCommand.php

Lines changed: 28 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,10 @@
99
use Illuminate\Support\Facades\Process;
1010
use Illuminate\Support\Number;
1111
use Illuminate\Support\Str;
12-
use Native\Electron\Traits\CleansEnvFile;
13-
use Native\Electron\Traits\CopiesToBuildDirectory;
1412
use Native\Electron\Traits\HandlesZephpyr;
15-
use Native\Electron\Traits\HasPreAndPostProcessing;
1613
use Native\Electron\Traits\InstallsAppIcon;
17-
use Native\Electron\Traits\LocatesPhpBinary;
1814
use Native\Electron\Traits\PatchesPackagesJson;
19-
use Native\Electron\Traits\PrunesVendorDirectory;
15+
use Native\Support\Bundler;
2016
use Symfony\Component\Console\Attribute\AsCommand;
2117
use Symfony\Component\Finder\Finder;
2218
use ZipArchive;
@@ -29,14 +25,9 @@
2925
)]
3026
class BundleCommand extends Command
3127
{
32-
use CleansEnvFile;
33-
use CopiesToBuildDirectory;
3428
use HandlesZephpyr;
35-
use HasPreAndPostProcessing;
3629
use InstallsAppIcon;
37-
use LocatesPhpBinary;
3830
use PatchesPackagesJson;
39-
use PrunesVendorDirectory;
4031

4132
protected $signature = 'native:bundle {--fetch} {--clear} {--without-cleanup}';
4233

@@ -46,6 +37,17 @@ class BundleCommand extends Command
4637

4738
private string $zipName;
4839

40+
private Bundler $bundler;
41+
42+
public function __construct()
43+
{
44+
parent::__construct();
45+
46+
$this->bundler = Bundler::make(
47+
buildPath: base_path('build/app/')
48+
);
49+
}
50+
4951
public function handle(): int
5052
{
5153
// Remove the bundle
@@ -94,21 +96,19 @@ public function handle(): int
9496
intro('Copying App to build directory...');
9597

9698
// We update composer.json later,
97-
$this->copyToBuildDirectory();
99+
$this->bundler->copyToBuildDirectory();
98100

99101
$this->newLine();
100102
intro('Cleaning .env file...');
101-
$this->cleanEnvFile();
103+
$this->bundler->cleanEnvFile();
102104

103105
$this->newLine();
104106
intro('Copying app icons...');
105107
$this->installIcon();
106108

107109
$this->newLine();
108110
intro('Pruning vendor directory');
109-
$this->pruneVendorDirectory();
110-
111-
$this->cleanEnvFile();
111+
$this->bundler->pruneVendorDirectory();
112112

113113
// Check composer.json for symlinked or private packages
114114
if (! $this->checkComposerJson()) {
@@ -161,7 +161,7 @@ private function zipApplication(): bool
161161

162162
private function checkComposerJson(): bool
163163
{
164-
$composerJson = json_decode(file_get_contents($this->buildPath('composer.json')), true);
164+
$composerJson = json_decode(file_get_contents($this->bundler->buildPath('composer.json')), true);
165165

166166
// // Fail if there is symlinked packages
167167
// foreach ($composerJson['repositories'] ?? [] as $repository) {
@@ -194,10 +194,10 @@ private function checkComposerJson(): bool
194194

195195
if (count($filteredRepo) !== count($composerJson['repositories'])) {
196196
$composerJson['repositories'] = $filteredRepo;
197-
file_put_contents($this->buildPath('composer.json'),
197+
file_put_contents($this->bundler->buildPath('composer.json'),
198198
json_encode($composerJson, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
199199

200-
// Process::path($this->buildPath())
200+
// Process::path($this->bundler->buildPath())
201201
// ->run('composer install --no-dev', function (string $type, string $output) {
202202
// echo $output;
203203
// });
@@ -228,7 +228,7 @@ private function addFilesToZip(ZipArchive $zip): void
228228
$finder = (new Finder)->files()
229229
->followLinks()
230230
// ->ignoreVCSIgnored(true) // TODO: Make our own list of ignored files
231-
->in($this->buildPath())
231+
->in($this->bundler->buildPath())
232232
->exclude([
233233
// We add those a few lines below and they are ignored by most .gitignore anyway
234234
'vendor',
@@ -246,39 +246,39 @@ private function addFilesToZip(ZipArchive $zip): void
246246
$this->finderToZip($finder, $zip);
247247

248248
// Why do I have to force this? please someone explain.
249-
if (file_exists($this->buildPath('public/build'))) {
249+
if (file_exists($this->bundler->buildPath('public/build'))) {
250250
$this->finderToZip(
251251
(new Finder)->files()
252252
->followLinks()
253-
->in($this->buildPath('public/build')), $zip, 'public/build');
253+
->in($this->bundler->buildPath('public/build')), $zip, 'public/build');
254254
}
255255

256256
// Add .env file manually because Finder ignores VCS and dot files
257-
$zip->addFile($this->buildPath('.env'), '.env');
257+
$zip->addFile($this->bundler->buildPath('.env'), '.env');
258258

259259
// Add auth.json file to support private packages
260260
// WARNING: Only for testing purposes, don't uncomment this
261-
// $zip->addFile($this->buildPath('auth.json'), 'auth.json');
261+
// $zip->addFile($this->bundler->buildPath('auth.json'), 'auth.json');
262262

263263
// Custom binaries
264-
$binaryPath = Str::replaceStart($this->buildPath('vendor'), '', config('nativephp.binary_path'));
264+
$binaryPath = Str::replaceStart($this->bundler->buildPath('vendor'), '', config('nativephp.binary_path'));
265265

266266
// Add composer dependencies without unnecessary files
267267
$vendor = (new Finder)->files()
268268
->exclude(array_filter([
269269
'nativephp/php-bin',
270-
'nativephp/electron/resources/electron',
270+
'nativephp/desktop/resources/electron',
271271
'*/*/vendor', // Exclude sub-vendor directories
272272
$binaryPath,
273273
]))
274-
->in($this->buildPath('vendor'));
274+
->in($this->bundler->buildPath('vendor'));
275275

276276
$this->finderToZip($vendor, $zip, 'vendor');
277277

278278
// Add javascript dependencies
279-
if (file_exists($this->buildPath('node_modules'))) {
279+
if (file_exists($this->bundler->buildPath('node_modules'))) {
280280
$nodeModules = (new Finder)->files()
281-
->in($this->buildPath('node_modules'));
281+
->in($this->bundler->buildPath('node_modules'));
282282

283283
$this->finderToZip($nodeModules, $zip, 'node_modules');
284284
}
@@ -395,18 +395,8 @@ protected function cleanUp(): void
395395
}
396396
}
397397

398-
protected function buildPath(string $path = ''): string
399-
{
400-
return base_path('build/app/'.$path);
401-
}
402-
403398
protected function zipPath(string $path = ''): string
404399
{
405400
return base_path('build/zip/'.$path);
406401
}
407-
408-
protected function sourcePath(string $path = ''): string
409-
{
410-
return base_path($path);
411-
}
412402
}

src/Electron/Commands/DevelopCommand.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
use Illuminate\Console\Command;
66
use Native\Electron\ElectronServiceProvider;
7-
use Native\Electron\Traits\CopiesCertificateAuthority;
87
use Native\Electron\Traits\Developer;
98
use Native\Electron\Traits\Installer;
109
use Native\Electron\Traits\InstallsAppIcon;
1110
use Native\Electron\Traits\PatchesPackagesJson;
11+
use Native\Support\Bundler;
1212
use Symfony\Component\Console\Attribute\AsCommand;
1313

1414
use function Laravel\Prompts\intro;
@@ -20,14 +20,19 @@
2020
)]
2121
class DevelopCommand extends Command
2222
{
23-
use CopiesCertificateAuthority;
2423
use Developer;
2524
use Installer;
2625
use InstallsAppIcon;
2726
use PatchesPackagesJson;
2827

2928
protected $signature = 'native:serve {--no-queue} {--D|no-dependencies} {--installer=npm}';
3029

30+
public function __construct(
31+
protected Bundler $bundler
32+
) {
33+
parent::__construct();
34+
}
35+
3136
public function handle(): void
3237
{
3338
intro('Starting NativePHP dev server…');
@@ -52,7 +57,7 @@ public function handle(): void
5257

5358
$this->installIcon();
5459

55-
$this->copyCertificateAuthorityCertificate();
60+
$this->bundler->copyCertificateAuthority(path: ElectronServiceProvider::ELECTRON_PATH.'/resources');
5661

5762
$this->runDeveloper(
5863
installer: $this->option('installer'),

0 commit comments

Comments
 (0)