|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Script for installing dependencies and building HTML documentation for Adapty SDK |
| 4 | +# This script uses Swift Package Manager to generate DocC documentation |
| 5 | + |
| 6 | +set -e # Exit on error |
| 7 | + |
| 8 | +# Colors for output |
| 9 | +RED='\033[0;31m' |
| 10 | +GREEN='\033[0;32m' |
| 11 | +YELLOW='\033[1;33m' |
| 12 | +NC='\033[0m' # No Color |
| 13 | + |
| 14 | +# Function to display informational messages |
| 15 | +info() { |
| 16 | + echo -e "${GREEN}[INFO]${NC} $1" |
| 17 | +} |
| 18 | + |
| 19 | +# Function to display warnings |
| 20 | +warn() { |
| 21 | + echo -e "${YELLOW}[WARN]${NC} $1" |
| 22 | +} |
| 23 | + |
| 24 | +# Function to display errors |
| 25 | +error() { |
| 26 | + echo -e "${RED}[ERROR]${NC} $1" |
| 27 | +} |
| 28 | + |
| 29 | +# Check for required tools |
| 30 | +check_requirements() { |
| 31 | + info "Checking for required tools..." |
| 32 | + |
| 33 | + # Check for Swift |
| 34 | + if ! command -v swift &> /dev/null; then |
| 35 | + error "swift not found. Please install Xcode Command Line Tools." |
| 36 | + exit 1 |
| 37 | + fi |
| 38 | + |
| 39 | + # Check Swift version (need 5.6+ for docc plugin) |
| 40 | + SWIFT_VERSION=$(swift --version | head -n 1 | awk '{print $4}') |
| 41 | + info "Swift version: $SWIFT_VERSION" |
| 42 | + |
| 43 | + info "All required tools are installed ✓" |
| 44 | +} |
| 45 | + |
| 46 | +# Get project root directory |
| 47 | +PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" |
| 48 | +info "Project root directory: $PROJECT_ROOT" |
| 49 | + |
| 50 | +# Output directory for documentation |
| 51 | +OUTPUT_DIR="${PROJECT_ROOT}/docs" |
| 52 | +info "Documentation directory: $OUTPUT_DIR" |
| 53 | + |
| 54 | +# Create documentation directory if it doesn't exist |
| 55 | +mkdir -p "$OUTPUT_DIR" |
| 56 | + |
| 57 | +# Function to build documentation for a module using manual docc compilation |
| 58 | +build_documentation() { |
| 59 | + local module_name=$1 |
| 60 | + info "Building documentation for module: $module_name" |
| 61 | + |
| 62 | + # Map module name to source directory |
| 63 | + local SOURCE_DIR="" |
| 64 | + case "$module_name" in |
| 65 | + "Adapty") |
| 66 | + SOURCE_DIR="${PROJECT_ROOT}/Sources" |
| 67 | + ;; |
| 68 | + "AdaptyUI") |
| 69 | + SOURCE_DIR="${PROJECT_ROOT}/Sources.AdaptyUI" |
| 70 | + ;; |
| 71 | + "AdaptyLogger") |
| 72 | + SOURCE_DIR="${PROJECT_ROOT}/Sources.Logger" |
| 73 | + ;; |
| 74 | + "AdaptyUIBuilder") |
| 75 | + SOURCE_DIR="${PROJECT_ROOT}/Sources.UIBuilder" |
| 76 | + ;; |
| 77 | + "AdaptyPlugin") |
| 78 | + SOURCE_DIR="${PROJECT_ROOT}/Sources.AdaptyPlugin" |
| 79 | + ;; |
| 80 | + *) |
| 81 | + error "Unknown module: $module_name" |
| 82 | + return 1 |
| 83 | + ;; |
| 84 | + esac |
| 85 | + |
| 86 | + # Check if source directory exists |
| 87 | + if [ ! -d "$SOURCE_DIR" ]; then |
| 88 | + error "Source directory not found: $SOURCE_DIR" |
| 89 | + return 1 |
| 90 | + fi |
| 91 | + |
| 92 | + info "Source directory: $SOURCE_DIR" |
| 93 | + |
| 94 | + # Build the module and generate symbol graph |
| 95 | + info "Building module and generating symbol graph..." |
| 96 | + |
| 97 | + # Create module-specific symbol graphs directory (keep existing graphs for incremental builds) |
| 98 | + local SYMBOL_GRAPH_DIR="${PROJECT_ROOT}/.build/symbol-graphs/${module_name}" |
| 99 | + mkdir -p "$SYMBOL_GRAPH_DIR" |
| 100 | + |
| 101 | + swift build --target "$module_name" \ |
| 102 | + -Xswiftc -emit-symbol-graph \ |
| 103 | + -Xswiftc -emit-symbol-graph-dir -Xswiftc "$SYMBOL_GRAPH_DIR" \ |
| 104 | + || { |
| 105 | + error "Failed to build $module_name and generate symbol graph" |
| 106 | + return 1 |
| 107 | + } |
| 108 | + |
| 109 | + # Fallback: incremental builds can skip compilation and not emit symbol graphs |
| 110 | + if ! compgen -G "$SYMBOL_GRAPH_DIR/*.symbols.json" > /dev/null; then |
| 111 | + warn "Symbol graphs were not generated. Forcing clean rebuild..." |
| 112 | + swift package clean |
| 113 | + rm -rf "$SYMBOL_GRAPH_DIR" |
| 114 | + mkdir -p "$SYMBOL_GRAPH_DIR" |
| 115 | + swift build --target "$module_name" \ |
| 116 | + -Xswiftc -emit-symbol-graph \ |
| 117 | + -Xswiftc -emit-symbol-graph-dir -Xswiftc "$SYMBOL_GRAPH_DIR" \ |
| 118 | + || { |
| 119 | + error "Failed to build $module_name and generate symbol graph" |
| 120 | + return 1 |
| 121 | + } |
| 122 | + fi |
| 123 | + |
| 124 | + info "Symbol graphs for $module_name and its dependencies generated ✓" |
| 125 | + |
| 126 | + # Build documentation with docc |
| 127 | + local ARCHIVE_PATH="${PROJECT_ROOT}/.build/${module_name}.doccarchive" |
| 128 | + mkdir -p "${PROJECT_ROOT}/.build" |
| 129 | + rm -rf "$ARCHIVE_PATH" |
| 130 | + |
| 131 | + info "Generating DocC archive..." |
| 132 | + xcrun docc convert "$SOURCE_DIR" \ |
| 133 | + --allow-arbitrary-catalog-directories \ |
| 134 | + --fallback-display-name "$module_name" \ |
| 135 | + --fallback-bundle-identifier "io.adapty.${module_name}" \ |
| 136 | + --fallback-bundle-version "1.0.0" \ |
| 137 | + --additional-symbol-graph-dir "$SYMBOL_GRAPH_DIR" \ |
| 138 | + --output-path "$ARCHIVE_PATH" \ |
| 139 | + || { |
| 140 | + error "Failed to generate documentation for $module_name" |
| 141 | + return 1 |
| 142 | + } |
| 143 | + |
| 144 | + info "Documentation archive created at $ARCHIVE_PATH ✓" |
| 145 | +} |
| 146 | + |
| 147 | +# Function to export documentation to HTML |
| 148 | +export_to_html() { |
| 149 | + local module_name=$1 |
| 150 | + local ARCHIVE_PATH="${PROJECT_ROOT}/.build/${module_name}.doccarchive" |
| 151 | + |
| 152 | + if [ ! -d "$ARCHIVE_PATH" ]; then |
| 153 | + error "Documentation archive for $module_name not found at $ARCHIVE_PATH, cannot export to HTML" |
| 154 | + return 1 |
| 155 | + fi |
| 156 | + |
| 157 | + info "Exporting $module_name documentation to HTML..." |
| 158 | + |
| 159 | + # Ensure OUTPUT_DIR is clean and exists |
| 160 | + rm -rf "$OUTPUT_DIR" |
| 161 | + mkdir -p "$OUTPUT_DIR" |
| 162 | + |
| 163 | + # Export to HTML using xcrun docc |
| 164 | + xcrun docc process-archive transform-for-static-hosting \ |
| 165 | + "$ARCHIVE_PATH" \ |
| 166 | + --output-path "$OUTPUT_DIR" \ |
| 167 | + || { |
| 168 | + error "Failed to export documentation for $module_name to HTML" |
| 169 | + return 1 |
| 170 | + } |
| 171 | + |
| 172 | + info "HTML documentation for $module_name created at $OUTPUT_DIR ✓" |
| 173 | +} |
| 174 | + |
| 175 | +# Main function |
| 176 | +main() { |
| 177 | + info "=== Starting Adapty SDK documentation build process ===" |
| 178 | + |
| 179 | + # Check requirements |
| 180 | + check_requirements |
| 181 | + |
| 182 | + # Navigate to project root directory |
| 183 | + cd "$PROJECT_ROOT" |
| 184 | + |
| 185 | + # We only build AdaptyUI (it now includes Adapty docs) |
| 186 | + local module="AdaptyUI" |
| 187 | + |
| 188 | + # 1. Build the archive |
| 189 | + build_documentation "$module" || exit 1 |
| 190 | + |
| 191 | + # 2. Export to HTML (Mandatory) |
| 192 | + export_to_html "$module" || exit 1 |
| 193 | + |
| 194 | + # 3. Use the landing template for the root index.html |
| 195 | + info "Setting up documentation hub (landing page)..." |
| 196 | + local template_path="${PROJECT_ROOT}/scripts/docs-generator/landing.html" |
| 197 | + if [ -f "$template_path" ]; then |
| 198 | + cp "$template_path" "${OUTPUT_DIR}/index.html" |
| 199 | + info "Documentation hub configured ✓" |
| 200 | + else |
| 201 | + warn "Landing template not found at $template_path, skipping..." |
| 202 | + fi |
| 203 | + |
| 204 | + info "=== Documentation build completed successfully! ===" |
| 205 | + info "Final documentation saved to: ${OUTPUT_DIR}" |
| 206 | + info "" |
| 207 | + info "To view the documentation:" |
| 208 | + info " 1. Start a local server: cd ${OUTPUT_DIR} && python3 -m http.server 8080" |
| 209 | + info " 2. Open: http://localhost:8080/" |
| 210 | +} |
| 211 | + |
| 212 | +# Run main function |
| 213 | +main "$@" |
0 commit comments