This repository has been archived by the owner on Dec 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRoboFile.php
231 lines (206 loc) · 6.2 KB
/
RoboFile.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
<?php
// @codingStandardsIgnoreStart
/**
* Base tasks for setting up a module to test within a full Drupal environment.
*
* This file expects to be called from the root of a Drupal site.
*
* @class RoboFile
* @codeCoverageIgnore
*/
class RoboFile extends \Robo\Tasks
{
/**
* @var string $db_url
* The database URL. This can be overridden by specifying a $DB_URL
* environment variable.
*/
protected $db_url = 'mysql://[email protected]/drupal8';
/**
* RoboFile constructor.
*/
public function __construct()
{
// Pull a DB_URL from the environment, if it exists.
if (filter_var(getenv('DB_URL'), FILTER_VALIDATE_URL)) {
$this->db_url = getenv('DB_URL');
}
// Treat this command like bash -e and exit as soon as there's a failure.
$this->stopOnFail();
}
/**
* Adds coding standard dependencies.
*/
public function addCodingStandardsDeps()
{
$config = json_decode(file_get_contents('composer.json'));
$config->require->{"drupal/coder"} = "^8.2";
file_put_contents('composer.json', json_encode($config));
}
/**
* Adds Behat dependencies.
*/
public function addBehatDeps()
{
$config = json_decode(file_get_contents('composer.json'));
$config->require->{"behat/mink-selenium2-driver"} = "^1.3";
$config->require->{"drupal/drupal-extension"} = "master-dev";
// Pin version until https://github.com/Behat/MinkExtension/pull/311 gets fixed.
$config->require->{"behat/mink-extension"} = "v2.2";
$config->require->{"drush/drush"} = "~8.1";
$config->require->{"drupal/console"} = "^1.0.2";
$config->require->{"guzzlehttp/guzzle"} = "^6.0@dev";
file_put_contents('composer.json', json_encode($config));
}
/**
* Installs composer dependencies.
*/
public function installDependencies()
{
$this->taskComposerInstall()
->optimizeAutoloader()
->run();
}
/**
* Updates composer dependencies.
*/
public function updateDependencies()
{
// The git checkout includes a composer.lock, and running composer update
// on it fails for the first time.
$this->taskFilesystemStack()->remove('composer.lock')->run();
$this->taskComposerUpdate()
->optimizeAutoloader()
->run();
}
/**
* Install Drupal.
*
* @param string $admin_user
* (optional) The administrator's username.
* @param string $admin_password
* (optional) The administrator's password.
* @param string $site_name
* (optional) The Drupal site name.
*/
public function setupDrupal(
$admin_user = null,
$admin_password = null,
$site_name = null
) {
$this->taskFilesystemStack()->remove('web/sites/default/settings.php')->run();
$this->taskFilesystemStack()->remove('artifacts/.gitkeep')->run();
$task = $this->drupal()
->args('site:install')
->option('force')
->option('no-interaction');
if ($admin_user) {
$task->option('account-name', $admin_user, '=');
}
if ($admin_password) {
$task->option('account-pass', $admin_password, '=');
}
if ($site_name) {
$task->option('site-name', $site_name, '=');
}
// Sending email will fail, so we need to allow this to always pass.
$this->stopOnFail(false);
$task->run();
$this->stopOnFail();
}
/**
* Return drupal with default arguments.
*
* @return \Robo\Task\Base\Exec
* A drupal exec command.
*/
protected function drupal()
{
return $this->taskExec('vendor/bin/drupal');
}
/**
* Get the absolute path to the docroot.
*
* @return string
*/
protected function getDocroot()
{
$docroot = (getcwd());
return $docroot;
}
/**
* Run PHPUnit and simpletests for the module.
*
* @param string $module
* The module name.
*/
public function test($module)
{
$this->phpUnit($module)
->run();
}
/**
* Return a configured phpunit task.
*
* This will check for PHPUnit configuration first in the module directory.
* If no configuration is found, it will fall back to Drupal's core
* directory.
*
* @param string $module
* The module name.
*
* @return \Robo\Task\Testing\PHPUnit
*/
private function phpUnit($module)
{
return $this->taskPhpUnit('vendor/bin/phpunit')
->option('verbose')
->option('debug')
->configFile('web/core')
->group($module);
}
/**
* Gathers coding standard statistics.
*
* @param string $path
* Path were cs.json and cs-practice.json files have been stored
* by the container where phpcs was executed.
*
* @return string
* A short string with the total violations.
*/
public function extractCodingStandardsStats($path)
{
$errors = 0;
$warnings = 0;
if (file_exists($path . '/cs.json')) {
$stats = json_decode(file_get_contents($path . '/cs.json'));
$errors += $stats->totals->errors;
$warnings += $stats->totals->warnings;
}
return $errors . ' errors and ' . $warnings . ' warnings.';
}
/**
* Gathers code coverage stats.
*
* @param string $path
* Path to a Clover report file.
*
* @return string
* A short string with the coverage percentage.
*/
public function extractCoverageStats($path)
{
if (file_exists($path . '/index.xml')) {
$data = file_get_contents($path . '/index.xml');
$xml = simplexml_load_string($data);
$totals = $xml->project->directory->totals;
$lines = (string)$totals->lines['percent'];
$methods = (string)$totals->methods['percent'];
$classes = (string)$totals->classes['percent'];
return 'Lines ' . $lines . ' Methods ' . $methods . ' Classes ' . $classes;
} else {
return 'Clover report was not found at ' . $path;
}
}
}