auto_build_launcher/scripts/project_update.py

175 lines
6.6 KiB
Python
Raw Normal View History

2025-07-07 13:52:21 +00:00
import os.path
import re
from scripts.context import Context
2025-07-07 03:27:26 +00:00
from scripts.task import Task
2025-07-07 13:52:21 +00:00
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
2025-09-07 14:43:06 +00:00
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
2025-07-07 13:52:21 +00:00
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
2025-08-01 02:14:00 +00:00
GAME_ACTIVITY_PATH_2 = f"LauncherCode/src/com/launchercode/activity/GameActivity.kt".replace("/", os.sep)
2025-09-07 14:43:06 +00:00
STRING_PATH = f"launcher-game/res/values/strings.xml".replace("/", os.sep)
2025-07-07 03:27:26 +00:00
class ProjectUpdate(Task):
2025-07-07 13:52:21 +00:00
def __init__(self, context: Context):
super().__init__(context)
self.build_gradle_path = None
def update_package_name(self):
2025-10-28 02:04:16 +00:00
if not self.context.update_config:
app_logger().info("配置文件没有变动package不需要更新")
return
2025-07-07 13:52:21 +00:00
"""
更新包名
:return:
"""
2025-09-07 14:43:06 +00:00
build_gradle_path = os.path.join(self.context.temp_project_path, "ad.gradle")
2025-07-07 13:52:21 +00:00
text = open(build_gradle_path, "r", encoding="utf-8").read()
2025-09-07 14:43:06 +00:00
text = text.replace(self.context.original_package, self.context.package_name)
2025-07-07 13:52:21 +00:00
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()
2025-09-07 14:43:06 +00:00
text = text.replace(self.context.original_package, self.context.package_name)
2025-07-07 13:52:21 +00:00
open(temp_xml_path, "w", encoding="utf-8").write(text)
pass
pass
def update_gradle_config(self):
"""
更新gradle里面的版本号
:return:
"""
2025-09-07 14:43:06 +00:00
build_gradle_path = os.path.join(self.context.temp_project_path, "ad.gradle")
2025-07-07 13:52:21 +00:00
text = open(build_gradle_path, "r", encoding="UTF-8").read()
2025-09-07 14:43:06 +00:00
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)
2025-07-07 13:52:21 +00:00
open(build_gradle_path, "w", encoding="UTF-8").write(text)
pass
2025-07-08 05:51:30 +00:00
def update_string(self):
2025-10-28 02:04:16 +00:00
if not self.context.update_config:
app_logger().info("配置文件没有变动string不需要更新")
return
2025-07-08 05:51:30 +00:00
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()
2025-09-07 14:43:06 +00:00
text = text.replace("https://doanvanquy.com/privacy.html", privacy)
text = text.replace("https://doanvanquy.com/TermsOfUse.html",
2025-07-08 05:51:30 +00:00
privacy.replace("privacy.html", "TermsOfUse.html"))
open(os.path.join(self.context.temp_project_path, STRING_PATH), "w", encoding="utf-8").write(text)
2025-08-01 02:14:00 +00:00
pass
2025-07-07 03:27:26 +00:00
def execute(self):
2025-07-07 13:52:21 +00:00
self.build_gradle_path = os.path.join(self.context.temp_project_path, "build.gradle")
self.update_package_name()
self.update_gradle_config()
2025-07-08 05:51:30 +00:00
self.update_string()
2025-07-07 03:27:26 +00:00
pass