|
| 1 | +"""Nox sessions for building and serving the Hugo documentation site.""" |
| 2 | + |
| 3 | +import platform |
| 4 | +import tarfile |
| 5 | +import urllib.request |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +import nox |
| 9 | + |
| 10 | +# Hugo version to download from GitHub releases |
| 11 | +HUGO_VERSION = "0.125.3" |
| 12 | + |
| 13 | +nox.options.default_venv_backend = "uv" |
| 14 | +nox.options.reuse_existing_virtualenvs = True |
| 15 | + |
| 16 | + |
| 17 | +@nox.session(name="docs") |
| 18 | +def docs(session): |
| 19 | + """Build the Hugo documentation site. |
| 20 | +
|
| 21 | + Downloads Hugo Extended v{HUGO_VERSION} from GitHub releases if not cached. |
| 22 | + """ |
| 23 | + hugo_path = install_hugo(session) |
| 24 | + |
| 25 | + # Build the site |
| 26 | + session.run(hugo_path, external=True) |
| 27 | + |
| 28 | + |
| 29 | +@nox.session(name="docs-live") |
| 30 | +def docs_live(session): |
| 31 | + """Build and serve the Hugo documentation site with live reload. |
| 32 | +
|
| 33 | + Downloads Hugo Extended v{HUGO_VERSION} from GitHub releases if not cached. |
| 34 | + The site will be available at http://localhost:1313 |
| 35 | + """ |
| 36 | + hugo_path = install_hugo(session) |
| 37 | + |
| 38 | + # Serve the site with live reload |
| 39 | + session.run(hugo_path, "server", external=True) |
| 40 | + |
| 41 | + |
| 42 | +def get_hugo_download_url(version): |
| 43 | + """Get the download URL for Hugo based on the current platform.""" |
| 44 | + system = platform.system().lower() |
| 45 | + machine = platform.machine().lower() |
| 46 | + |
| 47 | + if system == "darwin": |
| 48 | + # macOS uses universal binary |
| 49 | + filename = f"hugo_extended_{version}_darwin-universal.tar.gz" |
| 50 | + elif system == "linux": |
| 51 | + if machine in ("x86_64", "amd64"): |
| 52 | + filename = f"hugo_extended_{version}_linux-amd64.tar.gz" |
| 53 | + elif machine in ("arm64", "aarch64"): |
| 54 | + filename = f"hugo_extended_{version}_linux-arm64.tar.gz" |
| 55 | + else: |
| 56 | + raise RuntimeError(f"Unsupported Linux architecture: {machine}") |
| 57 | + elif system == "windows": |
| 58 | + if machine in ("x86_64", "amd64"): |
| 59 | + filename = f"hugo_extended_{version}_windows-amd64.zip" |
| 60 | + else: |
| 61 | + raise RuntimeError(f"Unsupported Windows architecture: {machine}") |
| 62 | + else: |
| 63 | + raise RuntimeError(f"Unsupported operating system: {system}") |
| 64 | + |
| 65 | + base_url = "https://github.com/gohugoio/hugo/releases/download" |
| 66 | + return f"{base_url}/v{version}/{filename}" |
| 67 | + |
| 68 | + |
| 69 | +def install_hugo(session): |
| 70 | + """Download and install Hugo if not already present in the session.""" |
| 71 | + # Create a bin directory in the session's virtualenv |
| 72 | + bin_dir = Path(session.virtualenv.location) / "bin" |
| 73 | + bin_dir.mkdir(exist_ok=True) |
| 74 | + hugo_path = bin_dir / "hugo" |
| 75 | + |
| 76 | + # Check if Hugo is already installed in this session |
| 77 | + if hugo_path.exists(): |
| 78 | + session.log(f"Hugo already installed at {hugo_path}") |
| 79 | + # Verify it's the correct version |
| 80 | + result = session.run( |
| 81 | + str(hugo_path), "version", silent=True, external=True |
| 82 | + ) |
| 83 | + if HUGO_VERSION in result: |
| 84 | + session.log(f"Using cached Hugo {HUGO_VERSION}") |
| 85 | + return str(hugo_path) |
| 86 | + else: |
| 87 | + session.log("Cached Hugo version mismatch, re-downloading...") |
| 88 | + hugo_path.unlink() |
| 89 | + |
| 90 | + # Download Hugo |
| 91 | + download_url = get_hugo_download_url(HUGO_VERSION) |
| 92 | + session.log(f"Downloading Hugo {HUGO_VERSION} from {download_url}") |
| 93 | + |
| 94 | + download_path = bin_dir / "hugo.tar.gz" |
| 95 | + urllib.request.urlretrieve(download_url, download_path) |
| 96 | + |
| 97 | + # Extract Hugo binary |
| 98 | + session.log("Extracting Hugo binary...") |
| 99 | + with tarfile.open(download_path, "r:gz") as tar: |
| 100 | + # Extract only the hugo binary |
| 101 | + for member in tar.getmembers(): |
| 102 | + if member.name == "hugo": |
| 103 | + member.name = "hugo" |
| 104 | + tar.extract(member, path=bin_dir) |
| 105 | + break |
| 106 | + |
| 107 | + # Make executable |
| 108 | + hugo_path.chmod(0o755) |
| 109 | + |
| 110 | + # Clean up downloaded archive |
| 111 | + download_path.unlink() |
| 112 | + |
| 113 | + session.log(f"Hugo {HUGO_VERSION} installed successfully at {hugo_path}") |
| 114 | + return str(hugo_path) |
0 commit comments