Files

78 lines
2.5 KiB
Python
Raw Permalink Normal View History

"""Build each declared static-content artifact independently."""
2026-05-06 08:07:28 -05:00
import shutil
import subprocess
import tempfile
from pathlib import Path
from utils import (
EXCLUDE_FILES,
env,
parse_site_yaml,
run,
selected_artifacts,
validate_artifact_inputs,
)
2026-05-06 08:07:28 -05:00
def build_artifact(site_dir, artifact):
html_dir = site_dir / artifact["build_dir"]
if html_dir.parent.exists():
shutil.rmtree(html_dir.parent)
2026-05-06 08:07:28 -05:00
content_dir = artifact["content_dir"]
2026-05-06 08:07:28 -05:00
src = site_dir / content_dir if content_dir else site_dir
if not src.exists():
raise FileNotFoundError(f"artifact {artifact['name']} content_dir not found: {src}")
2026-05-06 08:07:28 -05:00
if artifact["type"] == "static":
print(f"Copying artifact {artifact['name']} from {src}")
2026-05-06 08:07:28 -05:00
with tempfile.TemporaryDirectory() as tmp:
tmp_path = Path(tmp) / "html"
shutil.copytree(src, tmp_path, dirs_exist_ok=True)
for name in EXCLUDE_FILES:
p = tmp_path / name
if p.is_dir():
shutil.rmtree(p)
elif p.exists():
p.unlink()
html_dir.parent.mkdir(parents=True)
2026-05-06 08:07:28 -05:00
shutil.move(str(tmp_path), str(html_dir))
elif artifact["type"] == "hugo":
print(f"Building Hugo artifact {artifact['name']} from {src}")
run(["hugo", "--source", str(src), "--destination", str(html_dir)])
2026-05-06 08:07:28 -05:00
elif artifact["type"] == "mkdocs":
print(f"Building MkDocs artifact {artifact['name']} from {src}")
run(["mkdocs", "build", "-d", str(html_dir)], cwd=src)
2026-05-06 08:07:28 -05:00
if artifact["tidy"]:
2026-05-06 08:07:28 -05:00
print("Running tidy on HTML files...")
for html_file in html_dir.rglob("*.html"):
subprocess.run(
["tidy", "-modify", "-quiet",
"--wrap", "0", "--indent", "auto", "--indent-spaces", "2",
"--drop-empty-elements", "no", "--tidy-mark", "no",
"--show-warnings", "no", str(html_file)],
check=False,
)
if not any(path.is_file() for path in html_dir.rglob("*")):
raise FileNotFoundError(f"artifact {artifact['name']} produced no files in {html_dir}")
print(f"Artifact {artifact['name']} complete — content at {html_dir}")
2026-05-06 08:07:28 -05:00
def cmd_build():
site_dir = Path(env("SITE_DIR"))
cfg = parse_site_yaml(site_dir)
if not cfg["enabled"]:
print("Site disabled — skipping build")
return
validate_artifact_inputs(site_dir, cfg)
for artifact in selected_artifacts(cfg):
build_artifact(site_dir, artifact)