Skip to content

Commit 316c49c

Browse files
committed
[CI] check-i18n: add support for drifted_from_default field
1 parent afb7e18 commit 316c49c

File tree

3 files changed

+64
-21
lines changed

3 files changed

+64
-21
lines changed

.github/workflows/check-i18n.yml

+7-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ jobs:
1313
with:
1414
fetch-depth: 0 # all
1515
- name: Any files missing hash key?
16-
run: scripts/check-i18n.sh -n -x -v
16+
run: |
17+
scripts/check-i18n.sh -n -x -v
18+
.github/workflows/scripts/check-i18n-helper.sh
1719
- name: Any files with invalid hash keys?
1820
run: scripts/check-i18n.sh -v
19-
- run: .github/workflows/scripts/check-i18n-helper.sh
21+
- name: Drifted status needs updating?
22+
run: |
23+
scripts/check-i18n.sh -D
24+
# npm run _diff:fail

package.json

+2-3
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,9 @@
7373
"fix:filenames": "npm run _rename-to-kebab-case",
7474
"fix:format": "npm run format; npm run _fix:trailing-spaces",
7575
"fix:htmltest-config": "scripts/htmltest-config.sh",
76-
"fix:i18n:all": "scripts/check-i18n.sh -a -c HEAD",
77-
"fix:i18n:drifted": "scripts/check-i18n.sh -c HEAD",
76+
"fix:i18n:status": "scripts/check-i18n.sh -D",
7877
"fix:i18n:new": "scripts/check-i18n.sh -n -c HEAD",
79-
"fix:i18n": "npm run fix:i18n:new",
78+
"fix:i18n": "npm run seq -- fix:i18n:new fix:i18n:status",
8079
"fix:markdown": "npm run check:markdown -- --fix; echo '\nTrimming trailing whitespace'; npm run _fix:trailing-spaces",
8180
"fix:refcache:refresh": "npm run _refcache:prune -- -n ${PRUNE_N:-128}",
8281
"fix:refcache": "npm run check:links",

scripts/check-i18n.sh

+55-16
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@ DEFAULT_LANG="en"
88
DEFAULT_TARGET="$DEFAULT_CONTENT"
99
EXIT_STATUS=0
1010
EXTRA_DIFF_ARGS="--numstat"
11-
FLAG_DIFF_DETAILS=""
11+
FLAG_CHANGE_OR_ADD=0
12+
FLAG_DIFF_DETAILS=0
13+
FLAG_DRIFTED_STATUS=0
1214
FLAG_FAIL_ON_LIST_OR_MISSING=0
1315
FLAG_INFO=""
1416
FLAG_QUIET=""
1517
FLAG_VERBOSE=""
1618
I18N_DLC_KEY="default_lang_commit"
19+
I18N_DLD_KEY="drifted_from_default" # true, false, file not found
1720
LIST_KIND="DRIFTED" # or "ALL" or "NEW"
1821
TARGET_PATHS=""
1922

@@ -38,18 +41,20 @@ Options:
3841
3942
-a List/process all localization page files accessible through target paths.
4043
41-
-c HASH Update or add the '$I18N_DLC_KEY' key value to HASH for all selected
44+
-c HASH Change or add the '$I18N_DLC_KEY' key value to HASH for all selected
4245
localization pages. Use 'HEAD' as a shorthand for the hash of 'main'
43-
at HEAD (read locally).
46+
at HEAD (read locally). Also sets '$I18N_DLD_KEY' to true for files
47+
that have drifted.
4448
4549
TIP: first fetch and pull the upstream 'main' if you want to use the
4650
remote HEAD hash.
4751
4852
-d Output diff details.
53+
-D Update or add the '$I18N_DLD_KEY' key to all target localization pages.
4954
-h Help! Output this usage info.
5055
5156
-i Print commit hashes of the local 'main' branch that might be useful to
52-
se as an argument to -c.
57+
use as an argument to -c.
5358
5459
-n List/process only new localization pages, those without a '$I18N_DLC_KEY' key.
5560
-q Quiet mode. Do not list processed files.
@@ -65,15 +70,18 @@ function usage() {
6570
}
6671

6772
function process_CLI_args() {
68-
while getopts ":ac:dhinqvx" opt; do
73+
while getopts ":ac:dDhinqvx" opt; do
6974
case $opt in
7075
a)
7176
LIST_KIND="ALL";;
7277
c)
78+
FLAG_CHANGE_OR_ADD=1;
7379
COMMIT_HASH_ARG="$OPTARG";;
7480
d)
7581
FLAG_DIFF_DETAILS=1
7682
EXTRA_DIFF_ARGS="";;
83+
D)
84+
FLAG_DRIFTED_STATUS=1;;
7785
h)
7886
usage;;
7987
i)
@@ -98,16 +106,16 @@ function process_CLI_args() {
98106
if [[ -n $COMMIT_HASH_ARG ]]; then
99107
COMMIT_HASH_ARG=$(echo $COMMIT_HASH_ARG | tr '[:upper:]' '[:lower:]')
100108
validate_hash $COMMIT_HASH_ARG
109+
fi
101110

102-
if [[ -n $FLAG_DIFF_DETAILS ]]; then
103-
echo -e "ERROR: you can't use -c and -d at the same time, specify one or the other.\n"
104-
usage 1
105-
fi
111+
if (( FLAG_CHANGE_OR_ADD + FLAG_DIFF_DETAILS + FLAG_DRIFTED_STATUS > 1 )); then
112+
echo -e "ERROR: you can't use -c, -d, and -D at the same time; choose one. For help use -h.\n"
113+
exit 1
106114
fi
107115

108-
if [[ -n $FLAG_QUIET && -n $FLAG_DIFF_DETAILS ]]; then
109-
echo -e "ERROR: you can't use -d and -q at the same time, specify one or the other.\n"
110-
usage 1
116+
if [[ -n $FLAG_QUIET && $FLAG_DIFF_DETAILS != 0 ]]; then
117+
echo -e "ERROR: use -d or -q not both. For help use -h.\n"
118+
exit 1
111119
fi
112120

113121
if [[ $LIST_KIND == "ALL" && -n $COMMIT_HASH_ARG ]]; then
@@ -209,14 +217,41 @@ function update_file_i18n_hash() {
209217
# echo "WARNING: the given hash is not on 'main', using this instead: $HASH" >&2
210218
# fi
211219

212-
if ! (git branch --contains $HASH | grep -q "^\s*main\b"); then
213-
echo "ERROR: hash is empty or isn't on 'main', aborting: $HASH - $f" >&2
220+
if ! (git branch --contains $HASH | grep -qEe "^\S?\s*main$"); then
221+
echo "ERROR: hash isn't on the default branch (main), aborting: $HASH - $f" >&2
214222
exit 1
215223
fi
216224

217225
set_file_i18n_hash "$f" "$HASH" "$msg" $pre_msg $post_msg
218226
}
219227

228+
function set_file_drifted_status() {
229+
if [[ $FLAG_DRIFTED_STATUS == 0 ]]; then return; fi
230+
231+
local f="$1"
232+
local status="$2"
233+
local pre_msg="${3:- \t }"
234+
local post_msg="${4:-$I18N_DLD_KEY key}" # Not used atm
235+
236+
if [[ $status == "false" ]]; then
237+
perl -i -pe "s/(^$I18N_DLD_KEY):.*$//g" "$f"
238+
elif grep -q "^$I18N_DLD_KEY:" "$f"; then
239+
perl -i -pe "s/(^$I18N_DLD_KEY):.*$/\$1: $status/" "$f"
240+
post_msg="$post_msg UPDATED"
241+
elif ! grep -q "^$I18N_DLC_KEY:" "$f"; then
242+
echo "ERROR: $I18N_DLC_KEY key is missing. Cannot set $I18N_DLC_KEY in $f" >&2
243+
exit 1
244+
else
245+
# Add drifted status immediately after the i18n hash
246+
perl -i -0777 -pe "s/($I18N_DLC_KEY:.*?\n)/\$1$I18N_DLD_KEY: $status\n/sm" "$f"
247+
# perl -i -0777 -pe "s/^(---.*?)(\n---\n)/\$1\n$I18N_DLD_KEY: $status\$2/sm" "$f"
248+
post_msg="$post_msg ADDED"
249+
fi
250+
if [[ -n $FLAG_VERBOSE ]]; then
251+
echo -e "$pre_msg\t$f $I18N_DLD_KEY key set to $status"
252+
fi
253+
}
254+
220255
function main() {
221256
process_CLI_args "$@"
222257

@@ -277,12 +312,14 @@ function main() {
277312
if [[ ! -e "$EN_VERSION" ]]; then
278313
((FILE_PROCESSED_COUNT++))
279314
echo -e "File not found\t$EN_VERSION - $f - $DEFAULT_LANG was removed or renamed"
315+
set_file_drifted_status "$f" "file not found"
280316
continue
281317
fi
282318

283319
# Check default-language version for changes
284320
DIFF=$(git diff --exit-code $EXTRA_DIFF_ARGS $LASTCOMMIT...HEAD "$EN_VERSION" 2>&1)
285321
DIFF_STATUS=$?
322+
DRIFTED_STATUS="false"
286323
if [ $DIFF_STATUS -gt 1 ]; then
287324
((FILE_PROCESSED_COUNT++))
288325
EXIT_STATUS=$DIFF_STATUS
@@ -293,8 +330,9 @@ function main() {
293330
continue
294331
elif [[ -n "$DIFF" ]]; then
295332
((FILE_PROCESSED_COUNT++))
296-
if [[ -n $FLAG_DIFF_DETAILS ]]; then
297-
echo -n "$DIFF"
333+
DRIFTED_STATUS="true"
334+
if [[ $FLAG_DIFF_DETAILS != 0 ]]; then
335+
echo "$DIFF"
298336
elif [[ -n $COMMIT_HASH_ARG ]]; then
299337
update_file_i18n_hash "$f" "$COMMIT_HASH_ARG" "$DIFF"
300338
elif [[ -z $FLAG_QUIET ]]; then
@@ -312,6 +350,7 @@ function main() {
312350
((FILE_PROCESSED_COUNT++))
313351
echo -e "File is in sync\t$f - $LASTCOMMIT"
314352
fi
353+
set_file_drifted_status "$f" $DRIFTED_STATUS
315354
done
316355

317356
if [[ -z $FLAG_QUIET ]]; then

0 commit comments

Comments
 (0)