|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* PHPCI - Continuous Integration for PHP. |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright Copyright 2014, Block 8 Limited. |
|
6
|
|
|
* @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md |
|
7
|
|
|
* |
|
8
|
|
|
* @link https://www.phptesting.org/ |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace PHPCI\Plugin; |
|
12
|
|
|
|
|
13
|
|
|
use PHPCI\Builder; |
|
14
|
|
|
use PHPCI\Helper\Lang; |
|
15
|
|
|
use PHPCI\Model\Build; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Shell Plugin - Allows execute shell commands. |
|
19
|
|
|
* |
|
20
|
|
|
* @author Kinn Coelho Julião <[email protected]> |
|
21
|
|
|
*/ |
|
22
|
|
|
class Shell implements \PHPCI\Plugin |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var \PHPCI\Builder |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $phpci; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var \PHPCI\Model\Build |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $build; |
|
33
|
|
|
|
|
34
|
|
|
protected $args; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var string[] The commands to be executed |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $commands = array(); |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Standard Constructor. |
|
43
|
|
|
* |
|
44
|
|
|
* $options['directory'] Output Directory. Default: %BUILDPATH% |
|
45
|
|
|
* $options['filename'] Phar Filename. Default: build.phar |
|
46
|
|
|
* $options['regexp'] Regular Expression Filename Capture. Default: /\.php$/ |
|
47
|
|
|
* $options['stub'] Stub Content. No Default Value |
|
48
|
|
|
* |
|
49
|
|
|
* @param Builder $phpci |
|
50
|
|
|
* @param Build $build |
|
51
|
|
|
* @param array $options |
|
52
|
|
|
*/ |
|
53
|
|
|
public function __construct(Builder $phpci, Build $build, array $options = array()) |
|
54
|
|
|
{ |
|
55
|
|
|
$this->phpci = $phpci; |
|
56
|
|
|
$this->build = $build; |
|
57
|
|
|
|
|
58
|
|
|
if (isset($options['command'])) { |
|
59
|
|
|
// Keeping this for backwards compatibility, new projects should use interpolation vars. |
|
60
|
|
|
$options['command'] = str_replace('%buildpath%', $this->phpci->buildPath, $options['command']); |
|
61
|
|
|
$this->commands = array($options['command']); |
|
62
|
|
|
|
|
63
|
|
|
return; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/* |
|
67
|
|
|
* Support the new syntax: |
|
68
|
|
|
* |
|
69
|
|
|
* shell: |
|
70
|
|
|
* - "cd /www" |
|
71
|
|
|
* - "rm -f file.txt" |
|
72
|
|
|
*/ |
|
73
|
|
|
if (is_array($options)) { |
|
74
|
|
|
$this->commands = $options; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Runs the shell command. |
|
80
|
|
|
*/ |
|
81
|
|
|
public function execute() |
|
82
|
|
|
{ |
|
83
|
|
|
if (!defined('ENABLE_SHELL_PLUGIN') || !ENABLE_SHELL_PLUGIN) { |
|
84
|
|
|
throw new \Exception(Lang::get('shell_not_enabled')); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
$success = true; |
|
88
|
|
|
|
|
89
|
|
|
foreach ($this->commands as $command) { |
|
90
|
|
|
$command = $this->phpci->interpolate($command); |
|
91
|
|
|
|
|
92
|
|
|
if (!$this->phpci->executeCommand($command)) { |
|
93
|
|
|
$success = false; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return $success; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|