Skip to content

Commit b31faa5

Browse files
e-rouxSilverRainZ
authored andcommitted
feat(cli): add post-build argument
1 parent 86e2f37 commit b31faa5

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

README.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ which can seen by running ``sphinx-autobuild --help``:
6767
--delay DELAY how long to wait before opening the browser
6868
--watch DIR additional directories to watch
6969
--pre-build COMMAND additional command(s) to run prior to building the documentation
70+
--post-build COMMAND additional command(s) to run after building the documentation (default: [])
7071
7172
Using with Makefile
7273
-------------------
@@ -126,6 +127,14 @@ all pages are built from the same state of the HTML theme.
126127
It also works around a `known issue in Sphinx <relevant sphinx bugs_>`__
127128
which causes significant problems during theme development.
128129

130+
Post-build resources can be processed by passing a user-defined command to
131+
``--post-build``.
132+
133+
.. code-block:: bash
134+
135+
--post-build "npx tailwindcss -i ./src/input.css -o ./src/output.css"
136+
137+
129138
Working on multiple projects
130139
----------------------------
131140

sphinx_autobuild/__main__.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,12 @@ def main(argv=()):
4949
url_host = f"{host_name}:{port_num}"
5050

5151
pre_build_commands = list(map(shlex.split, args.pre_build))
52-
52+
post_build_commands = list(map(shlex.split, args.post_build))
5353
builder = Builder(
5454
build_args,
5555
url_host=url_host,
5656
pre_build_commands=pre_build_commands,
57+
post_build_commands=post_build_commands,
5758
)
5859

5960
watch_dirs = [src_dir] + args.additional_watched_dirs
@@ -233,6 +234,13 @@ def _add_autobuild_arguments(parser):
233234
default=[],
234235
help="additional command(s) to run prior to building the documentation",
235236
)
237+
group.add_argument(
238+
"--post-build",
239+
action="append",
240+
metavar="COMMAND",
241+
default=[],
242+
help="additional command(s) to run after building the documentation",
243+
)
236244
return group
237245

238246

sphinx_autobuild/build.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@
1515

1616

1717
class Builder:
18-
def __init__(self, sphinx_args, *, url_host, pre_build_commands):
18+
def __init__(self, sphinx_args, *, url_host, pre_build_commands, post_build_commands):
1919
self.sphinx_args = sphinx_args
2020
self.pre_build_commands = pre_build_commands
21+
self.post_build_commands = post_build_commands
2122
self.uri = f"http://{url_host}"
2223

2324
def __call__(self, *, changed_paths: Sequence[Path]):
@@ -70,5 +71,20 @@ def __call__(self, *, changed_paths: Sequence[Path]):
7071
"Please fix the cause of the error above or press Ctrl+C to stop the "
7172
"server."
7273
)
74+
else:
75+
# Run the post-build commands only if the build was successful
76+
try:
77+
for command in self.post_build_commands:
78+
show_message("post-build")
79+
show_command(command)
80+
subprocess.run(command, check=True)
81+
except subprocess.CalledProcessError as e:
82+
print(f"Post-build command exited with exit code: {e.returncode}")
83+
print(
84+
"Please fix the cause of the error above or press Ctrl+C to stop"
85+
" the server."
86+
)
87+
raise
88+
7389
# Remind the user of the server URL for convenience.
7490
show_message(f"Serving on {self.uri}")

0 commit comments

Comments
 (0)