-
-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathdocc.sh
executable file
·112 lines (93 loc) · 3.08 KB
/
docc.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/bin/bash
# Documentation:
# This script builds DocC for a <TARGET> and certain <PLATFORMS>.
# This script targets iOS, macOS, tvOS, watchOS, and xrOS by default.
# You can pass in a list of <PLATFORMS> if you want to customize the build.
# The documentation ends up in to .build/docs-<PLATFORM>.
# Usage:
# docc.sh <TARGET> [<PLATFORMS> default:iOS macOS tvOS watchOS xrOS]
# e.g. `bash scripts/docc.sh MyTarget iOS macOS`
# Exit immediately if a command exits with a non-zero status
set -e
# Fail if any command in a pipeline fails
set -o pipefail
# Verify that all required arguments are provided
if [ $# -eq 0 ]; then
echo "Error: This script requires at least one argument"
echo "Usage: $0 <TARGET> [<PLATFORMS> default:iOS macOS tvOS watchOS xrOS]"
echo "For instance: $0 MyTarget iOS macOS"
exit 1
fi
# Define argument variables
TARGET=$1
TARGET_LOWERCASED=$(echo "$1" | tr '[:upper:]' '[:lower:]')
# Remove TARGET from arguments list
shift
# Define platforms variable
if [ $# -eq 0 ]; then
set -- iOS macOS tvOS watchOS xrOS
fi
PLATFORMS=$@
# Prepare the package for DocC
swift package resolve;
# A function that builds $TARGET for a specific platform
build_platform() {
# Define a local $PLATFORM variable and set an exit code
local PLATFORM=$1
local EXIT_CODE=0
# Define the build folder name, based on the $PLATFORM
case $PLATFORM in
"iOS")
DEBUG_PATH="Debug-iphoneos"
;;
"macOS")
DEBUG_PATH="Debug"
;;
"tvOS")
DEBUG_PATH="Debug-appletvos"
;;
"watchOS")
DEBUG_PATH="Debug-watchos"
;;
"xrOS")
DEBUG_PATH="Debug-xros"
;;
*)
echo "Error: Unsupported platform '$PLATFORM'"
exit 1
;;
esac
# Build $TARGET docs for the $PLATFORM
echo "Building $TARGET docs for $PLATFORM..."
if ! xcodebuild docbuild -scheme $TARGET -derivedDataPath .build/docbuild -destination "generic/platform=$PLATFORM"; then
echo "Error: Failed to build documentation for $PLATFORM" >&2
return 1
fi
# Transform docs for static hosting
if ! $(xcrun --find docc) process-archive \
transform-for-static-hosting .build/docbuild/Build/Products/$DEBUG_PATH/$TARGET.doccarchive \
--output-path .build/docs-$PLATFORM \
--hosting-base-path "$TARGET"; then
echo "Error: Failed to transform documentation for $PLATFORM" >&2
return 1
fi
# Inject a root redirect script on the root page
echo "<script>window.location.href += \"/documentation/$TARGET_LOWERCASED\"</script>" > .build/docs-$PLATFORM/index.html;
# Complete successfully
echo "Successfully built $TARGET docs for $PLATFORM"
return 0
}
# Start script
echo ""
echo "Building $TARGET docs for [$PLATFORMS]..."
echo ""
# Loop through all platforms and call the build function
for PLATFORM in $PLATFORMS; do
if ! build_platform "$PLATFORM"; then
exit 1
fi
done
# Complete successfully
echo ""
echo "Building $TARGET docs completed successfully!"
echo ""