/
Bin.php
121 lines (104 loc) · 3.61 KB
/
Bin.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
<?php
namespace phake;
use phake\Builder;
use phake\OptionParser;
use phake\Utils;
use phake\TaskNotFoundException;
use Exception;
class Bin
{
public function execute($args) {
try {
//
// Defaults
$action = 'invoke';
$task_names = array('default');
$trace = false;
$runfile = false;
array_shift($args);
$parser = new OptionParser($args);
foreach ($parser->get_options() as $option => $value) {
switch ($option) {
case 't':
case 'trace':
$trace = true;
break;
case 'T':
case 'tasks':
$action = 'list';
break;
case 'f':
case 'file':
$runfile = $value;
break;
default:
throw new Exception("Unknown command line option '$option'");
}
}
$cli_args = array();
$cli_task_names = array();
foreach ($parser->get_non_options() as $option) {
if (strpos($option, '=') > 0) {
$cli_args[] = $option;
} else {
$cli_task_names[] = $option;
}
}
$cli_args = Utils::parse_args($cli_args);
if (count($cli_task_names)) {
$task_names = $cli_task_names;
}
$builder = builder();
/* @var $builder Builder */
//
// Locate runfile
if (!$runfile) {
$runfile = $builder->resolve_runfile(getcwd());
$directory = dirname($runfile);
if (!@chdir($directory)) {
throw new Exception("Couldn't change to directory '$directory'");
} else {
echo "(in $directory)\n";
}
}
$builder->load_runfile($runfile);
//
// Go, go, go
$application = $builder->get_application();
$application->set_args($cli_args);
$application->reset();
switch ($action) {
case 'list':
$task_list = $application->get_task_list();
if (count($task_list)) {
$max = max(array_map('strlen', array_keys($task_list)));
foreach ($task_list as $name => $desc) {
echo str_pad($name, $max + 4) . $desc . "\n";
}
}
break;
case 'invoke':
foreach ($task_names as $task_name) {
$application->invoke($task_name);
}
break;
}
} catch (TaskNotFoundException $tnfe) {
$this->fatal($tnfe, "Don't know how to build task '$task_name'\n", $trace);
} catch (Exception $e) {
$this->fatal($e, null, $trace);
}
}
private function fatal($exception, $message = null, $trace = false) {
echo "aborted!\n";
if (!$message) $message = $exception->getMessage();
if (!$message) $message = get_class($exception);
write(red($message), "\n\n");
if ($trace) {
echo $exception->getTraceAsString() . "\n";
} else {
echo "(See full trace by running task with --trace)\n";
}
die(1);
}
}