11#! /bin/bash
22
33# Documentation:
4- # This script builds DocC for a <TARGET> for all provided <PLATFORMS>.
4+ # This script builds DocC for a <TARGET> and certain <PLATFORMS>.
5+ # This script targets iOS, macOS, tvOS, watchOS, and xrOS by default.
6+ # You can pass in a list of <PLATFORMS> if you want to customize the build.
57# The documentation ends up in to .build/docs-<PLATFORM>.
68
79# Usage:
1113# Exit immediately if a command exits with a non-zero status
1214set -e
1315
16+ # Fail if any command in a pipeline fails
17+ set -o pipefail
18+
1419# Verify that all required arguments are provided
1520if [ $# -eq 0 ]; then
1621 echo " Error: This script requires at least one argument"
@@ -38,8 +43,9 @@ swift package resolve;
3843# A function that builds $TARGET for a specific platform
3944build_platform () {
4045
41- # Define a local $PLATFORM variable
46+ # Define a local $PLATFORM variable and set an exit code
4247 local PLATFORM=$1
48+ local EXIT_CODE=0
4349
4450 # Define the build folder name, based on the $PLATFORM
4551 case $PLATFORM in
@@ -66,19 +72,26 @@ build_platform() {
6672
6773 # Build $TARGET docs for the $PLATFORM
6874 echo " Building $TARGET docs for $PLATFORM ..."
69- xcodebuild docbuild -scheme $TARGET -derivedDataPath .build/docbuild -destination ' generic/platform=' $PLATFORM ;
75+ if ! xcodebuild docbuild -scheme $TARGET -derivedDataPath .build/docbuild -destination " generic/platform=$PLATFORM " ; then
76+ echo " Error: Failed to build documentation for $PLATFORM " >&2
77+ return 1
78+ fi
7079
7180 # Transform docs for static hosting
72- $( xcrun --find docc) process-archive \
81+ if ! $( xcrun --find docc) process-archive \
7382 transform-for-static-hosting .build/docbuild/Build/Products/$DEBUG_PATH /$TARGET .doccarchive \
7483 --output-path .build/docs-$PLATFORM \
75- --hosting-base-path " $TARGET " ;
84+ --hosting-base-path " $TARGET " ; then
85+ echo " Error: Failed to transform documentation for $PLATFORM " >&2
86+ return 1
87+ fi
7688
7789 # Inject a root redirect script on the root page
7890 echo " <script>window.location.href += \" /documentation/$TARGET_LOWERCASED \" </script>" > .build/docs-$PLATFORM /index.html;
7991
8092 # Complete successfully
8193 echo " Successfully built $TARGET docs for $PLATFORM "
94+ return 0
8295}
8396
8497# Start script
0 commit comments