Skip to content

Instantly share code, notes, and snippets.

@dehlen
Last active March 17, 2017 15:25
Show Gist options
  • Save dehlen/385915d84a4b801628a9e54575432194 to your computer and use it in GitHub Desktop.
Save dehlen/385915d84a4b801628a9e54575432194 to your computer and use it in GitHub Desktop.
Script to compile, install and launch your iOS app on multiple simulators. Interactive simulator chooser, universal links and command line interface supported.
#!/bin/bash
#color codes
red=`tput setaf 1`
green=`tput setaf 2`
white_bg=`tput setab 7`
reset=`tput sgr0`
#check dependencies
command -v xcpretty >/dev/null 2>&1 || { echo >&2 "${red}xcpretty is required but it's not installed. Install via [sudo] gem install xcpretty. Aborting.${reset}"; exit 1; }
command -v fbsimctl >/dev/null 2>&1 || { echo >&2 "${red}fbsimctl is required but it's not installed. Install via brew tap facebook/fb && brew install fbsimctl --HEAD. Aborting.${reset}"; exit 1; }
#build variables
BUNDLE_IDENTIFIER="com.mycompany.myapp"
BUILD_DIR="$HOME/Desktop/multisim-build"
WORKSPACE="MyApp"
SCHEME="MyApp"
# only set when building xcodeproj instead of xcworkspace
# optionally set via command line interface if you need to build a project
PROJECT=""
TARGET=""
#f.e bash multisim.sh --deeplink "http://myapp.com/somepath"
DEEPLINK=""
# help
SHOW_HELP=false
display_help() {
echo "${green}${white_bg}Usage: $0 [options]${reset}" >&2
echo
echo "${green}${white_bg}If you don't want to specify all build variables via the script arguments you can set your default values directly in the code.${reset}"
echo "${green}${white_bg}Therefore all the given options are not mandatory and only will override the specified build variables in code if they are explicitly set.${reset}"
echo ""
echo "${green}${white_bg} -i, --identifier bundle identifier of the app to start${reset}"
echo "${green}${white_bg} -d, --dir directory of the build output${reset}"
echo "${green}${white_bg} -w, --workspace name of the workspace to compile${reset}"
echo "${green}${white_bg} -s, --scheme name of the xcode scheme to compile, used when building workspaces${reset}"
echo "${green}${white_bg} -p, --project name of the xcode project to compile, takes precedence over workspace${reset}"
echo "${green}${white_bg} -t, --target name of the xcode target to compile, used when building projects${reset}"
echo "${green}${white_bg} -l, --deeplink open a deeplink in safari${reset}"
echo "${green}${white_bg} -h, --help show this help information${reset}"
echo
exit 1
}
# if cli arguments are given override the default build values
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-h|--help)
SHOW_HELP=true
shift
;;
-i|--identifier)
BUNDLE_IDENTIFIER="$2"
echo "${red}${white_bg}Overriding default bundle identifier to: $BUNDLE_IDENTIFIER${reset}"
shift
;;
-d|--dir)
BUILD_DIR="$2"
echo "${red}${white_bg}Overriding default bundle directory to: $BUILD_DIR${reset}"
shift
;;
-w|--workspace)
WORKSPACE="$2"
echo "${red}${white_bg}Overriding default workspace to: $WORKSPACE${reset}"
shift
;;
-s|--scheme)
SCHEME="$2"
echo "${red}${white_bg}Overriding default scheme to: $SCHEME${reset}"
shift
;;
-p|--project)
PROJECT="$2"
echo "${red}${white_bg}Building project instead of workspace: $PROJECT${reset}"
shift
;;
-t|--target)
TARGET="$2"
echo "${red}${white_bg}Building target: $TARGET${reset}"
shift
;;
-l|--deeplink)
DEEPLINK="$2"
echo "${red}${white_bg}Set deeplink to: $DEEPLINK${reset}"
shift
;;
*)
SHOW_HELP=true
display_help
;;
esac
shift
done
if [ "$SHOW_HELP" = true ] ; then
display_help
exit 1;
fi
if [[ -z $BUNDLE_IDENTIFIER || -z $BUILD_DIR ]]; then
echo "${red}${white_bg}Build variables are not set. Aborting.${reset}"
exit 1;
fi
if [[ -z $WORKSPACE && -z $PROJECT ]]; then
echo "${red}${white_bg}No project and no workspace configured. Aborting.${reset}"
exit 1;
fi
if [[ -z $SCHEME && -z $TARGET ]]; then
echo "${red}${white_bg}Neither a scheme nor a target was configured. Aborting.${reset}"
exit 1;
fi
# remove previous builds and create a fresh build directory
rm -r $BUILD_DIR 2> /dev/null || echo > /dev/null; mkdir -p $BUILD_DIR
echo "${red}${white_bg}Compiling app. Output in: $BUILD_DIR.${reset}"
# compile the app
if [ -z $PROJECT ]; then
xcodebuild -arch i386 -sdk iphonesimulator -workspace "$WORKSPACE.xcworkspace" -scheme "$SCHEME" CONFIGURATION_BUILD_DIR="$BUILD_DIR" | xcpretty
else
if [ -z $TARGET ]; then
echo "${red}${white_bg}When building a project instead of a workspace a target is needed, set in code or via -t|--target option.${reset}"
exit 1;
else
xcodebuild -arch i386 -sdk iphonesimulator -project "$PROJECT.xcodeproj" -target "$TARGET" CONFIGURATION_BUILD_DIR="$BUILD_DIR" | xcpretty
fi
fi
#choose which simulators to boot up
echo "${red}${white_bg}Type the numbers of the simulators you want to use comma-separated f.e: [1,2,4], followed by [ENTER]:${reset}"
fbsimctl list | awk '{printf("%01d | %s\n", NR, $0)}'
read SIMULATORS
if [ -z "${SIMULATORS}" ]; then
echo "${red}${white_bg}No simulator selected. Aborting.${reset}"
exit 1;
fi
SELECTED_SIMULATORS=`echo "$SIMULATORS" | sed -e "s/,/p;/g"`
SELECTED_SIMULATORS=`echo "$SELECTED_SIMULATORS" | awk '{printf("%sp", $0)}'`
#boot up simulators
PHONES=$(fbsimctl list | sed -n "$SELECTED_SIMULATORS" | cut -d '|' -f1 | tr -d '\n')
PHONE_NAMES=$(fbsimctl list | sed -n "$SELECTED_SIMULATORS" | cut -d '|' -f2)
echo -e "${red}${white_bg}Booting up Simulators for:\n$PHONE_NAMES${reset}"
fbsimctl $PHONES boot
#install app on simulators
echo "${red}${white_bg}Installing app on all the booted simulators.${reset}"
shopt -s extglob
SUFFIX='@(app)'
fbsimctl install "$BUILD_DIR/"*.$SUFFIX
#launch app on simulators or open via deeplink
if [ -z $DEEPLINK ]; then
echo "${red}${white_bg}Launching app.${reset}"
$(fbsimctl launch $BUNDLE_IDENTIFIER)
else
echo "${red}${white_bg}Launching app with specified deeplink: $DEEPLINK.${reset}"
$(fbsimctl open "$DEEPLINK")
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment