-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathcastor.php
More file actions
461 lines (376 loc) · 13 KB
/
Copy pathcastor.php
File metadata and controls
461 lines (376 loc) · 13 KB
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
<?php
// This script was tested with Castor 1.1.0
// https://castor.jolicode.com/changelog/#110-2025-11-26
declare(strict_types=1);
use Castor\Attribute\AsTask;
use function Castor\context;
use function Castor\exit_code;
use function Castor\io;
use function Castor\run;
use function Castor\task;
// use function Castor\parallel;
// Change your prod domain here
const DOMAIN = 'microsymfony.ovh';
// Modify the code coverage threshold here
const COVERAGE_THRESHOLD = 100;
const COVERAGE_TEXT_REPORT = 'var/coverage/coverage-text.txt';
function title(string $name): void
{
$task = task();
if ($task !== null && $task->getName() === $name) {
io()->title($task->getDescription());
}
}
function success(int $exitCode): int
{
if ($exitCode === 0) {
io()->success('Done!');
} else {
io()->error(sprintf('Failure (exit code %d returned).', $exitCode));
}
return $exitCode;
}
function aborted(string $message = 'Aborted'): void
{
io()->warning($message);
}
#[AsTask(namespace: 'symfony', description: 'Serve the application with the Symfony binary', aliases: ['start'])]
function start(): void
{
title('symfony:start');
loadFixtures();
run('symfony serve --daemon');
}
#[AsTask(namespace: 'symfony', description: 'Stop the web server', aliases: ['stop'])]
function stop(): void
{
title('symfony:stop');
run('symfony server:stop');
resetDatabase();
}
#[AsTask(namespace: 'symfony', description: 'Switch to the production environment', aliases: ['go-prod'])]
function go_prod(): void
{
title('symfony:go_prod');
if (io()->confirm('Are you sure you want to switch to the production environment? This will overwrite your .env.local file.', false)) {
run('cp .env.local.dist .env.local');
run('bin/console asset-map:compile');
success(0);
return;
}
aborted();
}
#[AsTask(namespace: 'symfony', description: 'Switch to the development environment', aliases: ['go-dev'])]
function go_dev(): void
{
title('symfony:go_dev');
if (io()->confirm('Are you sure you want to switch to the development environment? This will delete your .env.local file.', false)) {
run('rm -f .env.local');
run('rm -rf ./public/assets/*');
success(0);
return;
}
aborted();
}
#[AsTask(namespace: 'symfony', description: 'Purge all Symfony cache and logs', aliases: ['purge'])]
function purge(): void
{
title('symfony:purge');
success(exit_code('rm -rf ./var/cache/* ./var/log/* ./var/coverage/*'));
}
#[AsTask(namespace: 'app', description: 'Load the database fixtures', aliases: ['load-fixtures'])]
function loadFixtures(): void
{
title('app:load-fixtures');
resetDatabase();
io()->note('Running db migrations...');
success(exit_code('bin/console doctrine:migrations:migrate --no-interaction'));
io()->note('Load fixtures...');
success(exit_code('bin/console foundry:load-fixtures --env=dev --no-interaction'));
}
function resetDatabase(): void
{
io()->note('Resetting db...');
success(exit_code('rm -f ./var/data.db'));
}
const PHP_UNIT_CMD = '/vendor/bin/phpunit --testsuite=%s --filter=%s %s';
const PHP_UNIT_SUITES = ['api', 'e2e', 'functional', 'integration', 'unit'];
function getParameters(): array
{
$filter = !empty(getenv('filter')) ? getenv('filter') : '.';
$options = !empty(getenv('options')) ? getenv('options') : '--stop-on-failure';
return [$filter, $options];
}
#[AsTask(name: 'all', namespace: 'test', description: 'Run tests with optional filter and options, eg: "filter=slug options=--testdox castor test"', aliases: ['test'])]
function test_all(): int
{
title('test:all');
loadFixtures();
[$filter, $options] = getParameters();
$ec = exit_code(__DIR__.sprintf(PHP_UNIT_CMD, implode(',', PHP_UNIT_SUITES), $filter, $options));
io()->writeln('');
return $ec;
}
#[AsTask('api', namespace: 'test', description: 'Run API tests only', aliases: ['test-api'])]
function test_api(): int
{
title('test:api');
[$filter, $options] = getParameters();
$ec = exit_code(__DIR__.sprintf(PHP_UNIT_CMD, 'api', $filter, $options));
io()->writeln('');
return $ec;
}
#[AsTask('e2e', namespace: 'test', description: 'Run E2E tests only', aliases: ['test-e2e'])]
function test_e2e(): int
{
title('test:api');
[$filter, $options] = getParameters();
$ec = exit_code(__DIR__.sprintf(PHP_UNIT_CMD, 'e2e', $filter, $options));
io()->writeln('');
return $ec;
}
#[AsTask('functional', namespace: 'test', description: 'Run functional tests only', aliases: ['test-functional'])]
function test_functional(): int
{
title('test:functional');
[$filter, $options] = getParameters();
$ec = exit_code(__DIR__.sprintf(PHP_UNIT_CMD, 'functional', $filter, $options));
io()->writeln('');
return $ec;
}
#[AsTask('integration', namespace: 'test', description: 'Run integration tests only', aliases: ['test-integration'])]
function test_integration(): int
{
title('test:integration');
[$filter, $options] = getParameters();
$ec = exit_code(__DIR__.sprintf(PHP_UNIT_CMD, 'integration', $filter, $options));
io()->writeln('');
return $ec;
}
#[AsTask('unit', namespace: 'test', description: 'Run unit tests only', aliases: ['test-unit'])]
function test_unit(
): int {
title('test:unit');
[$filter, $options] = getParameters();
$ec = exit_code(__DIR__.sprintf(PHP_UNIT_CMD, 'unit', $filter, $options));
io()->writeln('');
return $ec;
}
#[AsTask(namespace: 'test', description: 'Generate the HTML PHPUnit code coverage report (stored in var/coverage)', aliases: ['coverage'])]
function coverage(): int
{
title('test:coverage');
purge();
loadFixtures();
$ec = exit_code(sprintf('php -d xdebug.enable=1 -d memory_limit=-1 vendor/bin/phpunit --coverage-html=var/coverage --coverage-text=%s', COVERAGE_TEXT_REPORT),
context: context()->withEnvironment(['XDEBUG_MODE' => 'coverage'])
);
if ($ec !== 0) {
return $ec;
}
return success(exit_code(sprintf('php bin/coverage-checker.php %s %d', COVERAGE_TEXT_REPORT, COVERAGE_THRESHOLD)));
}
#[AsTask(namespace: 'test', description: 'Open the PHPUnit code coverage report (var/coverage/index.html)', aliases: ['cov-report'])]
function cov_report(): int
{
title('test:cov-report');
$coverageFile = 'var/coverage/index.html';
if (!file_exists($coverageFile)) {
coverage();
}
return success(exit_code(sprintf('open %s', $coverageFile)));
}
#[AsTask(namespace: 'lint', description: 'Run PHPStan', aliases: ['stan'])]
function stan(): int
{
title('lint:stan');
if (!file_exists('var/cache/dev/App_KernelDevDebugContainer.xml')) {
io()->note('PHPStan needs the dev/debug cache. Generating it...');
run('bin/console cache:warmup',
context: context()->withEnvironment(['APP_ENV' => 'dev', 'APP_DEBUG' => 1])
);
}
return exit_code('vendor/bin/phpstan analyse -c phpstan.neon --memory-limit 1G -vv');
}
#[AsTask(name: 'php', namespace: 'fix', description: 'Fix PHP files with php-cs-fixer (ignore PHP 8.4 warnings)', aliases: ['fix-php'])]
function fix_php(): int
{
title('fix:fix-php');
$ec = exit_code('vendor/bin/php-cs-fixer fix',
context: context()->withEnvironment(['PHP_CS_FIXER_IGNORE_ENV' => 1])
);
return success($ec);
}
function checkBiome(): void
{
if (!file_exists('bin/biome')) {
io()->note('Biome executable not found, donwloading it...');
run('bin/console biomejs:download');
}
}
#[AsTask(name: 'js-css', namespace: 'fix', description: 'Format JS/CSS files with Biome', aliases: ['fix-js-css'])]
function fix_js_css(): int
{
checkBiome();
title('fix:js-css');
$ec = exit_code('bin/biome check . --write');
io()->newLine();
return success($ec);
}
#[AsTask(name: 'php', namespace: 'lint', description: 'Lint PHP files with php-cs-fixer (report only)', aliases: ['lint-php'])]
function lint_php(): int
{
title('lint:php');
$ec = exit_code('vendor/bin/php-cs-fixer fix --dry-run',
context: context()->withEnvironment(['PHP_CS_FIXER_IGNORE_ENV' => 1])
);
io()->newLine();
return success($ec);
}
#[AsTask(name: 'js-css', namespace: 'lint', description: 'Lint JS/CSS files with Biome', aliases: ['lint-js-css'])]
function lint_js_css(): int
{
checkBiome();
title('lint:js-css');
$ec = exit_code('bin/biome check .');
io()->newLine();
return success($ec);
}
#[AsTask(name: 'lint-js-css', namespace: 'ci', description: 'Lint JS/CSS files with Biome (CI)')]
function ci_lint_js_css(): int
{
title('ci:lint-js-css');
checkBiome();
$ec = exit_code('bin/biome ci . &> /dev/null');
io()->newLine();
return success($ec);
}
#[AsTask(name: 'lint-php', namespace: 'ci', description: 'Lint PHP files with php-cs-fixer (for CI)')]
function ci_lint_php(): int
{
title('ci:lint-php');
$ec = exit_code('command -v cs2pr &> /dev/null');
if ($ec !== 0) {
aborted('cs2pr not found. Locally, Please use the "lint:php" task.');
return 1;
}
return exit_code('vendor/bin/php-cs-fixer fix --allow-risky=yes --dry-run --format=checkstyle | cs2pr',
context: context()->withEnvironment(['PHP_CS_FIXER_IGNORE_ENV' => 1])
);
}
#[AsTask(name: 'all', namespace: 'fix', description: 'Run all fixers', aliases: ['fix'])]
function fix_all(): int
{
title('fix:all');
$ec1 = fix_php();
$ec2 = fix_js_css();
io()->newLine();
return success($ec1 + $ec2);
}
#[AsTask(name: 'container', namespace: 'lint', description: 'Lint the Symfony DI container', aliases: ['lint-container'])]
function lint_container(): int
{
title('lint:container');
return exit_code('bin/console lint:container');
}
#[AsTask(name: 'twig', namespace: 'lint', description: 'Lint Twig files', aliases: ['lint-twig'])]
function lint_twig(): int
{
title('lint:twig');
return exit_code('bin/console lint:twig templates/');
}
#[AsTask(name: 'doctrine', namespace: 'lint', description: 'Validate Doctrine schema', aliases: ['lint-doctrine'])]
function lint_doctrine(): int
{
title('lint:doctrine');
return exit_code('bin/console doctrine:schema:validate');
}
#[AsTask(name: 'all', namespace: 'lint', description: 'Run all lints', aliases: ['lint'])]
function lint_all(): int
{
title('lint:all');
$ec1 = stan();
$ec2 = lint_php();
$ec3 = lint_doctrine();
$ec4 = lint_js_css();
$ec5 = lint_container();
$ec6 = lint_twig();
return success($ec1 + $ec2 + $ec3 + $ec4 + $ec5 + $ec6);
// if you want to speed up the process, you can run these commands in parallel
// parallel(
// fn() => lint_php(),
// fn() => lint_doctrine(),
// fn() => lint_container(),
// fn() => lint_twig(),
// );
}
#[AsTask(name: 'all', namespace: 'ci', description: 'Run CI locally', aliases: ['ci'])]
function ci(): void
{
title('ci:all');
purge();
loadFixtures();
io()->section('Coverage');
coverage();
io()->section('Lints');
lint_all();
}
#[AsTask(name: 'versions', namespace: 'helpers', description: 'Output current stack versions', aliases: ['versions'])]
function versions(): void
{
title('helpers:versions');
io()->note('Castor');
run('castor --version');
io()->newLine();
io()->note('PHP');
run('php -v');
io()->newLine();
io()->note('Composer');
run('composer --version');
io()->newLine();
io()->note('Symfony');
run('bin/console --version');
io()->newLine();
io()->note('PHPUnit');
run('vendor/bin/phpunit --version');
io()->note('PHPStan');
run('vendor/bin/phpstan --version');
io()->newLine();
io()->note('php-cs-fixer');
exit_code('vendor/bin/php-cs-fixer --version',
context: context()->withEnvironment(['PHP_CS_FIXER_IGNORE_ENV' => 1])
);
io()->newLine();
success(0);
}
#[AsTask(name: 'check-requirements', namespace: 'helpers', description: 'Checks requirements for running Symfony', aliases: ['check-requirements'])]
function check_requirements(): int
{
$ec = exit_code('vendor/bin/requirements-checker');
io()->newLine();
return success($ec);
}
#[AsTask(name: 'deploy', namespace: 'prod', description: 'Simple manual deploy on a VPS (this is to update the demo site https://microsymfony.ovh/)', aliases: ['deploy'])]
function deploy(): int
{
$ec1 = exit_code('git pull');
io()->newLine();
$ec2 = exit_code('composer install -n');
io()->newLine();
$ec3 = exit_code('chown -R www-data: ./var/*');
io()->newLine();
$ec4 = exit_code('cp .env.local.dist .env.local');
io()->newLine();
$ec5 = exit_code('composer dump-env prod -n');
io()->newLine();
$ec6 = exit_code('bin/console asset-map:compile');
io()->newLine();
return success($ec1 + $ec2 + $ec3 + $ec4 + $ec5 + $ec6);
}
#[AsTask(name: 'le-renew', namespace: 'prod', description: "Renew Let's Encrypt HTTPS certificates", aliases: ['le-renew'])]
function le_renew(): int
{
$ec = exit_code(sprintf('certbot --apache -d %s -d www.%s', DOMAIN, DOMAIN));
io()->newLine();
return success($ec);
}