diff --git a/README.rst b/README.rst index 8d4da31..0ff4cb9 100644 --- a/README.rst +++ b/README.rst @@ -73,6 +73,7 @@ which can seen by running ``sphinx-autobuild --help``: --delay DELAY how long to wait before opening the browser (default: 5) --watch DIR additional directories to watch (default: []) --pre-build COMMAND additional command(s) to run prior to building the documentation (default: []) + --post-build COMMAND additional command(s) to run after building the documentation (default: []) --version show program's version number and exit sphinx's arguments: @@ -137,6 +138,14 @@ all pages are built from the same state of the HTML theme. It also works around a `known issue in Sphinx `__ which causes significant problems during theme development. +Post-build resources can be processed by passing a user-defined command to +``--post-build``. + +.. code-block:: bash + + --post-build "npx tailwindcss -i ./src/input.css -o ./src/output.css" + + Working on multiple projects ---------------------------- diff --git a/sphinx_autobuild/__main__.py b/sphinx_autobuild/__main__.py index 82150f5..18b15f8 100644 --- a/sphinx_autobuild/__main__.py +++ b/sphinx_autobuild/__main__.py @@ -34,12 +34,14 @@ def main(): server = Server() pre_build_commands = list(map(shlex.split, args.pre_build)) + post_build_commands = list(map(shlex.split, args.post_build)) builder = Builder( server.watcher, build_args, host=args.host, port=port_num, pre_build_commands=pre_build_commands, + post_build_commands=post_build_commands, ) ignore_handler = _get_ignore_handler( @@ -176,6 +178,13 @@ def _add_autobuild_arguments(parser): default=[], help="additional command(s) to run prior to building the documentation", ) + group.add_argument( + "--post-build", + action="append", + metavar="COMMAND", + default=[], + help="additional command(s) to run after building the documentation", + ) return group diff --git a/sphinx_autobuild/build.py b/sphinx_autobuild/build.py index 2f4ee1f..51e33db 100644 --- a/sphinx_autobuild/build.py +++ b/sphinx_autobuild/build.py @@ -24,10 +24,20 @@ def show(*, context=None, command=None): class Builder: - def __init__(self, watcher, sphinx_args, *, host, port, pre_build_commands): + def __init__( + self, + watcher, + sphinx_args, + *, + host, + port, + pre_build_commands, + post_build_commands, + ): self.watcher = watcher self.sphinx_args = sphinx_args self.pre_build_commands = pre_build_commands + self.post_build_commands = post_build_commands self.uri = f"http://{host}:{port}" def __call__(self): @@ -62,6 +72,21 @@ def __call__(self): "Please fix the cause of the error above or press Ctrl+C to stop the " "server." ) + + else: + # Run the post-build commands only if the build was successful + try: + for command in self.post_build_commands: + show(context="post-build", command=command) + subprocess.run(command, check=True) + except subprocess.CalledProcessError as e: + print(f"Post-build command exited with exit code: {e.returncode}") + print( + "Please fix the cause of the error above or press Ctrl+C to stop" + " the server." + ) + raise + # We present this information, so that the user does not need to keep track # of the port being used. It is presented by livereload when starting the # server, so don't present it in the initial build.