Skip to content

Instantly share code, notes, and snippets.

@EgzonArifi
Created May 13, 2023 11:57
Show Gist options
  • Save EgzonArifi/1beafd76c4d67a51fc8356a6211d210f to your computer and use it in GitHub Desktop.
Save EgzonArifi/1beafd76c4d67a51fc8356a6211d210f to your computer and use it in GitHub Desktop.
Update .local.xcconfig files from .env files
#!/bin/bash
set -e
ENV_NAME="$1"
if [ -z "$ENV_NAME" ]; then
echo "Error: Missing required environment name argument."
exit 1
fi
# Get the script's directory
script_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
configs_dir="${script_dir}/../../YourProject/Configs"
ENV_FILE="${configs_dir}/.env.${ENV_NAME}"
CONFIG_FILE="${configs_dir}/Config$(echo ${ENV_NAME:0:1} | tr "[:lower:]" "[:upper:]")${ENV_NAME:1}.xcconfig"
LOCAL_CONFIG_FILE="${CONFIG_FILE%.*}.local.xcconfig"
# Load the environment variables from the .env file
export $(grep -v "^#" $ENV_FILE | xargs)
# Define arrays for keys and values
KEYS=("API_KEY" "SDK_LICENSE_KEY")
VALUES=("${API_KEY}" "${SDK_LICENSE_KEY}")
# Copy the original .xcconfig file to a .xcconfig.local file
cp "$CONFIG_FILE" "$LOCAL_CONFIG_FILE"
# Iterate through the keys and replace the placeholders in the .xcconfig.local file
for i in "${!KEYS[@]}"; do
key="${KEYS[$i]}"
value="${VALUES[$i]}"
if [ -z "$value" ]; then
echo "Error: Missing value for environment variable $key"
exit 1
fi
sed -i '' "s/${key}_PLACEHOLDER/${value}/g" "${LOCAL_CONFIG_FILE}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment