From 64de7159cf0e886d3ff07cd680e5b4e0ecbdd58a Mon Sep 17 00:00:00 2001 From: Jonathan Miller Date: Tue, 12 May 2026 19:22:28 +0000 Subject: [PATCH] =?UTF-8?q?feat:=20add=20manifest=5Fread.php=20=EF=BF=BD?= =?UTF-8?q?=20full=20.manifest.xml=20parser=20for=20CI=20[skip=20ci]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cli/manifest_read.php | 164 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 164 insertions(+) create mode 100644 cli/manifest_read.php diff --git a/cli/manifest_read.php b/cli/manifest_read.php new file mode 100644 index 0000000..4c5f996 --- /dev/null +++ b/cli/manifest_read.php @@ -0,0 +1,164 @@ +#!/usr/bin/env php + + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + * FILE INFORMATION + * DEFGROUP: MokoStandards.CLI + * INGROUP: MokoStandards + * REPO: https://git.mokoconsulting.tech/MokoConsulting/moko-platform + * PATH: /cli/manifest_read.php + * VERSION: 05.00.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 + * + * --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 > .moko-platform (backward compat) +$candidates = [ + "{$root}/.mokogitea/.manifest.xml", + "{$root}/.mokogitea/.moko-platform", + "{$root}/.gitea/.mokostandards", // 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"} ?? ''), + '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);