tidy_api_mapping_annotations.py 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. """收尾:去掉 @GetMapping/@PostMapping 里多余的 value =,并按字母重排 import。"""
  2. import re
  3. import sys
  4. from pathlib import Path
  5. sys.path.insert(0, str(Path(__file__).parent))
  6. from _root import ROOT # noqa: E402
  7. CTRL_DIR = ROOT / "yoshop-java" / "src/main/java/cn/yoshop/api/controller"
  8. def clean(text: str) -> str:
  9. text = re.sub(r'@(Get|Post)Mapping\(value = ((\{[^}]*\}|"[^"]*"))\)',
  10. r'@\1Mapping(\2)', text)
  11. lines = text.split("\n")
  12. out, i = [], 0
  13. while i < len(lines):
  14. if lines[i].startswith("import "):
  15. j = i
  16. while j < len(lines) and lines[j].startswith("import "):
  17. j += 1
  18. out.extend(sorted(lines[i:j]))
  19. i = j
  20. else:
  21. out.append(lines[i])
  22. i += 1
  23. return "\n".join(out)
  24. n = 0
  25. for f in sorted(CTRL_DIR.glob("*.java")):
  26. src = f.read_text(encoding="utf-8")
  27. new = clean(src)
  28. if new != src:
  29. f.write_text(new, encoding="utf-8")
  30. n += 1
  31. print("整理文件数:", n)