From 780abd0c3dd0dc39f3333c7967e5ecabe88539b8 Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Fri, 28 Jan 2022 11:11:55 +0100 Subject: [PATCH] bin/serve-stub --- bin/ref2html | 9 +-- bin/serve-stub | 113 ++++++++++++++++++++++++++++++++++++++ bin/stub2ref | 12 +++- mdref/Generator.php | 4 +- mdref/Generator/Scrap.php | 2 +- 5 files changed, 130 insertions(+), 10 deletions(-) create mode 100755 bin/serve-stub diff --git a/bin/ref2html b/bin/ref2html index 779a1d0..2732636 100755 --- a/bin/ref2html +++ b/bin/ref2html @@ -18,9 +18,6 @@ if ($argc < 3) { fprintf(STDERR, " Note: the basedir will also be used as \n"); exit(1); } -function say($fmt, ...$args) { - return fprintf(STDERR, $fmt, ...$args); -}; $out = $argv[1]; if (!is_dir($out) && !mkdir($out, 0775, true)) { fprintf(STDERR, "Could not create output directory %s\n", $out); @@ -63,7 +60,7 @@ $gen = function(Entry $entry) use($fmt, $out, $xfm, $red, &$gen) { $dst = $dir . ".html"; foreach ($xfm as $from => $dest) { if (strpos($src, $dest) !== false) { - say("Redirecting from %s to %s\n", $from, $dest); + printf("Redirecting from %s to %s\n", $from, $dest); $red($from, $dest, $src); break; } @@ -76,12 +73,12 @@ $gen = function(Entry $entry) use($fmt, $out, $xfm, $red, &$gen) { $gen($subentry); } } - say("Generating %s from %s\n", $dst, $src); + printf("Generating %s from %s\n", $dst, $src); $fmt($src, $dst); }; /** @var $repo Repo */ foreach ($ref as $repo) { - say("Entering ref %s\n", $repo->getName()); + printf("Entering ref %s\n", $repo->getName()); if (is_file($stub = $repo->getPath($repo->getName().".stub.php"))) { copy($stub, $out . "/" . basename($stub)); } diff --git a/bin/serve-stub b/bin/serve-stub new file mode 100755 index 0000000..650d67c --- /dev/null +++ b/bin/serve-stub @@ -0,0 +1,113 @@ +#!/usr/bin/env php + []\n", $argv[0]); + exit(1); +} + +if (!($stub = realpath($argv[1]))) { + $stat = stat($stub); + assert(!$stat); + exit(2); +} +if ($argc > 2) { + $namespace = $argv[2]; +} else { + $namespace = basename($stub, ".stub.php"); +} + +$tmplck = tempnam(sys_get_temp_dir(), "mdref."); +$tmpdir = $tmplck . ".d"; +mkdir($tmpdir) && chdir($tmpdir) || exit(-1); + +$running = true; +$shutdown = function() use($tmpdir, $tmplck, &$running) { + $running = false; + chdir(__DIR__) && rm_r($tmpdir, $tmplck); +}; +register_shutdown_function($shutdown); +pcntl_signal(SIGINT, $shutdown, false); +pcntl_signal(SIGTERM, $shutdown, false); + +$passthru = fn($cmd) => fn() => printf("%s\n", $cmd) && !passthru("$cmd 2>&1 >/dev/null", $rc) && !$rc; +$stub2ref = $passthru( + sprintf("%s/stub2ref %s %s %s", + __DIR__, + escapeshellarg($namespace), + escapeshellarg($stub), + escapeshellarg($tmpdir) + ) +); +$ref2html = $passthru( + sprintf("%s/ref2html . .", + __DIR__, + ) +); +$update = fn() => $stub2ref() && $ref2html(); + +if ($update()) { + $ifd = inotify_init(); + inotify_add_watch($ifd, $stub, IN_MODIFY); + stream_set_blocking($ifd, false); + + file_put_contents("router.php", file_get_contents(__FILE__, false, null, __COMPILER_HALT_OFFSET__)); + $php = popen(sprintf("%s -S localhost:0 -t . router.php 2>&1 | grep --line-buffered -Ev 'Accepted|Closing|GET'", PHP_BINARY), "r"); + echo fgets($php); + stream_set_blocking($php, false); + + while ($running) { + $R = [$ifd, $php]; $W = []; $E = []; + if (stream_select($R, $W, $E, null)) { + foreach ($R as $r) { + switch ($r) { + case $php: + while (($string = fgets($php))) echo $string; + break; + case $ifd: + // cooldown + usleep(50 * 1000); + while (inotify_read($ifd)); + $update(); + break; + } + } + } + pcntl_signal_dispatch(); + } +} + +function rm_r(string ...$args) { + foreach ($args as $del) { + if (is_dir($del)) { + rm_r(...array_map(fn($sub) => "$del/$sub", array_slice(scandir($del), 2))); + rmdir($del); + } else if (file_exists($del)) { + unlink($del); + } + } +} + +__HALT_COMPILER(); +inspectNamespace($namespace); diff --git a/mdref/Generator.php b/mdref/Generator.php index 8cd96bd..ec1a23e 100644 --- a/mdref/Generator.php +++ b/mdref/Generator.php @@ -20,7 +20,7 @@ class Generator { $ns_path = $this->destination . "/" . strtr($ns, "\\", "/"); foreach ($funcs as $fn => $rf) { $fn_file = "$ns_path/$fn.md"; - fprintf(STDERR, "Generating %s\n", $fn_file); + printf("Generating %s\n", $fn_file); is_dir($ns_path) || mkdir($ns_path, 0770, true); file_put_contents($fn_file, new Func($this, $rf)); } @@ -37,7 +37,7 @@ class Generator { foreach ($cls as $cn => $rc) { $cn_path = "$ns_path/$cn"; $cn_file = "$cn_path.md"; - fprintf(STDERR, "Generating %s\n", $cn_file); + printf("Generating %s\n", $cn_file); is_dir($ns_path) || mkdir($ns_path, 0770, true); file_put_contents($cn_file, new Cls($this, $rc)); $this->generateMethods($rc); diff --git a/mdref/Generator/Scrap.php b/mdref/Generator/Scrap.php index 30b936b..82f81db 100644 --- a/mdref/Generator/Scrap.php +++ b/mdref/Generator/Scrap.php @@ -19,7 +19,7 @@ class Scrap { $this->createDocBlock(); } if (!$this->doc) { - fprintf(STDERR, ... match (get_class($ref)) { + printf(... match (get_class($ref)) { \ReflectionClass::class => ["Missing docs for class %s\n", $ref->name], \ReflectionMethod::class => ["Missing docs for method %s::%s()\n", $ref->class, $ref->name], \ReflectionProperty::class => ["Missing docs for property %s %s::\$%s\n", $ref->getType(), $ref->class, $ref->name], -- 2.30.2