uv: update to 0.4.30.
[void-pkg.git] / common / environment / setup / vsed.sh
blobc74c086a4ce85ed2895d9b25371cdc0379b61ccc
1 # Helper function for calling sed on files and checking if the
2 # file is actually changed
4 # NOTE: this will not check if the input is valid, you can problably
5 # make it execute arbirtrary commands via passing '; cmd' to a vsed
6 # call.
8 vsed() {
9 local files=() regexes=() OPTIND OPTSTRING="ie:" has_inline=
11 eval set -- "$(getopt -s bash "$OPTSTRING" "$@")";
13 while getopts "$OPTSTRING" opt; do
14 case $opt in
15 i) has_inline=1 ;;
16 e) regexes+=("$OPTARG") ;;
17 *) ;;
18 esac
19 done
21 if ! [ "$has_inline" ]; then
22 msg_red "$pkgver: vsed: you must specify -i.\n"
23 return 1
26 shift $(($OPTIND - 1))
28 if [ ${#regexes[@]} -eq 0 ] && [ $# -ge 2 ]; then
29 regexes+=("$1")
30 shift
33 if [ ${#regexes[@]} -eq 0 ]; then
34 msg_red "$pkgver: vsed: no regexes specified.\n"
35 return 1
38 for i; do
39 files+=("$i")
40 done
42 if [ ${#files[@]} -eq 0 ]; then
43 msg_red "$pkgver: vsed: no files specified.\n"
44 return 1
47 for f in "${files[@]}"; do
48 olddigest="$($XBPS_DIGEST_CMD "$f")"
49 olddigest="${olddigest%% *}"
51 for rx in "${regexes[@]}"; do
52 sed -i "$f" -e "$rx" || {
53 msg_red "$pkgver: vsed: sed call failed with regex \"$rx\" on file \"$f\"\n"
54 return 1
57 newdigest="$($XBPS_DIGEST_CMD "$f")"
58 newdigest="${newdigest%% *}"
60 msgfunc=msg_warn
61 if [ -n "$XBPS_STRICT" ]; then
62 msgfunc=msg_error
65 if [ "$olddigest" = "$newdigest" ]; then
66 $msgfunc "$pkgver: vsed: regex \"$rx\" didn't change file \"$f\"\n"
68 olddigest="${newdigest}"
69 done
70 done