Skip to content

Instantly share code, notes, and snippets.

@k-thorat
Last active July 4, 2023 16:23
Show Gist options
  • Save k-thorat/528106b9e34ec6079ba1172db3f1c9b8 to your computer and use it in GitHub Desktop.
Save k-thorat/528106b9e34ec6079ba1172db3f1c9b8 to your computer and use it in GitHub Desktop.
Xcode - Restrict build setting changes.
#!/bin/sh
# https://stackedheaps.com/2023/06/10/restrict-changes-in-xcode-build-settings
# **********************************************************************************
# Verified on Xcode 12, 13 & 14
# Running Script
# Apple.xcodeproj
# Tools/Orange.xcodeproj
# restrict_build_settings_changes.sh
#
# ./restrict_build_settings_changes.sh "Apple Tools/Orange"
# **********************************************************************************
# How empty build settings looks:
#
# buildSettings = {
# }
# How modified build settings looks:
#
# buildSettings = {
# $SETTINGS_NAME1
# $SETTINGS_NAME2
# }
# **********************************************************************************
# Space separated list of Project names (without extension)
for XCODEPROJECT in $1; do
echo "Validating project: $XCODEPROJECT"
PBXPROJ_FILE="$XCODEPROJECT.xcodeproj/project.pbxproj"
echo "Project file is $PBXPROJ_FILE"
# No of settings occurrences, just start line.
# Start Line = buildSettings = {
SETTINGS_START_OCCURRENCES=$(sed -n '/buildSettings = {/p' $PBXPROJ_FILE | wc -l)
# If buildSettings is not modified, we expect total number should be twice as many starts.
# Line 1 - buildSettings = {
# Line 2 - }
SETTINGS_EXPECTED_LINE_COUNT=$((SETTINGS_START_OCCURRENCES * 2))
# No of settings occurrences with it's contents
# Start Line = buildSettings = {
# Contents = $SETTINGS1
# End Line = }
SETTINGS_ACTUAL_START_TO_END_LINE_COUNT=$(sed -n '/buildSettings = {/,/}/p' $PBXPROJ_FILE | wc -l)
echo "Expected number of lines for empty buildSettings = $SETTINGS_EXPECTED_LINE_COUNT"
echo "Actual number of lines with buildSettings contents =$SETTINGS_ACTUAL_START_TO_END_LINE_COUNT"
if [ $SETTINGS_EXPECTED_LINE_COUNT != $SETTINGS_ACTUAL_START_TO_END_LINE_COUNT ]; then
echo "$PBXPROJ_FILE has build setting changes, please use XCConfig."
exit 1
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment