-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRoboFile.php
More file actions
619 lines (536 loc) · 16.7 KB
/
RoboFile.php
File metadata and controls
619 lines (536 loc) · 16.7 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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
<?php
declare(strict_types = 1);
use Robo\Result;
use Robo\ResultData;
use Robo\Tasks;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Symfony\Component\Process\Process;
/**
* Robo commands.
*/
class RoboFile extends Tasks {
public string $ddev_url = "drupal-actions";
public string $drush_alias_prefix = "current";
public string $mode = "dev";
public string $code_testing_dir = "web/modules/custom";
public function __construct() {
if (isset($_ENV['MODE'])) {
$this->mode = strtolower($_ENV['MODE']);
} else {
$this->mode = "dev";
}
}
/**
* Get drush executable path.
*
* @return string
*/
private function drush() {
$path = $this->projectPath();
return $path . "/vendor/bin/drush ";
}
/**
* Get Drupal site path. Useful for multi-sites.
*
* @param string $site
*
* @return string
*/
private function sitePath(string $site = "default") {
return $this->projectPath() . '/web/sites/' . $site;
}
/**
* Return current project root which contains the web folder.
*
* @return false|string|void
*/
public function projectPath() {
if (realpath("web") !== FALSE) {
return getcwd();
}
$this->say("Cannot run robo from another root path. Aborting.");
exit;
}
private function arrayImplode(array $array) {
return implode(" ", $array);
}
/**
* Checkout to the latest git tag.
*
* @return void
*/
public function pullLatestTag() {
$this->taskExec('git checkout master')->run();
$this->taskExec('git pull')->run();
$last_tag = $this->gitLastTag();
$this->taskExec('git checkout ' . $last_tag)->run();
}
/**
* Get latest git tag.
*
* @return string
*/
private function gitLastTag() {
return $this->taskExec("git describe --tags `git rev-list --tags --max-count=1`")
->printOutput(false)
->run()
->getMessage();
}
/**
* Perform a Code sniffer test, and fix when applicable.
*
* @return \Robo\ResultData|null
* If there was an error a result data object is returned. Or null if
* successful.
*/
public function phpcs(): ?ResultData {
$standards = [
'Drupal',
'DrupalPractice',
'PHPCompatibility',
];
$commands = [
'phpcbf',
'phpcs',
];
$directories = [
'modules/custom',
'themes/custom',
'profiles/custom',
];
$error_code = NULL;
foreach ($directories as $directory) {
foreach ($standards as $standard) {
if ($standard !== "PHPCompatibility") {
$extensions = "php,module,inc,install,test,profile,theme,js,css,yaml,yml,txt,md";
} else {
$extensions = "php,module,inc,install,test,profile,theme";
}
$arguments = "--standard=$standard -p --colors --extensions=" . $extensions;
foreach ($commands as $command) {
if (file_exists("web/" . $directory)) {
$result = $this->_exec("cd web && ../vendor/bin/$command $directory $arguments");
if ($error_code === NULL && !$result->wasSuccessful()) {
$error_code = $result->getExitCode();
}
}
}
}
}
if ($error_code !== NULL) {
return new ResultData($error_code, 'PHPCS found some issues');
}
return NULL;
}
/**
* List, Enable or Disable modules.
* See modules we exclude from config on "settings.php: $settings['config_exclude_modules']".
*
* $modules = "field_ui views_ui devel stage_file_proxy";
*
* @param string $modules
* @param string $status
* @param string $site
*/
public function modulesAction(string $modules = "", string $status = "", string $site = "default") {
$drush = $this->drush();
$command = "pml --no-core --status=enabled --type=module --format=table --fields=name,version";
if ($status === "enable") {
$command = "en -y ";
}
if ($status === "disable") {
$command = "pmu -y ";
}
$this->taskExec($drush . $command . $modules)->run();
}
/**
* Validate a mode.
*
* @param string $mode
*
* @return bool
*/
private function validateMode(string $mode) {
$modes = [
"prd",
"stg",
"dev",
];
if ($mode !== "" && !in_array($mode, $modes)) {
print("Mode " . $mode . " is not valid. Can only use 'dev, stg, prd'.\n");
return FALSE;
}
return TRUE;
}
/**
* Set site MODE (dev, stg, prd)
*
* @param string $mode
* @param string $site
*
* @return void
*/
public function siteSetMode(string $mode, string $site = "default") {
$this->validateMode($mode);
if ($mode === "prd") {
} else {
$this->configDisableCaches($site);
}
}
/**
* Several development tasks for a Dev site.
*
* @param string $site
*
* @return void
*/
public function configDisableCaches(string $site = "default") {
$drush = $this->drush();
// $this->taskExec($drush . 'generate phpstorm-metadata -y')->run();
$this->taskExec($drush . '-y config-set system.performance css.preprocess 0')->run();
$this->taskExec($drush . '-y config-set system.performance js.preprocess 0')->run();
$this->taskExec($drush . '-y config-set system.performance cache.page.max_age 0')->run();
}
/**
* Update an existing site from new code and configuration files.
*
* @param string $mode
* @param string $site
*/
public function siteUpdate(string $mode, string $site = "default") {
$this->validateMode($mode);
$drush = $this->drush();
$this->say("MODE:".$mode);
$this->say("drush:".$drush);
// Git pull master or latest tag.
// Ensure we use only git Tags on Production
if ($mode === "prd") {
$this->pullLatestTag();
} else {
$this->taskGitStack()
->stopOnFail()
->pull("origin", "master")
->run();
}
// Run drush deploy before fetching new code to run hook_update_N
$this->taskExec($drush . 'deploy -y')->run();
// Preparation, remove folders
$this->taskExec("rm -rf web/modules/contrib")->run();
$this->taskExec("rm -rf web/themes/contrib")->run();
$this->taskExec("rm -rf web/profiles/contrib")->run();
// Composer install
if ($mode === "prd") {
$this->taskComposerInstall()->noDev()->optimizeAutoloader()->noInteraction()->run();
} else {
$this->taskComposerInstall()->optimizeAutoloader()->noInteraction()->run();
}
// Run drush deploy again
$this->taskExec($drush . 'deploy -y')->run();
// Enable/disable modules according to MODE
$this->siteSetMode($mode, $site);
// Clear caches
$this->taskExec($drush . 'cache:rebuild -y')->run();
$message = "--- INFO ---\n";
$message .= "> Set mode: ".$mode.".\n";
$message .= "> Project was updated (git, composer, config files).\n";
$this->say($message);
}
/**
* Install a fresh site from existing configuration files.
*
* @param string $site
*/
public function siteInstall(string $site = "default") {
$mode = $this->mode;
$this->validateMode($mode);
$drush = $this->drush();
$path = $this->sitePath($site);
// Preparation
$this->taskExec("rm -rf web/modules/contrib")->run();
$this->taskExec("rm -rf web/themes/contrib")->run();
$this->taskExec("rm -rf web/profiles/contrib")->run();
// Composer install
if ($mode === "prd") {
$this->taskComposerInstall()->noDev()->optimizeAutoloader()->noInteraction()->run();
} else {
$this->taskComposerInstall()->optimizeAutoloader()->noInteraction()->run();
}
// Installation from existing config files
$this->taskExec($drush . 'si demo_umami --account-pass=admin -y')->run();
$this->taskExec($drush . 'en redirect404_home devel stage_file_proxy environment_indicator admin_toolbar -y')->run();
// Enable/disable modules according to MODE
$this->siteSetMode($mode, $site);
$message = "--- INFO ---\n";
$message .= "> Set mode: ".$mode.".\n";
$message .= "> Project was built from scratch.\n";
$message .= "> Re-downloaded core and contrib from composer.\n";
if ($mode !== "prd") {
$message .= "> CSS/JS aggregation is turned off!\n";
}
// Print message
$this->say($message);
// Ready for development, open a login url
$uli = "uli";
// Inside DDEV
if (isset($_ENV['IS_DDEV_PROJECT'])) {
$uli = "uli --uri='https://".$this->ddev_url.".ddev.site'";
}
// Inside live Server
if (isset($_ENV['DRUSH_OPTIONS_URI'])) {
$uli = "uli --uri=". $_ENV['DRUSH_OPTIONS_URI'];
}
if (isset($_ENV['HOME']) && $_ENV['HOME'] === "/home/gitpod") {
$links = $_ENV['DDEV_HOSTNAME'];
$link_list = explode(",", $links);
$last = end($link_list);
$uli="uli --uri=" . str_replace("https://", "https://8080-", $last);
}
$this->taskExec($drush . $uli)->run();
}
/**
* Run sql:dump for current site. This makes sense to stg and prd MODE.
*
* @param string $mode
* @param string $site
*
* @return void
*/
public function backupDatabase(string $mode, string $site = "default") {
$this->validateMode($mode);
$drush = $this->drush();
// Inside ddev we do not need a backup path
if (isset($_ENV['IS_DDEV_PROJECT'])) {
$backup_path = "/var/www/html/";
} else {
$path_var = "var_backup_".$mode."_path";
$backup_path = $this->{$path_var};
}
$tag = $this->gitLastTag();
$filename = $backup_path . $mode ."-tag-".$tag.".sql";
// drush sql:dump
$this->taskExec($drush . "sql:dump --gzip --result-file=". $filename)->run();
}
/**
* Generates a new Release (git tag).
* This updates also CHANGELOG.md using package from marcocesarato/php-conventional-changelog.
*
* @param string $site
* @param string $type (patch, minor, major)
*
* @return void
*/
public function generateRelease(string $site = "default", string $type = "patch") {
$current_branch = $this->taskExec("git rev-parse --abbrev-ref HEAD")
->printOutput(false)
->run()
->getMessage();
if ($current_branch !== "master") {
$message = "Can only generate CHANGELOG on branch master. Aborting.";
$this->say($message);
exit;
}
// Check latest tag with current Drupal version.
// According to validations we create a major or minor release accordingly.
$current_last_tag = $this->gitLastTag();
$git_tag_semver = $this->getTagAsArray($current_last_tag);
$core_version_command = "status --field=drupal-version";
$drush = $this->drush();
$drupal_version = $this->taskExec($drush . $core_version_command)
->printOutput(false)
->run()
->getMessage();
$drupal_semver = $this->getTagAsArray($drupal_version);
if ($drupal_semver["minor"] > $git_tag_semver["minor"]) {
$type = "minor";
}
if ($drupal_semver["major"] > $git_tag_semver["major"]) {
$type = "major";
}
// Update CHANGELOG.md, create a new git tag and push
$this->taskExec("composer release:" . $type)->run();
$last_tag = $this->gitLastTag();
$commit = "SYSTEM-00: New " . $type . " release with tag " . $last_tag;
$this->taskGitStack()
->stopOnFail()
->add("-A")
->commit($commit)
->pull('origin','master')
->push('origin','master')
->run();
$this->taskExec("git push --tags")->run();
}
/**
* Get minor and major version of a semver string like v9.3.0
*
* @param string $tag
*
* @return array
*/
private function getTagAsArray(string $tag) {
$current_tag = str_replace("v", "", $tag);
$git_tag_versions = explode(".", $current_tag);
return [
"minor" => $git_tag_versions[1],
"major" => $git_tag_versions[0],
];
}
/**
* Testing commands for CI (GitHub Actions).
* Based on RoboFile.php from https://github.com/Lullabot/drupal9ci,
* https://github.com/fjgarlin/d9-lagoon and
* https://github.com/juampynr/drupal8-github-actions
*/
/**
* Command to run unit tests.
*
* @return \Robo\Result
* The result of the collection of tasks.
*/
public function jobTestsUnit(): Result {
$collection = $this->collectionBuilder();
$collection->addTaskList($this->runUnitTests());
return $collection->run();
}
/**
* Command to generate a coverage report.
*
* @return \Robo\Result
* The result of the collection of tasks.
*/
public function jobCoverageReport(): Result {
$collection = $this->collectionBuilder();
$collection->addTaskList($this->runCoverageReport());
return $collection->run();
}
/**
* Command to check for Drupal's Coding Standards.
*
* @return \Robo\Result
* The result of the collection of tasks.
*/
public function jobCodingStandards(): Result {
$collection = $this->collectionBuilder();
$collection->addTaskList($this->runCodeSniffer());
return $collection->run();
}
/**
* Command to run Cypress tests.
*
* @return \Robo\Result
* The result tof the collection of tasks.
*/
public function jobTestsCypress(): Result {
$collection = $this->collectionBuilder();
$collection->addTaskList($this->runCypressTests());
return $collection->run();
}
/**
* Install Drupal from existing config (CI command).
*
* @return void
*/
public function CiInstallDrupal():void {
$web_root = "/var/www/html/web";
// Install Drupal
$drush = $this->drush();
$alias = ""; // "@current.ci"
// --db-url=mysql://USER:PASSWORD@HOSTNAME:PORT/DATABASE
$install_db = " --db-url=mysql://root:root@localhost:3306/drupal ";
$install_user = " --account-pass=admin ";
$install_uri = " --uri=http://localhost ";
$install_root = " --root=" . $web_root . " ";
$install_profile = " demo_umami ";
// Initial debugging
$this->taskExec($drush . $alias . $install_root . $install_uri . ' status -vvv')->run();
// Install demo_umami
$this->taskExec($drush . $alias . $install_root . $install_uri .
$install_user . $install_db . ' site:install ' . $install_profile . '-v -y')->run();
// Enable modules
$this->taskExec($drush . 'en redirect404_home devel stage_file_proxy environment_indicator admin_toolbar -y')->run();
// Disable caches
$this->configDisableCaches();
}
/**
* Command to run behat tests.
*
* @return \Robo\Result
* The result tof the collection of tasks.
*/
public function jobTestsBehat(): Result {
$collection = $this->collectionBuilder();
$collection->addTaskList($this->runBehatTests());
return $collection->run();
}
/**
* Runs Cypress tests.
*
* @return \Robo\Task\Base\Exec[]
* An array of tasks.
*/
protected function runCypressTests(): array {
$tasks = [];
$tasks[] = $this->taskExec('npm install cypress --save-dev');
$tasks[] = $this->taskExec('$(npm bin)/cypress run --browser chrome --config-file tests/cypress.json');
return $tasks;
}
/**
* Runs Behat tests.
*
* @return \Robo\Task\Base\Exec[]
* An array of tasks.
*/
protected function runBehatTests(): array {
$tasks = [];
$behat_path = "";
if (getenv('CI') === TRUE) {
$behat_path = "tests/behat-ci.yml";
}
if (getenv('IS_DDEV_PROJECT') === "true") {
$behat_path = "tests/behat.yml";
}
$tasks[] = $this->taskExec('vendor/bin/behat --verbose -c ' . $behat_path);
return $tasks;
}
/**
* Run unit tests.
*
* @return \Robo\Task\Base\Exec[]
* An array of tasks.
*/
protected function runUnitTests(): array {
$tasks = [];
$tasks[] = $this->taskExecStack()
->exec('vendor/bin/phpunit --debug --verbose --testsuite=unit,kernel --log-junit=junit.xml ' . $this->code_testing_dir);
return $tasks;
}
/**
* Generates a code coverage report.
*
* @return \Robo\Task\Base\Exec[]
* An array of tasks.
*/
protected function runCoverageReport(): array {
$tasks = [];
$tasks[] = $this->taskExecStack()
->exec('vendor/bin/phpunit --debug --verbose --coverage-html ../coverage --testsuite=unit,kernel ' . $this->code_testing_dir);
return $tasks;
}
/**
* Sets up and runs code sniffer.
*
* @return \Robo\Task\Base\Exec[]
* An array of tasks.
*/
protected function runCodeSniffer(): array {
$tasks = [];
$tasks[] = $this->taskExecStack()
->exec('vendor/bin/phpcs --config-set installed_paths vendor/drupal/coder/coder_sniffer');
$tasks[] = $this->taskFilesystemStack()
->mkdir('artifacts/phpcs');
$tasks[] = $this->taskExecStack()
->exec('vendor/bin/phpcs --standard=Drupal --report=junit --report-junit=artifacts/phpcs/phpcs.xml ' . $this->code_testing_dir)
->exec('vendor/bin/phpcs --standard=DrupalPractice --report=junit --report-junit=artifacts/phpcs/phpcs.xml ' . $this->code_testing_dir);
return $tasks;
}
}