refactor: Enhance Unicode handling in build scripts and rename sync_version function
This commit is contained in:
parent
ff804790e6
commit
aad2e59c1c
2 changed files with 43 additions and 23 deletions
|
|
@ -20,21 +20,28 @@ Usage:
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
import subprocess
|
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
# Fix Unicode output on Windows BEFORE any other imports
|
||||||
|
if sys.platform == "win32":
|
||||||
|
os.environ["PYTHONIOENCODING"] = "utf-8"
|
||||||
|
import io
|
||||||
|
# Reconfigure stdout/stderr for UTF-8 output
|
||||||
|
sys.stdout = io.TextIOWrapper(
|
||||||
|
sys.stdout.buffer, encoding="utf-8", errors="replace"
|
||||||
|
)
|
||||||
|
sys.stderr = io.TextIOWrapper(
|
||||||
|
sys.stderr.buffer, encoding="utf-8", errors="replace"
|
||||||
|
)
|
||||||
|
|
||||||
|
import subprocess
|
||||||
import shutil
|
import shutil
|
||||||
import argparse
|
import argparse
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
# Import shared version utilities
|
# Import shared version utilities
|
||||||
from sync_version import get_current_version, sync_version
|
from sync_version import get_current_version, do_sync_version
|
||||||
|
|
||||||
# Fix Unicode output on Windows (removed TextIOWrapper due to subprocess conflicts)
|
|
||||||
# TextIOWrapper causes issues when subprocess tries to write to file descriptors
|
|
||||||
if sys.platform == "win32":
|
|
||||||
import os
|
|
||||||
os.environ["PYTHONIOENCODING"] = "utf-8"
|
|
||||||
|
|
||||||
|
|
||||||
class WindowsBuilder:
|
class WindowsBuilder:
|
||||||
|
|
@ -397,7 +404,7 @@ def main() -> int:
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
print("🔄 Syncing version...")
|
print("🔄 Syncing version...")
|
||||||
sync_version()
|
do_sync_version()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
builder = WindowsBuilder(env_file=args.env_file)
|
builder = WindowsBuilder(env_file=args.env_file)
|
||||||
|
|
|
||||||
|
|
@ -130,34 +130,26 @@ def update_changelog(version: str) -> None:
|
||||||
print(f"✓ Added version header to CHANGELOG.md for {version}")
|
print(f"✓ Added version header to CHANGELOG.md for {version}")
|
||||||
|
|
||||||
|
|
||||||
def sync_version() -> int:
|
def do_sync_version(version: str | None = None) -> int:
|
||||||
"""Sync version across project.
|
"""Sync version across project.
|
||||||
|
|
||||||
Updates __init__.py (source of truth) and changelog.
|
Updates __init__.py (source of truth) and changelog.
|
||||||
Config and pyproject.toml automatically read from __init__.py.
|
Config and pyproject.toml automatically read from __init__.py.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
version: Version to set (if None, reads from __init__.py)
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
0 on success, 1 on error
|
0 on success, 1 on error
|
||||||
"""
|
"""
|
||||||
parser = argparse.ArgumentParser(
|
|
||||||
description="Sync version from __init__.py to dependent files"
|
|
||||||
)
|
|
||||||
parser.add_argument(
|
|
||||||
"--version",
|
|
||||||
type=str,
|
|
||||||
help="Version to set (if not provided, reads from __init__.py)",
|
|
||||||
)
|
|
||||||
args = parser.parse_args()
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if args.version:
|
if version:
|
||||||
if not re.match(r"^\d+\.\d+\.\d+", args.version):
|
if not re.match(r"^\d+\.\d+\.\d+", version):
|
||||||
print(
|
print(
|
||||||
"❌ Invalid version format. Use semantic versioning"
|
"❌ Invalid version format. Use semantic versioning"
|
||||||
" (e.g., 1.2.3)"
|
" (e.g., 1.2.3)"
|
||||||
)
|
)
|
||||||
return 1
|
return 1
|
||||||
version = args.version
|
|
||||||
update_init_version(version)
|
update_init_version(version)
|
||||||
else:
|
else:
|
||||||
version = get_current_version_from_init()
|
version = get_current_version_from_init()
|
||||||
|
|
@ -174,5 +166,26 @@ def sync_version() -> int:
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
|
|
||||||
|
def sync_version() -> int:
|
||||||
|
"""Sync version across project (command-line interface).
|
||||||
|
|
||||||
|
Parses command-line arguments and calls do_sync_version().
|
||||||
|
This function is only called when the script is run directly.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
0 on success, 1 on error
|
||||||
|
"""
|
||||||
|
parser = argparse.ArgumentParser(
|
||||||
|
description="Sync version from __init__.py to dependent files"
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--version",
|
||||||
|
type=str,
|
||||||
|
help="Version to set (if not provided, reads from __init__.py)",
|
||||||
|
)
|
||||||
|
args = parser.parse_args()
|
||||||
|
return do_sync_version(args.version)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
sys.exit(sync_version())
|
sys.exit(sync_version())
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue