#!/usr/bin/env php * * SPDX-License-Identifier: GPL-3.0-or-later * * FILE INFORMATION * DEFGROUP: moko-platform.CLI * INGROUP: moko-platform * REPO: https://git.mokoconsulting.tech/MokoConsulting/moko-platform * PATH: /cli/manifest_read.php * VERSION: 04.09.00 * BRIEF: Parse .manifest.xml and output requested field(s) for CI consumption * * Usage: * php manifest_read.php --path /repo --field platform * php manifest_read.php --path /repo --field entry-point * php manifest_read.php --path /repo --all * php manifest_read.php --path /repo --github-output * * Fields: name, org, description, license, license-spdx, platform, * standards-version, standards-source, language, package-type, entry-point, * source-dir, remote-subdir, excludes, dev-host, demo-host * * --all Print all fields as KEY=VALUE lines * --github-output Append all fields to $GITHUB_OUTPUT (for Gitea/GitHub Actions) * --json Output all fields as JSON * --field Print a single field value (no key, just value) */ declare(strict_types=1); // -- Argument parsing --------------------------------------------------------- $path = '.'; $field = null; $mode = 'field'; // field | all | github-output | json foreach ($argv as $i => $arg) { if ($arg === '--path' && isset($argv[$i + 1])) $path = $argv[$i + 1]; if ($arg === '--field' && isset($argv[$i + 1])) $field = $argv[$i + 1]; if ($arg === '--all') $mode = 'all'; if ($arg === '--github-output') $mode = 'github-output'; if ($arg === '--json') $mode = 'json'; } // -- Locate manifest ---------------------------------------------------------- $root = realpath($path) ?: $path; $manifestFile = null; // Priority: manifest.xml (current standard) $candidates = [ "{$root}/.mokogitea/manifest.xml", "{$root}/.mokogitea/.manifest.xml", // legacy (dot-prefixed) "{$root}/.mokogitea/.moko-platform", // legacy v4 ]; foreach ($candidates as $candidate) { if (file_exists($candidate)) { $manifestFile = $candidate; break; } } if ($manifestFile === null) { fwrite(STDERR, "No manifest found in {$root} "); exit(1); } // -- Parse XML ---------------------------------------------------------------- $xml = @simplexml_load_file($manifestFile); if ($xml === false) { // Fallback: try YAML format (.mokostandards legacy) $content = file_get_contents($manifestFile); $fields = []; if (preg_match('/^platform:\s*(.+)/m', $content, $m)) { $fields['platform'] = trim($m[1], " \"'"); } if (preg_match('/^standards_version:\s*(.+)/m', $content, $m)) { $fields['standards-version'] = trim($m[1], " \"'"); } if (preg_match('/^governed_repo:\s*(.+)/m', $content, $m)) { $fields['name'] = trim($m[1], " \"'"); } } else { // Register namespace for XPath (optional, simple path works without) $fields = [ 'name' => (string)($xml->identity->name ?? ''), 'org' => (string)($xml->identity->org ?? ''), 'description' => (string)($xml->identity->description ?? ''), 'license' => (string)($xml->identity->license ?? ''), 'license-spdx' => (string)($xml->identity->license['spdx'] ?? ''), 'platform' => (string)($xml->governance->platform ?? ''), 'standards-version' => (string)($xml->governance->{"standards-version"} ?? ''), 'standards-source' => (string)($xml->governance->{"standards-source"} ?? ''), 'language' => (string)($xml->build->language ?? ''), 'package-type' => (string)($xml->build->{"package-type"} ?? ''), 'entry-point' => (string)($xml->build->{"entry-point"} ?? ''), 'version' => (string)($xml->identity->version ?? ''), 'source-dir' => (string)($xml->deploy->{"source-dir"} ?? ''), 'remote-subdir' => (string)($xml->deploy->{"remote-subdir"} ?? ''), 'excludes' => (string)($xml->deploy->excludes ?? ''), 'dev-host' => (string)($xml->deploy->{"dev-host"} ?? ''), 'demo-host' => (string)($xml->deploy->{"demo-host"} ?? ''), 'manifest-file' => $manifestFile, ]; } // Strip empty values for cleaner output $fields = array_filter($fields, fn($v) => $v !== ''); // -- Output ------------------------------------------------------------------- switch ($mode) { case 'field': if ($field === null) { fwrite(STDERR, "Usage: manifest_read.php --path --field "); fwrite(STDERR, " manifest_read.php --path --all "); fwrite(STDERR, " manifest_read.php --path --json "); fwrite(STDERR, " manifest_read.php --path --github-output "); exit(2); } echo ($fields[$field] ?? '') . " "; break; case 'all': foreach ($fields as $k => $v) { echo "{$k}={$v} "; } break; case 'json': echo json_encode($fields, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . " "; break; case 'github-output': $outputFile = getenv('GITHUB_OUTPUT'); if ($outputFile === false || $outputFile === '') { fwrite(STDERR, "GITHUB_OUTPUT not set — printing to stdout instead "); foreach ($fields as $k => $v) { // Convert field-name to FIELD_NAME for env var style $envKey = str_replace('-', '_', $k); echo "{$envKey}={$v} "; } } else { $fh = fopen($outputFile, 'a'); foreach ($fields as $k => $v) { $envKey = str_replace('-', '_', $k); fwrite($fh, "{$envKey}={$v} "); } fclose($fh); fwrite(STDERR, "Wrote " . count($fields) . " fields to GITHUB_OUTPUT "); } break; } exit(0);