|
| 1 | +#!/bin/bash |
| 2 | +set -e |
| 3 | + |
| 4 | +# Generates versioned documentation links and commits to gh-pages branch |
| 5 | +# |
| 6 | +# PURPOSE: |
| 7 | +# This script generates a landing page with links to API documentation on |
| 8 | +# RubyDoc.info for a specific version tag. This script is invoked by the |
| 9 | +# publish-gh-pages job in the GitHub Actions workflow |
| 10 | +# (.github/workflows/release.yml) when a release is published. |
| 11 | +# |
| 12 | +# HOW IT WORKS: |
| 13 | +# - Creates isolated git worktrees for the specified tag and gh-pages branch |
| 14 | +# - Copies static Jekyll template files from docs/ |
| 15 | +# - Generates _data/versions.yml with list of versions |
| 16 | +# - Commits changes to gh-pages (does not push automatically) |
| 17 | +# |
| 18 | +# WORKFLOW: |
| 19 | +# 1. Run this script with a tag name: `generate-gh-pages.sh v1.2.3` |
| 20 | +# 2. Script generates docs and commits to local gh-pages branch |
| 21 | +# 3. Push gh-pages branch to deploy: `git push origin gh-pages` |
| 22 | + |
| 23 | +# Parse semantic version from tag name (ignoring arbitrary prefixes) |
| 24 | +if [[ "${1}" =~ ([0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z0-9.-]+)?)$ ]]; then |
| 25 | + VERSION="v${BASH_REMATCH[1]}" |
| 26 | +else |
| 27 | + echo "Error: Must specify a tag name that contains a valid semantic version" |
| 28 | + echo "Usage: ${0} <tag-name>" |
| 29 | + echo "Examples:" |
| 30 | + echo " ${0} 1.2.3" |
| 31 | + echo " ${0} v2.0.0-rc.1" |
| 32 | + exit 1 |
| 33 | +fi |
| 34 | + |
| 35 | +TAG_NAME="${1}" |
| 36 | +REPO_ROOT="$(git rev-parse --show-toplevel)" |
| 37 | + |
| 38 | +echo "Generating documentation for tag: ${TAG_NAME}" |
| 39 | + |
| 40 | +# Create temporary directories for both worktrees |
| 41 | +WORKTREE_DIR=$(mktemp -d) |
| 42 | +GHPAGES_WORKTREE_DIR=$(mktemp -d) |
| 43 | + |
| 44 | +# Set up trap to clean up both worktrees on exit |
| 45 | +trap 'git worktree remove --force "${WORKTREE_DIR}" 2>/dev/null || true; \ |
| 46 | + git worktree remove --force "${GHPAGES_WORKTREE_DIR}" 2>/dev/null || true' EXIT |
| 47 | + |
| 48 | +echo "Creating worktree for ${TAG_NAME}..." |
| 49 | +git worktree add --quiet "${WORKTREE_DIR}" "${TAG_NAME}" |
| 50 | + |
| 51 | +# Check if gh-pages branch exists |
| 52 | +if git show-ref --verify --quiet refs/heads/gh-pages; then |
| 53 | + echo "Creating worktree for existing gh-pages branch..." |
| 54 | + git worktree add --quiet "${GHPAGES_WORKTREE_DIR}" gh-pages |
| 55 | +elif git ls-remote --exit-code --heads origin gh-pages > /dev/null 2>&1; then |
| 56 | + echo "Creating worktree for gh-pages branch from remote..." |
| 57 | + git worktree add --quiet "${GHPAGES_WORKTREE_DIR}" -b gh-pages origin/gh-pages |
| 58 | +else |
| 59 | + echo "Creating worktree for new orphan gh-pages branch..." |
| 60 | + git worktree add --quiet --detach "${GHPAGES_WORKTREE_DIR}" |
| 61 | + git -C "${GHPAGES_WORKTREE_DIR}" checkout --orphan gh-pages |
| 62 | + git -C "${GHPAGES_WORKTREE_DIR}" rm -rf . > /dev/null 2>&1 || true |
| 63 | +fi |
| 64 | + |
| 65 | +# Change to gh-pages worktree |
| 66 | +cd "${GHPAGES_WORKTREE_DIR}" |
| 67 | + |
| 68 | +# Determine if this tag is the latest version |
| 69 | +echo "Determining if ${VERSION} is the latest version..." |
| 70 | + |
| 71 | +# Get all existing version tags from the repository (reverse sorted, newest first) |
| 72 | +ALL_VERSIONS=$( |
| 73 | + git -C "${REPO_ROOT}" tag --list | \ |
| 74 | + sed -nE 's/^[^0-9]*([0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z0-9.-]+)?)$/v\1/p' | \ |
| 75 | + sort -Vr |
| 76 | +) |
| 77 | + |
| 78 | +# Get the latest version from all version tags |
| 79 | +LATEST_VERSION=$(echo "${ALL_VERSIONS}" | head -n 1) |
| 80 | + |
| 81 | +if [ "${VERSION}" = "${LATEST_VERSION}" ]; then |
| 82 | + echo "${VERSION} is the latest version" |
| 83 | +else |
| 84 | + echo "${VERSION} is not the latest version (latest is ${LATEST_VERSION})" |
| 85 | +fi |
| 86 | + |
| 87 | +# Update custom documentation for latest version |
| 88 | +if [ "${VERSION}" = "${LATEST_VERSION}" ]; then |
| 89 | + echo "Updating custom documentation..." |
| 90 | + |
| 91 | + # Clean up old custom docs from gh-pages root |
| 92 | + echo "Cleaning gh-pages root..." |
| 93 | + git ls-tree --name-only HEAD | xargs -r git rm -rf |
| 94 | + |
| 95 | + # Copy custom docs from docs/ directory |
| 96 | + echo "Copying custom docs from ${WORKTREE_DIR}/docs/..." |
| 97 | + cp -r "${WORKTREE_DIR}/docs/." "${GHPAGES_WORKTREE_DIR}/" |
| 98 | +fi |
| 99 | + |
| 100 | +# Generate version data for Jekyll |
| 101 | +echo "Generating _data/versions.yml..." |
| 102 | +mkdir -p _data |
| 103 | +echo "${ALL_VERSIONS}" | sed 's/^v/- /' > _data/versions.yml |
| 104 | + |
| 105 | +# Stage all changes |
| 106 | +git add . |
| 107 | + |
| 108 | +# Commit if there are changes |
| 109 | +if git diff --staged --quiet; then |
| 110 | + echo "No changes to commit" |
| 111 | +else |
| 112 | + echo "Committing documentation for ${VERSION}..." |
| 113 | + git commit -m "Add ${VERSION} docs" |
| 114 | + |
| 115 | + echo "Documentation committed to gh-pages branch!" |
| 116 | + echo "Push to remote to deploy to GitHub Pages" |
| 117 | +fi |
| 118 | + |
| 119 | +echo "Done!" |
0 commit comments