From 9380a82971b289a00576806d9bad628bc41d778c Mon Sep 17 00:00:00 2001 From: gravel Date: Tue, 18 Apr 2023 13:21:35 +0200 Subject: [PATCH] Apply `--verbose` to `make dev` & propagate to SSG --- .phpenv | 6 ++++++ Makefile | 42 +++++++++++++++++++++--------------------- php/fetch-servers.php | 8 -------- php/generate-html.php | 38 +++++++++++++++++++++++--------------- php/utils/logging.php | 20 ++++++++++++++++---- 5 files changed, 66 insertions(+), 48 deletions(-) diff --git a/.phpenv b/.phpenv index 8040b40c..a57fb7fc 100644 --- a/.phpenv +++ b/.phpenv @@ -9,6 +9,12 @@ include_once "$PROJECT_ROOT/php/utils/logging.php"; + // Read the -v|--verbose option increasing logging verbosity to debug. + $options = getopt("v", ["verbose"]); + if (isset($options["v"]) or isset($options["verbose"])) { + $LOGGING_VERBOSITY = LoggingVerbosity::Debug; + } + // set timeout for file_get_contents() ini_set('default_socket_timeout', 6); // in seconds, default is 60 diff --git a/Makefile b/Makefile index f123dafe..76d07b46 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,7 @@ -port = 8081 -output = output +PORT ?= 8081 +OUTPUT ?= output +FLAGS ?= +MAKE = make FLAGS=$(FLAGS) # First goal is the default with `make`. @@ -14,49 +16,47 @@ all: fetch html # Fetch room listing. fetch: - /bin/php php/fetch-servers.php - -# Fetch room listing with extra verbosity. -fetch-v: - /bin/php php/fetch-servers.php --verbose + /bin/php php/fetch-servers.php $(FLAGS) # Generate HTML from data. html: - /bin/php php/generate-html.php - -# Last item run in foreground to receive interrupts. + /bin/php php/generate-html.php $(FLAGS) # Serve a local copy which responds to file changes. +dev: FLAGS = --verbose dev: open - make server & - make watchdog + $(MAKE) server & + $(MAKE) watchdog + +# (Last item run in foreground to receive interrupts.) # Serve a local copy on LAN which responds to file changes. +lan-dev: FLAGS = --verbose lan-dev: open - ip addr | fgrep -e ' 192.' -e ' 10.' - make lan-server & - make watchdog + -which ip 1>/dev/null 2>/dev/null && ip addr | fgrep -e ' 192.' -e ' 10.' || true + $(MAKE) lan-server & + $(MAKE) watchdog # Serve a local copy. server: - /bin/php -S localhost:$(port) -t $(output) + /bin/php -S "localhost:$(PORT)" -t "$(OUTPUT)" # Serve a local copy on all interfaces. lan-server: - /bin/php -S 0.0.0.0:$(port) -t $(output) + /bin/php -S "0.0.0.0:$(PORT)" -t "$(OUTPUT)" # Open locally served page in browser. open: - xdg-open http://localhost:$(port) >/dev/null 2>/dev/null & disown + xdg-open "http://localhost:$(PORT)" >/dev/null 2>/dev/null & disown # Update HTML on file change. Doesn't check for new files. watchdog: - find . | entr -n -s "make html" + find . | grep -v ".git" | entr -n -s "$(MAKE) html" # Remove artefacts clean: - -rm -r cache - -rm -r output/*.html + -rm -r cache 2>/dev/null || true + -rm -r output/*.html 2>/dev/null || true # Build everything from scratch and test functionality. test: clean all open server diff --git a/php/fetch-servers.php b/php/fetch-servers.php index b2d31ef7..ab42f928 100644 --- a/php/fetch-servers.php +++ b/php/fetch-servers.php @@ -21,14 +21,6 @@ * 6. De-dupe servers based on pubkey */ function main() { - global $LOGGING_VERBOSITY; - - // Read the -v|--verbose option increasing logging verbosity to debug. - $options = getopt("v", ["verbose"]); - if (isset($options["v"]) or isset($options["verbose"])) { - $LOGGING_VERBOSITY = LoggingVerbosity::Debug; - } - global $CACHE_ROOT, $ROOMS_FILE, $KNOWN_SERVERS, $KNOWN_PUBKEYS; // Create default directories with conservative permissions. diff --git a/php/generate-html.php b/php/generate-html.php index 318ed6af..638a48ba 100644 --- a/php/generate-html.php +++ b/php/generate-html.php @@ -15,26 +15,34 @@ return $files; } - foreach (rglob("$TEMPLATES_ROOT/*.php") as $phppath) { - // Do not render auxiliary PHP files. - if (str_contains("$phppath", "/+") || $phppath[0] == "+") - continue; + function generate_html() { + global $LOGGING_VERBOSITY, $TEMPLATES_ROOT, $DOCUMENT_ROOT; + $flags = LoggingVerbosity::getVerbosityFlags($LOGGING_VERBOSITY)[1]; - $docpath = str_replace($TEMPLATES_ROOT, $DOCUMENT_ROOT, $phppath); - $relpath = str_replace($TEMPLATES_ROOT, "", $phppath); - $docpath = str_replace(".php", ".html", $docpath); + foreach (rglob("$TEMPLATES_ROOT/*.php") as $phppath) { + // Do not render auxiliary PHP files. + if (str_contains("$phppath", "/+") || $phppath[0] == "+") + continue; - // This works? Yes, yes it does. - // We do this to isolate the environment and include-once triggers, - // otherwise we could include the documents in an ob_* wrapper. - // Same as shell_exec, except we don't have to escape quotes. - log_info("Generating output for $relpath."); - $document = `cd "$TEMPLATES_ROOT"; php $phppath`; + $docpath = str_replace($TEMPLATES_ROOT, $DOCUMENT_ROOT, $phppath); + $relpath = str_replace($TEMPLATES_ROOT, "", $phppath); + $docpath = str_replace(".php", ".html", $docpath); - file_put_contents($docpath, $document); + // This works? Yes, yes it does. + // We do this to isolate the environment and include-once triggers, + // otherwise we could include the documents in an ob_* wrapper. + + // Same as shell_exec, except we don't have to escape quotes. + log_info("Generating output for $relpath."); + $document = `cd "$TEMPLATES_ROOT"; php $phppath $flags`; + + file_put_contents($docpath, $document); + } + + log_info("Done generating HTML."); } - log_info("Done generating HTML."); + generate_html(); ?> diff --git a/php/utils/logging.php b/php/utils/logging.php index 3d340aa6..a4f6b36e 100644 --- a/php/utils/logging.php +++ b/php/utils/logging.php @@ -44,7 +44,7 @@ /** * Returns the color marker for the given logging verbosity. - * @param $verbosity Logging verbosity to used for printing. + * @param int $verbosity Logging verbosity to used for printing. * @return ?string Terminal escape sequence to color foreground text. */ static function getVerbosityColorMarker(int $verbosity): ?string { @@ -56,6 +56,18 @@ default => '' }; } + + /** + * Returns a pair of optíons trigerring the given verbosity. + * @param int $verbosity Logging verbosity to set using flag. + * @return string[] Pair of short and long command-line verbosity flags. + */ + static function getVerbosityFlags(int $verbosity): array { + return match($verbosity) { + LoggingVerbosity::Debug => ["-v", "--verbose"], + default => ['', ''] + }; + } } /** @@ -135,12 +147,12 @@ * Only logs when `$LOGGING_VERBOSITY` is debug and below. * @param string $msg String message to log. */ - function log_value(mixed $value) { - log_debug(var_export($value, true)); + function log_value(mixed $value, int $message_verbosity = LoggingVerbosity::Debug) { + _log_message(var_export($value, true), $message_verbosity); } /** - * @var $LOGGING_VERBOSITY + * @var int $LOGGING_VERBOSITY * Global setting. * Controls how detailed the displayed logs are. */