auto_build_launcher/scripts/project_update.py

175 lines
6.6 KiB
Python
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.

import os.path
import re
from scripts.context import Context
from scripts.task import Task
from utils.logger_utils import app_logger
def update_gradle_variable(content, variable_name, new_value):
"""
更新 Gradle 文件中的 final def 变量值
:param content: Gradle 文件内容
:param variable_name: 变量名 (如 "releaseName")
:param new_value: 新值 (字符串或数字)
:return: 更新后的内容
"""
# 处理字符串值(带引号)
if isinstance(new_value, str):
# 匹配带引号的字符串赋值
pattern = rf'(final\s+def\s+{re.escape(variable_name)}\s*=\s*["\'])(.*?)(["\'])'
replacement = rf'\g<1>{new_value}\g<3>'
else:
# 匹配数字值(不带引号)
pattern = rf'(final\s+def\s+{re.escape(variable_name)}\s*=\s*)(\d+)'
replacement = rf'\g<1>{new_value}'
updated_content = re.sub(pattern, replacement, content)
return updated_content
def modify_text_with_regex(original_text, key, new_value):
"""
使用正则表达式修改文本中指定key的值支持带引号和数字类型
参数:
original_text -- 原始文本内容
key -- 要修改的键名
new_value -- 新的值
返回:
修改后的文本内容
"""
# 改进的正则模式:更精确地匹配带引号的值和数字值
# 匹配带引号的值(单引号或双引号)或数字值
pattern = re.compile(
r'(final def ' + re.escape(key) + r' = )' # 前缀部分
r'(?:' # 非捕获组,用于分组不同情况
r'([\'"])(.*?)\2' # 带引号的值(单引号或双引号)
r'|' # 或者
r'(\d+)' # 数字值(整数)
r')'
)
# 查找匹配
match = pattern.search(original_text)
if not match:
print(f"未找到键: {key}")
return original_text
# 检查是带引号的情况还是数字情况
quote_type = match.group(2) # 引号类型(单引号或双引号)
is_number = match.group(4) is not None # 是否是数字类型
# 构造替换字符串
if quote_type:
# 有引号的情况,保持原有引号类型
replacement = f'\g<1>{quote_type}{new_value}{quote_type}'
elif is_number:
# 数字类型,直接替换数字
replacement = f'\g<1>{new_value}'
else:
# 其他情况,保持原样替换
replacement = f'\g<1>{new_value}'
# 执行替换,只替换第一个匹配项
modified_text = pattern.sub(replacement, original_text, count=1)
return modified_text
def update_gradle_property(content, key, new_value):
# 匹配两种格式:
# 1. resValue "string", "key", "value"
# 2. resValue("string", "key", "value")
pattern = rf'(resValue\s*\(?\s*["\']string["\']\s*,\s*["\']{re.escape(key)}["\']\s*,\s*["\'])(.*?)(["\']\s*\)?)'
# 替换为新值
updated_content = re.sub(pattern, rf'\g<1>{new_value}\g<3>', content)
return updated_content
GAME_ACTIVITY_PATH_2 = f"LauncherCode/src/com/launchercode/activity/GameActivity.kt".replace("/", os.sep)
STRING_PATH = f"launcher-game/res/values/strings.xml".replace("/", os.sep)
class ProjectUpdate(Task):
def __init__(self, context: Context):
super().__init__(context)
self.build_gradle_path = None
def update_package_name(self):
if not self.context.update_config:
app_logger().info("配置文件没有变动package不需要更新")
return
"""
更新包名
:return:
"""
build_gradle_path = os.path.join(self.context.temp_project_path, "ad.gradle")
text = open(build_gradle_path, "r", encoding="utf-8").read()
text = text.replace(self.context.original_package, self.context.package_name)
open(build_gradle_path, "w", encoding="utf-8").write(text)
xml_path = os.path.join(self.context.temp_project_path, "lawnchair/res/xml")
for root, dirs, files in os.walk(xml_path):
for file in files:
temp_xml_path = os.path.join(root, file)
text = open(temp_xml_path, "r", encoding="utf-8").read()
text = text.replace(self.context.original_package, self.context.package_name)
open(temp_xml_path, "w", encoding="utf-8").write(text)
pass
pass
def update_gradle_config(self):
"""
更新gradle里面的版本号
:return:
"""
build_gradle_path = os.path.join(self.context.temp_project_path, "ad.gradle")
text = open(build_gradle_path, "r", encoding="UTF-8").read()
text = modify_text_with_regex(text, "admob_app_id", self.context.admob_app_id)
text = modify_text_with_regex(text, "game_services_project_id", self.context.game_services_project_id)
text = modify_text_with_regex(text, "facebook_app_id", self.context.facebook_app_id)
text = modify_text_with_regex(text, "facebook_client_token", self.context.facebook_client_token)
text = modify_text_with_regex(text, "appName", self.context.get_app_name())
text = modify_text_with_regex(text, "appVersionName", self.context.version_display_name)
text = modify_text_with_regex(text, "appVersionCode", self.context.version_code)
open(build_gradle_path, "w", encoding="UTF-8").write(text)
pass
def update_string(self):
if not self.context.update_config:
app_logger().info("配置文件没有变动string不需要更新")
return
privacy = self.context.get_config("TkA_Url_Privacy")
if not privacy or privacy == "":
raise Exception("配置文件中没有配置 TkA_Url_Privacy")
tkg_custom = self.context.get_config("tkg_custom")
if not tkg_custom or tkg_custom == "":
raise Exception("配置文件中没有配置 tkg_custom")
text = open(os.path.join(self.context.temp_project_path, STRING_PATH), "r", encoding="utf-8").read()
text = text.replace("https://doanvanquy.com/privacy.html", privacy)
text = text.replace("https://doanvanquy.com/TermsOfUse.html",
privacy.replace("privacy.html", "TermsOfUse.html"))
open(os.path.join(self.context.temp_project_path, STRING_PATH), "w", encoding="utf-8").write(text)
pass
def execute(self):
self.build_gradle_path = os.path.join(self.context.temp_project_path, "build.gradle")
self.update_package_name()
self.update_gradle_config()
self.update_string()
pass