Skip to content

Commit

Permalink
Remove watcher state from Builder
Browse files Browse the repository at this point in the history
  • Loading branch information
AA-Turner committed Apr 8, 2024
1 parent 7b07a13 commit 21ecf64
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 14 deletions.
3 changes: 1 addition & 2 deletions sphinx_autobuild/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ def main():

pre_build_commands = list(map(shlex.split, args.pre_build))
builder = Builder(
server.watcher,
build_args,
host=args.host,
port=port_num,
Expand All @@ -53,7 +52,7 @@ def main():
server.watch(outdir, ignore=ignore_handler)

if not args.no_initial_build:
builder()
builder(rebuild=False)

if args.open_browser:
open_browser(f"{args.host}:{port_num}", args.delay)
Expand Down
19 changes: 7 additions & 12 deletions sphinx_autobuild/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,16 @@


class Builder:
def __init__(self, watcher, sphinx_args, *, host, port, pre_build_commands):
self.watcher = watcher
def __init__(self, sphinx_args, *, host, port, pre_build_commands):
self.sphinx_args = sphinx_args
self.pre_build_commands = pre_build_commands
self.uri = f"http://{host}:{port}"

def __call__(self):
def __call__(self, *, rebuild: bool = True):
"""Generate the documentation using ``sphinx``."""

if self.watcher.filepath:
show(context=f"Detected change: {self.watcher.filepath}")
if rebuild:
show(context="Detected change. Rebuilding...")

try:
for command in self.pre_build_commands:
Expand All @@ -38,17 +37,13 @@ def __call__(self):
# NOTE:
# sphinx.cmd.build.build_main is not considered to be public API,
# but as this is a first-party project, we can cheat a little bit.
return_code = build_main(self.sphinx_args)
if return_code:
if return_code := build_main(self.sphinx_args):
print(f"Sphinx exited with exit code: {return_code}")
print(
"The server will continue serving the build folder, but the contents "
"being served are no longer in sync with the documentation sources. "
"Please fix the cause of the error above or press Ctrl+C to stop the "
"server."
)
# 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.
if self.watcher.filepath:
show(context=f"Serving on {self.uri}")
# Remind the user of the server URL for convenience.
show(context=f"Serving on {self.uri}")

0 comments on commit 21ecf64

Please sign in to comment.