aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/deploy-docs.sh
blob: c59d0a0e310e5458ec6807336fd8fda08aa477fa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env bash
set -eou pipefail

source "$(dirname "${BASH_SOURCE[0]}")/lib/log.sh"
source "$(dirname "${BASH_SOURCE[0]}")/lib/path.sh"

readonly SITE="bolt.eugenetriguba.com"
readonly BUILD_DIR="${PATH_PROJECT_DIR}/site"
readonly VENV_DIR="${PATH_PROJECT_DIR}/.venv-docs"
readonly DOCS_REQUIREMENTS_FILE="${PATH_PROJECT_DIR}/docs/requirements-docs.txt"

usage() {
  echo "Usage: $0"
  echo ""
  echo "Build and deploy versioned documentation to SourceHut Pages"
  echo ""
  echo "Environment variables:"
  echo "  VERSION    - The version to deploy (default: 'latest' for main branch)"
  echo ""
  echo "Examples:"
  echo "  $0                # Deploy as 'latest'"
  echo "  VERSION=v1.0.0 $0 # Deploy as 'v1.0.0'"
}

main() {
  if [[ "${1:-}" == "-h" || "${1:-}" == "--help" ]]; then
    usage
    exit 0
  fi

  log_header "Deploying Documentation"

  local version="${VERSION:-latest}"
  log_info "Deploying version: $version"

  check_and_setup_docs_env
  fetch_existing_site
  build_version "$version"
  update_versions_manifest
  package_site
  deploy_to_pages "$version"
}

check_and_setup_docs_env() {
  log_step "Checking documentation dependencies"

  if command -v mkdocs >/dev/null && python3 -m pip list | grep -q "mkdocs-material"; then
    log_success "MkDocs dependencies found in system"
    return 0
  fi

  log_info "MkDocs dependencies not found, setting up virtual environment"
  if [[ ! -d "$VENV_DIR" ]]; then
    log_step "Creating virtual environment"
    python3 -m venv "$VENV_DIR"
    log_success "Virtual environment created"
  fi

  log_step "Activating virtual environment"
  # shellcheck disable=SC1091
  source "$VENV_DIR/bin/activate"
  log_step "Installing documentation dependencies"
  pip install --requirement "${DOCS_REQUIREMENTS_FILE}"
  log_success "Dependencies installed"
}

fetch_existing_site() {
  log_step "Fetching existing site"
  mkdir -p "$BUILD_DIR"

  if hut pages download "$SITE" 2>/dev/null | tar -xzf - -C "$BUILD_DIR" 2>/dev/null; then
    log_success "Existing site fetched"
  else
    log_info "No existing site found, starting fresh"
  fi
}

build_version() {
  local version="$1"
  log_step "Building documentation for version: $version"

  mkdocs build -d "$BUILD_DIR/$version"

  # If latest, also copy to root for default view
  if [[ "$version" == "latest" ]]; then
    log_step "Copying latest to root"
    rsync -a --delete "$BUILD_DIR/latest/" "$BUILD_DIR/" \
      --exclude="versions.json" \
      --exclude="v*" \
      --exclude="latest"
  fi

  log_success "Documentation built for $version"
}

update_versions_manifest() {
  log_step "Updating versions manifest"

  local versions_json="[]"
  if [[ -d "$BUILD_DIR/latest" ]]; then
    versions_json=$(echo "$versions_json" | jq '. += [{"version": "latest", "title": "Latest (Development)"}]')
  fi

  # Add all tagged versions (sorted in reverse)
  if compgen -G "$BUILD_DIR/v*" >/dev/null; then
    for dir in $(find "$BUILD_DIR" -maxdepth 1 -type d -name "v*" | sort -Vr); do
      local ver
      ver=$(basename "$dir")
      versions_json=$(echo "$versions_json" | jq --arg v "$ver" '. += [{"version": $v, "title": $v}]')
    done
  fi

  jq -n --argjson versions "$versions_json" '{"versions": $versions}' >"$BUILD_DIR/versions.json"

  local version_count
  version_count=$(echo "$versions_json" | jq 'length')
  log_success "Versions manifest updated with $version_count version(s)"
}

package_site() {
  log_step "Creating tarball"
  tar -C "$BUILD_DIR" -czf site.tar.gz .
  log_success "Site packaged as site.tar.gz"
}

deploy_to_pages() {
  local version="$1"
  log_step "Deploying to SourceHut Pages: $SITE"
  hut pages publish -d "$SITE" site.tar.gz
  log_success "Documentation deployed to https://$SITE"
  log_info "Version URL: https://$SITE/$version/"
}

main "$@"