Skip to content

Commit

Permalink
[CI] Script to help know if localized pages need updating (#4599)
Browse files Browse the repository at this point in the history
  • Loading branch information
chalin authored Jun 6, 2024
1 parent 1d9a1df commit 52ac0a1
Showing 1 changed file with 103 additions and 0 deletions.
103 changes: 103 additions & 0 deletions scripts/i18n-check.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#!/bin/bash
#
# Inspired by https://github.com/kubernetes/website/blob/main/scripts/lsync.sh

EXTRA_DIFF_ARGS="--numstat"
TARGET_PATHS=""

function _usage() {
cat <<EOS
Usage: $(basename "$0") [options] TARGET_PATH ...
For each localized page target, this script reports whether the English
language version of that page has changed since the localized file was
first written or last edited.
TARGET_PATH can be a single markdown file of a localized page, such as
'content/ja/_index.md', or a directory of localized pages, such as 'content/ja'.
-h Output this usage info.
-d Output diff details.
-v Verbose mode.
EOS
}

function usage() {
local status=${1:-0}
_usage 1>&2
exit $status
}

function process_CLI_args() {
while getopts ":hdv" opt; do
case $opt in
h)
usage
;;
d)
EXTRA_DIFF_ARGS=""
;;
v)
VERBOSE=1
;;
\?)
echo "ERROR: unrecognized flag: -$OPTARG"
usage 1
;;
esac
done

shift $((OPTIND-1))
if [ "$#" -lt 1 ]; then
echo "ERROR: target path argument is missing" >&2
usage 1
fi

TARGET_PATHS="$@"

if [[ -f "TARGET_PATHS" && ! -e "$TARGET_PATHS" ]] ; then
echo "Path not found: '$TARGET_PATHS'" >&2
exit 2
fi
}

function main() {
process_CLI_args "$@"

if [ -f "$TARGET_PATHS" ] ; then
TARGETS="$TARGET_PATHS"
else
TARGETS=$(find $TARGET_PATHS -name "*.md")
if [[ -z "$TARGETS" ]]; then
echo "ERROR: target directory contains no markdown files: '$TARGET_PATHS'" >&2
exit 1
fi
# if [[ -n $VERBOSE ]]; then echo -e "All targets: $TARGETS"; fi
fi

SYNCED=1
for f in $TARGETS; do
# if [[ -n $VERBOSE ]]; then echo -e "Checking\t$f"; fi
EN_VERSION=$(echo "$f" | sed "s/content\/.\{2,5\}\//content\/en\//g")
if [[ ! -e "$EN_VERSION" ]]; then
echo "Base file renamed or removed: $EN_VERSION"
SYNCED=0
continue
fi

LASTCOMMIT=$(git log -n 1 --pretty=format:%h -- "$f")
git diff --exit-code $EXTRA_DIFF_ARGS $LASTCOMMIT...HEAD "$EN_VERSION"
if [ $? -ne 0 ] ; then
SYNCED=0
elif [[ -n $VERBOSE ]]; then
echo -e "File is in sync\t$f"
fi
done
if [ $SYNCED -ne 1 ]; then
exit 1
fi

echo "$TARGET_PATHS is still in sync"
}

main "$@"

0 comments on commit 52ac0a1

Please sign in to comment.