def mkdocs(
*,
requirements: Sequence[str] = ("mkdocs>=1.5.3,<2.0.0"),
mkdocs_root: Path | str | None = None,
watch_files: Sequence[Path | str] = (),
task_prefix: str = "mkdocs",
project: Project | None = None,
) -> tuple[MkDocsTask, MkDocsTask]:
project = project or Project.current()
mkdocs_bin = pex_build(
console_script="mkdocs",
requirements=requirements,
project=project,
venv="prepend",
).output_file.map(str)
build_dir = (project.build_directory / task_prefix / "_site").absolute()
build_task = project.task(f"{task_prefix}.build", MkDocsTask)
build_task.mkdocs_root = project.directory / (mkdocs_root or "")
build_task.mkdocs_bin = mkdocs_bin
build_task.args = ["build", "-d", str(build_dir), "--strict"]
port = int(os.environ.get("MKDOCS_PORT", "8000"))
watch_files = [(project.directory / x).absolute() for x in watch_files]
serve_task = project.task(f"{task_prefix}.serve", MkDocsTask)
build_task.mkdocs_root = project.directory / (mkdocs_root or "")
serve_task.mkdocs_bin = mkdocs_bin
serve_args = ["serve", "-a", f"localhost:{port}"]
for w in watch_files:
serve_args += ["-w", str(w)]
serve_task.args = serve_args
return build_task, serve_task