sort_imports.py 706 B

1234567891011121314151617181920
  1. import re
  2. from pathlib import Path
  3. ROOT = Path(r"F:/shop/java-rewrite/yoshop-java/src/main/java/cn/yoshop/store/controller")
  4. for p in sorted(ROOT.glob("*.java")):
  5. lines = p.read_text(encoding="utf-8").split("\n")
  6. idx = [i for i, l in enumerate(lines) if l.startswith("import cn.yoshop.")]
  7. if not idx:
  8. continue
  9. start, end = idx[0], idx[-1]
  10. block = lines[start:end + 1]
  11. if any(not b.startswith("import cn.yoshop.") for b in block):
  12. print(f"SKIP {p.name}: 非连续块")
  13. continue
  14. ordered = sorted(block)
  15. if ordered != block:
  16. lines[start:end + 1] = ordered
  17. p.write_text("\n".join(lines), encoding="utf-8")
  18. print(f"OK {p.name}")