-
Notifications
You must be signed in to change notification settings - Fork 35
/
yay
executable file
·83 lines (59 loc) · 1.79 KB
/
yay
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
#!/usr/bin/env php
<?php declare(strict_types=1);
use Yay\{Engine, YayPreprocessorError};
set_error_handler(function($errno, $errstr) {
throw new Exception($errstr, $errno);
});
(function(){
$autoloads = [
__DIR__ . '/../../../autoload.php', // as dependency
__DIR__ . '/../vendor/autoload.php', // as main package
];
foreach ($autoloads as $file) if (file_exists($file) && is_readable($file)) {
require_once($file);
return;
}
throw new Exception('Could not find autoload file. Check your composer installation.');
})();
try {
$doc = <<<DOC
YAY!
Usage:
yay
yay <file>
yay --macros=<macros>
yay --macros=<macros> <file>
yay -h | --help
yay --version
Options:
-h --help Show this screen.
--version Show version.
--macros=<macros> PHP glob pattern for macros to be loaded. Ex: my/macros/*.yay [default: '']
Examples:
```
# Process file:
yay input.php > output.php
# Process file, preload project macros:
> yay --macros="./project-macros/*.yay" input.php > output.php
# Process stdin:
> cat input.php | yay > output.php
# Process stdin, preload project macros:
> cat input.php | yay --macros="./project-macros/*.yay" > output.php
```
DOC;
$argv = Docopt::handle($doc, include __DIR__ . '/../meta.php');
$file = $argv['<file>'] ?? 'php://stdin';
$source = file_get_contents($file);
$engine = new Engine;
gc_disable();
foreach(glob((string) $argv['--macros']) as $f) $engine->expand(file_get_contents($f), $f);
$expansion = $engine->expand($source, $file);
gc_enable();
file_put_contents('php://stdout', $expansion);
}
catch (YayPreprocessorError $e) {
file_put_contents('php://stderr', $e . PHP_EOL);
}
catch (Exception $e) {
file_put_contents('php://stderr', $e . PHP_EOL);
}