"""收尾:去掉 @GetMapping/@PostMapping 里多余的 value =,并按字母重排 import。""" import re import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).parent)) from _root import ROOT # noqa: E402 CTRL_DIR = ROOT / "yoshop-java" / "src/main/java/cn/yoshop/api/controller" def clean(text: str) -> str: text = re.sub(r'@(Get|Post)Mapping\(value = ((\{[^}]*\}|"[^"]*"))\)', r'@\1Mapping(\2)', text) lines = text.split("\n") out, i = [], 0 while i < len(lines): if lines[i].startswith("import "): j = i while j < len(lines) and lines[j].startswith("import "): j += 1 out.extend(sorted(lines[i:j])) i = j else: out.append(lines[i]) i += 1 return "\n".join(out) n = 0 for f in sorted(CTRL_DIR.glob("*.java")): src = f.read_text(encoding="utf-8") new = clean(src) if new != src: f.write_text(new, encoding="utf-8") n += 1 print("整理文件数:", n)