-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathMage.php
More file actions
111 lines (91 loc) · 2.81 KB
/
Copy pathMage.php
File metadata and controls
111 lines (91 loc) · 2.81 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
<?php
namespace PHPCensor\Plugin;
use Exception;
use PHPCensor\Builder;
use PHPCensor\Common\Exception\RuntimeException;
use PHPCensor\Model\Build;
use PHPCensor\Plugin;
/**
* Integrates PHPCensor with Mage: https://github.com/andres-montanez/Magallanes
*
* @package PHP Censor
* @subpackage Application
*
* @author Dmitry Khomutov <poisoncorpsee@gmail.com>
*/
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->findBinary(['mage', 'mage.phar']);
if (isset($options['env'])) {
$this->mageEnv = $builder->interpolate($options['env'], true);
}
}
/**
* {@inheritDoc}
*/
public function execute()
{
if (empty($this->mageEnv)) {
$this->builder->logFailure('You must specify environment.');
return false;
}
$result = $this->builder->executeCommand(Builder::PHP_CLI_TAG . ' ' . $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 (\Throwable $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 RuntimeException('Log directory not found');
}
$list = \scandir($logsDir);
if ($list === false) {
throw new RuntimeException('Log dir read fail');
}
$list = \array_filter($list, fn ($name) => \preg_match('/^log-\d+-\d+\.log$/', $name));
if (empty($list)) {
throw new RuntimeException('Log dir filter fail');
}
$res = \sort($list);
if ($res === false) {
throw new RuntimeException('Logs sort fail');
}
$lastLogFile = \end($list);
if ($lastLogFile === false) {
throw new RuntimeException('Get last Log name fail');
}
$logContent = \file_get_contents($logsDir . '/' . $lastLogFile);
if ($logContent === false) {
throw new RuntimeException('Get last Log content fail');
}
$lines = \explode("\n", $logContent);
$lines = \array_map('trim', $lines);
return \array_filter($lines);
}
}