-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRoboFile.php
328 lines (301 loc) · 11 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
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
<?php
use Robo\Tasks;
/**
* Class RoboFile
*/
class RoboFile extends Tasks
{
private const UPDATE_WARNING = 'I hope you you have all dependencies up to date. If not, run vendor/bin/robo build to install/update them.';
private const PHINX_CONFIG_FILE = 'application/configs/phinx.local.yml';
private const APP_DIR = 'application';
private const LIB_DIR = 'library';
private const NODE_MODULES_DIR = 'node_modules';
public function test()
{
$this->stopOnFail(true);
$this->taskExec('npm run test')->run();
$this->lintPhp();
$this->phpcs();
$this->codecept();
}
public function codecept()
{
$this->taskCodecept('vendor/bin/codecept')->run();
}
public function update()
{
$this->stopOnFail(true);
$this->clearCache();
// 23.01.2018 jiri@visionapps.cz: clean installation is needed due to error from phantomjs
// second level dependency (in grunt-uncss package)
if (file_exists(self::NODE_MODULES_DIR)) {
$this->taskExec(sprintf('rm -r %s/*', self::NODE_MODULES_DIR))->run();
}
$this->build();
$this->phinxMigrate('test');
$this->phinxMigrate('production');
$this->test();
}
public function testInstall()
{
$this->stopOnFail(true);
$this->createConfigs();
$this->createDatabase('test');
$this->build();
$this->test();
}
public function phpcs()
{
$this
->taskExec('vendor/bin/phpcs')
->args('--standard=.php_cs_ruleset.xml')
->args('--encoding=utf-8')
->args(self::APP_DIR)
->args(self::LIB_DIR)
->run();
}
public function lintPhp()
{
$this
->taskExec(vsprintf('find %s -name "*.php" -print0 | xargs -0 -n1 -P8 php -l', [
implode(' ', [self::APP_DIR, self::LIB_DIR]),
]))
->run();
}
public function createDeployTar()
{
$this->say(self::UPDATE_WARNING);
$this->taskExec('tar \
-zcf deployment.tar.gz \
--exclude=application/configs/config.local.ini \
--exclude=application/configs/phinx.local.yml \
--exclude=runtime/logs/* \
--exclude=runtime/cache/* \
--exclude=runtime/sessions/* \
--exclude=www/index_dev.php \
--exclude=www/index_test.php \
--exclude=www/image_cache/* \
--exclude=www/media/* \
application bin data languages languages_zend library runtime vendor www RoboFile.php')
->run();
}
public function createInstallZip()
{
$this->say(self::UPDATE_WARNING);
$fileName = sprintf('ePartool-install_%s.zip', $this->getVersion());
$this->stopOnFail(true);
$this->taskExec('cp install/images/consultation_thumb_micro_scholl.jpg www/media/consultations/1')->run();
$this->taskExec('mkdir www/media/folders/misc || exit 0')->run();
$this->taskExec('cp www/images/logo.svg www/media/folders/misc/logo.svg')->run();
$this->taskExec('cp install/images/epartool_logo.png www/media/folders/misc')->run();
$this->taskExec('cp install/images/Gruppenstunde_ImP-2013_Cover.jpg www/media/folders/misc')->run();
$this->taskExec('cp install/images/Methodenkarten-250x310.jpg www/media/folders/misc')->run();
$this->taskExec('zip')
->args('--recurse-paths')
->args('--quiet')
->args($fileName)
->args('.')
->option('--include', 'VERSION.txt')
->option('--include', 'README.md')
->option('--include', '.htaccess')
->option('--include', 'application/*')
->option('--include', 'data/*')
->option('--include', 'install/*')
->option('--include', 'languages/*')
->option('--include', 'languages_zend/*')
->option('--include', 'library/*')
->option('--include', 'runtime/*')
->option('--include', 'vendor/*')
->option('--include', 'www/css/*')
->option('--include', 'www/fonts/*')
->option('--include', 'www/images/*')
->option('--include', 'www/js/*')
->option('--include', 'www/vendor/*')
->option('--include', 'www/index.php')
->option('--include', 'www/robots.txt')
->option('--include', 'www/runMigrations.php')
->option('--include', 'www/.htaccess')
->option('--include', 'www/media/consultations/1/consultation_thumb_micro_scholl.jpg')
->option('--include', 'www/media/folders/misc/logo.svg')
->option('--include', 'www/media/folders/misc/epartool_logo.png')
->option('--include', 'www/media/folders/misc/Gruppenstunde_ImP-2013_Cover.jpg')
->option('--include', 'www/media/folders/misc/Methodenkarten-250x310.jpg')
->option('--exclude', 'application/configs/config.local.ini')
->option('--exclude', 'runtime/cache/*')
->option('--exclude', 'runtime/sessions/*')
->option('--exclude', 'runtime/logs/*')
->option('--exclude', '*.git*')
->option('--exclude', '*.keep')
->run();
}
public function createUpdateZip()
{
$this->say(self::UPDATE_WARNING);
$fileName = sprintf('ePartool-update_%s.zip', $this->getVersion());
$this->stopOnFail(true);
$this->taskExec('zip')
->args('--recurse-paths')
->args('--quiet')
->args($fileName)
->args('.')
->option('--include', 'VERSION.txt')
->option('--include', 'README.md')
->option('--include', 'application/*')
->option('--include', 'data/phinx-migrations/*')
->option('--include', 'languages/*')
->option('--include', 'languages_zend/*')
->option('--include', 'library/*')
->option('--include', 'vendor/*')
->option('--include', 'www/css/*')
->option('--include', 'www/fonts/*')
->option('--include', 'www/images/*')
->option('--include', 'www/js/*')
->option('--include', 'www/vendor/*')
->option('--include', 'www/index.php')
->option('--include', 'www/robots.txt')
->option('--include', 'www/runMigrations.php')
->option('--exclude', 'application/configs/config.local.ini')
->option('--exclude', 'application/configs/phinx.local.yml')
->option('--exclude', '*.git*')
->option('--exclude', '*.keep')
->run();
}
/**
* @param string $environment
*/
public function phinxMigrate($environment)
{
$this
->taskExec('vendor/bin/phinx migrate')
->option('-c', self::PHINX_CONFIG_FILE)
->option('-e', $environment)
->run();
}
/**
* @param string $name
*/
public function phinxCreate($name)
{
$this
->taskExec('bin/phinxCreate.sh')
->arg($name)
->run();
}
public function build()
{
$this->stopOnFail(true);
$this->taskNpmInstall()->run();
$this->taskExec('grunt')->run();
$this->taskExec('npm run build')->run();
}
private function clearCache()
{
$this->taskExec('rm -rf runtime/cache/*')->run();
}
private function createConfigs()
{
$file = self::APP_DIR . '/configs/config.local.ini';
$fileTemplate = self::APP_DIR . '/configs/config.local-example.ini';
if (!realpath($file)) {
copy($fileTemplate, $file);
}
$file = self::APP_DIR . '/configs/phinx.local.yml';
$fileTemplate = self::APP_DIR . '/configs/phinx.local-example.yml';
if (!realpath($file)) {
copy($fileTemplate, $file);
}
}
/**
* Cannot be called before the application is build
* @param string $environment
* @throws \Exception
*/
private function createDatabase($environment)
{
$credentials = $this->loadDbCredentials('config.local', $environment);
$this
->taskExec(sprintf(
'mysql -u %s -h %s -p%s -e \'DROP DATABASE IF EXISTS %s\';',
$credentials['username'],
$credentials['host'],
$credentials['password'],
$credentials['dbname']
))
->run();
$this
->taskExec(sprintf(
'mysql -u %s -h %s -p%s -e \'CREATE DATABASE %s\';',
$credentials['username'],
$credentials['host'],
$credentials['password'],
$credentials['dbname']
))
->run();
require_once 'install/src/Util/Db.php';
$db = new \Util\Db(
$credentials['dbname'],
$credentials['host'],
$credentials['username'],
$credentials['password']
);
$db->initDb(
realpath(dirname(__FILE__) . '/data'),
'admin',
'email@example.com',
'pass',
'de_DE',
function () use ($environment) {
$this->phinxMigrate($environment);
}
);
}
/**
* Cannot be called before the application is build
* @param string $iniConfigFileName
* @param string $environment
* @throws \Exception
* @return string[]
*/
private function loadDbCredentials($iniConfigFileName, $environment)
{
require_once 'vendor/autoload.php';
$configLocal = new Zend_Config_Ini(
sprintf('%s/configs/%s.ini', self::APP_DIR, $iniConfigFileName),
$environment
);
if (!$configLocal) {
throw new \Exception(
sprintf('Cannot load section %s from config %s.ini', $environment, $iniConfigFileName)
);
}
$dbCredentials = $configLocal;
foreach (['resources', 'db', 'params'] as $section) {
if (!$dbCredentials) {
throw new \Exception(sprintf('No default DB credentials found in %s.ini', $iniConfigFileName));
}
$dbCredentials = $dbCredentials->{$section};
}
$dbCredentials = $dbCredentials->toArray();
foreach (['host', 'dbname', 'username', 'password'] as $parameter) {
if (!array_key_exists($parameter, $dbCredentials)) {
throw new \Exception(
sprintf('%s parameter for db access is missing in %s.ini', $parameter, $iniConfigFileName)
);
}
}
return $dbCredentials;
}
/**
* @return string
*/
private function getVersion()
{
return trim(file_get_contents('VERSION.txt'));
}
public function deployFinalize()
{
$this->stopOnFail(true);
$this->clearCache();
$this->phinxMigrate('production');
}
}