-
Notifications
You must be signed in to change notification settings - Fork 0
/
make.php
61 lines (53 loc) · 1.4 KB
/
make.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
#!/usr/bin/php
<?php
// TODO: move this to `buildpro`
exit((int) main($argc, $argv));
/**
* @param integer $argc
* @param array $argv
* @return integer
*/
function main($argc, array $argv) {
$rules = array();
$target = '';
foreach (file('Makefile') as $line) {
$line = trim($line);
if (preg_match('/^(.*)=(.*)$/', $line, $matches)) {
$vars[$matches[1]] = $matches[2];
} elseif (preg_match('/(.*):$/', $line, $matches)) {
$target = $matches[1];
} elseif ($target && $line) {
if (substr($line, 0, 1) != '#') {
$line = replace_vars($line, $vars);
$line = preg_replace('/\$\((.*)\)/', '\${$1}', $line);
$rules[$target][] = $line;
}
}
}
if ($argc >= 2) {
$selected_target = $argv[1];
foreach ($rules[$selected_target] as $cmd) {
echo shell_exec($cmd);
}
} else {
echo "Options:\n";
foreach (array_keys($rules) as $target) {
echo " | $target:\n";
}
}
return 0;
}
/**
* @param string $subject
* @param array $vars
* @return string
*/
function replace_vars($subject, array $vars) {
$keys = array();
$values = array();
foreach ($vars as $key => $value) {
$keys[] = '$('.$key.')';
$values[] = $value;
}
return str_replace($keys, $values, $subject);
}