autoreload: improve code & which files are watched

This commit is contained in:
miruka 2021-01-16 17:44:16 -04:00
parent da7c1541a5
commit 2049141bfd

View File

@ -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.""" Use `pip3 install --user -U requirements-dev.txt` before running this."""
import os
import subprocess import subprocess
import sys import sys
from contextlib import suppress
from pathlib import Path 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 from watchgod import DefaultWatcher, run_process
SCRIPT_DIR = Path(__file__).parent ROOT = Path(__file__).parent
class Watcher(DefaultWatcher): class Watcher(DefaultWatcher):
ignored_dirs = ( def accept_change(self, entry: os.DirEntry) -> bool:
*DefaultWatcher.ignored_dirs, "build", "packaging", "config", "themes", path = Path(entry.path)
)
ignored_file_regexes = ( for bad in ("src/config", "src/themes"):
*DefaultWatcher.ignored_file_regexes, if path.is_relative_to(ROOT / bad):
r".*\.md$", return False
r"^tags$",
r"^COPYING(\.LESSER)?$", for good in ("src", "submodules"):
r"^requirements.*\.txt$", if path.is_relative_to(ROOT / good):
r"^Makefile$", return True
r"^\.qmake\.stash$",
r"^mirage$", return False
r"^autoreload\.py$",
) 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: 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: with suppress(KeyboardInterrupt):
subprocess.run(("qmake", "mirage.pro", "CONFIG+=dev"), cwd=SCRIPT_DIR) cmd("qmake", "mirage.pro", "CONFIG+=dev")
subprocess.run("make", cwd=SCRIPT_DIR) cmd("make")
p = subprocess.run(("./mirage", "-name", "dev", *args), cwd=SCRIPT_DIR) cmd("./mirage", "-name", "dev", *args)
if p.returncode != 0:
print(f"App exited with code {p.returncode}")
except KeyboardInterrupt:
pass
if __name__ == "__main__": if __name__ == "__main__":
if len(sys.argv) > 2 and sys.argv[1] in ("-h", "--help"): if len(sys.argv) > 2 and sys.argv[1] in ("-h", "--help"):
print(__doc__) print(__doc__)
else: else:
subprocess.run(("make", "clean"), cwd=SCRIPT_DIR) (ROOT / "Makefile").exists() and cmd("make", "clean")
run_process(SCRIPT_DIR, run_app, watcher_cls=Watcher, callback=print) run_process(ROOT, run_app, callback=print, watcher_cls=Watcher)