Files
MapLibreParticleLab/tools/particle_release_pipeline/prepare_release.py

100 lines
3.1 KiB
Python
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
from __future__ import annotations
import json
import re
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parents[2]
ANDROID_VERSION_FILE = ROOT / "platform/android/VERSION"
IOS_VERSION_FILE = ROOT / "platform/ios/VERSION"
RN_ROOT = ROOT / "packages/maplibre-particle-rn"
def die(message: str) -> None:
raise SystemExit(f"PREPARE_RELEASE_ERROR: {message}")
def base_version(value: str) -> str:
return re.sub(r"-particle\.\d+$", "", value.strip())
def replace_current_particle(path: Path, base: str, target: str) -> None:
text = path.read_text()
pattern = rf"{re.escape(base)}-particle\.\d+"
updated, count = re.subn(pattern, target, text)
if count == 0:
die(f"no particle version reference found in {path}")
path.write_text(updated)
def update_changelog(target: str, revision: int) -> None:
path = RN_ROOT / "CHANGELOG.md"
text = path.read_text()
heading = f"## {target}(Android / iOS 统一发行)"
if heading in text:
return
marker = "## Unreleased\n\n"
if marker not in text:
die("CHANGELOG missing Unreleased heading")
entry = (
marker
+ heading
+ "\n\n"
+ "- Mac unified release pipeline 统一生成 Android Native、RN package 与 iOS SwiftPM/XCFramework;"
+ f"共享 Particle revision {revision}。\n"
)
path.write_text(text.replace(marker, entry, 1))
def main() -> None:
if len(sys.argv) != 2 or not sys.argv[1].isdigit():
die("usage: prepare_release.py <particle-revision>")
revision = int(sys.argv[1])
if revision < 1:
die("revision must be positive")
android_base = base_version(ANDROID_VERSION_FILE.read_text())
ios_base = base_version(IOS_VERSION_FILE.read_text())
android_version = f"{android_base}-particle.{revision}"
ios_version = f"{ios_base}-particle.{revision}"
ANDROID_VERSION_FILE.write_text(android_version + "\n")
package_path = RN_ROOT / "package.json"
package = json.loads(package_path.read_text())
package["version"] = android_version
package_path.write_text(json.dumps(package, ensure_ascii=False, indent=2) + "\n")
for relative in (
"packages/maplibre-particle-rn/android/build.gradle",
"packages/maplibre-particle-rn/app.plugin.js",
"integration/react-native/MapLibreParticleRNTestHost/plugins/withParticleNativeAndroid.js",
):
replace_current_particle(ROOT / relative, android_base, android_version)
readme = RN_ROOT / "README.md"
readme_text = readme.read_text()
readme_text = re.sub(
rf"{re.escape(android_base)}-particle\.\d+",
android_version,
readme_text,
)
readme_text = re.sub(
r"必须指向同一 `\.\d+`",
f"必须指向同一 `.{revision}`",
readme_text,
)
readme.write_text(readme_text)
update_changelog(android_version, revision)
print(f"PARTICLE_REVISION={revision}")
print(f"ANDROID_RN_VERSION={android_version}")
print(f"IOS_DISTRIBUTION_VERSION={ios_version}")
if __name__ == "__main__":
main()