-
Notifications
You must be signed in to change notification settings - Fork 8
/
castor.php
327 lines (260 loc) · 8.91 KB
/
castor.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
<?php
// Until the 1.x Castor version the API may be unstable
// this script was tested with Castor 0.21.0
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 coverage threshold here
const COVERAGE_THRESHOLD = 100;
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');
run('symfony serve --daemon');
}
#[AsTask(namespace: 'symfony', description: 'Stop the web server', aliases: ['stop'])]
function stop(): void
{
title('symfony:stop');
run('symfony server:stop');
}
#[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/logs/* ./var/coverage/*'));
}
#[AsTask(name: 'all', namespace: 'test', description: 'Run all PHPUnit tests', aliases: ['test'])]
function test_all(): int
{
title('test:all');
$ec = exit_code(__DIR__.'/vendor/bin/phpunit');
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');
$ec = exit_code('php -d xdebug.enable=1 -d memory_limit=-1 vendor/bin/phpunit --coverage-html=var/coverage --coverage-clover=var/coverage/clover.xml',
context: context()->withEnvironment(['XDEBUG_MODE' => 'coverage'])
);
if ($ec !== 0) {
return $ec;
}
return success(exit_code(sprintf('php bin/coverage-checker.php var/coverage/clover.xml %d', COVERAGE_THRESHOLD)));
}
#[AsTask(namespace: 'test', description: 'Open the PHPUnit code coverage report (var/coverage/index.html)', aliases: ['cov-report'])]
function cov_report(): void
{
title('test:cov-report');
success(exit_code('open var/coverage/index.html'));
}
#[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(namespace: 'fix', description: 'Fix PHP files with php-cs-fixer (ignore PHP 8.2 warning)', 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);
}
#[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: '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 CS checks', aliases: ['fix'])]
function fix_all(): int
{
title('fix:all');
$ec1 = fix_php();
io()->newLine();
return success($ec1);
}
#[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: 'yaml', namespace: 'lint', description: 'Lint Yaml files', aliases: ['lint-yaml'])]
function lint_yaml(): int
{
title('lint:yaml');
return exit_code('bin/console lint:yaml --parse-tags config/');
}
#[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_container();
$ec4 = lint_twig();
$ec5 = lint_yaml();
return success($ec1 + $ec2 + $ec3 + $ec4 + $ec5);
// if you want to speed up the process, you can run these commands in parallel
// parallel(
// fn() => lint_php(),
// fn() => lint_container(),
// fn() => lint_twig(),
// fn() => lint_yaml(),
// );
}
#[AsTask(name: 'all', namespace: 'ci', description: 'Run CI locally', aliases: ['ci'])]
function ci(): void
{
title('ci:all');
purge();
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();
$ec4 = exit_code('composer dump-env prod -n');
io()->newLine();
$ec5 = exit_code('bin/console asset-map:compile');
io()->newLine();
return success($ec1 + $ec2 + $ec3 + $ec4 + $ec5);
}
#[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);
}