forked from Spomky-Labs/otphp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcastor.php
More file actions
254 lines (225 loc) · 7.17 KB
/
castor.php
File metadata and controls
254 lines (225 loc) · 7.17 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
<?php
declare(strict_types=1);
use Castor\Attribute\AsRawTokens;
use Castor\Attribute\AsTask;
use function Castor\context;
use function Castor\guard_min_version;
use function Castor\io;
use function Castor\run;
guard_min_version('v0.23.0');
/**
* @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->withEnvironment([
'XDEBUG_MODE' => 'off',
]);
$context->withQuiet();
$result = run($command, context: $context);
if (! $result->isSuccessful()) {
io()->error('Cannot determine licenses');
exit(1);
}
$licenses = json_decode((string) $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: '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
{
$inContainer = file_exists('/.dockerenv');
$hasDocker = trim(shell_exec('command -v docker') ?? '') !== '';
if (! $hasDocker || $inContainer) {
run($args);
return;
}
run(['php', ...$args]);
}
function phpqa(array $command, array $dockerOptions = []): void
{
$inContainer = file_exists('/.dockerenv');
$hasDocker = trim(shell_exec('command -v docker') ?? '') !== '';
$phpVersion = getenv('PHP_VERSION') ?: \PHP_MAJOR_VERSION . '.' . \PHP_MINOR_VERSION;
if (! $hasDocker || $inContainer) {
run($command);
return;
}
$defaultDockerOptions = [
'--rm',
'--init',
'-it',
'--user', sprintf('%s:%s', getmyuid(), getmygid()),
'--pull', 'always',
'-v', getcwd() . ':/project',
'-v', getcwd() . '/tmp-phpqa:/project/tmp-phpqa',
'-w', '/project',
'-e', 'XDEBUG_MODE=off',
'-e', 'PHP_INI_SCAN_DIR=/usr/local/etc/php/conf.d',
'-e', 'PHP_INI_ENTRY=sys_temp_dir=/project/tmp-phpqa',
];
run([
'docker', 'run',
...$defaultDockerOptions,
...$dockerOptions,
sprintf('ghcr.io/spomky-labs/phpqa:%s', $phpVersion),
...$command,
]);
}
#[AsTask(description: 'Update the PHPQA Docker image')]
function phpqa_update(): void
{
$phpVersion = getenv('PHP_VERSION') ?: (\PHP_MAJOR_VERSION . '.' . \PHP_MINOR_VERSION);
run(['docker', 'pull', 'ghcr.io/spomky-labs/phpqa:' . $phpVersion]);
}
#[AsTask(description: 'Run PHPUnit tests with coverage', ignoreValidationErrors: true)]
function phpunit(#[AsRawTokens] array $args = []): void
{
phpqa(
[
'composer', 'exec', '--', 'phpunit-11',
'--coverage-xml', '.ci-tools/coverage',
'--log-junit=.ci-tools/coverage/junit.xml',
'--configuration', '.ci-tools/phpunit.xml.dist',
'--display-warnings',
'--display-deprecations',
...$args,
],
['-e', 'XDEBUG_MODE=coverage']
);
}
#[AsTask(description: 'Run Easy Coding Standard')]
function ecs(): void
{
phpqa(['composer', 'exec', '--', 'ecs', 'check', '--config', '.ci-tools/ecs.php']);
}
#[AsTask(description: 'Fix coding style with Easy Coding Standard')]
function ecs_fix(): void
{
phpqa(['composer', 'exec', '--', 'ecs', 'check', '--config', '.ci-tools/ecs.php', '--fix']);
}
#[AsTask(description: 'Run Rector dry-run')]
function rector(): void
{
phpqa(['composer', 'exec', '--', 'rector', 'process', '--dry-run', '--config', '.ci-tools/rector.php']);
}
#[AsTask(description: 'Run Rector with fix')]
function rector_fix(): void
{
phpqa(['composer', 'exec', '--', 'rector', 'process', '--config', '.ci-tools/rector.php']);
}
#[AsTask(description: 'Run PHPStan')]
function phpstan(): void
{
phpqa(
[
'composer', 'exec', '--', 'phpstan', 'analyse', '--error-format=github', '--configuration=.ci-tools/phpstan.neon']
);
}
#[AsTask(description: 'Generate PHPStan baseline')]
function phpstan_baseline(): void
{
phpqa([
'composer', 'exec', '--', 'phpstan', 'analyse',
'--configuration=.ci-tools/phpstan.neon',
'--generate-baseline=.ci-tools/phpstan-baseline.neon',
]);
}
#[AsTask(description: 'Run Deptrac')]
function deptrac(): void
{
phpqa([
'composer', 'exec', '--', 'deptrac',
'--config-file', '.ci-tools/deptrac.yaml',
'--report-uncovered',
'--report-skipped',
'--fail-on-uncovered',
]);
}
#[AsTask(description: 'Run PHP parallel linter')]
function lint(): void
{
phpqa(['composer', 'exec', '--', 'parallel-lint', 'src', 'tests']);
}
#[AsTask(description: 'Run Infection for mutation testing')]
function infect($minMsi = 0, $minCoveredMsi = 0): void
{
phpqa([
'composer', 'exec', '--', 'infection',
'--coverage=coverage',
sprintf('--min-msi=%d', $minMsi),
sprintf('--min-covered-msi=%d', $minCoveredMsi),
'--threads=max',
'--logger-github',
'-s',
'--filter=src/',
'--configuration=.ci-tools/infection.json.dist',
], ['-e', 'XDEBUG_MODE=coverage']);
}
#[AsTask(description: 'Run JS tests')]
function js(): void
{
io()->title('Running JS tests');
run(['npm', 'install', '--force']);
run(['npm', 'test']);
}
#[AsTask(description: 'Fix code style and apply Rector rules, then run static analysis.')]
function prepare_pr(): void
{
io()->title('Preparing code for pull request…');
ecs_fix();
rector_fix();
io()
->section('Running static analysis…');
phpstan_baseline();
deptrac();
lint();
io()
->success('Code is ready. You may now commit and push your changes.');
}