Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): add post-build argument #143

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
feat(cli): add post-build argument
  • Loading branch information
e-roux committed Apr 9, 2024
commit 721072735dd70df22369bb59af8235c39b994d2f
9 changes: 9 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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 <relevant sphinx bugs_>`__
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
----------------------------

Expand Down
9 changes: 9 additions & 0 deletions sphinx_autobuild/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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


Expand Down
27 changes: 26 additions & 1 deletion sphinx_autobuild/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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.
Expand Down