|
1 | 1 | #!/bin/bash |
2 | | -fail=false |
3 | | - |
4 | | -# Get all string keys |
5 | | -KEYS=$(grep -oP '<entry[\ ]+lang="[^"]+"[\ ]+key="\K[^"]+' "$1"/src/Common/Language.xml) |
6 | | - |
7 | | -for file in {"$1"/Translations/Language.*.xml,"$1"/src/Common/Language.xml}; do |
8 | | - echo "$file" |
9 | | - passes=true |
10 | | - |
11 | | - # Validate xml |
12 | | - output=$(fxparser -V "$file") |
13 | | - returnvalue=$? |
14 | | - |
15 | | - if [ "$returnvalue" -ne "0" ]; then |
16 | | - passes=false |
17 | | - fail=true |
18 | | - echo $output |
19 | | - fi |
20 | | - |
21 | | - # Ensure each key found in common xml is found in translation xmls |
22 | | - for key in $KEYS; do |
23 | | - if ! grep -q "$key" "$file"; then |
24 | | - echo "Key $key not found in $file" |
25 | | - passes=false |
26 | | - fail=true |
| 2 | +set -euo pipefail # Exit on error, undefined variable, or pipe failure |
| 3 | + |
| 4 | +# --- Configuration --- |
| 5 | +SCRIPT_NAME=$(basename "$0") |
| 6 | +FAIL_FLAG=false |
| 7 | + |
| 8 | +# --- Colors --- |
| 9 | +COLOR_RED='\033[0;31m' |
| 10 | +COLOR_GREEN='\033[0;32m' |
| 11 | +COLOR_YELLOW='\033[0;33m' |
| 12 | +COLOR_CYAN='\033[0;36m' |
| 13 | +COLOR_MAGENTA='\033[0;35m' |
| 14 | +COLOR_RESET='\033[0m' # No Color |
| 15 | + |
| 16 | +# --- Helper Functions for Output --- |
| 17 | +log_info() { echo -e "${COLOR_CYAN}$1${COLOR_RESET}"; } |
| 18 | +log_success() { echo -e "${COLOR_GREEN}$1${COLOR_RESET}"; } |
| 19 | +log_warning() { echo -e "${COLOR_YELLOW}Warning: $1${COLOR_RESET}"; } |
| 20 | +log_error() { echo -e "${COLOR_RED}Error: $1${COLOR_RESET}" >&2; } |
| 21 | +log_detail() { echo -e " $1"; } |
| 22 | +log_detail_success() { echo -e " ${COLOR_GREEN}$1${COLOR_RESET}"; } |
| 23 | +log_detail_error() { echo -e " ${COLOR_RED}$1${COLOR_RESET}"; } |
| 24 | +log_detail_warning() { echo -e " ${COLOR_YELLOW}$1${COLOR_RESET}"; } |
| 25 | +log_detail_magenta() { echo -e " ${COLOR_MAGENTA}$1${COLOR_RESET}"; } |
| 26 | + |
| 27 | +# --- Parameter Validation --- |
| 28 | +if [ "$#" -ne 1 ]; then |
| 29 | + log_error "Usage: $SCRIPT_NAME <Path_To_VeraCrypt_Root>" |
| 30 | + log_error "Example: $SCRIPT_NAME /path/to/VeraCrypt" |
| 31 | + exit 1 |
| 32 | +fi |
| 33 | + |
| 34 | +ROOT_PATH="$1" |
| 35 | + |
| 36 | +if [ ! -d "$ROOT_PATH" ]; then |
| 37 | + log_error "Root path '$ROOT_PATH' not found or is not a directory." |
| 38 | + exit 1 |
| 39 | +fi |
| 40 | + |
| 41 | +# Define the path to the common Language.xml |
| 42 | +COMMON_FILE="$ROOT_PATH/src/Common/Language.xml" |
| 43 | + |
| 44 | +# Check if the common Language.xml exists |
| 45 | +if [ ! -f "$COMMON_FILE" ]; then |
| 46 | + log_error "Common Language.xml not found or is not a file at path: $COMMON_FILE" |
| 47 | + exit 1 |
| 48 | +fi |
| 49 | + |
| 50 | +log_info "Extracting keys from $COMMON_FILE" |
| 51 | + |
| 52 | +# Define regex pattern to extract 'key' attributes from <entry> elements |
| 53 | +KEY_EXTRACTION_PATTERN='<entry\s+lang="[^"]+"\s+key="([^"]+)"' |
| 54 | + |
| 55 | +# Extract all keys using grep with PCRE (-P) and only outputting the captured group (-o) |
| 56 | +# Use process substitution and readarray to populate the KEYS array |
| 57 | +# Ensure grep returns 0 even if no match, or handle non-zero for no match |
| 58 | +KEYS_STRING=$(grep -oP "$KEY_EXTRACTION_PATTERN" "$COMMON_FILE" | sed -E 's/.*key="([^"]+)".*/\1/' || true) |
| 59 | +if [ -z "$KEYS_STRING" ]; then |
| 60 | + KEYS=() |
| 61 | +else |
| 62 | + readarray -t KEYS < <(echo "$KEYS_STRING") |
| 63 | +fi |
| 64 | + |
| 65 | + |
| 66 | +if [ ${#KEYS[@]} -eq 0 ]; then |
| 67 | + log_warning "No keys found in $COMMON_FILE using pattern: $KEY_EXTRACTION_PATTERN" |
| 68 | + # If this should be an error, uncomment next lines: |
| 69 | + # log_error "No keys found in $COMMON_FILE." |
| 70 | + # exit 1 |
| 71 | +else |
| 72 | + log_info "Found ${#KEYS[@]} keys." |
| 73 | +fi |
| 74 | + |
| 75 | +# Define the regex for finding invalid escape sequences. |
| 76 | +# Valid sequences: \n, \r, \t, \\, \" |
| 77 | +INVALID_ESCAPE_REGEX='(?<!\\)(?:\\\\)*\\([^nrt\\"])' # This is better |
| 78 | +ALLOWED_ESCAPES_MESSAGE="Allowed sequences are: \\n, \\r, \\t, \\\\ (for literal backslash), \\\" (for literal quote)" |
| 79 | + |
| 80 | +# Retrieve all translation XML files in the Translations folder |
| 81 | +TRANSLATION_FOLDER="$ROOT_PATH/Translations" |
| 82 | +FILES_TO_PROCESS=() |
| 83 | + |
| 84 | +# Add common file first |
| 85 | +FILES_TO_PROCESS+=("$COMMON_FILE") |
| 86 | + |
| 87 | +if [ ! -d "$TRANSLATION_FOLDER" ]; then |
| 88 | + log_warning "Translations folder not found at path: $TRANSLATION_FOLDER. Skipping translation files." |
| 89 | +else |
| 90 | + # Use find to get translation files. nullglob helps avoid errors if no files match. |
| 91 | + shopt -s nullglob |
| 92 | + for lang_file in "$TRANSLATION_FOLDER"/Language.*.xml; do |
| 93 | + FILES_TO_PROCESS+=("$lang_file") |
| 94 | + done |
| 95 | + shopt -u nullglob # Reset nullglob |
| 96 | + if [ ${#FILES_TO_PROCESS[@]} -eq 1 ]; then # Only common file was added |
| 97 | + log_warning "No Language.*.xml files found in $TRANSLATION_FOLDER." |
| 98 | + fi |
| 99 | +fi |
| 100 | + |
| 101 | +if [ ${#FILES_TO_PROCESS[@]} -eq 0 ]; then |
| 102 | + log_warning "No files found to process." |
| 103 | + exit 0 # Or 1 if this is an error condition |
| 104 | +fi |
| 105 | + |
| 106 | +# Iterate through each file and perform validations |
| 107 | +for file in "${FILES_TO_PROCESS[@]}"; do |
| 108 | + if [ ! -f "$file" ]; then |
| 109 | + log_warning "File not found or is not a file, skipping: $file" |
| 110 | + continue |
| 111 | + fi |
| 112 | + echo # Newline for readability |
| 113 | + log_info "Processing file: $file" |
| 114 | + CURRENT_FILE_PASSES=true |
| 115 | + |
| 116 | + # 1. Validate XML using fxparser |
| 117 | + log_detail "Validating XML structure..." |
| 118 | + # Capture stdout and stderr, get exit code |
| 119 | + FXPARSER_OUTPUT=$(fxparser -V "$file" 2>&1) |
| 120 | + FXPARSER_EXIT_CODE=$? |
| 121 | + |
| 122 | + if [ "$FXPARSER_EXIT_CODE" -ne 0 ]; then |
| 123 | + CURRENT_FILE_PASSES=false |
| 124 | + FAIL_FLAG=true |
| 125 | + log_detail_error "XML Validation Failed for $file (fxparser exit code: $FXPARSER_EXIT_CODE):" |
| 126 | + while IFS= read -r line; do |
| 127 | + log_detail_error " $line" |
| 128 | + done <<< "$FXPARSER_OUTPUT" |
| 129 | + else |
| 130 | + log_detail_success "XML structure is valid." |
| 131 | + fi |
| 132 | + |
| 133 | + # 2. Check for invalid backslash escape sequences |
| 134 | + log_detail "Checking for invalid escape sequences..." |
| 135 | + # Use grep -P for PCRE, -n for line numbers. --color=never to avoid grep's own coloring here. |
| 136 | + # We check grep's exit code: 0 if found, 1 if not found, >1 for error. |
| 137 | + INVALID_ESCAPE_MATCHES=$(grep -P -n --color=never "$INVALID_ESCAPE_REGEX" "$file" || true) |
| 138 | + |
| 139 | + if [ -n "$INVALID_ESCAPE_MATCHES" ]; then |
| 140 | + log_detail_error "File '$file' contains potentially invalid backslash escape sequences." |
| 141 | + log_detail_error "$ALLOWED_ESCAPES_MESSAGE" |
| 142 | + log_detail_error "Instances found:" |
| 143 | + while IFS= read -r line_match; do |
| 144 | + # Extract line number and the line content from grep's output (e.g., "123:content") |
| 145 | + LINE_NUMBER=$(echo "$line_match" | cut -d: -f1) |
| 146 | + LINE_CONTENT=$(echo "$line_match" | cut -d: -f2-) |
| 147 | + # Trim whitespace (optional, but PowerShell did it) |
| 148 | + TRIMMED_LINE_CONTENT=$(echo "$LINE_CONTENT" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//') |
| 149 | + log_detail_magenta "Line $LINE_NUMBER: $TRIMMED_LINE_CONTENT" |
| 150 | + done <<< "$INVALID_ESCAPE_MATCHES" |
| 151 | + CURRENT_FILE_PASSES=false |
| 152 | + FAIL_FLAG=true |
| 153 | + else |
| 154 | + log_detail_success "No invalid escape sequences found." |
| 155 | + fi |
| 156 | + |
| 157 | + # 3. Check for the presence of each key in the current file (if keys were found) |
| 158 | + if [ ${#KEYS[@]} -gt 0 ]; then |
| 159 | + log_detail "Checking for key completeness..." |
| 160 | + KEYS_MISSING_IN_CURRENT_FILE=0 |
| 161 | + for key_entry in "${KEYS[@]}"; do |
| 162 | + # Search for key="KEY_NAME" |
| 163 | + SEARCH_PATTERN_FOR_KEY="key=\"$key_entry\"" |
| 164 | + if ! grep -q "$SEARCH_PATTERN_FOR_KEY" "$file"; then |
| 165 | + log_detail_error "Key '$key_entry' (from $COMMON_FILE) not found in $file" |
| 166 | + CURRENT_FILE_PASSES=false |
| 167 | + FAIL_FLAG=true |
| 168 | + ((KEYS_MISSING_IN_CURRENT_FILE++)) |
| 169 | + fi |
| 170 | + done |
| 171 | + if [ "$KEYS_MISSING_IN_CURRENT_FILE" -eq 0 ]; then |
| 172 | + log_detail_success "All keys from $COMMON_FILE are present." |
| 173 | + else |
| 174 | + log_detail_error "$KEYS_MISSING_IN_CURRENT_FILE key(s) missing." |
| 175 | + fi |
| 176 | + else |
| 177 | + log_detail_warning "Skipping key completeness check as no keys were extracted from $COMMON_FILE." |
| 178 | + fi |
| 179 | + |
| 180 | + # Output the result for the current file |
| 181 | + if [ "$CURRENT_FILE_PASSES" = true ]; then |
| 182 | + log_success "$file PASSED all checks." |
| 183 | + else |
| 184 | + log_error "$file FAILED one or more checks." |
27 | 185 | fi |
28 | | - done |
29 | | - |
30 | | - if [ "$passes" = true ]; then |
31 | | - echo -e "\e[32m$file passes xml validation.\e[0m" |
32 | | - else |
33 | | - echo -e "\e[31m$file fails xml validation.\e[0m" |
34 | | - fi |
35 | | - |
36 | 186 | done |
37 | 187 |
|
38 | | -if [ "$fail" = true ]; then |
39 | | - exit 1 |
| 188 | +# Exit with appropriate status code |
| 189 | +echo # Newline for readability |
| 190 | +if [ "$FAIL_FLAG" = true ]; then |
| 191 | + log_error "Overall Result: One or more files failed validation." |
| 192 | + exit 1 |
40 | 193 | else |
41 | | - exit 0 |
| 194 | + log_success "Overall Result: All processed files passed all checks successfully." |
| 195 | + exit 0 |
42 | 196 | fi |
0 commit comments