#!/usr/bin/python # -*- coding: UTF-8 -*- import os from utils.include_utils import auto_parse_include from utils.other_utils import get_file_suffix, extract_codes_colors, replace_old_color, update_themes, \ replace_res_utils, bg_white_radius10, bg_btn_ffffff_10dp, add_xml_res_auto, \ add_import_R, bg_homeindex_item_10, unify_path_separator, \ get_included_suffix, get_excluded_files, update_deprecated, bg_btn_white_topr10, \ bg_btn_white_topr12, bg_btn_white_topr16, replace_xml_imageview, replace_xml_textview, \ replace_system_bar, delete_xml_remark, replace_background, hykb_dimens, replace_back_icon, \ replace_arrow_icon # 替换java/kotlin/xml文件中的颜色硬编码 def replace_file_by_path(file_path: str, auto_include_callback=None): # 整理文件路径 file_path = unify_path_separator(file_path) if not os.path.isfile(file_path): print(f"{file_path} 不是一个文件") return # 获取文件后缀 suffix = get_file_suffix(file_path) if suffix not in get_included_suffix(): print(f"忽略{suffix}格式文件") return # 获取文件名 file_name = os.path.basename(file_path) if file_name in get_excluded_files(): print(f"{file_name} 是忽略文件") return print(f"处理文件:{file_path}") with open(file_path, 'r', encoding='utf-8') as f: text = f.read() # 自动解析include auto_parse_include(text, suffix, auto_include_callback) # fix过时方法 text = update_deprecated(text) # 提取文件中的颜色硬编码 text = extract_codes_colors(text, suffix) # 替换旧的颜色 text = replace_old_color(text) # 更新主题 text = update_themes(text) # 删除xml注释 # text = delete_xml_remark(text) # 替换旧的dimen # text = hykb_dimens(text) text = bg_white_radius10(text) text = bg_homeindex_item_10(text) text = bg_btn_ffffff_10dp(text) # text = MoreButtonStyle(text) # text = BaseTextOneLineStyle(text) text = bg_btn_white_topr10(text) text = bg_btn_white_topr12(text) text = bg_btn_white_topr16(text) text = replace_xml_imageview(text) text = replace_xml_textview(text) text = replace_arrow_icon(text) text = replace_back_icon(text) text = replace_background(text) # 添加xml头信息 text = add_xml_res_auto(text) # 替换旧方法 text = replace_res_utils(text, suffix) # 添加R文件引入 text = add_import_R(text, suffix) # 删除Java多余空白行 # text = delete_java_blank_line(text) # 删除xml多余空白行 # text = delete_xml_blank_line(text, suffix) # 处理状态栏 text = replace_system_bar(text) # 替换旧文件中的内容 with open(file_path, 'w', encoding='utf-8') as f: f.write(text)