Skip to content

Commit

Permalink
Add experimental non opinionated command line linker to bin/yay:
Browse files Browse the repository at this point in the history
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
```
  • Loading branch information
marcioAlmada committed Sep 29, 2019
1 parent 975d96a commit 36e8191
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 6 deletions.
50 changes: 45 additions & 5 deletions bin/yay
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,61 @@ set_error_handler(function($errno, $errstr) {
})();

try {
$file = $argv[1] ?? '';

$source = file_get_contents($file) ?: file_get_contents('php://stdin');
$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();

$expansion = (new Engine)->expand($source, $file);
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', (getenv('YAY_STACK_TRACE') ? $e : sprintf("% % \n", get_class($e), $e->getMessage())));
file_put_contents('php://stderr', $e . PHP_EOL);
}
catch (Exception $e) {
file_put_contents('php://stderr', $e);
file_put_contents('php://stderr', $e . PHP_EOL);
}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"php": "7.*",
"ext-mbstring": "*",
"ext-tokenizer": "*",
"nikic/php-parser": "^2.1|^3.0|^4.0"
"nikic/php-parser": "^2.1|^3.0|^4.0",
"docopt/docopt": "^1.0"
},
"autoload": {
"files": [
Expand Down
3 changes: 3 additions & 0 deletions meta.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php return [
'version' => '0.7'
];

0 comments on commit 36e8191

Please sign in to comment.