-
-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathBundleCommand.php
401 lines (313 loc) · 13 KB
/
BundleCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
<?php
namespace Native\Electron\Commands;
use Carbon\CarbonInterface;
use Illuminate\Console\Command;
use Illuminate\Http\Client\Response;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Process;
use Illuminate\Support\Number;
use Illuminate\Support\Str;
use Native\Electron\Traits\CleansEnvFile;
use Native\Electron\Traits\CopiesToBuildDirectory;
use Native\Electron\Traits\HandleApiRequests;
use Native\Electron\Traits\HasPreAndPostProcessing;
use Native\Electron\Traits\InstallsAppIcon;
use Native\Electron\Traits\PrunesVendorDirectory;
use Native\Electron\Traits\SetsAppName;
use Symfony\Component\Finder\Finder;
use ZipArchive;
use function Laravel\Prompts\intro;
class BundleCommand extends Command
{
use CleansEnvFile;
use CopiesToBuildDirectory;
use HandleApiRequests;
use HasPreAndPostProcessing;
use InstallsAppIcon;
use PrunesVendorDirectory;
use SetsAppName;
protected $signature = 'native:bundle {--fetch} {--clear} {--without-cleanup}';
protected $description = 'Bundle your application for distribution.';
private ?string $key;
private string $zipPath;
private string $zipName;
public function handle(): int
{
// Remove the bundle
if ($this->option('clear')) {
if (file_exists(base_path('build/__nativephp_app_bundle'))) {
unlink(base_path('build/__nativephp_app_bundle'));
}
$this->info('Bundle removed. Building in this state would be unsecure.');
return static::SUCCESS;
}
// Check for ZEPHPYR_KEY
if (! $this->checkForZephpyrKey()) {
return static::FAILURE;
}
// Check for ZEPHPYR_TOKEN
if (! $this->checkForZephpyrToken()) {
return static::FAILURE;
}
// Check if the token is valid
if (! $this->checkAuthenticated()) {
$this->error('Invalid API token: check your ZEPHPYR_TOKEN on '.$this->baseUrl().'user/api-tokens');
return static::FAILURE;
}
// Download the latest bundle if requested
if ($this->option('fetch')) {
if (! $this->fetchLatestBundle()) {
return static::FAILURE;
}
$this->info('Latest bundle downloaded.');
return static::SUCCESS;
}
$this->preProcess();
$this->setAppName();
intro('Copying App to build directory...');
// We update composer.json later,
$this->copyToBuildDirectory();
$this->newLine();
intro('Cleaning .env file...');
$this->cleanEnvFile();
$this->newLine();
intro('Copying app icons...');
$this->installIcon();
$this->newLine();
intro('Pruning vendor directory');
$this->pruneVendorDirectory();
$this->cleanEnvFile();
// Check composer.json for symlinked or private packages
if (! $this->checkComposerJson()) {
return static::FAILURE;
}
// Package the app up into a zip
if (! $this->zipApplication()) {
$this->error("Failed to create zip archive at {$this->zipPath}.");
return static::FAILURE;
}
// Send the zip file
$result = $this->sendToZephpyr();
$this->handleApiErrors($result);
// Success
$this->info('Successfully uploaded to Zephpyr.');
$this->line('Use native:bundle --fetch to retrieve the latest bundle.');
// Clean up temp files
$this->cleanUp();
return static::SUCCESS;
}
private function zipApplication(): bool
{
$this->zipName = 'app_'.str()->random(8).'.zip';
$this->zipPath = $this->zipPath($this->zipName);
// Create zip path
if (! @mkdir(dirname($this->zipPath), recursive: true) && ! is_dir(dirname($this->zipPath))) {
return false;
}
$zip = new ZipArchive;
if ($zip->open($this->zipPath, ZipArchive::CREATE | ZipArchive::OVERWRITE) !== true) {
return false;
}
$this->addFilesToZip($zip);
$zip->close();
return true;
}
private function checkComposerJson(): bool
{
$composerJson = json_decode(file_get_contents($this->buildPath('composer.json')), true);
// // Fail if there is symlinked packages
// foreach ($composerJson['repositories'] ?? [] as $repository) {
//
// $symlinked = $repository['options']['symlink'] ?? true;
// if ($repository['type'] === 'path' && $symlinked) {
// $this->error('Symlinked packages are not supported. Please remove them from your composer.json.');
//
// return false;
// }
// // Work with private packages but will not in the future
// // elseif ($repository['type'] === 'composer') {
// // if (! $this->checkComposerPackageAuth($repository['url'])) {
// // $this->error('Cannot authenticate with '.$repository['url'].'.');
// // $this->error('Go to '.$this->baseUrl().' and add your composer package credentials.');
// //
// // return false;
// // }
// // }
// }
// Remove repositories with type path, we include symlinked packages
if (! empty($composerJson['repositories'])) {
$this->newLine();
intro('Patching composer.json in development mode…');
$filteredRepo = array_filter($composerJson['repositories'],
fn ($repository) => $repository['type'] !== 'path');
if (count($filteredRepo) !== count($composerJson['repositories'])) {
$composerJson['repositories'] = $filteredRepo;
file_put_contents($this->buildPath('composer.json'),
json_encode($composerJson, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
// Process::path($this->buildPath())
// ->run('composer install --no-dev', function (string $type, string $output) {
// echo $output;
// });
}
}
return true;
}
// private function checkComposerPackageAuth(string $repositoryUrl): bool
// {
// // Check if the user has authenticated the package on Zephpyr
// $host = parse_url($repositoryUrl, PHP_URL_HOST);
// $this->line('Checking '.$host.' authentication…');
//
// return Http::acceptJson()
// ->withToken(config('nativephp-internal.zephpyr.token'))
// ->get($this->baseUrl().'api/v1/project/'.$this->key.'/composer/auth/'.$host)
// ->successful();
// }
private function addFilesToZip(ZipArchive $zip): void
{
$this->newLine();
intro('Creating zip archive…');
$finder = (new Finder)->files()
->followLinks()
// ->ignoreVCSIgnored(true) // TODO: Make our own list of ignored files
->in($this->buildPath())
->exclude([
// We add those a few lines below and they are ignored by most .gitignore anyway
'vendor',
'node_modules',
// Exclude the following directories
'dist', // Compiled nativephp assets
'build', // Compiled box assets
'temp', // Temp files
'tests', // Tests
])
->exclude(config('nativephp.cleanup_exclude_files', []));
$this->finderToZip($finder, $zip);
// Why do I have to force this? please someone explain.
$this->finderToZip(
(new Finder)->files()
->followLinks()
->in($this->buildPath('public/build')), $zip, 'public/build');
// Add .env file manually because Finder ignores VCS and dot files
$zip->addFile($this->buildPath('.env'), '.env');
// Add auth.json file to support private packages
// WARNING: Only for testing purposes, don't uncomment this
// $zip->addFile($this->buildPath('auth.json'), 'auth.json');
// Custom binaries
$binaryPath = Str::replaceStart($this->buildPath('vendor'), '', config('nativephp.binary_path'));
// Add composer dependencies without unnecessary files
$vendor = (new Finder)->files()
->exclude(array_filter([
'nativephp/php-bin',
'nativephp/electron/resources/js',
'*/*/vendor', // Exclude sub-vendor directories
$binaryPath,
]))
->in($this->buildPath('vendor'));
$this->finderToZip($vendor, $zip, 'vendor');
// Add javascript dependencies
if (file_exists($this->buildPath('node_modules'))) {
$nodeModules = (new Finder)->files()
->in($this->buildPath('node_modules'));
$this->finderToZip($nodeModules, $zip, 'node_modules');
}
}
private function finderToZip(Finder $finder, ZipArchive $zip, ?string $path = null): void
{
foreach ($finder as $file) {
if ($file->getRealPath() === false) {
continue;
}
$zip->addFile($file->getRealPath(), str($path)->finish(DIRECTORY_SEPARATOR).$file->getRelativePathname());
}
}
private function sendToZephpyr()
{
intro('Uploading zip to Zephpyr…');
return Http::acceptJson()
->timeout(300) // 5 minutes
->withoutRedirecting() // Upload won't work if we follow redirects (it transform POST to GET)
->withToken(config('nativephp-internal.zephpyr.token'))
->attach('archive', fopen($this->zipPath, 'r'), $this->zipName)
->post($this->baseUrl().'api/v1/project/'.$this->key.'/build/');
}
private function fetchLatestBundle(): bool
{
intro('Fetching latest bundle…');
$response = Http::acceptJson()
->withToken(config('nativephp-internal.zephpyr.token'))
->get($this->baseUrl().'api/v1/project/'.$this->key.'/build/download');
if ($response->failed()) {
if ($response->status() === 404) {
$this->error('Project or bundle not found.');
} elseif ($response->status() === 500) {
$url = $response->json('url');
if ($url) {
$this->error('Build failed. Inspect the build here: '.$url);
} else {
$this->error('Build failed. Please try again later.');
}
} elseif ($response->status() === 503) {
$retryAfter = intval($response->header('Retry-After'));
$diff = now()->addSeconds($retryAfter);
$diffMessage = $retryAfter <= 60 ? 'a minute' : $diff->diffForHumans(syntax: CarbonInterface::DIFF_ABSOLUTE);
$this->warn('Bundle not ready. Please try again in '.$diffMessage.'.');
} else {
$this->handleApiErrors($response);
}
return false;
}
// Save the bundle
@mkdir(base_path('build'), recursive: true);
file_put_contents(base_path('build/__nativephp_app_bundle'), $response->body());
return true;
}
protected function exitWithMessage(string $message): void
{
$this->error($message);
$this->cleanUp();
exit(static::FAILURE);
}
private function handleApiErrors(Response $result): void
{
if ($result->status() === 413) {
$fileSize = Number::fileSize(filesize($this->zipPath));
$this->exitWithMessage('File is too large to upload ('.$fileSize.'). Please contact support.');
} elseif ($result->status() === 422) {
$this->error('Request refused:'.$result->json('message'));
} elseif ($result->status() === 429) {
$this->exitWithMessage('Too many requests. Please try again in '.now()->addSeconds(intval($result->header('Retry-After')))->diffForHumans(syntax: CarbonInterface::DIFF_ABSOLUTE).'.');
} elseif ($result->failed()) {
$this->exitWithMessage("Request failed. Error code: {$result->status()}");
}
}
protected function cleanUp(): void
{
$this->postProcess();
if ($this->option('without-cleanup')) {
return;
}
$previousBuilds = glob($this->zipPath().'/app_*.zip');
$failedZips = glob($this->zipPath().'/app_*.part');
$deleteFiles = array_merge($previousBuilds, $failedZips);
if (empty($deleteFiles)) {
return;
}
$this->line('Cleaning up…');
foreach ($deleteFiles as $file) {
@unlink($file);
}
}
protected function buildPath(string $path = ''): string
{
return base_path('build/app/'.$path);
}
protected function zipPath(string $path = ''): string
{
return base_path('build/zip/'.$path);
}
protected function sourcePath(string $path = ''): string
{
return base_path($path);
}
}