Skip to content

Commit 232d1d3

Browse files
ss-gxpcorpsee
authored andcommitted
Add plugin Mage
1 parent 62afcee commit 232d1d3

File tree

3 files changed

+146
-0
lines changed

3 files changed

+146
-0
lines changed

docs/en/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ Plugins
4343
* [Hipchat](plugins/hipchat_notify.md) - `hipchat_notify`
4444
* [IRC](plugins/irc.md) - `irc`
4545
* [Lint](plugins/lint.md) - `lint`
46+
* [Mage](plugins/mage.md) - `mage`
4647
* [MySQL](plugins/mysql.md) - `mysql`
4748
* [Package Build](plugins/package_build.md) - `package_build`
4849
* [PDepend](plugins/pdepend.md) - `pdepend`

docs/en/plugins/mage.md

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
Plugin Mage
2+
===========
3+
4+
Triggers a deployment of the project to run via [Mage](https://github.com/andres-montanez/Magallanes)
5+
6+
Configuration
7+
-------------
8+
9+
### Options
10+
11+
* **env** [required, string] - The environment name
12+
13+
### Examples
14+
15+
```yaml
16+
deploy:
17+
mage:
18+
env: production
19+
```
20+
21+
### Options for config.yml
22+
23+
* **bin** [optional, string] - The mage executable path
24+
25+
### Examples
26+
27+
```yaml
28+
mage:
29+
bin: /usr/local/bin/mage
30+
```

src/PHPCensor/Plugin/Mage.php

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
/**
3+
* PHPCensor - Continuous Integration for PHP
4+
*/
5+
6+
namespace PHPCensor\Plugin;
7+
8+
use PHPCensor\Builder;
9+
use PHPCensor\Model\Build;
10+
use Psr\Log\LogLevel;
11+
12+
/**
13+
* Integrates PHPCensor with Mage: https://github.com/andres-montanez/Magallanes
14+
* @package PHPCensor
15+
* @subpackage Plugins
16+
*/
17+
class Mage extends \PHPCensor\Plugin
18+
{
19+
protected $mage_bin = 'mage';
20+
protected $mage_env;
21+
22+
/**
23+
* {@inheritdoc}
24+
*/
25+
public static function pluginName()
26+
{
27+
return 'mage';
28+
}
29+
30+
/**
31+
* {@inheritdoc}
32+
*/
33+
public function __construct(Builder $builder, Build $build, array $options = [])
34+
{
35+
parent::__construct($builder, $build, $options);
36+
37+
$config = $builder->getSystemConfig('mage');
38+
if (isset($config['bin'])) {
39+
$this->mage_bin = $config['bin'];
40+
}
41+
42+
if (isset($options['env'])) {
43+
$this->mage_env = $options['env'];
44+
}
45+
}
46+
47+
/**
48+
* {@inheritdoc}
49+
*/
50+
public function execute()
51+
{
52+
if (empty($this->mage_env)) {
53+
$this->builder->logFailure('You must specify environment.');
54+
return false;
55+
}
56+
57+
$result = $this->builder->executeCommand($this->mage_bin . ' deploy to:' . $this->mage_env);
58+
59+
try {
60+
$this->builder->log('########## MAGE LOG BEGIN ##########');
61+
$this->builder->log($this->getMageLog());
62+
$this->builder->log('########## MAGE LOG END ##########');
63+
} catch (\Exception $e) {
64+
$this->builder->log($e->getMessage(), LogLevel::NOTICE);
65+
}
66+
67+
return $result;
68+
}
69+
70+
/**
71+
* Get mage log lines
72+
* @return array
73+
* @throws \Exception
74+
*/
75+
protected function getMageLog()
76+
{
77+
$logs_dir = $this->build->getBuildPath() . '/.mage/logs';
78+
if (!is_dir($logs_dir)) {
79+
throw new \Exception('Log directory not found');
80+
}
81+
82+
$list = scandir($logs_dir);
83+
if ($list === false) {
84+
throw new \Exception('Log dir read fail');
85+
}
86+
87+
$list = array_filter($list, function ($name) {
88+
return preg_match('/^log-\d+-\d+\.log$/', $name);
89+
});
90+
if (empty($list)) {
91+
throw new \Exception('Log dir filter fail');
92+
}
93+
94+
$res = sort($list);
95+
if ($res === false) {
96+
throw new \Exception('Logs sort fail');
97+
}
98+
99+
$last_log_file = end($list);
100+
if ($last_log_file === false) {
101+
throw new \Exception('Get last Log name fail');
102+
}
103+
104+
$log_content = file_get_contents($logs_dir . '/' . $last_log_file);
105+
if ($log_content === false) {
106+
throw new \Exception('Get last Log content fail');
107+
}
108+
109+
$lines = explode("\n", $log_content);
110+
$lines = array_map('trim', $lines);
111+
$lines = array_filter($lines);
112+
113+
return $lines;
114+
}
115+
}

0 commit comments

Comments
 (0)