milahu / nixpkgs

Nix Packages collection
MIT License
0 stars 0 forks source link

substituteInPlace should accept multiple replacement strings for multiple lines #44

Open milahu opened 2 months ago

milahu commented 2 months ago

using substituteInPlace to replace one line with multiple lines is a bit ugly now

    substituteInPlace scripts/generate.py \
      --replace-fail \
        "Path(\"" \
        "Path(\"$out/opt/get-pip/" \
      --replace-fail \
        '    data = requests.get(' \
        "$(
          echo '    if os.getenv("GET_PIP_OFFLINE") == "1":'
          echo '        return {Version("${pip-whl.version}"): ("${pip-whl}", "'$(sha256sum ${pip-whl} | cut -c1-64)'")}'
          echo '    data = requests.get('
        )" \

maybe we can allow 2 or more arguments for --replace the first argument is the search string arguments 2 and following are the replacement string and multiple strings are joined with \n

so this would look like

    # NOTE this does not work
    substituteInPlace scripts/generate.py \
      --replace-fail \
        "Path(\"" \
        "Path(\"$out/opt/get-pip/" \
      --replace-fail \
        '    data = requests.get(' \
        '    if os.getenv("GET_PIP_OFFLINE") == "1":' \
        '        return {Version("${pip-whl.version}"): ("${pip-whl}", "'$(sha256sum ${pip-whl} | cut -c1-64)'")}' \
        '    data = requests.get(' \

why? patching with substituteInPlace is more stable than with patch files the leading quotes for every line are needed to preserve indents

how? this would require that all file paths come before the --replace arguments

alternatives? lib.escapeShellArg and builtins.concatStringsSep ~~but with $'...' dollar-prefixed bash strings for special characters like newlines see also #45~~

    substituteInPlace scripts/generate.py \
      --replace-fail \
        "Path(\"" \
        "Path(\"$out/opt/get-pip/" \
      --replace-fail \
        '    data = requests.get(' \
        ${lib.escapeShellArg (builtins.concatStringsSep "\n" [
          ''    if os.getenv("GET_PIP_OFFLINE") == "1":''
          ''        return {Version("${pip-whl.version}"): ("${pip-whl}", "'$(sha256sum ${pip-whl} | cut -c1-64)'")}''
          ''    data = requests.get(''
        ])} \