File: update.sh

package info (click to toggle)
fzf 0.70.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,016 kB
  • sloc: ruby: 5,776; sh: 1,595; asm: 456; makefile: 173
file content (70 lines) | stat: -rwxr-xr-x 1,580 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env bash

# This script applies the contents of "common.sh" to the other files.

set -e

dir=${0%"${0##*/}"}

update() {
  {
    sed -n "1,/^#----BEGIN INCLUDE $1/p" "$2"
    cat << EOF
# NOTE: Do not directly edit this section, which is copied from "$1".
# To modify it, one can edit "$1" and run "./update.sh" to apply
# the changes. See code comments in "$1" for the implementation details.
EOF
    echo
    grep -v '^[[:blank:]]*#' "$dir/$1" # remove code comments from the common file
    sed -n '/^#----END INCLUDE/,$p' "$2"
  } > "$2.part"

  mv -f "$2.part" "$2"
}

update "common.sh" "$dir/completion.bash"
update "common.sh" "$dir/completion.zsh"
update "common.sh" "$dir/key-bindings.bash"
update "common.sh" "$dir/key-bindings.zsh"
update "common.fish" "$dir/completion.fish"
update "common.fish" "$dir/key-bindings.fish"

# Check if --check is in ARGV
check=0
rest=()
for arg in "$@"; do
  case $arg in
    --check) check=1 ;;
    *) rest+=("$arg") ;;
  esac
done

fmt() {
  if ! grep -q "^#----BEGIN shfmt" "$1"; then
    if [[ $check == 1 ]]; then
      shfmt -d "$1"
      return $?
    else
      shfmt -w "$1"
    fi
  else
    {
      sed -n '1,/^#----BEGIN shfmt/p' "$1" | sed '$d'
      sed -n '/^#----BEGIN shfmt/,/^#----END shfmt/p' "$1" | shfmt --filename "$1"
      sed -n '/^#----END shfmt/,$p' "$1" | sed '1d'
    } > "$1.part"

    if [[ $check == 1 ]]; then
      diff -q "$1" "$1.part"
      ret=$?
      rm -f "$1.part"
      return $ret
    fi

    mv -f "$1.part" "$1"
  fi
}

for file in "${rest[@]}"; do
  fmt "$file" || exit $?
done