"""收尾:去掉 @GetMapping/@PostMapping 里多余的 value =,并按字母重排 import。 覆盖三个模块:api / store / admin。import 分组里 com.baomidou.*、tools.jackson.* 保持原有靠前位置,其余按字母排序。 """ import re import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).parent)) from _root import ROOT # noqa: E402 MODULES = ("api", "store", "admin") 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 grp = lines[i:j] head = [x for x in grp if x.startswith(("import com.baomidou.", "import tools.jackson."))] rest = sorted(x for x in grp if x not in head) out.extend(sorted(head) + rest) i = j else: out.append(lines[i]) i += 1 return "\n".join(out) n = 0 for mod in MODULES: ctrl = ROOT / "yoshop-java" / f"src/main/java/cn/yoshop/{mod}/controller" for f in sorted(ctrl.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)