diff --git a/autoreload.py b/autoreload.py index bd1dcdd9..d9a0c043 100755 --- a/autoreload.py +++ b/autoreload.py @@ -9,51 +9,55 @@ Any other arguments will be passed to the app, see `mirage --help`. Use `pip3 install --user -U requirements-dev.txt` before running this.""" +import os import subprocess import sys +from contextlib import suppress from pathlib import Path -from shutil import get_terminal_size +from shutil import get_terminal_size as term_size from watchgod import DefaultWatcher, run_process -SCRIPT_DIR = Path(__file__).parent +ROOT = Path(__file__).parent class Watcher(DefaultWatcher): - ignored_dirs = ( - *DefaultWatcher.ignored_dirs, "build", "packaging", "config", "themes", - ) + def accept_change(self, entry: os.DirEntry) -> bool: + path = Path(entry.path) - ignored_file_regexes = ( - *DefaultWatcher.ignored_file_regexes, - r".*\.md$", - r"^tags$", - r"^COPYING(\.LESSER)?$", - r"^requirements.*\.txt$", - r"^Makefile$", - r"^\.qmake\.stash$", - r"^mirage$", - r"^autoreload\.py$", - ) + for bad in ("src/config", "src/themes"): + if path.is_relative_to(ROOT / bad): + return False + + 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) + + +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", "─" * get_terminal_size().columns, "\x1b[0m\n", sep="") + print("\n\x1b[36m", "─" * term_size().columns, "\x1b[0m\n", sep="") - try: - subprocess.run(("qmake", "mirage.pro", "CONFIG+=dev"), cwd=SCRIPT_DIR) - subprocess.run("make", cwd=SCRIPT_DIR) - p = subprocess.run(("./mirage", "-name", "dev", *args), cwd=SCRIPT_DIR) - - if p.returncode != 0: - print(f"App exited with code {p.returncode}") - except KeyboardInterrupt: - pass + with suppress(KeyboardInterrupt): + cmd("qmake", "mirage.pro", "CONFIG+=dev") + cmd("make") + cmd("./mirage", "-name", "dev", *args) if __name__ == "__main__": if len(sys.argv) > 2 and sys.argv[1] in ("-h", "--help"): print(__doc__) else: - subprocess.run(("make", "clean"), cwd=SCRIPT_DIR) - run_process(SCRIPT_DIR, run_app, watcher_cls=Watcher, callback=print) + (ROOT / "Makefile").exists() and cmd("make", "clean") + run_process(ROOT, run_app, callback=print, watcher_cls=Watcher)