generated from dunglas/symfony-docker
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcastor.php
313 lines (284 loc) · 8.8 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
<?php
declare(strict_types=1);
use Castor\Attribute\AsRawTokens;
use Castor\Attribute\AsTask;
use Castor\Context;
use function Castor\context;
use function Castor\io;
use function Castor\notify;
use function Castor\run;
#[AsTask(description: 'Run tests')]
function test(
bool $coverageHtml = false,
bool $coverageText = false,
bool $gitlab = false,
null|string $coverageCobertura = null,
null|string $logJunit = null,
null|string $group = null
): void {
io()->title('Running tests');
if (! $gitlab) {
run(
[
'docker', 'exec', '-it', 'chp-database-1', 'mariadb', '-p!ChangeMe!', '-e', 'CREATE DATABASE IF NOT EXISTS app_test; GRANT ALL ON app_test.* TO \'app\'@\'%\'; flush privileges;']
);
console(['doctrine:migrations:migrate', '--env=test', '--no-interaction']);
}
$command = ['vendor/bin/phpunit', '--color'];
if ($coverageText) {
$command = ['vendor/bin/phpunit'];
}
$xdebugMode = 'Off';
if ($coverageHtml) {
$command[] = '--coverage-html=build/coverage';
$xdebugMode = 'coverage';
}
if ($coverageText) {
$command[] = '--coverage-text';
$xdebugMode = 'coverage';
}
if ($coverageCobertura) {
$command[] = sprintf('--coverage-cobertura=%s', $coverageCobertura);
$xdebugMode = 'coverage';
}
if ($logJunit) {
$command[] = sprintf('--log-junit=%s', $logJunit);
$xdebugMode = 'coverage';
}
if ($group !== null) {
$command[] = sprintf('--group=%s', $group);
}
if ($gitlab) {
$command[] = '--stop-on-defect';
run($command, context: context()->withEnvironment([
'XDEBUG_MODE' => $xdebugMode,
]));
return;
}
php($command, context: context()->withEnvironment([
'XDEBUG_MODE' => $xdebugMode,
]));
}
#[AsTask(description: 'Coding standards check')]
function cs(bool $fix = false, bool $clearCache = false, bool $gitlab = false): void
{
io()->title('Running coding standards check');
$command = ['vendor/bin/ecs', 'check'];
if ($fix) {
$command[] = '--fix';
}
if ($clearCache) {
$command[] = '--clear-cache';
}
if ($gitlab) {
run($command, context: context()->withEnvironment([
'XDEBUG_MODE' => 'off',
]));
return;
}
php($command, context: context()->withEnvironment([
'XDEBUG_MODE' => 'off',
]));
}
#[AsTask(description: 'Running PHPStan')]
function stan(bool $baseline = false, bool $gitlab = false): void
{
io()->title('Running PHPStan');
$options = ['analyse'];
if ($baseline) {
$options[] = '--generate-baseline';
}
$command = ['vendor/bin/phpstan', ...$options];
if ($gitlab) {
run($command, context: context()->withEnvironment([
'XDEBUG_MODE' => 'off',
]));
return;
}
php($command, context: context()->withEnvironment([
'XDEBUG_MODE' => 'off',
]));
}
#[AsTask(description: 'Validate Composer configuration')]
function validate(): void
{
io()->title('Validating Composer configuration');
$command = ['composer', 'validate', '--strict'];
run($command, context: context()->withEnvironment([
'XDEBUG_MODE' => 'off',
]));
$command = ['composer', 'dump-autoload', '--optimize', '--strict-psr'];
run($command, context: context()->withEnvironment([
'XDEBUG_MODE' => 'off',
]));
}
#[AsTask(description: 'Composer audit')]
function audit(): void
{
io()->title('Running composer audit');
$command = ['composer', 'audit'];
run($command, context: context()->withEnvironment([
'XDEBUG_MODE' => 'off',
]));
}
#[AsTask(description: 'Run Rector')]
function rector(bool $fix = false, bool $clearCache = false, bool $gitlab = false): void
{
io()->title('Running Rector');
$command = ['vendor/bin/rector', 'process', '--ansi'];
if (! $fix) {
$command[] = '--dry-run';
}
if ($clearCache) {
$command[] = '--clear-cache';
}
if ($gitlab) {
run($command, context: context()->withEnvironment([
'XDEBUG_MODE' => 'off',
]));
return;
}
php($command, context: context()->withEnvironment([
'XDEBUG_MODE' => 'off',
]));
}
#[AsTask(description: 'Run Deptrac')]
function deptrac(bool $show = false, bool $gitlab = false): void
{
io()->title('Running Deptrac');
$command = ['vendor/bin/deptrac', 'analyse', '--fail-on-uncovered', '--no-cache'];
if ($show) {
$command[] = '--report-uncovered';
}
if ($gitlab) {
run($command, context: context()->withEnvironment([
'XDEBUG_MODE' => 'off',
]));
return;
}
php($command, context: context()->withEnvironment([
'XDEBUG_MODE' => 'off',
]));
}
#[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 down(): void
{
run(['docker', 'compose', 'down']);
}
#[AsTask(description: 'Stops the containers.')]
function stop(): void
{
run(['docker', 'compose', 'stop']);
}
#[AsTask(description: 'Wakes up the containers.')]
function up(string $xdebugMode = 'develop'): void
{
run(['docker', 'compose', 'up', '-d', '--wait'], context: context()->withEnvironment([
'XDEBUG_MODE' => $xdebugMode,
]));
}
#[AsTask(description: 'Starts the containers.')]
function start(string $xdebugMode = 'develop', bool $fixtures = false): void
{
up($xdebugMode);
console(['doctrine:migrations:migrate', '--no-interaction']);
if ($fixtures) {
console(['doctrine:fixtures:load', '--no-interaction']);
}
frontend();
}
#[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'] : [],
'sass:build' => [],
];
if ($watch === false) {
$commandsToRun['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(bool $fixtures = false): void
{
run(['composer', 'update']);
$consoleOutput = run(['bin/console'], context: context()->withQuiet());
$commandsToRun = [
'doctrine:migrations:migrate' => [],
'doctrine:schema:validate' => [],
'geoip2:update' => [],
'app:browscap:update' => [],
'importmap:update' => [],
];
if ($fixtures) {
$commandsToRun['doctrine:fixtures:load'] = [];
}
foreach ($commandsToRun as $command => $arguments) {
if (str_contains($consoleOutput->getOutput(), $command)) {
php(['bin/console', $command, ...$arguments]);
}
}
}
#[AsTask(description: 'Runs a Consumer from the Docker Container.')]
function consume(): void
{
php(['bin/console', 'messenger:consume', '--all']);
}
#[AsTask(description: 'Runs a Symfony Console command from the Docker Container.', ignoreValidationErrors: true)]
function console(#[AsRawTokens] array $args = []): void
{
php(['bin/console', ...$args]);
}
#[AsTask(description: 'Runs a PHP command from the Docker Container.', ignoreValidationErrors: true)]
function php(#[AsRawTokens] array $args = [], ?Context $context = null): void
{
run(['docker', 'compose', 'exec', '-T', 'php', ...$args], context: $context);
}
#[AsTask(description: 'Initialize the database and fixtures.')]
function init(): void
{
console(['d:d:c', '--if-not-exists']);
console(['d:m:m', '-n']);
console(['app:init:users']);
console(['app:init:regions']);
console(['app:init:departments']);
console(['app:init:cities']);
}