Last active
February 2, 2022 23:36
-
-
Save acecilia/a5ff5566c785c0f9e17490fe034d2070 to your computer and use it in GitHub Desktop.
Create a simulator with a specific ID
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/zsh | |
# | |
# To call this function, pass a valid simulator ID: | |
# > ./create_simulator.sh create_and_boot "00000000-0000-0000-0000-000000000000" | |
set -euo pipefail | |
readonly SIMULATOR="iPhone 8" | |
readonly SIMULATOR_RUNTIME="15.2" | |
readonly SIMULATOR_NAME="${SIMULATOR}" | |
readonly SIMULATOR_PARENT_DIR="${HOME}/Library/Developer/CoreSimulator/Devices" | |
# Create and boot a simulator with a specific id | |
create_and_boot() { | |
readonly SIMULATOR_ID="${1}" | |
readonly simulator_entry="$(xcrun simctl list | grep ${SIMULATOR_ID})" | |
if [ -z "${simulator_entry}" ]; then | |
# If simulator with matching id does not exist, create it | |
readonly created_id="$(xcrun simctl create "${SIMULATOR_NAME}" "${SIMULATOR}" "iOS${SIMULATOR_RUNTIME}")" | |
rm -rf "${SIMULATOR_PARENT_DIR}/${SIMULATOR_ID}" | |
mv "${SIMULATOR_PARENT_DIR}/${created_id}" "${SIMULATOR_PARENT_DIR}/${SIMULATOR_ID}" | |
sed -i '' "s|${created_id}|${SIMULATOR_ID}|" "${SIMULATOR_PARENT_DIR}/${SIMULATOR_ID}/device.plist" | |
# A new simulator with custom id was created on disk. For it to be detected, the | |
# simulator daemon has to be restarted (this will kill the simulator app if running) | |
killall com.apple.CoreSimulator.CoreSimulatorService | |
else | |
# If simulator with matching id exists, erase it and reuse it | |
if [[ "${simulator_entry}" =~ "Booted" ]]; then | |
xcrun simctl shutdown "${SIMULATOR_ID}" | |
fi | |
xcrun simctl erase "${SIMULATOR_ID}" | |
fi | |
xcrun simctl boot "${SIMULATOR_ID}" | |
echo "Simulator of type '${SIMULATOR}' with id '${SIMULATOR_ID}' was created and booted" | |
} | |
"$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment