|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Copyright 2025 The Kubernetes Authors. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +set -e |
| 18 | + |
| 19 | +SCRIPT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 20 | +PROJECT_ROOT="$(cd "${SCRIPT_ROOT}/../.." && pwd)" |
| 21 | + |
| 22 | +# Default demo name |
| 23 | +DEMO_NAME="${1:-kb-demo}" |
| 24 | +DEMO_SCRIPT="${2:-${SCRIPT_ROOT}/run.sh}" |
| 25 | + |
| 26 | +# Colors for output |
| 27 | +RED='\033[0;31m' |
| 28 | +GREEN='\033[0;32m' |
| 29 | +YELLOW='\033[1;33m' |
| 30 | +NC='\033[0m' # No Color |
| 31 | + |
| 32 | +log() { |
| 33 | + echo -e "${GREEN}[INFO]${NC} $1" |
| 34 | +} |
| 35 | + |
| 36 | +warn() { |
| 37 | + echo -e "${YELLOW}[WARN]${NC} $1" |
| 38 | +} |
| 39 | + |
| 40 | +error() { |
| 41 | + echo -e "${RED}[ERROR]${NC} $1" |
| 42 | + exit 1 |
| 43 | +} |
| 44 | + |
| 45 | +usage() { |
| 46 | + cat << EOF |
| 47 | +Usage: $0 [DEMO_NAME] [DEMO_SCRIPT] |
| 48 | +
|
| 49 | +Generate an asciinema demo recording and convert it to SVG. |
| 50 | +
|
| 51 | +Arguments: |
| 52 | + DEMO_NAME Name of the demo (default: kb-demo) |
| 53 | + DEMO_SCRIPT Path to the demo script to run (default: ${SCRIPT_ROOT}/run.sh) |
| 54 | +
|
| 55 | +Examples: |
| 56 | + $0 # Generate default kb-demo |
| 57 | + $0 my-custom-demo # Generate custom demo with default script |
| 58 | + $0 advanced-demo ./my-demo.sh # Generate custom demo with custom script |
| 59 | +
|
| 60 | +EOF |
| 61 | + exit 0 |
| 62 | +} |
| 63 | + |
| 64 | +check_prerequisites() { |
| 65 | + log "Checking prerequisites..." |
| 66 | + |
| 67 | + # Check for help flag |
| 68 | + if [[ "$1" == "-h" || "$1" == "--help" ]]; then |
| 69 | + usage |
| 70 | + fi |
| 71 | + |
| 72 | + # Validate demo script exists |
| 73 | + if [[ ! -f "$DEMO_SCRIPT" ]]; then |
| 74 | + error "Demo script not found: $DEMO_SCRIPT" |
| 75 | + fi |
| 76 | + |
| 77 | + local missing_tools=() |
| 78 | + |
| 79 | + if ! command -v asciinema &> /dev/null; then |
| 80 | + missing_tools+=("asciinema") |
| 81 | + fi |
| 82 | + |
| 83 | + if ! command -v svg-term &> /dev/null; then |
| 84 | + missing_tools+=("svg-term") |
| 85 | + fi |
| 86 | + |
| 87 | + if ! command -v kind &> /dev/null; then |
| 88 | + missing_tools+=("kind") |
| 89 | + fi |
| 90 | + |
| 91 | + if ! command -v kubectl &> /dev/null; then |
| 92 | + missing_tools+=("kubectl") |
| 93 | + fi |
| 94 | + |
| 95 | + if [ ${#missing_tools[@]} -ne 0 ]; then |
| 96 | + error "Missing required tools:\n$(printf ' - %s\n' "${missing_tools[@]}")" |
| 97 | + fi |
| 98 | + |
| 99 | + log "All prerequisites are installed ✓" |
| 100 | +} |
| 101 | + |
| 102 | +setup_cluster() { |
| 103 | + log "Setting up Kind cluster for demo..." |
| 104 | + "${SCRIPT_ROOT}/setup-kind.sh" |
| 105 | + |
| 106 | + log "Verifying cluster connection..." |
| 107 | + kubectl cluster-info --context kind-kubebuilder-demo > /dev/null |
| 108 | + log "Cluster connection verified ✓" |
| 109 | +} |
| 110 | + |
| 111 | +record_demo() { |
| 112 | + local recording_dir="/tmp/kb-demo-recording" |
| 113 | + |
| 114 | + log "Cleaning up any previous recording files..." |
| 115 | + rm -rf "$recording_dir" |
| 116 | + mkdir -p "$recording_dir" |
| 117 | + |
| 118 | + log "Starting demo recording for '${DEMO_NAME}' in 3 seconds..." |
| 119 | + sleep 3 |
| 120 | + |
| 121 | + cd "$recording_dir" |
| 122 | + asciinema rec \ |
| 123 | + --command "$DEMO_SCRIPT" \ |
| 124 | + --env "DEMO_AUTO_RUN=1" \ |
| 125 | + --title "Kubebuilder Demo: ${DEMO_NAME}" \ |
| 126 | + --idle-time-limit 2 \ |
| 127 | + "${DEMO_NAME}.cast" |
| 128 | +} |
| 129 | + |
| 130 | +convert_to_svg() { |
| 131 | + local recording_dir="/tmp/kb-demo-recording" |
| 132 | + local version="$1" |
| 133 | + local svg_file="${PROJECT_ROOT}/docs/gif/${DEMO_NAME}.${version}.svg" |
| 134 | + |
| 135 | + log "Converting recording to SVG..." |
| 136 | + svg-term \ |
| 137 | + --in="${recording_dir}/${DEMO_NAME}.cast" \ |
| 138 | + --out="$svg_file" \ |
| 139 | + --window \ |
| 140 | + --width=120 \ |
| 141 | + --height=30 |
| 142 | + |
| 143 | + log "Demo updated! New file: docs/gif/${DEMO_NAME}.${version}.svg" |
| 144 | + return 0 |
| 145 | +} |
| 146 | + |
| 147 | +update_readme() { |
| 148 | + local version="$1" |
| 149 | + |
| 150 | + # Only update README for the default kb-demo |
| 151 | + if [[ "$DEMO_NAME" != "kb-demo" ]]; then |
| 152 | + log "Skipping README update for custom demo '${DEMO_NAME}'" |
| 153 | + return 0 |
| 154 | + fi |
| 155 | + |
| 156 | + log "Updating README.md with new demo..." |
| 157 | + if [[ "$OSTYPE" == "darwin"* ]]; then |
| 158 | + # macOS |
| 159 | + sed -i '' "s|docs/gif/kb-demo\.v[^)]*\.svg|docs/gif/kb-demo.${version}.svg|g" "${PROJECT_ROOT}/README.md" |
| 160 | + else |
| 161 | + # Linux |
| 162 | + sed -i "s|docs/gif/kb-demo\.v[^)]*\.svg|docs/gif/kb-demo.${version}.svg|g" "${PROJECT_ROOT}/README.md" |
| 163 | + fi |
| 164 | + |
| 165 | + log "README.md updated with new demo file ✓" |
| 166 | +} |
| 167 | + |
| 168 | +cleanup() { |
| 169 | + log "Cleaning up temporary files..." |
| 170 | + rm -rf /tmp/kb-demo-recording |
| 171 | + log "To clean up the demo cluster, run: make clean-demo" |
| 172 | +} |
| 173 | + |
| 174 | +main() { |
| 175 | + # Check for help flag first |
| 176 | + if [[ "$1" == "-h" || "$1" == "--help" ]]; then |
| 177 | + usage |
| 178 | + fi |
| 179 | + |
| 180 | + log "Starting Kubebuilder demo generation for '${DEMO_NAME}'..." |
| 181 | + log "Using demo script: ${DEMO_SCRIPT}" |
| 182 | + |
| 183 | + # Extract version once to avoid duplication |
| 184 | + local version |
| 185 | + version=$(git -C "$PROJECT_ROOT" describe --tags --abbrev=0 2>/dev/null || echo "v4.0.0") |
| 186 | + |
| 187 | + check_prerequisites |
| 188 | + setup_cluster |
| 189 | + record_demo |
| 190 | + convert_to_svg "$version" |
| 191 | + update_readme "$version" |
| 192 | + cleanup |
| 193 | + |
| 194 | + log "Demo generation completed successfully! 🎉" |
| 195 | + log "Generated: docs/gif/${DEMO_NAME}.${version}.svg" |
| 196 | +} |
| 197 | + |
| 198 | +main "$@" |
0 commit comments