Files
flutter_wisetronic/tools/badge_migrate.py

81 lines
3.2 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.

#!/usr/bin/env python3
"""badge_migrate.py — 把 badges 2.x 用法迁移到 3.x。
- animationType: BadgeAnimationType.X -> animation: BadgeAnimation.X()
- Badge(... badgeColor: C, padding: P ...) -> Badge(... badgeStyle: BadgeStyle(badgeColor: C, padding: P) ...)
"""
import re, glob
def find_calls(text, name):
calls=[]
for m in re.finditer(r'\b'+name+r'\s*\(', text):
if m.start()>0 and (text[m.start()-1].isalnum() or text[m.start()-1]=='_'):
continue # 跳过 BadgeStyle/BadgeAnimation 等同名前缀
i=m.end()-1; depth=0; j=i
while j<len(text):
c=text[j]
if c=='(' : depth+=1
elif c==')':
depth-=1
if depth==0: break
j+=1
if j<len(text):
calls.append((m.start(), j, text[i+1:j])) # inner 不含外层括号
return calls
def extract_param(inner, key):
"""从 inner 中提取 'key: value' 的 valuevalue 含平衡括号,到顶层逗号)。返回 (value, start, end)。"""
for m in re.finditer(r'\b'+key+r'\s*:\s*', inner):
start=m.end(); depth=0; j=start
while j<len(inner):
c=inner[j]
if c in '([{': depth+=1
elif c in ')]}':
if depth==0: break
depth-=1
elif c==',' and depth==0: break
j+=1
val=inner[start:j].strip().rstrip(',').strip()
return val, m.start(), j
return None, None, None
changed_files=0
for path in glob.glob('lib/**/*.dart', recursive=True):
t=open(path).read()
if 'Badge(' not in t: continue
orig=t
# 1) animationType: BadgeAnimationType.X -> animation: BadgeAnimation.X()
t=re.sub(r'animationType:\s*BadgeAnimationType\.(\w+)', r'animation: BadgeAnimation.\1()', t)
# 2) 合并 badgeColor + padding -> badgeStyle从后往前处理避免偏移
while True:
calls=find_calls(t, 'Badge')
did=False
for s,e,inner in reversed(calls):
bc,_,_=extract_param(inner,'badgeColor')
pd,_,_=extract_param(inner,'padding')
if not (bc or pd): continue
new_inner=inner
parts=[]
if bc:
v,vs,ve=extract_param(new_inner,'badgeColor')
# 删除该参数(含尾随逗号)
seg=new_inner[vs:ve]
after=new_inner[ve:]
if after.lstrip().startswith(','): after=after.lstrip()[1:]
new_inner=new_inner[:vs]+after
parts.append('badgeColor: '+bc)
if pd:
v,vs,ve=extract_param(new_inner,'padding')
seg=new_inner[vs:ve]
after=new_inner[ve:]
if after.lstrip().startswith(','): after=after.lstrip()[1:]
new_inner=new_inner[:vs]+after
parts.append('padding: '+pd)
badge_style='\n badgeStyle: BadgeStyle('+', '.join(parts)+'),'
new_inner=badge_style+new_inner
t=t[:s]+'Badge('+new_inner+')'+t[e+1:]
did=True; break
if not did: break
if t!=orig:
open(path,'w').write(t); changed_files+=1; print('migrated', path)
print('done, files:', changed_files)