11#! /bin/bash
22
3- # Documentation:
4- # This script builds a <TARGET> for all provided <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.
7-
8- # Usage:
9- # build.sh <TARGET> [<PLATFORMS> default:iOS macOS tvOS watchOS xrOS]
10- # e.g. `bash scripts/build.sh MyTarget iOS macOS`
11-
123# Exit immediately if a command exits with a non-zero status
134set -e
145
15- # Verify that all required arguments are provided
16- if [ $# -eq 0 ]; then
17- echo " Error: This script requires at least one argument"
18- echo " Usage: $0 <TARGET> [<PLATFORMS> default:iOS macOS tvOS watchOS xrOS]"
19- echo " For instance: $0 MyTarget iOS macOS"
6+ # Function to display usage information
7+ show_usage () {
8+ echo
9+ echo " This script builds a <TARGET> for all provided <PLATFORMS>."
10+
11+ echo
12+ echo " Usage: $0 [TARGET] [-p|--platforms <PLATFORM1> <PLATFORM2> ...]"
13+ echo " [TARGET] Optional. The target to build (defaults to package name)"
14+ echo " -p, --platforms Optional. List of platforms (default: iOS macOS tvOS watchOS xrOS)"
15+
16+ echo
17+ echo " Examples:"
18+ echo " $0 "
19+ echo " $0 MyTarget"
20+ echo " $0 -p iOS macOS"
21+ echo " $0 MyTarget -p iOS macOS"
22+ echo " $0 MyTarget --platforms iOS macOS tvOS watchOS 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
2032 exit 1
21- fi
33+ }
2234
2335# Define argument variables
24- TARGET=$1
36+ TARGET=" "
37+ PLATFORMS=" iOS macOS tvOS watchOS xrOS" # Default platforms
2538
26- # Remove TARGET from arguments list
27- shift
39+ # Parse command line arguments
40+ while [[ $# -gt 0 ]]; do
41+ case $1 in
42+ -p|--platforms)
43+ shift # Remove --platforms from arguments
44+ PLATFORMS=" " # Clear default platforms
45+
46+ # Collect all platform arguments until we hit another flag or run out of args
47+ while [[ $# -gt 0 && ! " $1 " =~ ^- ]]; do
48+ PLATFORMS=" $PLATFORMS $1 "
49+ shift
50+ done
51+
52+ # Remove leading space and check if we got any platforms
53+ PLATFORMS=$( echo " $PLATFORMS " | sed ' s/^ *//' )
54+ if [ -z " $PLATFORMS " ]; then
55+ show_error_and_exit " --platforms requires at least one platform"
56+ fi
57+ ;;
58+ -h|--help)
59+ show_usage; exit 0 ;;
60+ -* )
61+ show_error_and_exit " Unknown option $1 " ;;
62+ * )
63+ if [ -z " $TARGET " ]; then
64+ TARGET=" $1 "
65+ else
66+ show_error_and_exit " Unexpected argument '$1 '"
67+ fi
68+ shift
69+ ;;
70+ esac
71+ done
2872
29- # Define platforms variable
30- if [ $# -eq 0 ]; then
31- set -- iOS macOS tvOS watchOS xrOS
73+ # If no TARGET was provided, try to get package name
74+ if [ -z " $TARGET " ]; then
75+ # Use the script folder to refer to other scripts
76+ FOLDER=" $( cd " $( dirname " ${BASH_SOURCE[0]} " ) " & > /dev/null && pwd ) "
77+ SCRIPT_PACKAGE_NAME=" $FOLDER /package_name.sh"
78+
79+ # Check if package_name.sh exists
80+ if [ -f " $SCRIPT_PACKAGE_NAME " ]; then
81+ echo " No target provided, attempting to get package name..."
82+ if TARGET=$( " $SCRIPT_PACKAGE_NAME " ) ; then
83+ echo " Using package name: $TARGET "
84+ else
85+ echo " "
86+ read -p " Failed to get package name. Please enter the target to build: " TARGET
87+ if [ -z " $TARGET " ]; then
88+ show_error_and_exit " TARGET is required"
89+ fi
90+ fi
91+ else
92+ echo " "
93+ read -p " Please enter the target to build: " TARGET
94+ if [ -z " $TARGET " ]; then
95+ show_error_and_exit " TARGET is required"
96+ fi
97+ fi
3298fi
33- PLATFORMS=$@
3499
35100# A function that builds $TARGET for a specific platform
36101build_platform () {
@@ -41,18 +106,16 @@ build_platform() {
41106 # Build $TARGET for the $PLATFORM
42107 echo " Building $TARGET for $PLATFORM ..."
43108 if ! xcodebuild -scheme $TARGET -derivedDataPath .build -destination generic/platform=$PLATFORM ; then
44- echo " Failed to build $TARGET for $PLATFORM "
45- return 1
109+ echo " Failed to build $TARGET for $PLATFORM " ; return 1
46110 fi
47111
48112 # Complete successfully
49113 echo " Successfully built $TARGET for $PLATFORM "
50114}
51115
52116# Start script
53- echo " "
117+ echo
54118echo " Building $TARGET for [$PLATFORMS ]..."
55- echo " "
56119
57120# Loop through all platforms and call the build function
58121for PLATFORM in $PLATFORMS ; do
@@ -62,6 +125,6 @@ for PLATFORM in $PLATFORMS; do
62125done
63126
64127# Complete successfully
65- echo " "
128+ echo
66129echo " Building $TARGET completed successfully!"
67- echo " "
130+ echo
0 commit comments