Skip to content

Instantly share code, notes, and snippets.

@FredNandrin
Created February 28, 2025 16:18
Show Gist options
  • Save FredNandrin/2478b6cce816a3596a83a690be0330a4 to your computer and use it in GitHub Desktop.
Save FredNandrin/2478b6cce816a3596a83a690be0330a4 to your computer and use it in GitHub Desktop.
From the root of the xCode project, this will transform all environment variables to a Secrets.swift in the Source folder.
#!/bin/bash
# prepareSecrets
#
# prepare the Secrets.swift file from environment variables
#
# Define output file
OUTPUT_FILE="Sources/Secrets.swift"
echo "//" > "$OUTPUT_FILE"
echo "// Secrets.swift" >> "$OUTPUT_FILE"
echo "// SeenOnScreen" >> "$OUTPUT_FILE"
echo "//" >> "$OUTPUT_FILE"
echo "// Generated file. Please do not delete, nor edit" >> "$OUTPUT_FILE"
echo "//" >> "$OUTPUT_FILE"
echo "" >> "$OUTPUT_FILE"
# Start writing to the Secrets.swift file
echo "enum Secrets {" >> "$OUTPUT_FILE"
# Read each environment variable
env | while IFS='=' read -r key value; do
# Ignore empty lines and comments
if [[ -z "$key" || "$key" =~ ^# || "$key" == "_"|| "$key" == "PATH" ]]; then
continue
fi
# Trim whitespace
key=$(echo "$key" | xargs)
value=$(echo "$value" | xargs)
# Convert key to lowercase before processing
key=$(echo "$key" | tr '[:upper:]' '[:lower:]')
# Convert key to camelCase (basic transformation)
key=$(echo "$key" | awk 'BEGIN{FS=OFS="_"}{
for (i=1; i<=NF; i++) {
if (i == 1) printf("%s", tolower($i));
else printf("%s%s", toupper(substr($i,1,1)), substr($i,2));
}
}')
# Escape double quotes in values
value=$(echo "$value" | sed 's/"/\\"/g')
# Write to Secrets.swift
echo " static var $key: String { \"$value\" }" >> "$OUTPUT_FILE"
done
# Close the enum
echo "}" >> "$OUTPUT_FILE"
echo "Secrets.swift has been generated successfully."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment