Skip to content

Commit 4b3d0bb

Browse files
committed
Fix hot-reload and add -w --no-livereload
Closes #9
1 parent 577adf6 commit 4b3d0bb

File tree

4 files changed

+65
-4
lines changed

4 files changed

+65
-4
lines changed

src/pulp_docs/cli.py

+34-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import subprocess
99
import sys
1010
import tempfile
11+
import typing as t
1112
from pathlib import Path
1213

1314
import click
@@ -69,6 +70,13 @@ def main(config: Config, verbose: bool):
6970
config.verbose = verbose
7071

7172

73+
# mkdocs help wrapper
74+
watch_help = (
75+
"A directory or file to watch for live reloading. Can be supplied multiple times."
76+
)
77+
no_reload_help = "Disable the live reloading in the development server."
78+
79+
7280
@main.command()
7381
@click.option(
7482
"--clear-cache",
@@ -77,17 +85,40 @@ def main(config: Config, verbose: bool):
7785
help="Whether to clear the cache before serving (default=False).",
7886
)
7987
@click.option("--verbose", "-v", is_flag=True)
88+
@click.option(
89+
"-w",
90+
"--watch",
91+
help=watch_help,
92+
type=click.Path(exists=True),
93+
multiple=True,
94+
default=[],
95+
)
96+
@click.option("--no-livereload", "livereload", flag_value=False, help=no_reload_help)
97+
@click.option("--livereload", "livereload", flag_value=True, default=True, hidden=True)
8098
@pass_config
81-
def serve(config: Config, clear_cache: bool, verbose: bool):
99+
def serve(
100+
config: Config,
101+
clear_cache: bool,
102+
verbose: bool,
103+
watch: t.List[Path],
104+
livereload: bool,
105+
):
82106
"""Run mkdocs server."""
83107
env = os.environ.copy()
84108
config.clear_cache = clear_cache
85109
config.verbose = verbose
86110
env.update(config.get_environ_dict())
87111

88-
options = (("--config-file", config.mkdocs_file),)
89-
cmd = ["mkdocs", "serve"]
112+
watch_list = [("--watch", watched) for watched in watch]
113+
flag_list = []
114+
if livereload is False:
115+
flag_list.append(("--no-livereload",))
90116

117+
options: t.List[tuple] = [("--config-file", config.mkdocs_file)]
118+
options.extend(watch_list)
119+
options.extend(flag_list)
120+
121+
cmd = ["mkdocs", "serve"]
91122
for opt in options:
92123
cmd.extend(opt)
93124
print("Running:", " ".join(str(s) for s in cmd))

src/pulp_docs/data/mkdocs.yml

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ theme:
3636
icon: material/toggle-switch
3737
name: Switch to light mode
3838

39+
hooks:
40+
- '../mkdocs_hooks.py'
3941
plugins:
4042
- search
4143
- site-urls

src/pulp_docs/mkdocs_hooks.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"""
2+
Hooks for mkdocs events.
3+
4+
See: https://www.mkdocs.org/user-guide/configuration/#hooks
5+
"""
6+
7+
8+
def on_serve(server, config, builder):
9+
"""
10+
Hook to unwatch the temp dirs.
11+
12+
See: https://www.mkdocs.org/dev-guide/plugins/#on_serve
13+
"""
14+
tmpdir = config["docs_dir"]
15+
mkdocs_yml = config["config_file_path"]
16+
server.unwatch(tmpdir)
17+
server.unwatch(mkdocs_yml)
18+
return server

src/pulp_docs/mkdocs_macros.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,11 @@ def define_env(env):
273273
# Workaround for making "pulpcore/cli/common" from pulp-cli be available as:
274274
# '::: pulpcore.cli.common' (from any markdown)
275275
# This should be a general solution.
276-
shutil.copytree(source_dir / "pulp-cli/pulpcore", source_dir / "pulpcore/pulpcore", dirs_exist_ok=True)
276+
shutil.copytree(
277+
source_dir / "pulp-cli/pulpcore",
278+
source_dir / "pulpcore/pulpcore",
279+
dirs_exist_ok=True,
280+
)
277281
Path(source_dir / "pulpcore/pulpcore/cli/__init__.py").touch(exist_ok=True)
278282
Path(source_dir / "pulpcore/pulpcore/cli/common/__init__.py").touch(exist_ok=True)
279283

@@ -286,6 +290,12 @@ def define_env(env):
286290
env.conf["docs_dir"] = docs_dir
287291
env.conf["nav"] = get_navigation(docs_dir, repos)
288292

293+
# Try to watch CWD/staging_docs
294+
watched_workdir = Path("staging_docs")
295+
if watched_workdir.exists():
296+
env.conf["watch"].append(str(watched_workdir.resolve()))
297+
298+
# Pass relevant data for future processing
289299
log.info("[pulp-docs] Done with pulp-docs.")
290300
env.conf["pulp_repos"] = repos
291301
env.config["pulp_repos"] = repos

0 commit comments

Comments
 (0)