44
55set -e
66
7- RES_DIR=" app/src/main/res"
7+ # Validate script is run from project root
8+ SCRIPT_DIR=" $( cd " $( dirname " ${BASH_SOURCE[0]} " ) " && pwd) "
9+ PROJECT_ROOT=" $( cd " $SCRIPT_DIR /.." && pwd) "
10+ RES_DIR=" $PROJECT_ROOT /app/src/main/res"
11+
12+ if [ ! -d " $RES_DIR " ]; then
13+ echo " Error: Resource directory not found: $RES_DIR "
14+ echo " Please run this script from the project root directory."
15+ exit 1
16+ fi
17+
18+ if [ ! -f " $PROJECT_ROOT /.tx/config" ]; then
19+ echo " Error: Transifex config not found: $PROJECT_ROOT /.tx/config"
20+ echo " Please ensure Transifex is configured for this project."
21+ exit 1
22+ fi
823
924# Helper function to rename or merge directories
1025rename_or_merge () {
1126 local src=" $1 "
1227 local dst=" $2 "
1328 local src_name=$( basename " $src " )
1429 local dst_name=$( basename " $dst " )
30+ local merge_errors=0
31+
32+ if [ ! -d " $src " ]; then
33+ echo " Warning: Source directory does not exist: $src_name "
34+ return 1
35+ fi
1536
1637 if [ ! -d " $dst " ]; then
1738 echo " Renaming: $src_name -> $dst_name "
18- mv " $src " " $dst " && return 0
39+ if mv " $src " " $dst " ; then
40+ return 0
41+ else
42+ echo " Error: Failed to rename $src_name to $dst_name "
43+ return 1
44+ fi
1945 else
2046 echo " Merging: $src_name -> $dst_name "
21- find " $src " -mindepth 1 -maxdepth 1 -exec mv {} " $dst /" \; 2> /dev/null || true
22- rmdir " $src " 2> /dev/null && return 0 || return 1
47+ while IFS= read -r -d ' ' item; do
48+ if ! mv " $item " " $dst /" 2> /dev/null; then
49+ echo " Warning: Failed to move $( basename " $item " ) from $src_name "
50+ merge_errors=$(( merge_errors + 1 ))
51+ fi
52+ done < <( find " $src " -mindepth 1 -maxdepth 1 -print0 2> /dev/null)
53+
54+ # Only remove source directory if merge was successful
55+ if [ " $merge_errors " -eq 0 ]; then
56+ if rmdir " $src " 2> /dev/null; then
57+ return 0
58+ else
59+ echo " Warning: Could not remove empty directory: $src_name "
60+ return 1
61+ fi
62+ else
63+ echo " Error: Some files could not be merged from $src_name "
64+ return 1
65+ fi
2366 fi
2467}
2568
69+ # Validate XML file is well-formed before processing
70+ validate_xml () {
71+ local file=" $1 "
72+ # Basic validation: check for opening and closing resources tags
73+ if ! grep -q ' <resources' " $file " 2> /dev/null || ! grep -q ' </resources>' " $file " 2> /dev/null; then
74+ echo " Warning: $file appears to be malformed XML, skipping normalization"
75+ return 1
76+ fi
77+ return 0
78+ }
79+
2680echo " Pulling translations from Transifex..."
81+
82+ # Check if tx command is available
83+ if ! command -v tx & > /dev/null; then
84+ echo " Error: Transifex CLI (tx) is not installed or not in PATH"
85+ echo " Please install it: https://developers.transifex.com/docs/cli"
86+ exit 1
87+ fi
88+
89+ # Run tx pull and check for errors
90+ set +e # Temporarily disable exit on error to check tx pull status
2791tx pull -a
92+ TX_EXIT_CODE=$?
93+ set -e # Re-enable exit on error
94+
95+ if [ " $TX_EXIT_CODE " -ne 0 ]; then
96+ echo " Error: Transifex pull failed with exit code $TX_EXIT_CODE "
97+ echo " Please check your Transifex configuration and authentication."
98+ exit 1
99+ fi
28100
29101echo " "
30102echo " Renaming and cleaning up directories..."
@@ -66,7 +138,7 @@ while IFS= read -r dir; do
66138 fi
67139 ;;
68140 esac
69- done < <( find " $RES_DIR " -type d -name " values-*" | sort)
141+ done < <( find " $RES_DIR " -type d -name " values-*" 2> /dev/null | sort)
70142
71143echo " Renamed $RENAMED_COUNT directories"
72144echo " Removed $REMOVED_COUNT directories"
@@ -77,7 +149,12 @@ echo "Normalizing XML formatting..."
77149# Normalize XML
78150NORMALIZED_COUNT=0
79151while IFS= read -r file; do
80- awk '
152+ # Validate XML before processing
153+ if ! validate_xml " $file " ; then
154+ continue
155+ fi
156+
157+ if awk '
81158 {
82159 gsub(/[[:space:]]+$/, "")
83160 if ($0 ~ /^[[:space:]]*<\/resources>[[:space:]]*$/) {
@@ -89,7 +166,17 @@ while IFS= read -r file; do
89166 if (saw_resources && $0 != "") saw_resources = 0
90167 print
91168 }
92- ' " $file " > " $file .tmp" && mv " $file .tmp" " $file " 2> /dev/null && NORMALIZED_COUNT=$(( NORMALIZED_COUNT + 1 ))
169+ ' " $file " > " $file .tmp" ; then
170+ if mv " $file .tmp" " $file " 2> /dev/null; then
171+ NORMALIZED_COUNT=$(( NORMALIZED_COUNT + 1 ))
172+ else
173+ echo " Warning: Failed to replace $file "
174+ rm -f " $file .tmp"
175+ fi
176+ else
177+ echo " Warning: Failed to normalize $file "
178+ rm -f " $file .tmp"
179+ fi
93180done < <( find " $RES_DIR " -type f -path " */values-*/strings.xml" 2> /dev/null)
94181
95182echo " Normalized $NORMALIZED_COUNT files"
@@ -102,6 +189,7 @@ DELETED_DIRS=0
102189declare -a dirs_to_check
103190
104191# Delete empty files and collect their directories
192+ set +e # Allow commands to fail for empty file detection
105193while IFS= read -r file; do
106194 line_count=$( wc -l < " $file " 2> /dev/null | tr -d ' ' || echo " 0" )
107195 string_count=$( grep -c ' <string name=' " $file " 2> /dev/null || echo " 0" )
@@ -112,15 +200,20 @@ while IFS= read -r file; do
112200 rm -f " $file "
113201 EMPTY_COUNT=$(( EMPTY_COUNT + 1 ))
114202 fi
115- done < <( find " $RES_DIR " -type f -path " */values-*/strings.xml" )
203+ done < <( find " $RES_DIR " -type f -path " */values-*/strings.xml" 2> /dev/null)
204+ set -e
116205
117206# Remove empty directories
118- for dir in $( printf ' %s\n' " ${dirs_to_check[@]} " | sort -u) ; do
207+ for dir in $( printf ' %s\n' " ${dirs_to_check[@]} " | sort -u | sort -r ) ; do
119208 [ -d " $dir " ] || continue
209+ set +e # Allow find to fail if directory is already gone
120210 file_count=$( find " $dir " -type f ! -name ' .gitkeep' ! -name ' .DS_Store' 2> /dev/null | wc -l | tr -d ' ' )
211+ set -e
121212 if [ " $file_count " -eq 0 ]; then
122213 echo " Deleting empty directory: $dir "
123- rmdir " $dir " 2> /dev/null && DELETED_DIRS=$(( DELETED_DIRS + 1 ))
214+ if rmdir " $dir " 2> /dev/null; then
215+ DELETED_DIRS=$(( DELETED_DIRS + 1 ))
216+ fi
124217 fi
125218done
126219
0 commit comments