diff --git a/autoreload.py b/autoreload.py new file mode 100755 index 00000000..b789be08 --- /dev/null +++ b/autoreload.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python3 +"""Usage: ./autoreload.py [MIRAGE_ARGUMENTS]... + +Automatically rebuild and restart the application when source files change. +CONFIG+=dev will be passed to qmake, see mirage.pro. +The application will be launched with `-name dev`, which sets the first +part of the WM_CLASS as returned by xprop on Linux. + +Use `pip3 install --user -U requirements-dev.txt` before running this.""" + +import subprocess +import sys +from pathlib import Path +from shutil import get_terminal_size + +from watchgod import DefaultWatcher, run_process + +SCRIPT_DIR = Path(__file__).parent + + +class Watcher(DefaultWatcher): + ignored_dirs = ( + *DefaultWatcher.ignored_dirs, "build", "packaging", "config", "themes", + ) + + 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$", + ) + + +def run_app(args=sys.argv[1:]) -> None: + print("\n\x1b[36m", "─" * get_terminal_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 + + +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) diff --git a/live-reload.sh b/live-reload.sh deleted file mode 100755 index 96c390b0..00000000 --- a/live-reload.sh +++ /dev/null @@ -1,29 +0,0 @@ -#!/usr/bin/env sh - -# Dependencies: findutils, entr -# -# This script will watch for source file changes and recompile-restart Mirage -# when needed. If it gets stuck restarting in loop, press CTRL-C a bunch of -# times and try again. -# -# pdb won't be usable due to entr, use https://pypi.org/project/remote-pdb/ -# instead (should be present if you install requirements-dev.txt). -# -# An argument can be given to specify which QML file in src/gui to load, -# for example "Test.qml" would load "src/gui/Test.qml". -# If no argument is given, the default is "UI.qml". - -make clean -qmake mirage.pro CONFIG+=dev && make - -while true; do - # app already handles reloading config and theme files - find src mirage.pro \ - -type f -not -path 'src/themes/*' -not -path 'src/config/*' | - - # -name affects the first part of the WM_CLASS returned by xprop on Linux - entr -cdnr sh -c \ - "qmake mirage.pro CONFIG+=dev && make && ./mirage -name dev $*" - - sleep 0.2 -done diff --git a/src/main.cpp b/src/main.cpp index d13aad47..d9362b76 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -189,7 +189,7 @@ int main(int argc, char *argv[]) { QObject::connect(&engine, &QQmlEngine::quit, &QApplication::quit); // Set the debugMode properties depending of if we're running in debug mode - // or not (`qmake CONFIG+=dev ...`, default in live-reload.sh) + // or not (`qmake CONFIG+=dev ...`, default in autoreload.py) #ifdef QT_DEBUG objectContext->setContextProperty("debugMode", true); #else