|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +TOP="$(realpath "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)/..")" |
| 4 | +readonly TOP |
| 5 | +# Define the directory where .apns files are located |
| 6 | +DIRECTORY="${TOP}/tests/ios-push-notifications" |
| 7 | + |
| 8 | +# Function to extract unique placeholders from a file using grep and cut (compatible with macOS) |
| 9 | +extract_placeholders() { |
| 10 | + grep -o '<[A-Z_]\+>' "$1" | cut -d'<' -f2 | cut -d'>' -f1 | sort -u |
| 11 | +} |
| 12 | + |
| 13 | +# Parse options |
| 14 | +use_standalone=false |
| 15 | +while getopts ":s" opt; do |
| 16 | + case $opt in |
| 17 | + s) |
| 18 | + use_standalone=true |
| 19 | + ;; |
| 20 | + \?) |
| 21 | + echo "Invalid option: -$OPTARG" >&2 |
| 22 | + exit 1 |
| 23 | + ;; |
| 24 | + esac |
| 25 | +done |
| 26 | +shift $((OPTIND-1)) |
| 27 | + |
| 28 | +# Check the first argument for 'list' command |
| 29 | +if [ "$1" == "list" ]; then |
| 30 | + echo "Available push notifications:" |
| 31 | + for file in "$DIRECTORY"/*.apns; do |
| 32 | + if [[ "$file" =~ \.standalone\.apns$ ]]; then |
| 33 | + base_name=$(basename "$file" .standalone.apns) |
| 34 | + if [[ ! -f "$DIRECTORY/$base_name.apns" ]]; then |
| 35 | + echo "$base_name (Standalone only)" |
| 36 | + fi |
| 37 | + else |
| 38 | + base_name=$(basename "$file" .apns) |
| 39 | + if [[ -f "$DIRECTORY/$base_name.standalone.apns" ]]; then |
| 40 | + echo "$base_name (+ Standalone)" |
| 41 | + else |
| 42 | + echo "$base_name" |
| 43 | + fi |
| 44 | + fi |
| 45 | + done |
| 46 | + echo "" |
| 47 | + exit 0 |
| 48 | +fi |
| 49 | + |
| 50 | +# Check if the file name without extension is provided |
| 51 | +if [ "$#" -lt 1 ]; then |
| 52 | + echo "Proper usage: ios-push-notification [-s] <name>" |
| 53 | + echo "Use 'list' to display available notifications." |
| 54 | + echo "" |
| 55 | + exit 1 |
| 56 | +fi |
| 57 | + |
| 58 | +# Assign the file path by adding .apns or .standalone.apns extension |
| 59 | +if [ "$use_standalone" == true ]; then |
| 60 | + input_file="${DIRECTORY}/$1.standalone.apns" |
| 61 | +else |
| 62 | + input_file="${DIRECTORY}/$1.apns" |
| 63 | +fi |
| 64 | + |
| 65 | +# Verify that the file exists |
| 66 | +if [ ! -f "$input_file" ]; then |
| 67 | + echo "$1 does not exist, use 'list' to display available notifications." |
| 68 | + echo "Proper usage: ios-push-notification [-s] <name>" |
| 69 | + echo "" |
| 70 | + exit 2 |
| 71 | +fi |
| 72 | + |
| 73 | +# Extract placeholders |
| 74 | +placeholders=$(extract_placeholders "$input_file") |
| 75 | +if [ -z "$placeholders" ]; then |
| 76 | + cat "$input_file" |
| 77 | + exit 3 |
| 78 | +fi |
| 79 | + |
| 80 | +# Check if correct number of replacements were provided |
| 81 | +if [ $(($# - 1)) -lt $(echo "$placeholders" | wc -l) ]; then |
| 82 | + echo "Insufficient parameters provided." |
| 83 | + echo "Proper usage: ios-push-notification [-s] $1 $(echo "$placeholders" | sed 's/^/</' | sed 's/$/>/' | tr '\n' ' ')" |
| 84 | + echo "" |
| 85 | + exit 4 |
| 86 | +fi |
| 87 | + |
| 88 | +# Remove the first argument (file name), leaving only the replacement values |
| 89 | +shift |
| 90 | + |
| 91 | +# Go through the file and replace each placeholder with the successive argument |
| 92 | +sed_script="" |
| 93 | +placeholder_index=1 |
| 94 | +for placeholder in $placeholders; do |
| 95 | + replacement="${!placeholder_index}" |
| 96 | + # Check if the placeholder is for a string and add quotes if necessary |
| 97 | + if [[ "$placeholder" == *_STRING ]]; then |
| 98 | + replacement="\"$replacement\"" |
| 99 | + fi |
| 100 | + # Escape special characters for sed |
| 101 | + replacement_esc="$(printf '%s' "$replacement" | sed 's/[&/\]/\\&/g')" |
| 102 | + sed_script+="s|<$placeholder>|$replacement_esc|g;" |
| 103 | + ((placeholder_index++)) |
| 104 | +done |
| 105 | + |
| 106 | +# Execute the sed command and print the result |
| 107 | +sed "$sed_script" "$input_file" | xcrun simctl push booted - |
0 commit comments