Skip to content

Commit ccbd573

Browse files
committed
Update scripts
1 parent 72f6d53 commit ccbd573

File tree

13 files changed

+866
-102
lines changed

13 files changed

+866
-102
lines changed

scripts/docc-multiplatform.sh

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
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

scripts/git-default-branch.sh

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
#!/bin/bash
22

3-
# Exit immediately if a command exits with a non-zero status
4-
set -e
5-
63
# Function to display usage information
74
show_usage() {
85
echo
96
echo "This script outputs the default git branch name."
107

118
echo
129
echo "Usage: $0 [OPTIONS]"
10+
echo " -f, --fallback Optional fallback branch name if detection fails (default: main)"
1311
echo " -h, --help Show this help message"
14-
12+
1513
echo
1614
echo "Examples:"
1715
echo " $0"
16+
echo " $0 -f main"
17+
echo " $0 --fallback develop"
1818
echo
1919
}
2020

@@ -27,24 +27,42 @@ show_error_and_exit() {
2727
exit 1
2828
}
2929

30+
# Define argument variables
31+
FALLBACK="main"
32+
3033
# Parse command line arguments
3134
while [[ $# -gt 0 ]]; do
3235
case $1 in
36+
-f|--fallback)
37+
shift
38+
if [[ $# -eq 0 || "$1" =~ ^- ]]; then
39+
show_error_and_exit "--fallback requires a branch name"
40+
fi
41+
FALLBACK="$1"
42+
shift
43+
;;
3344
-h|--help)
3445
show_usage; exit 0 ;;
3546
-*)
3647
show_error_and_exit "Unknown option $1" ;;
3748
*)
3849
show_error_and_exit "Unexpected argument '$1'" ;;
3950
esac
40-
shift
4151
done
4252

43-
# Get the default git branch name
44-
if ! BRANCH=$(git symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@'); then
45-
echo "Failed to get default git branch"
46-
exit 1
53+
# Try to get the default git branch name from symbolic ref
54+
BRANCH=$(git symbolic-ref refs/remotes/origin/HEAD 2>/dev/null | sed 's@^refs/remotes/origin/@@' || echo "")
55+
if [ -n "$BRANCH" ]; then
56+
echo $BRANCH
57+
exit 0
58+
fi
59+
60+
# If that fails, query the remote for the default branch
61+
BRANCH=$(git remote show origin 2>/dev/null | grep 'HEAD branch' | cut -d' ' -f5 || echo "")
62+
if [ -n "$BRANCH" ]; then
63+
echo $BRANCH
64+
exit 0
4765
fi
4866

49-
# Output the branch name
50-
echo $BRANCH
67+
# If all methods fail, use fallback
68+
echo $FALLBACK

scripts/l10n-gen.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ get_absolute_path() {
5151
echo "$path"
5252
else
5353
# Make it absolute relative to current directory
54-
echo "$(cd "$(dirname "$path")" 2>/dev/null && pwd)/$(basename "$path")"
54+
echo "$(pwd)/$path"
5555
fi
5656
}
5757

@@ -181,4 +181,4 @@ echo "Running: swift run l10n-gen $ARGS"
181181

182182
# Complete successfully
183183
echo "Code generation completed successfully!"
184-
echo
184+
echo

scripts/package-platforms.sh

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#!/bin/bash
2+
3+
# Exit immediately if a command exits with a non-zero status
4+
set -e
5+
6+
# Default values
7+
MAP_VISIONOS=false
8+
9+
# Function to display usage information
10+
show_usage() {
11+
echo
12+
echo "This script finds all supported platforms in Package.swift."
13+
14+
echo
15+
echo "Usage: $0 [OPTIONS]"
16+
echo " -h, --help Show this help message"
17+
echo " -xros, --map-visionos-to-xros Map visionOS to xrOS in output"
18+
19+
echo
20+
echo "Examples:"
21+
echo " $0"
22+
echo " $0 --map-visionos-to-xros"
23+
echo
24+
}
25+
26+
# Function to display error message, show usage, and exit
27+
show_error_and_exit() {
28+
echo
29+
local error_message="$1"
30+
echo "Error: $error_message"
31+
show_usage
32+
exit 1
33+
}
34+
35+
# Parse command line arguments
36+
while [[ $# -gt 0 ]]; do
37+
case $1 in
38+
-h|--help)
39+
show_usage; exit 0 ;;
40+
-xros|--map-visionos-to-xros)
41+
MAP_VISIONOS=true ;;
42+
-*)
43+
show_error_and_exit "Unknown option $1" ;;
44+
*)
45+
show_error_and_exit "Unexpected argument '$1'" ;;
46+
esac
47+
shift
48+
done
49+
50+
# Check that a Package.swift file exists
51+
if [ ! -f "Package.swift" ]; then
52+
show_error_and_exit "Package.swift not found in current directory"
53+
fi
54+
55+
# Extract platforms array and parse platform names
56+
# Matches patterns like: .iOS(.v15), .macOS(.v12), .tvOS(.v15), etc.
57+
if [ "$MAP_VISIONOS" = true ]; then
58+
if ! platforms=$(grep -A 20 "platforms:" "Package.swift" | \
59+
grep -oE '\.(iOS|macOS|tvOS|watchOS|visionOS|macCatalyst)' | \
60+
sed 's/^\.//' | \
61+
sed 's/visionOS/xrOS/' | \
62+
sort -u); then
63+
show_error_and_exit "Could not extract platforms from Package.swift"
64+
fi
65+
else
66+
if ! platforms=$(grep -A 20 "platforms:" "Package.swift" | \
67+
grep -oE '\.(iOS|macOS|tvOS|watchOS|visionOS|macCatalyst)' | \
68+
sed 's/^\.//' | \
69+
sort -u); then
70+
show_error_and_exit "Could not extract platforms from Package.swift"
71+
fi
72+
fi
73+
74+
if [ -z "$platforms" ]; then
75+
show_error_and_exit "No platforms found in Package.swift"
76+
fi
77+
78+
# Output the platforms, one per line
79+
echo "$platforms"

0 commit comments

Comments
 (0)