forked from consolidation/robo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Runner.php
299 lines (265 loc) · 10.4 KB
/
Runner.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
<?php
namespace Robo;
use Robo\Config;
use Robo\Common\IO;
use Robo\Container\RoboContainer;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Output\ConsoleOutput;
class Runner
{
use IO;
const VERSION = '0.6.1';
const ROBOCLASS = 'RoboFile';
const ROBOFILE = 'RoboFile.php';
/**
* @var string PassThoughArgs
*/
protected $passThroughArgs = null;
/**
* @var string RoboClass
*/
protected $roboClass;
/**
* @var string RoboFile
*/
protected $roboFile;
/**
* @var string working dir of Robo
*/
protected $dir;
/**
* Class Constructor
* @param null $roboClass
* @param null $roboFile
*/
public function __construct($roboClass = null, $roboFile = null, $container = null)
{
// set the const as class properties to allow overwriting in child classes
$this->roboClass = $roboClass ? $roboClass : self::ROBOCLASS ;
$this->roboFile = $roboFile ? $roboFile : self::ROBOFILE;
$this->dir = getcwd();
// Store the container in our config object if it was provided.
if ($container != null) {
Config::setContainer($container);
}
}
protected function loadRoboFile()
{
if (class_exists($this->roboClass)) {
return true;
}
if (!file_exists($this->dir)) {
$this->yell("Path in `{$this->dir}` is invalid, please provide valid absolute path to load Robofile", 40, 'red');
return false;
}
$this->dir = realpath($this->dir);
chdir($this->dir);
if (!file_exists($this->dir . DIRECTORY_SEPARATOR . $this->roboFile)) {
return false;
}
require_once $this->dir . DIRECTORY_SEPARATOR .$this->roboFile;
if (!class_exists($this->roboClass)) {
$this->writeln("<error>Class ".$this->roboClass." was not loaded</error>");
return false;
}
return true;
}
public function execute($input = null)
{
register_shutdown_function(array($this, 'shutdown'));
set_error_handler(array($this, 'handleError'));
// If we were not provided a container, then create one
if (!Config::hasContainer()) {
$input = $this->prepareInput($input ? $input : $this->shebang($_SERVER['argv']));
// Set up our dependency injection container.
$container = new RoboContainer();
static::configureContainer($container, $input);
static::addServiceProviders($container);
$container->share('application', \Robo\Application::class)
->withArgument('Robo')
->withArgument(self::VERSION);
Config::setContainer($container);
}
$container = Config::getContainer();
$app = $container->get('application');
if (!$this->loadRoboFile()) {
$this->yell("Robo is not initialized here. Please run `robo init` to create a new RoboFile", 40, 'yellow');
$app->addInitRoboFileCommand($this->roboFile, $this->roboClass);
$app->run(Config::input(), Config::output());
return;
}
$app->addCommandsFromClass($this->roboClass, $this->passThroughArgs);
$app->run($container->get('input'), $container->get('output'));
}
/**
* Create a container and initiailze it.
*/
public static function configureContainer($container, $input = null, $output = null)
{
// Self-referential container refernce for the inflector
$container->add('container', $container);
// Create default input and output objects if they were not provided
if (!$input) {
$input = new StringInput('');
}
if (!$output) {
$output = new \Symfony\Component\Console\Output\ConsoleOutput();
}
$container->add('input', $input);
$container->add('output', $output);
// Register logging and related services.
$container->share('logStyler', \Robo\Log\RoboLogStyle::class);
$container->share('logger', \Robo\Log\RoboLogger::class)
->withArgument('output')
->withMethodCall('setLogOutputStyler', ['logStyler']);
$container->share('resultPrinter', \Robo\Log\ResultPrinter::class);
$container->add('simulator', \Robo\Task\Simulator::class);
// Register our various inflectors.
$container->inflector(\Psr\Log\LoggerAwareInterface::class)
->invokeMethod('setLogger', ['logger']);
$container->inflector(\League\Container\ContainerAwareInterface::class)
->invokeMethod('setContainer', ['container']);
$container->inflector(\Symfony\Component\Console\Input\InputAwareInterface::class)
->invokeMethod('setInput', ['input']);
}
/**
* Register our service providers
*/
public static function addServiceProviders($container)
{
$container->addServiceProvider(\Robo\Collection\Collection::getCollectionServices());
$container->addServiceProvider(\Robo\Task\ApiGen\loadTasks::getApiGenServices());
$container->addServiceProvider(\Robo\Task\Archive\loadTasks::getArchiveServices());
$container->addServiceProvider(\Robo\Task\Assets\loadTasks::getAssetsServices());
$container->addServiceProvider(\Robo\Task\Base\loadTasks::getBaseServices());
$container->addServiceProvider(\Robo\Task\Bower\loadTasks::getBowerServices());
$container->addServiceProvider(\Robo\Task\Composer\loadTasks::getComposerServices());
$container->addServiceProvider(\Robo\Task\Development\loadTasks::getDevelopmentServices());
$container->addServiceProvider(\Robo\Task\Docker\loadTasks::getDockerServices());
$container->addServiceProvider(\Robo\Task\File\loadTasks::getFileServices());
$container->addServiceProvider(\Robo\Task\FileSystem\loadTasks::getFileSystemServices());
$container->addServiceProvider(\Robo\Task\Remote\loadTasks::getRemoteServices());
$container->addServiceProvider(\Robo\Task\Testing\loadTasks::getTestingServices());
$container->addServiceProvider(\Robo\Task\Vcs\loadTasks::getVcsServices());
}
/**
* Process a shebang script, if one was used to launch this Runner.
*
* @param array $args
* @return $args with shebang script removed
*/
protected function shebang($args)
{
// Option 1: Shebang line names Robo, but includes no parameters.
// #!/bin/env robo
// The robo class may contain multiple commands; the user may
// select which one to run, or even get a list of commands or
// run 'help' on any of the available commands as usual.
if ((count($args) > 1) && $this->isShebangFile($args[1])) {
return array_merge([$args[0]], array_slice($args, 2));
}
// Option 2: Shebang line stipulates which command to run.
// #!/bin/env robo mycommand
// The robo class must contain a public method named 'mycommand'.
// This command will be executed every time. Arguments and options
// may be provided on the commandline as usual.
if ((count($args) > 2) && $this->isShebangFile($args[2])) {
return array_merge([$args[0]], explode(' ', $args[1]), array_slice($args, 3));
}
return $args;
}
/**
* Determine if the specified argument is a path to a shebang script.
* If so, load it.
*
* @param $filepath file to check
* @return true if shebang script was processed
*/
protected function isShebangFile($filepath)
{
if (!file_exists($filepath)) {
return false;
}
$fp = fopen($filepath, "r");
if ($fp === false) {
return false;
}
$line = fgets($fp);
$result = $this->isShebangLine($line);
if ($result) {
while ($line = fgets($fp)) {
$line = trim($line);
if ($line == '<?php') {
$script = stream_get_contents($fp);
if (preg_match('#^class *([^ ]+)#m', $script, $matches)) {
$this->roboClass = $matches[1];
eval($script);
$result = true;
}
}
}
}
fclose($fp);
return $result;
}
/**
* Test to see if the provided line is a robo 'shebang' line.
*/
protected function isShebangLine($line)
{
return ((substr($line, 0, 2) == '#!') && (strstr($line, 'robo') !== false));
}
/**
* @param $argv
* @return ArgvInput
*/
protected function prepareInput($argv)
{
$pos = array_search('--', $argv);
// cutting pass-through arguments
if ($pos !== false) {
$this->passThroughArgs = implode(' ', array_slice($argv, $pos+1));
$argv = array_slice($argv, 0, $pos);
}
// loading from other directory
$pos = array_search('--load-from', $argv) ?: array_search('-f', $argv);
if ($pos !== false) {
if (isset($argv[$pos +1])) {
$this->dir = $argv[$pos +1];
unset($argv[$pos +1]);
}
unset($argv[$pos]);
// Make adjustments if '--load-from' points at a file.
if (is_file($this->dir)) {
$this->roboFile = basename($this->dir);
$this->dir = dirname($this->dir);
$className = basename($this->roboFile, '.php');
if ($className != $this->roboFile) {
$this->roboClass = $className;
}
}
}
return new ArgvInput($argv);
}
public function shutdown()
{
$error = error_get_last();
if (!is_array($error)) return;
$this->writeln(sprintf("<error>ERROR: %s \nin %s:%d\n</error>", $error['message'], $error['file'], $error['line']));
}
/**
* This is just a proxy error handler that checks the current error_reporting level.
* In case error_reporting is disabled the error is marked as handled, otherwise
* the normal internal error handling resumes.
*
* @return bool
*/
public function handleError()
{
if (error_reporting() === 0) {
return true;
}
return false;
}
}