davideisinger.com

My personal website
Log | Files | Refs | README

upgrade (1302B)


      1 #!/usr/bin/env bash
      2 set -euo pipefail
      3 
      4 root_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
      5 build_file="$root_dir/.build.yml"
      6 
      7 if ! command -v hugo >/dev/null 2>&1; then
      8   echo "hugo not found in PATH" >&2
      9   exit 1
     10 fi
     11 
     12 hugo_version_raw="$(hugo version)"
     13 hugo_version="$(printf '%s\n' "$hugo_version_raw" | sed -E 's/^hugo v?([0-9]+\.[0-9]+\.[0-9]+).*/\1/')"
     14 if [[ -z "$hugo_version" || "$hugo_version" == "$hugo_version_raw" ]]; then
     15   echo "Unable to parse hugo version from: $hugo_version_raw" >&2
     16   exit 1
     17 fi
     18 
     19 current_version="$(rg -n "HUGO_VERSION:" "$build_file" | sed -E 's/.*HUGO_VERSION:[[:space:]]*//')"
     20 if [[ -z "$current_version" ]]; then
     21   echo "Unable to read HUGO_VERSION from $build_file" >&2
     22   exit 1
     23 fi
     24 
     25 if [[ "$hugo_version" == "$current_version" ]]; then
     26   echo "Hugo already at $hugo_version"
     27   exit 0
     28 fi
     29 
     30 python3 - <<PY
     31 from pathlib import Path
     32 
     33 path = Path("$build_file")
     34 data = path.read_text()
     35 old = "$current_version"
     36 new = "$hugo_version"
     37 needle = f"HUGO_VERSION: {old}"
     38 if needle not in data:
     39     raise SystemExit("Expected HUGO_VERSION not found in .build.yml")
     40 path.write_text(data.replace(needle, f"HUGO_VERSION: {new}", 1))
     41 PY
     42 
     43 git add "$build_file"
     44 git commit -m "Upgrade Hugo to $hugo_version"
     45 
     46 echo "Updated HUGO_VERSION from $current_version to $hugo_version"