Files
openclaw/scripts/lib/ios-version.ts
2026-07-01 20:24:38 -05:00

223 lines
6.9 KiB
TypeScript

// Ios Version script supports OpenClaw repository automation.
import { readFileSync, writeFileSync } from "node:fs";
import path from "node:path";
import { parseReleaseVersion } from "./npm-publish-plan.mjs";
const IOS_CHANGELOG_FILE = "apps/ios/CHANGELOG.md";
const IOS_VERSION_XCCONFIG_FILE = "apps/ios/Config/Version.xcconfig";
const IOS_RELEASE_NOTES_FILE = "apps/ios/fastlane/metadata/en-US/release_notes.txt";
type ResolvedIosVersion = {
canonicalVersion: string;
marketingVersion: string;
buildVersion: string;
changelogPath: string;
versionXcconfigPath: string;
releaseNotesPath: string;
versionSource: "explicit" | "package";
versionSourcePath: string | null;
};
type SyncIosVersioningMode = "check" | "write";
function normalizeTrailingNewline(value: string): string {
return value.endsWith("\n") ? value : `${value}\n`;
}
function parsePinnedReleaseVersion(rawVersion: string): string | null {
const parsed = parseReleaseVersion(rawVersion.trim());
if (!parsed || parsed.version !== parsed.baseVersion) {
return null;
}
return parsed.baseVersion;
}
export function normalizePinnedIosVersion(rawVersion: string): string {
const trimmed = rawVersion.trim();
if (!trimmed) {
throw new Error("Missing iOS release version.");
}
const pinnedVersion = parsePinnedReleaseVersion(trimmed);
if (!pinnedVersion) {
throw new Error(`Invalid iOS version '${rawVersion}'. Expected release version like 2026.6.5.`);
}
return pinnedVersion;
}
export function normalizeGatewayVersionToPinnedIosVersion(rawVersion: string): string {
const trimmed = rawVersion.trim().replace(/^v/u, "");
if (!trimmed) {
throw new Error("Missing root package.json version.");
}
const parsed = parseReleaseVersion(trimmed);
if (!parsed) {
throw new Error(
`Invalid gateway version '${rawVersion}'. Expected YYYY.M.PATCH, YYYY.M.PATCH-alpha.N, YYYY.M.PATCH-beta.N, or YYYY.M.PATCH-N.`,
);
}
return parsed.baseVersion;
}
function rootPackageJsonPath(rootDir = path.resolve(".")): string {
return path.join(rootDir, "package.json");
}
function readRootPackageVersion(rootDir = path.resolve(".")): string {
const packageJsonPath = rootPackageJsonPath(rootDir);
const parsed = JSON.parse(readFileSync(packageJsonPath, "utf8")) as { version?: unknown };
const version = typeof parsed.version === "string" ? parsed.version.trim() : "";
if (!version) {
throw new Error(`Missing package.json version in ${packageJsonPath}.`);
}
return version;
}
export function resolveGatewayVersionForIosRelease(rootDir = path.resolve(".")): {
packageVersion: string;
pinnedIosVersion: string;
} {
const packageVersion = readRootPackageVersion(rootDir);
return {
packageVersion,
pinnedIosVersion: normalizeGatewayVersionToPinnedIosVersion(packageVersion),
};
}
export function resolveIosVersion(
rootDir = path.resolve("."),
options?: { releaseVersion?: string | null },
): ResolvedIosVersion {
const changelogPath = path.join(rootDir, IOS_CHANGELOG_FILE);
const versionXcconfigPath = path.join(rootDir, IOS_VERSION_XCCONFIG_FILE);
const releaseNotesPath = path.join(rootDir, IOS_RELEASE_NOTES_FILE);
const explicitReleaseVersion = options?.releaseVersion?.trim() ?? "";
const canonicalVersion = explicitReleaseVersion
? normalizePinnedIosVersion(explicitReleaseVersion)
: resolveGatewayVersionForIosRelease(rootDir).pinnedIosVersion;
return {
canonicalVersion,
marketingVersion: canonicalVersion,
buildVersion: "1",
changelogPath,
versionXcconfigPath,
releaseNotesPath,
versionSource: explicitReleaseVersion ? "explicit" : "package",
versionSourcePath: explicitReleaseVersion ? null : rootPackageJsonPath(rootDir),
};
}
export function renderIosVersionXcconfig(version: ResolvedIosVersion): string {
return `// Shared iOS version defaults.\n// Source of truth: package.json or explicit release --version.\n// Generated by scripts/ios-sync-versioning.ts.\n\nOPENCLAW_IOS_VERSION = ${version.canonicalVersion}\nOPENCLAW_MARKETING_VERSION = ${version.marketingVersion}\nOPENCLAW_BUILD_VERSION = ${version.buildVersion}\n\n#include? "../build/Version.xcconfig"\n`;
}
function matchChangelogHeading(line: string, heading: string): boolean {
const normalized = line.trim();
return normalized === `## ${heading}` || normalized.startsWith(`## ${heading} - `);
}
export function extractChangelogSection(content: string, heading: string): string | null {
const lines = content.split(/\r?\n/);
const startIndex = lines.findIndex((line) => matchChangelogHeading(line, heading));
if (startIndex === -1) {
return null;
}
let endIndex = lines.length;
for (let index = startIndex + 1; index < lines.length; index += 1) {
if (lines[index]?.startsWith("## ")) {
endIndex = index;
break;
}
}
const body = lines
.slice(startIndex + 1, endIndex)
.join("\n")
.trim();
return body || null;
}
export function renderIosReleaseNotes(
version: ResolvedIosVersion,
changelogContent: string,
): string {
const candidateHeadings = [version.canonicalVersion, "Unreleased"];
for (const heading of candidateHeadings) {
const body = extractChangelogSection(changelogContent, heading);
if (body) {
return `${body}\n`;
}
}
throw new Error(
`Unable to find iOS changelog notes for ${version.canonicalVersion}. Add a matching section to ${IOS_CHANGELOG_FILE}.`,
);
}
function syncFile(params: {
mode: SyncIosVersioningMode;
path: string;
nextContent: string;
label: string;
}): boolean {
const nextContent = normalizeTrailingNewline(params.nextContent);
const currentContent = readFileSync(params.path, "utf8");
if (currentContent === nextContent) {
return false;
}
if (params.mode === "check") {
throw new Error(`${params.label} is stale: ${path.relative(process.cwd(), params.path)}`);
}
writeFileSync(params.path, nextContent, "utf8");
return true;
}
export function syncIosVersioning(params?: {
mode?: SyncIosVersioningMode;
releaseVersion?: string | null;
rootDir?: string;
}): {
updatedPaths: string[];
} {
const mode = params?.mode ?? "write";
const rootDir = path.resolve(params?.rootDir ?? ".");
const releaseVersion = params?.releaseVersion;
const version = resolveIosVersion(rootDir, { releaseVersion });
const changelogContent = readFileSync(version.changelogPath, "utf8");
const nextVersionXcconfig = renderIosVersionXcconfig(version);
const nextReleaseNotes = renderIosReleaseNotes(version, changelogContent);
const updatedPaths: string[] = [];
if (
syncFile({
mode,
path: version.versionXcconfigPath,
nextContent: nextVersionXcconfig,
label: "iOS version xcconfig",
})
) {
updatedPaths.push(version.versionXcconfigPath);
}
if (
syncFile({
mode,
path: version.releaseNotesPath,
nextContent: nextReleaseNotes,
label: "iOS release notes",
})
) {
updatedPaths.push(version.releaseNotesPath);
}
return { updatedPaths };
}