forked from php-censor/php-censor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMage.php
More file actions
113 lines (94 loc) · 2.74 KB
/
Copy pathMage.php
File metadata and controls
113 lines (94 loc) · 2.74 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
<?php
/**
* PHPCensor - Continuous Integration for PHP
*/
namespace PHPCensor\Plugin;
use Exception;
use PHPCensor\Builder;
use PHPCensor\Model\Build;
use PHPCensor\Plugin;
/**
* Integrates PHPCensor with Mage: https://github.com/andres-montanez/Magallanes
*
* @package PHPCensor
* @subpackage Plugins
*/
class Mage extends Plugin
{
protected $mageEnv;
/**
* {@inheritdoc}
*/
public static function pluginName()
{
return 'mage';
}
/**
* {@inheritdoc}
*/
public function __construct(Builder $builder, Build $build, array $options = [])
{
parent::__construct($builder, $build, $options);
$this->executable = $this->builder->findBinary(['mage', 'mage.phar']);
if (isset($options['env'])) {
$this->mageEnv = $builder->interpolate($options['env']);
}
}
/**
* {@inheritdoc}
*/
public function execute()
{
if (empty($this->mageEnv)) {
$this->builder->logFailure('You must specify environment.');
return false;
}
$result = $this->builder->executeCommand($this->executable . ' deploy to:' . $this->mageEnv);
try {
$this->builder->log('########## MAGE LOG BEGIN ##########');
$this->builder->log($this->getMageLog());
$this->builder->log('########## MAGE LOG END ##########');
} catch (Exception $e) {
$this->builder->logFailure($e->getMessage());
}
return $result;
}
/**
* Get mage log lines
* @return array
* @throws Exception
*/
protected function getMageLog()
{
$logsDir = $this->build->getBuildPath() . '/.mage/logs';
if (!is_dir($logsDir)) {
throw new Exception('Log directory not found');
}
$list = scandir($logsDir);
if ($list === false) {
throw new Exception('Log dir read fail');
}
$list = array_filter($list, function ($name) {
return preg_match('/^log-\d+-\d+\.log$/', $name);
});
if (empty($list)) {
throw new Exception('Log dir filter fail');
}
$res = sort($list);
if ($res === false) {
throw new Exception('Logs sort fail');
}
$lastLogFile = end($list);
if ($lastLogFile === false) {
throw new Exception('Get last Log name fail');
}
$logContent = file_get_contents($logsDir . '/' . $lastLogFile);
if ($logContent === false) {
throw new Exception('Get last Log content fail');
}
$lines = explode("\n", $logContent);
$lines = array_map('trim', $lines);
$lines = array_filter($lines);
return $lines;
}
}