chore: add a script to update copyright end-year

This commit is contained in:
Zaiming (Stone) Shi 2022-01-05 20:36:43 +01:00
parent b04d91a356
commit 153a44876e
1 changed files with 36 additions and 0 deletions

View File

@ -0,0 +1,36 @@
#!/usr/bin/env bash
## run this script onece a year
set -euo pipefail
THIS_YEAR="$(date +'%Y')"
fix_copyright_year() {
local file="$1"
if [[ $file != *.erl ]] && \
[[ $file != *.ex ]] && \
[[ $file != *.hrl ]] && \
[[ $file != *.proto ]]; then
## Ignore other file
return 0
fi
local copyright_line
copyright_line="$(head -2 "$file" | grep -E "Copyright\s\(c\)\s.+\sEMQ" || true)"
local begin_year
begin_year="$(echo "$copyright_line" | sed -E 's#%% Copyright \(c\) (20..).*#\1#g')"
if [ "$begin_year" = "$THIS_YEAR" ]; then
## new file added this year
return 0
fi
if [ -z "$copyright_line" ]; then
## No Copyright info, it is not intended to add one from this script
echo "Ignored $file"
return 0
fi
sed -E "s#(%% Copyright \(c\) 20..)(-20.. | )(.*)#\1-$THIS_YEAR \3#g" -i "$file"
}
while read -r file; do
fix_copyright_year "$file"
done < <(git ls-files)