123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428 |
- #!/usr/bin/python
- # -*- coding: UTF-8 -*-
- import os
- import re
- from pathlib import Path
- from utils.color_set import get_new_colors, get_old_colors, get_old_dimens
- # 需要忽略的文件
- def get_excluded_files():
- return {'colors.xml', 'colors_alpha.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 name="{color_name}">{color_code.lower()}</color>')
- if new_colors:
- new_contents = text.replace('</resources>', '\n'.join(new_colors) + '\n</resources>')
- 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("<item name=\"android:windowBackground\">@color/white</item>",
- "<item name=\"android:windowBackground\">@color/bg_white</item>")
- 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"<!-[\s\S]*?-->", 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()
- 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}")
- 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 "<ImageView\n" in text:
- text = text.replace("<ImageView\n",
- "<androidx.appcompat.widget.AppCompatImageView\n")
- return text
- def replace_xml_textview(text):
- text = text.replace("com.xmcy.hykb.app.view.KBTextView", "com.xmcy.hykb.view.KipoTextView")
- text = text.replace("KBTextView", "KipoTextView")
- return text
- def replace_system_bar(text):
- text = text.replace("getColorResId(R.color.white)", "getColorResId(R.color.bg_white)")
- text = text.replace("getColorResId(R.color.color_cccfd1d0)", "getColorResId(R.color.black_h5_80)")
- text = text.replace("getColorResId(R.color.whitesmoke)", "getColorResId(R.color.bg_deep)")
- text = text.replace("SystemBarHelper.setStatusBarMode(this, true)",
- "SystemBarHelper.setStatusBarMode(this, getResources().getBoolean(R.bool.status_bar_dark_font))")
- text = text.replace("SystemBarHelper.tintStatusBar(this, getColorResId(R.color.bg_light))",
- "SystemBarHelper.tintStatusBar(this, getColorResId(R.color.bg_deep))")
- return text
- def replace_icon(text):
- text = text.replace("gamedetail_icon_nav_come_back3", "bar_back")
- text = text.replace("icon_back_black", "bar_back")
- text = text.replace("accredit_icon_check", "set_sign_tick")
- text = text.replace("gamedetails_icon_into", "set_icon_arrow")
- text = text.replace("home_icon_arrow_gray2", "set_icon_arrow")
- text = text.replace("android:drawableRight=\"@drawable/icon_more\"",
- "app:drawableEndCompat=\"@drawable/set_icon_arrow\"")
- text = text.replace("android:src=\"@drawable/icon_more\"",
- "android:src=\"@drawable/set_icon_arrow\"")
- return text
- def replace_background(text):
- text = text.replace("android:background=\"@android:color/white\"", "android:background=\"@color/bg_white\"")
- text = text.replace("android:background=\"@color/white\"", "android:background=\"@color/bg_white\"")
- text = text.replace("android:background=\"@color/whitesmoke\"", "android:background=\"@color/bg_deep\"")
- text = text.replace("app:bl_solid_color=\"@color/white\"", "app:bl_solid_color=\"@color/bg_white\"")
- text = text.replace("app:bl_solid_color=\"@android:color/white\"", "app:bl_solid_color=\"@color/bg_white\"")
- text = text.replace("app:bl_solid_color=\"#ffffffff\"", "app:bl_solid_color=\"@color/bg_white\"")
- text = text.replace("app:bl_solid_color=\"#ffffff\"", "app:bl_solid_color=\"@color/bg_white\"")
- text = text.replace("app:bl_solid_color=\"#FFFFFFFF\"", "app:bl_solid_color=\"@color/bg_white\"")
- text = text.replace("app:bl_solid_color=\"#FFFFFF\"", "app:bl_solid_color=\"@color/bg_white\"")
- return text
|