From 99cb230e2fb744ba7d689a791e0b2f64b0c122ba Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Tue, 25 Jan 2022 09:19:30 +0100 Subject: [PATCH] generator: use a proper destination --- bin/stub2ref | 21 +++++++++++++-------- composer.json | 2 +- mdref/Generator.php | 10 ++++++++-- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/bin/stub2ref b/bin/stub2ref index f0d11b1..37e55f2 100755 --- a/bin/stub2ref +++ b/bin/stub2ref @@ -6,25 +6,30 @@ namespace mdref; require_once $_composer_autoload_path ?? __DIR__."/../vendor/autoload.php"; if ($argc < 3) { - fprintf(STDERR, "Usage: cd ref- && %s \n", $argv[0]); + fprintf(STDERR, "Usage: %s []\n", $argv[0]); exit(1); } $namespace = $argv[1]; require_once $argv[2]; +$destination = ($argc > 3) ? $argv[3] : "."; -if (!file_exists("$namespace.mdref")) { - fprintf(STDERR, "Missing $namespace.mdref; generated default.\n"); - file_put_contents("$namespace.mdref", "./%s"); +if (!is_dir($destination)) { + fprintf(STDERR, "Missing $destination/; Making dir."); + mkdir($destination) || exit -1; } -if (!file_exists("$namespace.md")) { - fprintf(STDERR, "Missing $namespace.md; hard linking README.md\n"); - link(dirname($argv[2]) . "/README.md", "$namespace.md"); +if (!file_exists("$destination/$namespace.mdref")) { + fprintf(STDERR, "Missing $destination/$namespace.mdref; generated default.\n"); + file_put_contents("$destination/$namespace.mdref", "./%s") || exit -2; +} +if (!file_exists("$destination/$namespace.md")) { + fprintf(STDERR, "Missing $destination/$namespace.md; hard linking README.md\n"); + link(dirname($argv[2]) . "/README.md", "$destination/$namespace.md") || exit -3; } $inspector = new Inspector; $inspector->inspectNamespace($namespace); -$generator = new Generator; +$generator = new Generator($destination); $generator->generateFunctions($inspector->getFunctions()); $generator->generateClasses($inspector->getClasses()); diff --git a/composer.json b/composer.json index 4a49999..a66e582 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,7 @@ } }, "require": { - "php": "^7.3 || ^8.0", + "php": "^7.4 || ^8.0", "ext-ctype": "*", "ext-filter": "*", "ext-pcre": "*", diff --git a/mdref/Generator.php b/mdref/Generator.php index 84a323e..8cd96bd 100644 --- a/mdref/Generator.php +++ b/mdref/Generator.php @@ -5,13 +5,19 @@ namespace mdref; use mdref\Generator\{Cls, Func}; class Generator { + protected string $destination; + + public function __construct(string $destination = ".") { + $this->destination = $destination; + } + /** * @param array> $functions * @return void */ public function generateFunctions(array $functions) : void { foreach ($functions as $ns => $funcs) { - $ns_path = strtr($ns, "\\", "/"); + $ns_path = $this->destination . "/" . strtr($ns, "\\", "/"); foreach ($funcs as $fn => $rf) { $fn_file = "$ns_path/$fn.md"; fprintf(STDERR, "Generating %s\n", $fn_file); @@ -27,7 +33,7 @@ class Generator { */ public function generateClasses(array $classes) : void { foreach ($classes as $ns => $cls) { - $ns_path = strtr($ns, "\\", "/"); + $ns_path = $this->destination . "/" . strtr($ns, "\\", "/"); foreach ($cls as $cn => $rc) { $cn_path = "$ns_path/$cn"; $cn_file = "$cn_path.md"; -- 2.30.2