forked from consolidation/robo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Runner.php
183 lines (157 loc) · 5.76 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
<?php
namespace Robo;
use Robo\Common\IO;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\ConsoleOutput;
class Runner
{
use IO;
const VERSION = '0.5.3';
const ROBOCLASS = 'RoboFile';
const ROBOFILE = 'RoboFile.php';
protected $currentDir = '.';
protected $passThroughArgs = null;
/**
* @var ConsoleOutput
*/
protected static $printer;
protected function loadRoboFile()
{
if (!file_exists(self::ROBOFILE)) {
$this->writeln("<comment> ".self::ROBOFILE." not found in this dir </comment>");
$answer = $this->ask(" Should I create RoboFile here? (y/n) \n");
if (strtolower(trim($answer)) === 'y') {
$this->initRoboFile();
}
exit;
}
require_once self::ROBOFILE;
if (!class_exists(self::ROBOCLASS)) {
$this->writeln("<error>Class ".self::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'));
Config::setOutput(new ConsoleOutput());
$input = $this->prepareInput($input ? $input : $_SERVER['argv']);
if (!$this->loadRoboFile()) {
$app = new Application('Robo', self::VERSION);
$app->add(new Init('init'));
$app->run();
return;
}
$app = $this->createApplication(self::ROBOCLASS);
$app->run($input);
}
public function createApplication($className)
{
$app = new Application('Robo', self::VERSION);
$roboTasks = new $className;
$commandNames = array_filter(get_class_methods($className), function($m) {
return !in_array($m, ['__construct']);
});
$passThrough = $this->passThroughArgs;
foreach ($commandNames as $commandName) {
$command = $this->createCommand(new TaskInfo($className, $commandName));
$command->setCode(function(InputInterface $input) use ($roboTasks, $commandName, $passThrough) {
// get passthru args
$args = $input->getArguments();
array_shift($args);
if ($passThrough) {
$args[key(array_slice($args, -1, 1, TRUE))] = $passThrough;
}
$args[] = $input->getOptions();
$res = call_user_func_array([$roboTasks, $commandName], $args);
if (is_int($res)) exit($res);
if (is_bool($res)) exit($res ? 0 : 1);
if ($res instanceof Result) exit($res->getExitCode());
});
$app->add($command);
}
return $app;
}
protected function prepareInput($argv)
{
$pos = array_search('--', $argv);
if ($pos !== false) {
$this->passThroughArgs = implode(' ', array_slice($argv, $pos+1));
$argv = array_slice($argv, 0, $pos);
}
return new ArgvInput($argv);
}
public function createCommand(TaskInfo $taskInfo)
{
$task = new Command($taskInfo->getName());
$task->setDescription($taskInfo->getDescription());
$task->setHelp($taskInfo->getHelp());
$args = $taskInfo->getArguments();
foreach ($args as $name => $val) {
$description = $taskInfo->getArgumentDescription($name);
if ($val === TaskInfo::PARAM_IS_REQUIRED) {
$task->addArgument($name, InputArgument::REQUIRED, $description);
} elseif (is_array($val)) {
$task->addArgument($name, InputArgument::IS_ARRAY, $description, $val);
} else {
$task->addArgument($name, InputArgument::OPTIONAL, $description, $val);
}
}
$opts = $taskInfo->getOptions();
foreach ($opts as $name => $val) {
$description = $taskInfo->getOptionDescription($name);
$fullname = $name;
$shortcut = '';
if (strpos($name, '|')) {
list($fullname, $shortcut) = explode('|', $name, 2);
}
if (is_bool($val)) {
$task->addOption($fullname, $shortcut, InputOption::VALUE_NONE, $description);
} else {
$task->addOption($fullname, $shortcut, InputOption::VALUE_OPTIONAL, $description, $val);
}
}
return $task;
}
protected function initRoboFile()
{
file_put_contents(
self::ROBOFILE,
'<?php'
. "\n/**"
. "\n * This is project's console commands configuration for Robo task runner."
. "\n *"
. "\n * @see http://robo.li/"
. "\n */"
. "\nclass " . self::ROBOCLASS . " extends \\Robo\\Tasks\n{\n // define public methods as commands\n}"
);
$this->writeln(self::ROBOFILE . " created");
}
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;
}
}