2021-01-06 01:08:23 +11:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
"""Usage: ./autoreload.py [MIRAGE_ARGUMENTS]...
|
|
|
|
|
|
|
|
Automatically rebuild and restart the application when source files change.
|
2022-01-25 01:23:56 +11:00
|
|
|
CONFIG+=dev will be passed to qmake, see moment.pro.
|
2024-01-08 00:34:49 +11:00
|
|
|
The application will be launched with `-name dev`, which sets the first
|
|
|
|
part of the WM_CLASS as returned by xprop on Linux.
|
|
|
|
Any other arguments will be passed to the app, see `moment --help`.
|
2021-01-06 01:08:23 +11:00
|
|
|
|
|
|
|
Use `pip3 install --user -U requirements-dev.txt` before running this."""
|
|
|
|
|
2021-01-17 08:44:16 +11:00
|
|
|
import os
|
2021-01-06 01:08:23 +11:00
|
|
|
import subprocess
|
2024-01-03 13:25:54 +11:00
|
|
|
import shutil
|
2021-01-06 01:08:23 +11:00
|
|
|
import sys
|
2021-01-17 08:44:16 +11:00
|
|
|
from contextlib import suppress
|
2021-01-06 01:08:23 +11:00
|
|
|
from pathlib import Path
|
2021-01-17 08:44:16 +11:00
|
|
|
from shutil import get_terminal_size as term_size
|
2021-01-06 01:08:23 +11:00
|
|
|
|
|
|
|
from watchgod import DefaultWatcher, run_process
|
|
|
|
|
2021-01-17 08:44:16 +11:00
|
|
|
ROOT = Path(__file__).parent
|
2021-01-06 01:08:23 +11:00
|
|
|
|
|
|
|
|
|
|
|
class Watcher(DefaultWatcher):
|
2021-01-17 08:44:16 +11:00
|
|
|
def accept_change(self, entry: os.DirEntry) -> bool:
|
|
|
|
path = Path(entry.path)
|
2021-01-06 01:08:23 +11:00
|
|
|
|
2021-01-17 08:44:16 +11:00
|
|
|
for bad in ("src/config", "src/themes"):
|
|
|
|
if path.is_relative_to(ROOT / bad):
|
|
|
|
return False
|
2021-01-06 01:08:23 +11:00
|
|
|
|
2021-01-17 08:44:16 +11:00
|
|
|
for good in ("src", "submodules"):
|
|
|
|
if path.is_relative_to(ROOT / good):
|
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
def should_watch_dir(self, entry: os.DirEntry) -> bool:
|
|
|
|
return super().should_watch_dir(entry) and self.accept_change(entry)
|
|
|
|
|
|
|
|
def should_watch_file(self, entry: os.DirEntry) -> bool:
|
|
|
|
return super().should_watch_file(entry) and self.accept_change(entry)
|
2021-01-06 01:08:23 +11:00
|
|
|
|
|
|
|
|
2021-01-17 08:44:16 +11:00
|
|
|
def cmd(*parts) -> subprocess.CompletedProcess:
|
|
|
|
return subprocess.run(parts, cwd=ROOT, check=True)
|
|
|
|
|
|
|
|
|
|
|
|
def run_app(args=sys.argv[1:]) -> None:
|
|
|
|
print("\n\x1b[36m", "─" * term_size().columns, "\x1b[0m\n", sep="")
|
|
|
|
|
2024-01-03 13:25:54 +11:00
|
|
|
if shutil.which("qmake-qt5"):
|
|
|
|
QMAKE_CMD = "qmake-qt5"
|
|
|
|
else:
|
|
|
|
QMAKE_CMD = "qmake"
|
|
|
|
|
2021-01-17 08:44:16 +11:00
|
|
|
with suppress(KeyboardInterrupt):
|
2024-01-03 13:25:54 +11:00
|
|
|
cmd(QMAKE_CMD, "moment.pro", "CONFIG+=dev")
|
2021-01-17 08:44:16 +11:00
|
|
|
cmd("make")
|
2024-01-08 00:34:49 +11:00
|
|
|
cmd("./moment", "-name", "dev", *args)
|
2021-01-06 01:08:23 +11:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
if len(sys.argv) > 2 and sys.argv[1] in ("-h", "--help"):
|
|
|
|
print(__doc__)
|
|
|
|
else:
|
2021-01-17 08:44:16 +11:00
|
|
|
(ROOT / "Makefile").exists() and cmd("make", "clean")
|
|
|
|
run_process(ROOT, run_app, callback=print, watcher_cls=Watcher)
|