autoreload.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #!/usr/bin/env python3
  2. """Usage: ./autoreload.py [MIRAGE_ARGUMENTS]...
  3. Automatically rebuild and restart the application when source files change.
  4. CONFIG+=dev will be passed to qmake, see moment.pro.
  5. The application will be launched with `-name dev`, which sets the first
  6. part of the WM_CLASS as returned by xprop on Linux.
  7. Any other arguments will be passed to the app, see `moment --help`.
  8. Use `pip3 install --user -U requirements-dev.txt` before running this."""
  9. import os
  10. import subprocess
  11. import shutil
  12. import sys
  13. from contextlib import suppress
  14. from pathlib import Path
  15. from shutil import get_terminal_size as term_size
  16. from watchfiles import DefaultWatcher, run_process
  17. ROOT = Path(__file__).parent
  18. class Watcher(DefaultWatcher):
  19. def accept_change(self, entry: os.DirEntry) -> bool:
  20. path = Path(entry.path)
  21. for bad in ("src/config", "src/themes"):
  22. if path.is_relative_to(ROOT / bad):
  23. return False
  24. for good in ("src", "submodules"):
  25. if path.is_relative_to(ROOT / good):
  26. return True
  27. return False
  28. def should_watch_dir(self, entry: os.DirEntry) -> bool:
  29. return super().should_watch_dir(entry) and self.accept_change(entry)
  30. def should_watch_file(self, entry: os.DirEntry) -> bool:
  31. return super().should_watch_file(entry) and self.accept_change(entry)
  32. def cmd(*parts) -> subprocess.CompletedProcess:
  33. return subprocess.run(parts, cwd=ROOT, check=True)
  34. def run_app(args=sys.argv[1:]) -> None:
  35. print("\n\x1b[36m", "─" * term_size().columns, "\x1b[0m\n", sep="")
  36. if shutil.which("qmake-qt5"):
  37. QMAKE_CMD = "qmake-qt5"
  38. else:
  39. QMAKE_CMD = "qmake"
  40. with suppress(KeyboardInterrupt):
  41. cmd(QMAKE_CMD, "moment.pro", "CONFIG+=dev")
  42. cmd("make")
  43. cmd("./moment", "-name", "dev", *args)
  44. if __name__ == "__main__":
  45. if len(sys.argv) > 2 and sys.argv[1] in ("-h", "--help"):
  46. print(__doc__)
  47. else:
  48. (ROOT / "Makefile").exists() and cmd("make", "clean")
  49. run_process(ROOT, run_app, callback=print, watcher_cls=Watcher)