milahu / nixpkgs

Nix Packages collection
MIT License
1 stars 0 forks source link

substituteStream: dont replace if pattern and replacement are equal #27

Open milahu opened 9 months ago

milahu commented 9 months ago

currently, substituteStream runs the replace if pattern and replacement are equal

so currently, this is *not* a noop

substituteInPlace somefile.txt --replace asdf asdf

actual

substituteStream() {
    local var=$1;
    local description=$2;
    shift 2;
    while (( "$#" )); do
        case "$1" in 
            --replace)
                pattern="$2";
                replacement="$3";
                shift 3;
                local savedvar;
                savedvar="${!var}";
                eval "$var"'=${'"$var"'//"$pattern"/"$replacement"}';
                if [ "$pattern" != "$replacement" ]; then
                    if [ "${!var}" == "$savedvar" ]; then
                        echo "substituteStream(): WARNING: pattern '$pattern' doesn't match anything in $description" 1>&2;
                    fi;
                fi
            ;;