#!/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_element.php * BRIEF: Extract element name, type, type prefix, and ZIP name from manifest * * Usage: * php manifest_element.php --path . * php manifest_element.php --path . --version 09.01.00 --stability dev --github-output * * Detects platform (joomla, dolibarr, generic) and resolves: * ext_element — canonical element name (e.g. mokojgdpc) * ext_type — extension type (plugin, module, component, package, etc.) * ext_folder — group/folder for plugins (e.g. system) * ext_name — human-readable name (e.g. "Moko JGDPC") * type_prefix — Joomla type prefix (plg_system_, com_, mod_, etc.) * zip_name — computed ZIP filename */ declare(strict_types=1); $path = '.'; $version = null; $stability = 'stable'; $githubOutput = false; $repoName = ''; foreach ($argv as $i => $arg) { if ($arg === '--path' && isset($argv[$i + 1])) { $path = $argv[$i + 1]; } if ($arg === '--version' && isset($argv[$i + 1])) { $version = $argv[$i + 1]; } if ($arg === '--stability' && isset($argv[$i + 1])) { $stability = $argv[$i + 1]; } if ($arg === '--repo' && isset($argv[$i + 1])) { $repoName = $argv[$i + 1]; } if ($arg === '--github-output') { $githubOutput = true; } } $root = realpath($path) ?: $path; // ── Detect platform from manifest.xml ──────────────────────────────────────── $platform = 'generic'; $manifestXml = "{$root}/.mokogitea/manifest.xml"; if (file_exists($manifestXml)) { $content = file_get_contents($manifestXml); if (preg_match('/([^<]+)<\/platform>/', $content, $pm)) { $platform = trim($pm[1]); } } // ── Find extension manifest (Joomla XML) ───────────────────────────────────── $extManifest = null; $manifestFiles = array_merge( glob("{$root}/src/pkg_*.xml") ?: [], glob("{$root}/src/*.xml") ?: [], glob("{$root}/*.xml") ?: [] ); foreach ($manifestFiles as $file) { $c = file_get_contents($file); if (strpos($c, ', plugin= attribute, , or filename if (preg_match('/([^<]+)<\/element>/', $xml, $em)) { $extElement = $em[1]; } if (empty($extElement) && preg_match('/plugin="([^"]*)"/', $xml, $pm)) { $extElement = $pm[1]; } if ($extType === 'package' && preg_match('/([^<]+)<\/packagename>/', $xml, $pn)) { $extElement = $pn[1]; } if (empty($extElement)) { $extElement = strtolower(basename($extManifest, '.xml')); if (in_array($extElement, ['templatedetails', 'manifest'], true)) { $extElement = strtolower(str_replace([' ', '-'], '', $repoName ?: basename($root))); } } // Human-readable name if (preg_match('/([^<]+)<\/name>/', $xml, $nm)) { $extName = trim($nm[1]); } break; // Dolibarr platforms case in_array($platform, ['dolibarr', 'crm-module'], true) && $modFile !== null: $extType = 'dolibarr-module'; $modBasename = basename($modFile, '.class.php'); $extElement = strtolower(preg_replace('/^mod/', '', $modBasename)); $modContent = file_get_contents($modFile); if (preg_match('/\$this->name\s*=\s*[\'"]([^\'"]+)[\'"]/', $modContent, $nm)) { $extName = $nm[1]; } break; // Generic / fallback default: $extElement = strtolower(str_replace([' ', '-'], '', $repoName ?: basename($root))); $extType = 'generic'; break; } // ── Strip existing type prefix from element to prevent duplication ──────────── $extElement = preg_replace('/^(pkg_|com_|mod_|plg_[a-z]+_|tpl_|lib_)/', '', $extElement); // ── Compute type prefix ────────────────────────────────────────────────────── $typePrefix = ''; switch ($extType) { case 'plugin': $typePrefix = "plg_{$extFolder}_"; break; case 'module': $typePrefix = 'mod_'; break; case 'component': $typePrefix = 'com_'; break; case 'template': $typePrefix = 'tpl_'; break; case 'library': $typePrefix = 'lib_'; break; case 'package': $typePrefix = 'pkg_'; break; } // ── Compute ZIP name ───────────────────────────────────────────────────────── $suffixMap = [ 'development' => '-dev', 'dev' => '-dev', 'alpha' => '-alpha', 'beta' => '-beta', 'rc' => '-rc', 'release-candidate' => '-rc', 'stable' => '', ]; $suffix = $suffixMap[$stability] ?? ''; $zipName = ''; if ($version !== null) { $zipName = "{$typePrefix}{$extElement}-{$version}{$suffix}.zip"; } // Fallback name if (empty($extName)) { $extName = $repoName ?: basename($root); } // ── Output ─────────────────────────────────────────────────────────────────── $outputs = [ 'platform' => $platform, 'ext_element' => $extElement, 'ext_type' => $extType, 'ext_folder' => $extFolder, 'ext_name' => $extName, 'type_prefix' => $typePrefix, 'zip_name' => $zipName, ]; if ($githubOutput) { $ghOutput = getenv('GITHUB_OUTPUT'); $lines = []; foreach ($outputs as $key => $value) { $lines[] = "{$key}={$value}"; } if ($ghOutput) { file_put_contents($ghOutput, implode("\n", $lines) . "\n", FILE_APPEND); } else { // Fallback: echo ::set-output (legacy) foreach ($outputs as $key => $value) { echo "::set-output name={$key}::{$value}\n"; } } } else { foreach ($outputs as $key => $value) { echo "{$key}={$value}\n"; } } exit(0);