|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Exit immediately if a command exits with a non-zero status |
| 4 | +set -e |
| 5 | + |
| 6 | +# Fail if any command in a pipeline fails |
| 7 | +set -o pipefail |
| 8 | + |
| 9 | +# Function to display usage information |
| 10 | +show_usage() { |
| 11 | + echo |
| 12 | + echo "This script builds DocC for a <TARGET> and certain <PLATFORMS>." |
| 13 | + echo "Unlike the 'docc' script, this scripts combines DocC for all platform into a single output." |
| 14 | + |
| 15 | + echo |
| 16 | + echo "Usage: $0 [TARGET] [-p|--platforms <PLATFORM1> <PLATFORM2> ...] [--hosting-base-path <PATH>]" |
| 17 | + echo " [TARGET] Optional. The target to build documentation for (defaults to package name)" |
| 18 | + echo " -p, --platforms Optional. List of platforms (default: iOS macOS tvOS watchOS xrOS)" |
| 19 | + echo " --hosting-base-path Optional. Base path for static hosting (default: TARGET name, use empty string \"\" for root)" |
| 20 | + |
| 21 | + echo |
| 22 | + echo "The web transformed documentation ends up in .build/docc." |
| 23 | + |
| 24 | + echo |
| 25 | + echo "Examples:" |
| 26 | + echo " $0" |
| 27 | + echo " $0 MyTarget" |
| 28 | + echo " $0 -p iOS macOS" |
| 29 | + echo " $0 MyTarget -p iOS macOS" |
| 30 | + echo " $0 MyTarget --platforms iOS macOS tvOS watchOS xrOS" |
| 31 | + echo " $0 MyTarget --hosting-base-path \"\"" |
| 32 | + echo " $0 MyTarget --hosting-base-path \"custom/path\"" |
| 33 | + echo |
| 34 | +} |
| 35 | + |
| 36 | +# Function to display error message, show usage, and exit |
| 37 | +show_error_and_exit() { |
| 38 | + echo |
| 39 | + local error_message="$1" |
| 40 | + echo "Error: $error_message" |
| 41 | + show_usage |
| 42 | + exit 1 |
| 43 | +} |
| 44 | + |
| 45 | +# Define argument variables |
| 46 | +TARGET="" |
| 47 | +PLATFORMS="iOS macOS tvOS watchOS xrOS" # Default platforms |
| 48 | +HOSTING_BASE_PATH="" # Will be set to TARGET if not specified |
| 49 | + |
| 50 | +# Define paths |
| 51 | +SYMBOL_GRAPHS_DIR=".build/symbol-graphs" |
| 52 | +DOCC_OUTPUT_DIR=".build/docc" |
| 53 | + |
| 54 | +# Parse command line arguments |
| 55 | +while [[ $# -gt 0 ]]; do |
| 56 | + case $1 in |
| 57 | + -p|--platforms) |
| 58 | + shift # Remove --platforms from arguments |
| 59 | + PLATFORMS="" # Clear default platforms |
| 60 | + |
| 61 | + # Collect all platform arguments until we hit another flag or run out of args |
| 62 | + while [[ $# -gt 0 && ! "$1" =~ ^- ]]; do |
| 63 | + PLATFORMS="$PLATFORMS $1" |
| 64 | + shift |
| 65 | + done |
| 66 | + |
| 67 | + # Remove leading space and check if we got any platforms |
| 68 | + PLATFORMS=$(echo "$PLATFORMS" | sed 's/^ *//') |
| 69 | + if [ -z "$PLATFORMS" ]; then |
| 70 | + show_error_and_exit "--platforms requires at least one platform" |
| 71 | + fi |
| 72 | + ;; |
| 73 | + --hosting-base-path) |
| 74 | + shift # Remove --hosting-base-path from arguments |
| 75 | + if [[ $# -eq 0 ]]; then |
| 76 | + show_error_and_exit "--hosting-base-path requires a value (use \"\" for empty path)" |
| 77 | + fi |
| 78 | + HOSTING_BASE_PATH="$1" |
| 79 | + shift |
| 80 | + ;; |
| 81 | + -h|--help) |
| 82 | + show_usage; exit 0 ;; |
| 83 | + -*) |
| 84 | + show_error_and_exit "Unknown option $1" ;; |
| 85 | + *) |
| 86 | + if [ -z "$TARGET" ]; then |
| 87 | + TARGET="$1" |
| 88 | + else |
| 89 | + show_error_and_exit "Unexpected argument '$1'" |
| 90 | + fi |
| 91 | + shift |
| 92 | + ;; |
| 93 | + esac |
| 94 | +done |
| 95 | + |
| 96 | +# If no TARGET was provided, try to get package name |
| 97 | +if [ -z "$TARGET" ]; then |
| 98 | + FOLDER="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )" |
| 99 | + SCRIPT_PACKAGE_NAME="$FOLDER/package-name.sh" |
| 100 | + |
| 101 | + if [ ! -f "$SCRIPT_PACKAGE_NAME" ]; then |
| 102 | + show_error_and_exit "Script not found: $SCRIPT_PACKAGE_NAME" |
| 103 | + fi |
| 104 | + |
| 105 | + if ! TARGET=$("$SCRIPT_PACKAGE_NAME"); then |
| 106 | + show_error_and_exit "Failed to get package name" |
| 107 | + fi |
| 108 | +fi |
| 109 | + |
| 110 | +# Set default hosting base path if not specified |
| 111 | +if [ -z "$HOSTING_BASE_PATH" ]; then |
| 112 | + HOSTING_BASE_PATH="$TARGET" |
| 113 | +fi |
| 114 | + |
| 115 | +# Define target lowercase for redirect script |
| 116 | +TARGET_LOWERCASED=$(echo "$TARGET" | tr '[:upper:]' '[:lower:]') |
| 117 | + |
| 118 | +# Prepare the package for DocC |
| 119 | +swift package resolve; |
| 120 | + |
| 121 | +# A function that builds $TARGET documentation for a specific platform |
| 122 | +build_platform() { |
| 123 | + |
| 124 | + # Define a local $PLATFORM variable |
| 125 | + local PLATFORM=$1 |
| 126 | + |
| 127 | + # Define the build folder name, based on the $PLATFORM |
| 128 | + case $PLATFORM in |
| 129 | + "iOS") |
| 130 | + DEBUG_PATH="Debug-iphoneos" |
| 131 | + ;; |
| 132 | + "macOS") |
| 133 | + DEBUG_PATH="Debug" |
| 134 | + ;; |
| 135 | + "tvOS") |
| 136 | + DEBUG_PATH="Debug-appletvos" |
| 137 | + ;; |
| 138 | + "watchOS") |
| 139 | + DEBUG_PATH="Debug-watchos" |
| 140 | + ;; |
| 141 | + "xrOS") |
| 142 | + DEBUG_PATH="Debug-xros" |
| 143 | + ;; |
| 144 | + *) |
| 145 | + echo "Error: Unsupported platform '$PLATFORM'" |
| 146 | + return 1 |
| 147 | + ;; |
| 148 | + esac |
| 149 | + |
| 150 | + # Build $TARGET docs for the $PLATFORM |
| 151 | + echo "Building $TARGET docs for $PLATFORM..." |
| 152 | + if ! xcodebuild \ |
| 153 | + -scheme $TARGET \ |
| 154 | + -derivedDataPath .build/.deriveddata \ |
| 155 | + -destination "generic/platform=$PLATFORM" \ |
| 156 | + OTHER_SWIFT_FLAGS="-emit-symbol-graph -emit-symbol-graph-dir $SYMBOL_GRAPHS_DIR/$PLATFORM"; then |
| 157 | + echo "Failed to build documentation for $PLATFORM" |
| 158 | + return 1 |
| 159 | + fi |
| 160 | +} |
| 161 | + |
| 162 | +# Start script |
| 163 | +echo |
| 164 | +echo "Building $TARGET docs for [$PLATFORMS]..." |
| 165 | +if [ -n "$HOSTING_BASE_PATH" ]; then |
| 166 | + echo "Hosting base path: '$HOSTING_BASE_PATH'" |
| 167 | +else |
| 168 | + echo "Hosting base path: (empty - root level)" |
| 169 | +fi |
| 170 | + |
| 171 | +# Loop through all platforms and call the build function |
| 172 | +for PLATFORM in $PLATFORMS; do |
| 173 | + if ! build_platform "$PLATFORM"; then |
| 174 | + exit 1 |
| 175 | + fi |
| 176 | +done |
| 177 | + |
| 178 | +# Create a .doccarchive from the symbols. |
| 179 | +$(xcrun --find docc) convert "Sources/$TARGET/$TARGET.docc" \ |
| 180 | + --emit-lmdb-index \ |
| 181 | + --fallback-display-name "$TARGET" \ |
| 182 | + --fallback-bundle-identifier "$TARGET" \ |
| 183 | + --fallback-bundle-version 0 \ |
| 184 | + --output-dir "$DOCC_OUTPUT_DIR" \ |
| 185 | + --additional-symbol-graph-dir "$SYMBOL_GRAPHS_DIR" \ |
| 186 | + --hosting-base-path "$HOSTING_BASE_PATH" |
| 187 | + |
| 188 | +# Inject a root redirect script on the root page |
| 189 | +echo "<script>window.location.href += \"/documentation/$TARGET_LOWERCASED\"</script>" > $DOCC_OUTPUT_DIR/index.html; |
| 190 | + |
| 191 | +# Complete successfully |
| 192 | +echo |
| 193 | +echo "Building $TARGET docs completed successfully!" |
| 194 | +echo |
0 commit comments