import re import subprocess import sys from datetime import datetime from typing import Tuple VERSION_NAME_REGEX = r'versionName: "(.+?)"' VERSION_CODE_REGEX = r"versionCode: ([0-9]+)" VERSION_NAME_FORMAT = 'versionName: "{}"' VERSION_CODE_FORMAT = "versionCode: {}" def get_project_dir() -> str: project_dir = sys.argv[1] if project_dir[-1:] == "/" or project_dir[-1:] == "\\": project_dir = project_dir[:-1] return project_dir def read_gradle_version(project_dir: str) -> Tuple[int, str]: GRADLE_PATH = f"{project_dir}/build.gradle" with open(GRADLE_PATH, "r") as f: gradle = f.read() version_name = re.search(VERSION_NAME_REGEX, gradle).group(1) version_code = int(re.search(VERSION_CODE_REGEX, gradle).group(1)) return (version_code, version_name) def write_gradle_version(project_dir: str, version_code: int, version_name: str): GRADLE_PATH = f"{project_dir}/build.gradle" with open(GRADLE_PATH, "r") as f: gradle = f.read() gradle = re.sub( VERSION_NAME_REGEX, VERSION_NAME_FORMAT.format(version_name), gradle ) gradle = re.sub( VERSION_CODE_REGEX, VERSION_CODE_FORMAT.format(version_code), gradle ) with open(GRADLE_PATH, "w") as f: f.write(gradle) def build_version_code(version_name: str) -> int: version = version_name.split("+")[0].split("-") version_base = version[0] version_suffix = version[1] if len(version) == 2 else "" base_parts = version_base.split(".") major = int(base_parts[0]) or 0 minor = int(base_parts[1]) if len(base_parts) > 1 else 0 patch = int(base_parts[2]) if len(base_parts) > 2 else 0 beta = 9 rc = 9 if "dev" in version_suffix: beta = 0 rc = 0 elif "beta." in version_suffix: beta = int(version_suffix.split(".")[1]) rc = 0 elif "rc." in version_suffix: beta = 0 rc = int(version_suffix.split(".")[1]) version_code = beta + rc * 10 + patch * 100 + minor * 10000 + major * 1000000 return version_code def get_changelog(project_dir: str, format: str) -> Tuple[str, str]: with open( f"{project_dir}/app/src/main/assets/pl-changelog.html", "r", encoding="utf-8" ) as f: changelog = f.read() title = re.search(r"