|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +echo "********************************" |
| 4 | +echo "Performing feature flag checks" |
| 5 | +echo "********************************" |
| 6 | + |
| 7 | +failed_checks=0 |
| 8 | + |
| 9 | +item_in_array() { |
| 10 | + local item="$1" |
| 11 | + shift |
| 12 | + local array=("$@") |
| 13 | + |
| 14 | + for element in "${array[@]}"; do |
| 15 | + if [[ "$element" == "$item" ]]; then |
| 16 | + echo 1 |
| 17 | + return |
| 18 | + fi |
| 19 | + done |
| 20 | + |
| 21 | + echo 0 |
| 22 | +} |
| 23 | + |
| 24 | +function get_classes_from_constants_file() { |
| 25 | + # Get the file path of the FeatureFlagConstants File |
| 26 | + file_path=$(find . -name FeatureFlagConstants.kt) |
| 27 | + |
| 28 | + # Get a list of feature flag annotation classes |
| 29 | + annotation_classes=$(grep -F -E '[\s\w]*annotation\s*class' "$file_path") |
| 30 | + |
| 31 | + # Convert the string into an array, splitting by newline |
| 32 | + IFS=$'\n' read -rd '' -a array <<<"$annotation_classes" |
| 33 | + |
| 34 | + # Create an empty array to hold individual class names |
| 35 | + class_names=() |
| 36 | + |
| 37 | + # Iterate through each line and take the last word of the list |
| 38 | + for line in "${array[@]}"; do |
| 39 | + # Convert each line into an array of words, splitting by space |
| 40 | + IFS=' ' read -ra words <<<"$line" |
| 41 | + |
| 42 | + # Get last element of the list |
| 43 | + last_element="${words[${#words[@]}-1]}" |
| 44 | + |
| 45 | + # Add the element into class_names |
| 46 | + class_names+=("$last_element") |
| 47 | + done |
| 48 | + |
| 49 | + echo "${class_names[@]}" |
| 50 | +} |
| 51 | + |
| 52 | +function get_imported_classes_in_logger() { |
| 53 | + # File path containing the block of text |
| 54 | + file_path=$(find . -name FeatureFlagsLogger.kt) |
| 55 | + |
| 56 | + # Use sed to extract the block based on a starting pattern and an ending pattern |
| 57 | + extracted_block=$(sed -n '/class FeatureFlagsLogger @Inject constructor(/,/^)/p' "$file_path") |
| 58 | + |
| 59 | + # Get annotation classes |
| 60 | + logged_classes=$(grep -E '@Enable[A-Za-z0-9_]+' "$file_path") |
| 61 | + |
| 62 | + # Print the logged classes |
| 63 | + echo "$logged_classes" |
| 64 | +} |
| 65 | + |
| 66 | +function get_flags_added_into_the_logging_map() { |
| 67 | + # File path containing the block of text |
| 68 | + file_path=$(find . -name FeatureFlagsLogger.kt) |
| 69 | + |
| 70 | + # Use sed to extract the block based on a starting pattern and an ending pattern |
| 71 | + extracted_block=$(sed -n '/Map<String, PlatformParameterValue<Boolean>> = mapOf(/,/)$/p' "$file_path") |
| 72 | + |
| 73 | + # Get any word beginning with enable from the extracted block |
| 74 | + enable_flags=$(grep -E 'enable[A-Za-z0-9_]+' <<<"$extracted_block") |
| 75 | + |
| 76 | + added_flags=() |
| 77 | + |
| 78 | + # Convert the string into an array, splitting by newline |
| 79 | + IFS=$'\n' read -rd '' -a added_flags <<<"$enable_flags" |
| 80 | + |
| 81 | + # Create an empty array to hold individual class names |
| 82 | + class_names=() |
| 83 | + |
| 84 | + # Iterate through each line and take the last word of the list |
| 85 | + for line in "${added_flags[@]}"; do |
| 86 | + # Remove the comma at the end of the line |
| 87 | + line=$(echo "$line" | awk '{sub(/,$/, ""); print}') |
| 88 | + |
| 89 | + # Convert each line into an array of words, splitting by space |
| 90 | + IFS=' ' read -ra words <<<"$line" |
| 91 | + |
| 92 | + # Get last element of the list |
| 93 | + last_element="${words[${#words[@]}-1]}" |
| 94 | + |
| 95 | + # Capitalize last element using sed |
| 96 | + last_element=$(echo "$last_element" | awk '{for(i=1;i<=NF;i++) $i=toupper(substr($i,1,1)) substr($i,2)}1') |
| 97 | + |
| 98 | + # Add the element into class_names |
| 99 | + class_names+=("$last_element") |
| 100 | + done |
| 101 | + |
| 102 | + echo "${class_names[@]}" |
| 103 | +} |
| 104 | + |
| 105 | +function ensure_all_flags_are_imported() { |
| 106 | + feature_flags=($(get_classes_from_constants_file)) |
| 107 | + imported_classes=($(get_imported_classes_in_logger)) |
| 108 | + flags_added_to_map=($(get_flags_added_into_the_logging_map)) |
| 109 | + |
| 110 | + # Replace the @ symbol and spaces within each element of the list |
| 111 | + for i in "${!imported_classes[@]}"; do |
| 112 | + imported_classes[$i]=$(echo "${imported_classes[$i]}" | tr -d '@' | tr -d ' ') |
| 113 | + done |
| 114 | + |
| 115 | + # Iterate through each element in the feature_flags array to check if it is imported |
| 116 | + # in the FeatureFlagsLogger.kt |
| 117 | + for element in "${feature_flags[@]}"; do |
| 118 | + # Check if the element is in the imported_classes array |
| 119 | + in_array=$(item_in_array "$element" "${imported_classes[@]}") |
| 120 | + if [[ $in_array -ne 1 ]]; then |
| 121 | + failed_checks=$((failed_checks + 1)) |
| 122 | + echo "ERROR: $element is not imported in FeatureFlagsLogger.kt" |
| 123 | + fi |
| 124 | + done |
| 125 | + |
| 126 | + # Iterate through each element in the feature_flags array to check if it is added to the logging map |
| 127 | + for element in "${feature_flags[@]}"; do |
| 128 | + # Check if the element is in the feature_flags array |
| 129 | + in_array=$(item_in_array "$element" "${flags_added_to_map[@]}") |
| 130 | + if [[ $in_array -ne 1 ]]; then |
| 131 | + failed_checks=$((failed_checks + 1)) |
| 132 | + echo "ERROR: $element is not added to the logging map in FeatureFlagsLogger.kt" |
| 133 | + fi |
| 134 | + done |
| 135 | + |
| 136 | + if [[ $failed_checks -eq 0 ]]; then |
| 137 | + echo "All feature flags are imported and added to the logging map in FeatureFlagsLogger.kt" |
| 138 | + exit 0 |
| 139 | + else |
| 140 | + echo "********************************" |
| 141 | + echo "$failed_checks Feature flag issues found." |
| 142 | + echo "Please fix the above issues." |
| 143 | + echo "********************************" |
| 144 | + exit 1 |
| 145 | + fi |
| 146 | +} |
| 147 | + |
| 148 | +ensure_all_flags_are_imported |
0 commit comments