#!/usr/bin/python # -*- coding: UTF-8 -*- import os import re from pathlib import Path from colors.utils.color_set import get_new_colors, get_old_colors, get_old_dimens # 需要忽略的文件 def get_excluded_files(): return {'colors.xml', 'colors_alpha.xml', 'colors_hyif.xml', 'colors_wsy.xml', 'strings.xml', 'TimeSpendDetailFragment.kt', 'TimeSpendDetailDialog.kt', 'PostTypeHelper.java', 'ProduceDataRecycleViewScrollBar.java', 'DefaultTitleDialog.java', 'GlideUtils.java'} # 需要忽略的文件夹 def get_excluded_dirs(): return ['.git', '.svn', 'build', '.gradle', '.idea', 'test', 'androidTest'] # 需要处理的文件后缀 def get_included_suffix(): return ['.xml', '.java', '.kt'] # 统一路径分隔符 def unify_path_separator(path: str): path = path.replace("\\", os.path.sep) path = path.replace("/", os.path.sep) return path def update_colors_resource_file(colors_xml_path: str, color_codes): with open(colors_xml_path, 'r', encoding='utf-8') as f: text = f.read() new_colors = set() for color_code in color_codes: color_name = get_color_name(color_code) if color_name.lower() not in text and color_name.upper() not in text: new_colors.add(f' {color_code.lower()}') if new_colors: new_contents = text.replace('', '\n'.join(new_colors) + '\n') with open(colors_xml_path, 'w', encoding='utf-8') as f: f.write(new_contents) # 提取指定文件中的颜色值 def extract_color_codes_from_file(file_path: str): with open(file_path, 'r', encoding='utf-8') as f: text = f.read() color_codes = set(re.findall(r'#(?:[0-9a-fA-F]{8}|[0-9a-fA-F]{6})', text)) return color_codes # 根据颜色值获取颜色名称 def get_color_name(color_code): name = f'color_{color_code.lower()}' name = name.replace("#", "") return name.lower() # 根据颜色值获取颜色名称 def get_file_suffix(file_path: str): if not os.path.isfile(file_path): print(f"{file_path} 不是一个文件") return "" return Path(file_path).suffix # 提取指定文件中的颜色值 def extract_codes_colors(text, suffix): # 提取所有颜色 color_codes = set(re.findall(r'#(?:[0-9a-fA-F]{8}|[0-9a-fA-F]{6})', text)) if color_codes: # print(f"提取的颜色:{color_codes}") if suffix == ".java" or suffix == ".kt": # pattern = r'Color.parseColor(#(?:[0-9a-fA-F]{8}|[0-9a-fA-F]{6})' # 匹配模式 pattern = r"(Color\.parseColor\s*\(\s*\"#[0-9A-Fa-f]+\s*\"\s*\))" # 匹配模式 matches = re.findall(pattern, text) if matches: for match in matches: color_code = re.search(r'"#([0-9A-Fa-f]+)"', match).group(1) color_name = get_color_name(color_code) replacement = "ResUtils.getColor(R.color.{})".format(color_name) # 替换的文本 text = text.replace(match, replacement) # 替换 if suffix == ".java": if "import com.xmcy.hykb.utils.ResUtils;" not in text: text = text.replace("import android.graphics.Color;", "import android.graphics.Color;\nimport com.xmcy.hykb.utils.ResUtils;") if suffix == ".kt": if "import com.xmcy.hykb.utils.ResUtils" not in text: text = text.replace("import android.graphics.Color", "import android.graphics.Color\nimport com.xmcy.hykb.utils.ResUtils") elif suffix == ".xml": pattern = r'#(?:[0-9a-fA-F]{8}|[0-9a-fA-F]{6})' # 匹配模式 result = re.findall(pattern, text) if result: for color_code in result: color_name = get_color_name(color_code) replacement = r'@color/{}'.format(color_name) # 替换的文本 text = text.replace(color_code, replacement) # 替换 return text # 修改主题 def update_base_class(text): pattern = r'#(?:[0-9a-fA-F]{8}|[0-9a-fA-F]{6})' # 匹配模式 result = re.findall(pattern, text) if result: for color_code in result: color_name = get_color_name(color_code) replacement = r'@color/{}'.format(color_name) # 替换的文本 text = text.replace(color_code, replacement) # 替换 return text # 修改过时方法 def update_deprecated(text): text = text.replace("DensityUtils.dp2px(mContext,", "DensityUtils.dp2px(") text = text.replace("DensityUtils.dp2px(mActivity,", "DensityUtils.dp2px(") text = text.replace("DensityUtils.dp2px(context,", "DensityUtils.dp2px(") text = text.replace("DensityUtils.dp2px(activity,", "DensityUtils.dp2px(") text = text.replace("DensityUtils.dp2px(getActivity(),", "DensityUtils.dp2px(") text = text.replace("DensityUtils.dp2px(HYKBApplication.getContext(), ", "DensityUtils.dp2px(") text = text.replace("DensityUtils.dp2px(getContext(),", "DensityUtils.dp2px(") # text = text.replace("setBackgroundDrawable", "setBackground") text = text.replace("setBackground(context.getResources().getDrawable(R.", "setBackgroundResource((R.") text = text.replace(r'\(\(R\.drawable\.[^\)]+\)\)', r'\(R\.drawable\.[^\)]+\)') return text # 修改主题 def update_themes(text): text = text.replace("\"android:style/Theme.Dialog\"", "\"@style/Theme.Material3.DayNight.Dialog\"") text = text.replace("\"@android:style/Theme.Dialog\"", "\"@style/Theme.Material3.DayNight.Dialog\"") text = text.replace("\"@android:style/Theme.Holo.Dialog\"", "\"@style/Theme.Material3.DayNight.Dialog\"") text = text.replace("ThemeOverlay.AppCompat.Light", "Theme.Material3.DayNight") text = text.replace("Theme.AppCompat.NoActionBar", "Theme.Material3.DayNight.NoActionBar") text = text.replace("Theme.AppCompat.Light", "Theme.Material3.DayNight") text = text.replace("Theme.AppCompat", "Theme.Material3.DayNight") text = text.replace("\"Theme.Material3.DayNight", "\"@style/Theme.Material3.DayNight") text = text.replace("@color/white", "@color/bg_white") return text # 匹配两行或更多连续的空白行,并将它们替换为一行空白行 def delete_java_blank_line(text): text = re.sub("\n\s+\n+", "\n\n", text) return text # 删除xml多余空白行 def delete_xml_blank_line(text, suffix): if suffix == ".xml": text = re.sub("\n\s+\n+", "\n\n", text) return text # 添加R文件引入 def add_import_R(text, suffix): if suffix == ".java": if "import android.graphics.Color;" in text: if "package com.xmcy.hykb.app" in text and "import com.xmcy.hykb.R;" not in text: text = text.replace("import android.graphics.Color;", "import android.graphics.Color;\nimport com.xmcy.hykb.R;") if "import androidx.core.content.ContextCompat;" not in text: text = text.replace("import android.graphics.Color;", "import android.graphics.Color;\nimport androidx.core.content.ContextCompat;") elif suffix == ".kt": if "import android.graphics.Color" in text: if "package com.xmcy.hykb.app" in text and "import com.xmcy.hykb.R" not in text: text = text.replace("import android.graphics.Color", "import android.graphics.Color\nimport com.xmcy.hykb.R") if "import androidx.core.content.ContextCompat" not in text: text = text.replace("import android.graphics.Color", "import android.graphics.Color\nimport androidx.core.content.ContextCompat") return text # 添加头信息 def add_xml_res_auto(text): if "xmlns:app=\"http://schemas.android.com/apk/res-auto\"" not in text and "app:" in text: text = text.replace("xmlns:android=\"http://schemas.android.com/apk/res/android\"", "xmlns:android=\"http://schemas.android.com/apk/res/android\"\n xmlns:app=\"http://schemas.android.com/apk/res-auto\"") return text # 删除xml注释 def delete_xml_remark(text): matches = re.findall(r"", text) if matches: for match in matches: text = text.replace(match + "\n", "") text = text.replace(match, "") return text # 替换旧的dimen def hykb_dimens(text): dimens = get_old_dimens() text = text.replace("@dimen/post_detail_item_bottom_12dp", "12dp") for dimen in dimens: key = dimen[0] value = dimen[1] text = text.replace(f"@dimen/{key}", f"{value}") text = text.replace(f"@dimen/{key}", f"{value}") return text # 替换旧的颜色 def replace_old_color(text): new_colors = get_new_colors() old_colors = get_old_colors() text = text.replace("\"#ffffffff\"", "\"#ffffff\"") text = text.replace("\"#FFFFFFFF\"", "\"#ffffff\"") for new_color in new_colors: new_color_name = new_color[0] new_color_code = new_color[1] for old_color in old_colors: old_color_name = old_color[0] old_color_code = old_color[1] if len(old_color_code) == 8 and old_color_code.startswith("00"): print(f"发现透明颜色:{old_color_name}->{old_color_code}") text = text.replace(f"@color/{old_color_name}", f"@color/transparent") text = text.replace(f"R.color.{old_color_name}", f"R.color.transparent") elif old_color_code.lower() == new_color_code.lower(): # print(f"新旧颜色装换{old_color_name}->{new_color_name}->{new_color_code}") text = text.replace(f"@color/{old_color_name}", f"@color/{new_color_name}") text = text.replace(f"R.color.{old_color_name}", f"R.color.{new_color_name}") text = text.replace("@android:color/white", "@color/white") text = text.replace("@android:color/black", "@color/black") text = text.replace("android.R.color.white", "R.color.white") text = text.replace("android.R.color.black", "R.color.black") text = text.replace("bg_pressed", "green_brand_8") text = text.replace("menu_item_bg_pressed_eb23c268", "green_brand_8") text = text.replace("menu_item_bg_pressed", "green_brand_8") # 错误校正,非必须 text = text.replace("green_brandDark", "green_brand") text = text.replace("green_brand_brand", "green_brand") text = text.replace("green_brand_brandDark", "green_brand") text = text.replace("green_brandDark", "green_brand") text = text.replace("green_brand_08", "green_brand_7") text = text.replace("green_brand_brand_word", "green_word") text = text.replace("green_brand_word", "green_word") text = text.replace("green_brand_hover", "green_hover") text = text.replace("green_brand_line", "green_line") text = text.replace("green_brand_e0fce7", "green_e0fce7") text = text.replace("bg_light_00", "transparent") text = text.replace("font_color_ff131715", "black_h1") text = text.replace("green_brand_bg", "green_bg") text = text.replace("color_ff131715", "black_h1") text = text.replace("color_green_", "green_brand_") text = text.replace("green_brand_8_e623c268", "green_brand_8") # text = text.replace("Color.parseColor(\"#ffffff\")", "ResUtils.getColor(R.color.white)") # text = text.replace("Color.parseColor(\"#ffffffff\")", "ResUtils.getColor(R.color.white)") # text = text.replace("android:textColor=\"#ffffffff\"", "android:textColor=\"@color/white\"") # text = text.replace("android:background=\"#ffffffff\"", "android:background=\"@color/bg_white\"") # text = text.replace("setImageDrawable(ContextCompat.getDrawable(mActivity, ", "setImageResource((") text = text.replace("color_333333", "black_h2") text = text.replace("color_999999", "black_h4") return text # 替换旧方法 def replace_res_utils(text, suffix): if "import androidx.core.content.ContextCompat\"" not in text and ( "ResUtils.getColor(" in text or "ResUtils.getDrawable(" in text): if suffix == ".java": text = text.replace("import com.xmcy.hykb.utils.ResUtils;", "import com.xmcy.hykb.utils.ResUtils;\nimport androidx.core.content.ContextCompat;") elif suffix == ".kt": text = text.replace("import com.xmcy.hykb.utils.ResUtils", "import com.xmcy.hykb.utils.ResUtils\nimport androidx.core.content.ContextCompat") matches = re.findall(r'extends\s+\w+Activity', text) if matches: text = text.replace("ResUtils.getColor(", "getColorResId(") text = text.replace("ResUtils.getDrawable(", "ContextCompat.getDrawable(this, ") matches = re.findall(r'extends\s+\w+Fragment', text) if matches: text = text.replace("ResUtils.getColor(", "getColor(") text = text.replace("ResUtils.getDrawable(", "ContextCompat.getDrawable(getContext(), ") matches = re.findall(r'extends\s+\w+PopupWindow', text) if matches: text = text.replace("ResUtils.getColor(", "ContextCompat.getColor(context, ") text = text.replace("ResUtils.getDrawable(", "ContextCompat.getDrawable(context, ") matches = re.findall(r'extends\s+\w+RecyclerView.Adapter', text) if matches: text = text.replace("ResUtils.getColor(", "ContextCompat.getColor(viewHolder.itemView.getContext(), ") text = text.replace("ResUtils.getDrawable(", "ContextCompat.getDrawable(viewHolder.itemView.getContext(), ") matches = re.findall(r'extends\s+\w+BaseRecyclerViewBindAdapter', text) if matches: text = text.replace("ResUtils.getColor(", "ContextCompat.getColor(context, ") text = text.replace("ResUtils.getDrawable(", "ContextCompat.getDrawable(context, ") matches = re.findall(r'extends\s+\w+BaseMultipleAdapter', text) if matches: text = text.replace("ResUtils.getColor(", "ContextCompat.getColor(activity, ") text = text.replace("ResUtils.getDrawable(", "ContextCompat.getDrawable(activity, ") if suffix == ".java": if "Activity mActivity" in text: text = text.replace("ResUtils.getColor(", "ContextCompat.getColor(mActivity, ") text = text.replace("ResUtils.getDrawable(", "ContextCompat.getDrawable(mActivity, ") elif "Context mContext" in text: text = text.replace("ResUtils.getColor(", "ContextCompat.getColor(mContext, ") text = text.replace("ResUtils.getDrawable(", "ContextCompat.getDrawable(mContext, ") else: text = text.replace("ResUtils.getColor(", "ContextCompat.getColor(getContext(), ") text = text.replace("ResUtils.getDrawable(", "ContextCompat.getDrawable(getContext(), ") elif suffix == ".kt": text = text.replace("ResUtils.getColor(", "ContextCompat.getColor(context, ") text = text.replace("ResUtils.getDrawable(", "ContextCompat.getDrawable(context, ") if ("ResUtils.getString(" not in text and "ResUtils.getDimension(" not in text and "ResUtils.getDimensionPixelOffset(" not in text and "ResUtils.getDimensionPixelSize(" not in text and "ResUtils.getContextResource(" not in text): text = text.replace("import com.xmcy.hykb.utils.ResUtils;", "") return text def BaseTextOneLineStyle(text): if "style=\"@style/BaseTextOneLineStyle\"" in text: print(f"发现:BaseTextOneLineStyle") text = text.replace("style=\"@style/BaseTextOneLineStyle\"", "android:includeFontPadding=\"false\" android:singleLine=\"true\" android:ellipsize=\"end\"") return text def MoreButtonStyle(text): if "style=\"@style/MoreButtonStyle\"" in text: print(f"发现:MoreButtonStyle") text = text.replace("style=\"@style/MoreButtonStyle\"", "android:text=\"@string/more\"\n" + " android:drawableRight=\"@drawable/home_icon_title_arrow\"\n" + " android:textColor=\"@color/black_h3\"\n" + " android:textSize=\"12sp\"\n" + " android:layout_width=\"wrap_content\"\n" + " android:layout_height=\"24dp\"\n" + " android:gravity=\"center\"\n" + " android:background=\"@drawable/bg_more_button_20\"\n" + " android:paddingLeft=\"8dp\"\n" + " android:paddingRight=\"8dp\"") return text def bg_white_radius10(text): if "android:background=\"@drawable/bg_white_radius10\"" in text: print(f"发现:bg_white_radius10") text = text.replace("android:background=\"@drawable/bg_white_radius10\"", "app:bl_solid_color=\"@color/bg_white\"\n app:bl_corners_radius=\"10dp\"") return text def bg_homeindex_item_10(text): if "android:background=\"@drawable/bg_homeindex_item_10\"" in text: print(f"发现:bg_homeindex_item_10") text = text.replace("android:background=\"@drawable/bg_homeindex_item_10\"", "app:bl_solid_color=\"@color/bg_white\"\n app:bl_corners_radius=\"10dp\"") return text def bg_btn_ffffff_10dp(text): if "android:background=\"@drawable/bg_btn_ffffff_10dp\"" in text: print(f"发现:bg_white_radius10") text = text.replace("android:background=\"@drawable/bg_btn_ffffff_10dp\"", "app:bl_solid_color=\"@color/bg_white\"\n app:bl_corners_radius=\"10dp\"") return text def bg_btn_white_topr10(text): if "android:background=\"@drawable/bg_btn_white_topr10\"" in text: print(f"发现:bg_btn_white_topr10") text = text.replace("android:background=\"@drawable/bg_btn_white_topr10\"", "app:bl_solid_color=\"@color/bg_white\"\n app:bl_corners_topRadius=\"10dp\"") return text def bg_btn_white_topr12(text): if "android:background=\"@drawable/bg_btn_white_topr12\"" in text: print(f"发现:bg_btn_white_topr12") text = text.replace("android:background=\"@drawable/bg_btn_white_topr12\"", "app:bl_solid_color=\"@color/bg_white\"\n app:bl_corners_topRadius=\"12dp\"") return text def bg_btn_white_topr16(text): if "android:background=\"@drawable/bg_btn_white_topr16\"" in text: print(f"发现:bg_btn_white_topr16") text = text.replace("android:background=\"@drawable/bg_btn_white_topr16\"", "app:bl_solid_color=\"@color/bg_white\" app:bl_corners_topRadius=\"16dp\"") return text def replace_xml_imageview(text): if "