-
Notifications
You must be signed in to change notification settings - Fork 17
/
RoboFile.php
469 lines (405 loc) · 15.7 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
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
<?php
/**
* RoboFile.php
*
* PHP version 7
*
* @author Tim Wagner <[email protected]>
* @copyright 2016 TechDivision GmbH <[email protected]>
* @license https://opensource.org/licenses/MIT
* @link https://github.com/techdivision/import-cli-simple
* @link http://www.techdivision.com
*/
use Lurker\Event\FilesystemEvent;
use Symfony\Component\Finder\Finder;
/**
* Defines the available build tasks.
*
* @author Tim Wagner <[email protected]>
* @copyright 2016 TechDivision GmbH <[email protected]>
* @license https://opensource.org/licenses/MIT
* @link https://github.com/techdivision/import-cli-simple
* @link http://www.techdivision.com
*/
class RoboFile extends \Robo\Tasks
{
/**
* The build properties.
*
* @var array
*/
protected $properties = array(
'base.dir' => __DIR__,
'etc.dir' => __DIR__ . '/etc',
'dist.dir' => __DIR__ . '/dist',
'vendor.dir' => __DIR__ . '/vendor',
'target.dir' => __DIR__ . '/target',
'symfony.dir' => __DIR__ . '/symfony',
'webapp.name' => 'import-cli-simple',
'webapp.version' => '5.0.0'
);
/**
* Run's the composer install command.
*
* @return \Robo\Result The result
*/
public function composerInstall()
{
// optimize autoloader with custom path
return $this->taskComposerInstall()
->preferDist()
->optimizeAutoloader()
->run();
}
/**
* Run's the composer update command.
*
* @return \Robo\Result The result
*/
public function composerUpdate()
{
// optimize autoloader with custom path
return $this->taskComposerUpdate()
->preferDist()
->optimizeAutoloader()
->run();
}
/**
* Clean up the environment for a new build.
*
* @return \Robo\Result The result
*/
public function clean()
{
return $this->taskDeleteDir($this->properties['target.dir'])->run();
}
/**
* Prepare's the environment for a new build.
*
* @return \Robo\Result The result
*/
public function prepare()
{
// prepare the directories
return $this->taskFileSystemStack()
->mkdir($this->properties['dist.dir'])
->mkdir($this->properties['target.dir'])
->mkdir(sprintf('%s/reports', $this->properties['target.dir']))
->run();
}
/**
* Prepare's the Docker environment for a new build.
*
* @param string $domainName The domain name used to invoke the Magento 2 instance inside the Docker container
* @param string $containerName The Docker container name
*
* @return void
*/
public function prepareDocker($domainName, $containerName)
{
// stop the build on first failure of a task
$this->stopOnFail(true);
// prepare the filesystem
$this->prepare();
// initialize the variables to query whether or not the docker container has been started successfully
$counter = 0;
$magentoNotAvailable = true;
do {
// reset the result of the CURL request
$res = null;
// query whether or not the image already has been loaded
exec(
str_replace(
array('{domain-name}'),
array($domainName),
'curl --resolve {domain-name}:80:127.0.0.1 http://{domain-name}/magento_version'
),
$res
);
// query whether or not the Docker has been started
foreach ($res as $val) {
if (strstr($val, 'Magento/')) {
$magentoNotAvailable = false;
}
}
// raise the counter
$counter++;
// sleep while the docker container is not available
if ($magentoNotAvailable === true) {
sleep(1);
}
} while ($magentoNotAvailable && $counter < 30);
// activate batch commit behaviour to improve performance
$this->taskDockerExec($containerName)
->exec('mysql -uroot -proot -e \'SET GLOBAL innodb_flush_log_at_trx_commit = 2\'')
->run();
// grant the privilieges to connection from outsite the container
$this->taskDockerExec($containerName)
->exec('mysql -uroot -proot -e \'GRANT ALL ON *.* TO "magento"@"%" IDENTIFIED BY "magento"\'')
->run();
// flush the privileges
$this->taskDockerExec($containerName)
->exec('mysql -uroot -proot -e "FLUSH PRIVILEGES"')
->run();
}
/**
* Creates the a PHAR archive from the sources.
*
* @return void
*/
public function createPhar()
{
// stop the build on first failure of a task
$this->stopOnFail(true);
// run the build process
$this->build();
// prepare the PHAR archive name
$archiveName = sprintf(
'%s/%s.phar',
$this->properties['target.dir'],
$this->properties['webapp.name']
);
// prepare the target directory
$targetDir = $this->properties['target.dir'] . DIRECTORY_SEPARATOR . $this->properties['webapp.version'];
// copy the composer.json file
$this->taskFilesystemStack()
->copy(
__DIR__ . DIRECTORY_SEPARATOR . 'composer.json',
$targetDir. DIRECTORY_SEPARATOR. 'composer.json'
)->run();
// copy the .semver file
$this->taskFilesystemStack()
->copy(
__DIR__ . DIRECTORY_SEPARATOR . '.semver',
$targetDir. DIRECTORY_SEPARATOR. '.semver'
)->run();
// copy the bootstrap.php file
$this->taskFilesystemStack()
->copy(
__DIR__ . DIRECTORY_SEPARATOR . 'bootstrap.php',
$targetDir. DIRECTORY_SEPARATOR. 'bootstrap.php'
)->run();
// install the composer dependencies
$this->taskComposerInstall()
->dir($targetDir)
->noDev()
->optimizeAutoloader()
->run();
// prepare the task
$pharTask = $this->taskPackPhar($archiveName)
->compress()
->stub('stub.php');
// load a list with all the source files from the vendor directory
$finder = Finder::create()->files()
->name('*.php')
->name('*.json')
->name('.semver')
->name('services.xml')
->name('services-1.0.xsd')
->in($targetDir)
->ignoreDotFiles(false);
// iterate over the source files of the vendor directory and add them to the PHAR archive
foreach ($finder as $file) {
$pharTask->addFile($file->getRelativePathname(), $file->getRealPath());
}
// create the PHAR archive
$pharTask->run();
// verify PHAR archive is packed correctly
$this->_exec(sprintf('php %s', $archiveName));
// prepare the PHAR archive distribution name
$distArchiveName = sprintf('%s/%s.phar', $this->properties['dist.dir'], $this->properties['webapp.name']);
// clean up the dist directory
$this->taskCleanDir($this->properties['dist.dir'])->run();
// copy the latest PHAR archive to the dist directory
$this->taskFilesystemStack()->copy($archiveName, $distArchiveName)->run();
}
/**
* Load the repository source directories that matches the passed pattern.
*
* @param string $glue The glue used the the directory are concatenated to a string
* @param string $pattern The pattern used to load the source directories
*
* @return string The concatenated relative source directories
*/
protected function loadLibrarySourceDirs(string $glue = ',', string $pattern = 'techdivision/*/src') : string
{
// load the source directories
$sourceDirs = glob(sprintf('%s/%s', $this->properties['vendor.dir'], $pattern), GLOB_ONLYDIR);
// cut-off the actual path
array_walk($sourceDirs, function (&$value) {
$value = ltrim(str_replace(__DIR__, '', $value), '/');
});
// implode and return the source directories
return implode($glue, $sourceDirs);
}
/**
* Run's the PHPMD.
*
* @return \Robo\Result The result
*/
public function runMd()
{
// run the mess detector
return $this->_exec(
sprintf(
'%s/bin/phpmd %s xml phpmd.xml --reportfile %s/reports/pmd.xml --ignore-violations-on-exit',
$this->properties['vendor.dir'],
$this->loadLibrarySourceDirs(),
$this->properties['target.dir']
)
);
}
/**
* Run's the PHPCodeSniffer.
*
* @return \Robo\Result The result
*/
public function runCs()
{
// load the repositories that matches the pattern the vendor/techdivision/*/src directories
$dirs = glob(sprintf('%s/techdivision/*/src', $this->properties['vendor.dir']), GLOB_ONLYDIR);
// run the code sniffer
return $this->_exec(
sprintf(
'%s/bin/phpcs -n --report-full --extensions=php --standard=phpcs.xml --report-checkstyle=%s/reports/phpcs.xml %s',
$this->properties['vendor.dir'],
$this->properties['target.dir'],
$this->loadLibrarySourceDirs(' ')
)
);
}
/**
* Run's the PHPCPD.
*
* @return \Robo\Result The result
*/
public function runCpd()
{
// prepare the patterns for the files that has to be ignored
$ignore = array(
$this->properties['vendor.dir'].'/techdivision/import/src/Plugins/MissingOptionValuesPlugin.php',
$this->properties['vendor.dir'].'/techdivision/import/src/Subjects/AbstractSubject.php',
$this->properties['vendor.dir'].'/techdivision/import-attribute/src/Utils/MemberNames.php',
$this->properties['vendor.dir'].'/techdivision/import-attribute/src/Subjects/OptionSubject.php',
$this->properties['vendor.dir'].'/techdivision/import-attribute/src/Loaders/RawEntityLoader.php',
$this->properties['vendor.dir'].'/techdivision/import-attribute-set/src/Loaders/RawEntityLoader.php',
$this->properties['vendor.dir'].'/techdivision/import-category/src/Loaders/RawEntityLoader.php',
$this->properties['vendor.dir'].'/techdivision/import-category/src/Services/CategoryBunchProcessor.php',
$this->properties['vendor.dir'].'/techdivision/import-product/src/Loaders/RawEntityLoader.php',
$this->properties['vendor.dir'].'/techdivision/import-product/src/Subjects/BunchSubject.php',
$this->properties['vendor.dir'].'/techdivision/import-product/src/Observers/UrlKeyObserver.php',
$this->properties['vendor.dir'].'/techdivision/import-product-bundle/src/Loaders/RawEntityLoader.php',
$this->properties['vendor.dir'].'/techdivision/import-product-bundle-ee/src/Observers/EeBundleOptionObserver.php',
$this->properties['vendor.dir'].'/techdivision/import-product-media/src/Loaders/RawEntityLoader.php',
$this->properties['vendor.dir'].'/techdivision/import-product-media/src/Observers/MediaGalleryValueObserver.php',
$this->properties['vendor.dir'].'/techdivision/import-product-variant/src/Loaders/RawEntityLoader.php',
$this->properties['vendor.dir'].'/techdivision/import-converter-customer-attribute/src/Subjects/ConverterSubject.php',
$this->properties['vendor.dir'].'/techdivision/import-converter-product-attribute/src/Subjects/ConverterSubject.php',
$this->properties['vendor.dir'].'/techdivision/import-converter-product-attribute/src/Observers/ProductToAttributeOptionValueConverterObserver.php',
$this->properties['vendor.dir'].'/techdivision/import-customer-address/src/Observers/CustomerAddressAttributeObserver.php',
$this->properties['vendor.dir'].'/techdivision/import-serializer-csv/src/CategoryCsvSerializer.php',
$this->properties['vendor.dir'].'/techdivision/import-product-tier-price/src/Utils/PrimarySkuToPkMappingUtil.php',
);
// run the copy past detector
return $this->_exec(
sprintf(
'%s/bin/phpcpd %s/techdivision/*/src --exclude %s --log-pmd %s/reports/pmd-cpd.xml',
$this->properties['vendor.dir'],
$this->properties['vendor.dir'],
implode(' --exclude ', $ignore),
$this->properties['target.dir']
)
);
}
/**
* Run's the PHPUnit testsuite.
*
* @return \Robo\Result The result
*/
public function runTestsUnit()
{
// run PHPUnit
return $this->taskPHPUnit(
sprintf(
'%s/bin/phpunit --testsuite "techdivision/import-cli-simple PHPUnit testsuite"',
$this->properties['vendor.dir']
)
)
->configFile('phpunit.xml')
->run();
}
/**
* Run's the integration testsuite.
*
* This task uses the Magento 2 docker image generator from https://github.com/techdivision/magento2-docker-imgen. To execute
* this task, it is necessary that you've setup a running container with the domain name, passed as argument.
*
* @return \Robo\Result The result
*/
public function runTestsIntegration()
{
// run the integration testsuite
return $this->taskPHPUnit(
sprintf(
'%s/bin/phpunit --testsuite "techdivision/import-cli-simple PHPUnit integration testsuite"',
$this->properties['vendor.dir']
)
)
->configFile('phpunit.xml')
->run();
}
/**
* Run's the acceptance testsuite.
*
* This task uses the Magento 2 docker image generator from https://github.com/techdivision/magento2-docker-imgen. To execute
* this task, it is necessary that you've setup a running container with the domain name, passed as argument.
*
* @return \Robo\Result The result
*/
public function runTestsAcceptance($magentoEdition = 'ce', $magentoVersion = '2.3.5')
{
// initialize the default tags
$tags = sprintf('@%s&&@%s&&~@customer&&~@customer-address', strtolower($magentoEdition), implode('.', sscanf($magentoVersion, "%d.%d")));
// query whether or not the version is lower than 2.3.3, because then we've to ignore the MSI tests
if (version_compare($magentoVersion, '2.3.2') < 1) {
$tags = sprintf('%s&&~msi', $tags);
}
// finally, invoke the acceptance tests
return $this->taskBehat()
->format('pretty')
->option('tags', $tags)
->noInteraction()
->run();
}
/**
* Raising the semver version number.
*
* @return \Robo\Result The result
*/
public function semver()
{
return $this->taskSemVer('.semver')
->prerelease('beta')
->run();
}
/**
* The complete build process.
*
* @return void
*/
public function build()
{
// stop the build on first failure of a task
$this->stopOnFail(true);
// process the build
$this->clean();
$this->prepare();
try {
$this->runCpd();
} catch (\Exception $e) {
}
$this->runCs();
$this->runMd();
$this->runTestsUnit();
}
}