Files
moko-platform/cli/version_set_platform.php
T
Jonathan Miller 3d1af376a0 feat(cli): add release automation CLI scripts
New CLI scripts to replace inline bash in CI workflows:
- changelog_promote.php: promote [Unreleased] to versioned entry
- badge_update.php: update [VERSION: XX.XX.XX] in markdown files
- updates_xml_build.php: generate Joomla updates.xml with stability suffixes
- package_build.php: build ZIP/tar.gz install packages
- release_manage.php: create/update/delete Gitea releases + upload assets
- release_cascade.php: delete lesser pre-release channels

Updated version_set_platform.php with --stability flag to append
-dev/-alpha/-beta/-rc suffixes to non-stable versions.

Authored-by: Moko Consulting
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-05-21 21:25:35 -05:00

145 lines
4.6 KiB
PHP

#!/usr/bin/env php
<?php
/* Copyright (C) 2026 Moko Consulting <hello@mokoconsulting.tech>
*
* 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/version_set_platform.php
* BRIEF: Set version in platform-specific files (Dolibarr $this->version, Joomla <version>)
*
* Usage:
* php version_set_platform.php --path . --version 04.01.00
* php version_set_platform.php --path . --version 04.01.00 --stability alpha
*
* When --stability is set to anything other than "stable", the suffix is
* appended to the version (e.g. 04.01.00-dev, 04.01.00-alpha, 04.01.00-rc).
*/
declare(strict_types=1);
$path = '.';
$version = null;
$branch = null;
$stability = 'stable';
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 === '--branch' && isset($argv[$i + 1])) $branch = $argv[$i + 1];
if ($arg === '--stability' && isset($argv[$i + 1])) $stability = $argv[$i + 1];
}
// Auto-detect branch from git or GitHub env
if ($branch === null) {
$branch = trim((string) @shell_exec('git rev-parse --abbrev-ref HEAD 2>/dev/null'));
if (empty($branch) || $branch === 'HEAD') {
$branch = getenv('GITHUB_REF_NAME') ?: 'main';
}
}
if ($version === null) {
fwrite(STDERR, "Usage: version_set_platform.php --path . --version 04.01.00 [--stability dev]\n");
exit(1);
}
// Append stability suffix for non-stable releases
$stabilitySuffixMap = [
'stable' => '',
'development' => '-dev',
'dev' => '-dev',
'alpha' => '-alpha',
'beta' => '-beta',
'rc' => '-rc',
'release-candidate' => '-rc',
];
$suffix = $stabilitySuffixMap[$stability] ?? '';
if ($suffix !== '' && !str_ends_with($version, $suffix)) {
$version .= $suffix;
echo "Version with stability suffix: {$version}\n";
}
$root = realpath($path) ?: $path;
// Detect platform
$platform = '';
$mokoStandards = "{$root}/.github/.mokostandards";
if (!file_exists($mokoStandards)) {
$mokoStandards = "{$root}/.mokostandards";
}
if (file_exists($mokoStandards)) {
$content = file_get_contents($mokoStandards);
if (preg_match('/^platform:\s*(.+)/m', $content, $m)) {
$platform = trim($m[1], " \t\n\r\"'");
}
}
$changed = 0;
// Dolibarr: $this->version + $this->url_last_version in mod*.class.php
if ($platform === 'crm-module') {
$pattern = "{$root}/src/core/modules/mod*.class.php";
foreach (glob($pattern) ?: [] as $file) {
$content = file_get_contents($file);
// Set $this->version
$updated = preg_replace(
'/(\$this->version\s*=\s*)[\'"][^\'"]*[\'"]/',
"\${1}'{$version}'",
$content
);
// Rewrite $this->url_last_version to point to current branch
if (preg_match('/\$this->url_last_version\s*=\s*[\'"]([^\'"]+)[\'"]/', $updated, $urlMatch)) {
$oldUrl = $urlMatch[1];
// Replace the branch segment: .../BRANCH/update.txt
$newUrl = preg_replace(
'#(raw\.githubusercontent\.com/[^/]+/[^/]+/)[^/]+(/update\.json)#',
"\${1}{$branch}\${2}",
$oldUrl
);
if ($newUrl !== $oldUrl) {
$updated = str_replace($oldUrl, $newUrl, $updated);
echo "Dolibarr: url_last_version → {$branch}/update.txt\n";
}
}
if ($updated !== $content) {
file_put_contents($file, $updated);
echo "Dolibarr: " . basename($file) . " → version={$version}, branch={$branch}\n";
$changed++;
}
}
}
// Joomla: <version> in XML manifests
if ($platform === 'waas-component') {
foreach (glob("{$root}/src/*.xml") ?: glob("{$root}/*.xml") ?: [] as $file) {
$content = file_get_contents($file);
if (!str_contains($content, '<extension')) continue;
$updated = preg_replace(
'|<version>[^<]*</version>|',
"<version>{$version}</version>",
$content
);
if ($updated !== $content) {
file_put_contents($file, $updated);
echo "Joomla: " . basename($file) . " → {$version}\n";
$changed++;
}
}
}
if ($changed === 0) {
if (empty($platform)) {
echo "No .mokostandards file — skipping platform version set\n";
} else {
echo "No platform-specific version files found for {$platform}\n";
}
}
exit(0);