69 lines
2.4 KiB
Python
69 lines
2.4 KiB
Python
"""Build each declared static-content artifact independently."""
|
|
|
|
import shutil
|
|
import subprocess
|
|
import tempfile
|
|
from pathlib import Path
|
|
|
|
from utils import EXCLUDE_FILES, env, parse_site_yaml, run
|
|
|
|
|
|
def build_artifact(site_dir, artifact):
|
|
html_dir = site_dir / artifact["build_dir"]
|
|
if html_dir.parent.exists():
|
|
shutil.rmtree(html_dir.parent)
|
|
|
|
content_dir = artifact["content_dir"]
|
|
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}")
|
|
|
|
if artifact["type"] == "static":
|
|
print(f"Copying artifact {artifact['name']} from {src}")
|
|
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)
|
|
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)])
|
|
|
|
elif artifact["type"] == "mkdocs":
|
|
print(f"Building MkDocs artifact {artifact['name']} from {src}")
|
|
run(["mkdocs", "build", "-d", str(html_dir)], cwd=src)
|
|
|
|
if artifact["tidy"]:
|
|
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}")
|
|
|
|
|
|
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
|
|
|
|
for artifact in cfg["artifacts"]:
|
|
build_artifact(site_dir, artifact)
|