-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
castor.php
290 lines (264 loc) · 8.41 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
<?php
declare(strict_types=1);
use Castor\Attribute\AsRawTokens;
use Castor\Attribute\AsTask;
use function Castor\context;
use function Castor\io;
use function Castor\notify;
use function Castor\run;
#[AsTask(description: 'Run mutation testing.')]
function infect(int $minMsi = 0, int $minCoveredMsi = 0, bool $ci = false): void
{
io()->title('Running infection');
$nproc = run('nproc', context: context()->withQuiet());
if (! $nproc->isSuccessful()) {
io()->warning('Cannot determine the number of processors. Setting 1 thread.');
$threads = '1';
} else {
$threads = (int) $nproc->getOutput();
}
$command = [
'php',
'vendor/bin/infection',
sprintf('--min-msi=%s', $minMsi),
sprintf('--min-covered-msi=%s', $minCoveredMsi),
sprintf('--threads=%s', $threads),
];
if ($ci) {
$command[] = '--logger-github';
$command[] = '-s';
}
$context = context();
$context = $context->withEnvironment([
'XDEBUG_MODE' => 'coverage',
]);
run($command, context: $context);
}
#[AsTask(description: 'Run PHPUnit tests.', ignoreValidationErrors: true)]
function test(#[AsRawTokens] array $args = []): void
{
io()->title('Running tests');
$command = ['php', 'vendor/bin/phpunit', ...$args];
$context = context();
$context = $context->withEnvironment([
'XDEBUG_MODE' => 'coverage',
]);
run($command, context: $context);
}
#[AsTask(description: 'Coding standards check.')]
function cs(bool $fix = false): void
{
io()->title('Running coding standards check');
$command = ['php', 'vendor/bin/ecs', 'check'];
$context = context();
$context = $context->withEnvironment([
'XDEBUG_MODE' => 'off',
]);
if ($fix) {
$command[] = '--fix';
}
run($command, context: $context);
}
#[AsTask(description: 'Running PHPStan.')]
function stan(bool $baseline = false): void
{
io()->title('Running PHPStan');
$options = ['analyse'];
if ($baseline) {
$options[] = '--generate-baseline';
}
$command = ['php', 'vendor/bin/phpstan', ...$options];
$context = context();
$context = $context->withEnvironment([
'XDEBUG_MODE' => 'off',
]);
run($command, context: $context);
}
#[AsTask(description: 'Validate Composer configuration.')]
function validate(): void
{
io()->title('Validating Composer configuration');
$command = ['composer', 'validate', '--strict'];
$context = context();
$context = $context->withEnvironment([
'XDEBUG_MODE' => 'off',
]);
run($command, context: $context);
$command = ['composer', 'dump-autoload', '--optimize', '--strict-psr'];
run($command, context: $context);
}
/**
* @param array<string> $allowedLicenses
*/
#[AsTask(description: 'Check licenses.')]
function checkLicenses(
array $allowedLicenses = ['Apache-2.0', 'BSD-2-Clause', 'BSD-3-Clause', 'ISC', 'MIT', 'MPL-2.0', 'OSL-3.0']
): void {
io()->title('Checking licenses');
$allowedExceptions = [];
$command = ['composer', 'licenses', '-f', 'json'];
$context = context();
$context = $context->withEnvironment([
'XDEBUG_MODE' => 'off',
]);
$context = $context->withQuiet();
$result = run($command, context: $context);
if (! $result->isSuccessful()) {
io()->error('Cannot determine licenses');
exit(1);
}
$licenses = json_decode($result->getOutput(), true);
$disallowed = array_filter(
$licenses['dependencies'],
static fn (array $info, $name) => ! in_array($name, $allowedExceptions, true)
&& count(array_diff($info['license'], $allowedLicenses)) === 1,
\ARRAY_FILTER_USE_BOTH
);
$allowed = array_filter(
$licenses['dependencies'],
static fn (array $info, $name) => in_array($name, $allowedExceptions, true)
|| count(array_diff($info['license'], $allowedLicenses)) === 0,
\ARRAY_FILTER_USE_BOTH
);
if (count($disallowed) > 0) {
io()
->table(
['Package', 'License'],
array_map(
static fn ($name, $info) => [$name, implode(', ', $info['license'])],
array_keys($disallowed),
$disallowed
)
);
io()
->error('Disallowed licenses found');
exit(1);
}
io()
->table(
['Package', 'License'],
array_map(
static fn ($name, $info) => [$name, implode(', ', $info['license'])],
array_keys($allowed),
$allowed
)
);
io()
->success('All licenses are allowed');
}
#[AsTask(description: 'Run Rector to upgrade code.')]
function rector(bool $fix = false): void
{
io()->title('Running Rector');
$command = ['php', 'vendor/bin/rector', 'process', '--ansi'];
if (! $fix) {
$command[] = '--dry-run';
}
$context = context();
$context = $context->withEnvironment([
'XDEBUG_MODE' => 'off',
]);
run($command, context: $context);
}
#[AsTask(description: 'Run Deptrac to analyze dependencies.')]
function deptrac(): void
{
io()->title('Running Rector');
$command = ['php', 'vendor/bin/deptrac', 'analyse', '--fail-on-uncovered', '--no-cache'];
$context = context();
$context = $context->withEnvironment([
'XDEBUG_MODE' => 'off',
]);
run($command, context: $context);
}
#[AsTask(description: 'Restart the containers.')]
function restart(): void
{
stop();
start();
}
#[AsTask(description: 'Clean the infrastructure (remove container, volume, networks).')]
function destroy(bool $force = false): void
{
if (! $force) {
io()->warning('This will permanently remove all containers, volumes, networks... created for this project.');
io()
->comment('You can use the --force option to avoid this confirmation.');
if (! io()->confirm('Are you sure?', false)) {
io()->comment('Aborted.');
return;
}
}
run('docker-compose down -v --remove-orphans --volumes --rmi=local');
notify('The infrastructure has been destroyed.');
}
#[AsTask(description: 'Stops and removes the containers.')]
function stop(): void
{
run(['docker', 'compose', 'down']);
}
#[AsTask(description: 'Starts the containers.')]
function start(): void
{
run(['docker', 'compose', 'up', '-d']);
frontend(true);
}
#[AsTask(description: 'Build the images.')]
function build(): void
{
run(['docker', 'compose', 'build', '--no-cache', '--pull']);
}
#[AsTask(description: 'Compile the frontend.')]
function frontend(bool $watch = false): void
{
$consoleOutput = run(['bin/console'], context: context()->withQuiet());
$commandsToRun = [
'assets:install' => [],
'importmap:install' => [],
'tailwind:build' => $watch ?['--watch'] : [],
'asset-map:compile' => [],
];
foreach ($commandsToRun as $command => $arguments) {
if (str_contains($consoleOutput->getOutput(), $command)) {
php(['bin/console', $command, ...$arguments]);
}
}
if (file_exists('yarn.lock')) {
run(['yarn', 'install']);
run(['yarn', $watch ? 'watch' : 'build']);
}
}
#[AsTask(description: 'Update the dependencies and other features.')]
function update(): void
{
run(['composer', 'update']);
$consoleOutput = run(['bin/console'], context: context()->withQuiet());
$commandsToRun = [
'doctrine:migrations:migrate' => [],
'doctrine:schema:validate' => [],
'doctrine:fixtures:load' => [],
'geoip2:update' => [],
'app:browscap:update' => [],
'importmap:update' => [],
];
foreach ($commandsToRun as $command => $arguments) {
if (str_contains($consoleOutput->getOutput(), $command)) {
php(['bin/console', $command, ...$arguments]);
}
}
}
#[AsTask(description: 'Runs a Consumer from the Docket Container.')]
function consume(): void
{
php(['bin/console', 'messenger:consume', '--all']);
}
#[AsTask(description: 'Runs a Symfony Console command from the Docket Container.', ignoreValidationErrors: true)]
function console(#[AsRawTokens] array $args = []): void
{
php(['bin/console', ...$args]);
}
#[AsTask(description: 'Runs a PHP command from the Docket Container.', ignoreValidationErrors: true)]
function php(#[AsRawTokens] array $args = []): void
{
run(['docker', 'compose', 'exec', '-T', 'php', ...$args]);
}