| 1 |
#!/usr/bin/env bash |
| 2 |
|
| 3 |
# This script applies the contents of "common.sh" to the other files. |
| 4 |
|
| 5 |
set -e |
| 6 |
|
| 7 |
dir=${0%"${0##*/}"} |
| 8 |
|
| 9 |
update() { |
| 10 |
{ |
| 11 |
sed -n '1,/^#----BEGIN INCLUDE common\.sh/p' "$1" |
| 12 |
cat << EOF |
| 13 |
# NOTE: Do not directly edit this section, which is copied from "common.sh". |
| 14 |
# To modify it, one can edit "common.sh" and run "./update.sh" to apply |
| 15 |
# the changes. See code comments in "common.sh" for the implementation details. |
| 16 |
EOF |
| 17 |
echo |
| 18 |
grep -v '^[[:blank:]]*#' "$dir/common.sh" # remove code comments in common.sh |
| 19 |
sed -n '/^#----END INCLUDE/,$p' "$1" |
| 20 |
} > "$1.part" |
| 21 |
|
| 22 |
mv -f "$1.part" "$1" |
| 23 |
} |
| 24 |
|
| 25 |
update "$dir/completion.bash" |
| 26 |
update "$dir/completion.zsh" |
| 27 |
update "$dir/key-bindings.bash" |
| 28 |
update "$dir/key-bindings.zsh" |
| 29 |
|
| 30 |
# Check if --check is in ARGV |
| 31 |
check=0 |
| 32 |
rest=() |
| 33 |
for arg in "$@"; do |
| 34 |
case $arg in |
| 35 |
--check) check=1 ;; |
| 36 |
*) rest+=("$arg") ;; |
| 37 |
esac |
| 38 |
done |
| 39 |
|
| 40 |
fmt() { |
| 41 |
if ! grep -q "^#----BEGIN shfmt" "$1"; then |
| 42 |
if [[ $check == 1 ]]; then |
| 43 |
shfmt -d "$1" |
| 44 |
return $? |
| 45 |
else |
| 46 |
shfmt -w "$1" |
| 47 |
fi |
| 48 |
else |
| 49 |
{ |
| 50 |
sed -n '1,/^#----BEGIN shfmt/p' "$1" | sed '$d' |
| 51 |
sed -n '/^#----BEGIN shfmt/,/^#----END shfmt/p' "$1" | shfmt --filename "$1" |
| 52 |
sed -n '/^#----END shfmt/,$p' "$1" | sed '1d' |
| 53 |
} > "$1.part" |
| 54 |
|
| 55 |
if [[ $check == 1 ]]; then |
| 56 |
diff -q "$1" "$1.part" |
| 57 |
ret=$? |
| 58 |
rm -f "$1.part" |
| 59 |
return $ret |
| 60 |
fi |
| 61 |
|
| 62 |
mv -f "$1.part" "$1" |
| 63 |
fi |
| 64 |
} |
| 65 |
|
| 66 |
for file in "${rest[@]}"; do |
| 67 |
fmt "$file" || exit $? |
| 68 |
done |