tidy_api_mapping_annotations.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. """收尾:去掉 @GetMapping/@PostMapping 里多余的 value =,并按字母重排 import。
  2. 覆盖三个模块:api / store / admin。import 分组里 com.baomidou.*、tools.jackson.*
  3. 保持原有靠前位置,其余按字母排序。
  4. """
  5. import re
  6. import sys
  7. from pathlib import Path
  8. sys.path.insert(0, str(Path(__file__).parent))
  9. from _root import ROOT # noqa: E402
  10. MODULES = ("api", "store", "admin")
  11. def clean(text: str) -> str:
  12. text = re.sub(r'@(Get|Post)Mapping\(value = ((\{[^}]*\}|"[^"]*"))\)',
  13. r'@\1Mapping(\2)', text)
  14. lines = text.split("\n")
  15. out, i = [], 0
  16. while i < len(lines):
  17. if lines[i].startswith("import "):
  18. j = i
  19. while j < len(lines) and lines[j].startswith("import "):
  20. j += 1
  21. grp = lines[i:j]
  22. head = [x for x in grp if x.startswith(("import com.baomidou.", "import tools.jackson."))]
  23. rest = sorted(x for x in grp if x not in head)
  24. out.extend(sorted(head) + rest)
  25. i = j
  26. else:
  27. out.append(lines[i])
  28. i += 1
  29. return "\n".join(out)
  30. n = 0
  31. for mod in MODULES:
  32. ctrl = ROOT / "yoshop-java" / f"src/main/java/cn/yoshop/{mod}/controller"
  33. for f in sorted(ctrl.glob("*.java")):
  34. src = f.read_text(encoding="utf-8")
  35. new = clean(src)
  36. if new != src:
  37. f.write_text(new, encoding="utf-8")
  38. n += 1
  39. print("整理文件数:", n)