installShellFiles: Allow installManPage to take a piped input

+106 -10
+62 -10
pkgs/by-name/in/installShellFiles/setup-hook.sh
··· 16 16 # 17 17 # See comments on each function for more details. 18 18 19 - # installManPage <path> [...<path>] 19 + # installManPage [--name <path>] <path> [...<path>] 20 20 # 21 21 # Each argument is checked for its man section suffix and installed into the appropriate 22 22 # share/man/man<n>/ directory. The function returns an error if any paths don't have the man 23 23 # section suffix (with optional .gz compression). 24 + # 25 + # Optionally accepts pipes as input, which when provided require the `--name` argument to 26 + # name the output file. 27 + # 28 + # installManPage --name foobar.1 <($out/bin/foobar --manpage) 24 29 installManPage() { 25 - local path 26 - for path in "$@"; do 27 - if test -z "$path"; then 30 + local arg name='' continueParsing=1 31 + while { arg=$1; shift; }; do 32 + if (( continueParsing )); then 33 + case "$arg" in 34 + --name) 35 + name=$1 36 + shift || { 37 + nixErrorLog "${FUNCNAME[0]}: --name flag expected an argument" 38 + return 1 39 + } 40 + continue;; 41 + --name=*) 42 + # Treat `--name=foo` that same as `--name foo` 43 + name=${arg#--name=} 44 + continue;; 45 + --) 46 + continueParsing=0 47 + continue;; 48 + esac 49 + fi 50 + 51 + nixInfoLog "${FUNCNAME[0]}: installing $arg${name:+ as $name}" 52 + local basename 53 + 54 + # Check if path is empty 55 + if test -z "$arg"; then 56 + # It is an empty string 28 57 nixErrorLog "${FUNCNAME[0]}: path cannot be empty" 29 58 return 1 30 59 fi 31 - nixInfoLog "${FUNCNAME[0]}: installing $path" 32 - local basename 33 - basename=$(stripHash "$path") # use stripHash in case it's a nix store path 60 + 61 + if test -n "$name"; then 62 + # Provided name. Required for pipes, optional for paths 63 + basename=$name 64 + elif test -p "$arg"; then 65 + # Named pipe requires a file name 66 + nixErrorLog "${FUNCNAME[0]}: named pipe requires --name argument" 67 + else 68 + # Normal file without a name 69 + basename=$(stripHash "$arg") # use stripHash in case it's a nix store path 70 + fi 71 + 72 + # Check that it is well-formed 34 73 local trimmed=${basename%.gz} # don't get fooled by compressed manpages 35 74 local suffix=${trimmed##*.} 36 75 if test -z "$suffix" -o "$suffix" = "$trimmed"; then 37 - nixErrorLog "${FUNCNAME[0]}: path missing manpage section suffix: $path" 76 + nixErrorLog "${FUNCNAME[0]}: path missing manpage section suffix: $arg" 38 77 return 1 39 78 fi 79 + 80 + # Create the out-path 40 81 local outRoot 41 82 if test "$suffix" = 3; then 42 83 outRoot=${!outputDevman:?} 43 84 else 44 85 outRoot=${!outputMan:?} 45 86 fi 46 - local outPath="${outRoot}/share/man/man$suffix/$basename" 47 - install -D --mode=644 --no-target-directory "$path" "$outPath" 87 + local outPath="${outRoot}/share/man/man$suffix/" 88 + nixInfoLog "${FUNCNAME[0]}: installing to $outPath" 89 + 90 + # Install 91 + if test -p "$arg"; then 92 + # install doesn't work with pipes on Darwin 93 + mkdir -p "$outPath" && cat "$arg" > "$outPath/$basename" 94 + else 95 + install -D --mode=644 --no-target-directory -- "$arg" "$outPath/$basename" 96 + fi 97 + 98 + # Reset the name for the next page 99 + name= 48 100 done 49 101 } 50 102
+23
pkgs/by-name/in/installShellFiles/tests/install-manpage-fifo.nix
··· 1 + { 2 + lib, 3 + installShellFiles, 4 + runCommandLocal, 5 + }: 6 + 7 + runCommandLocal "install-shell-files--install-manpage-fifo" 8 + { 9 + nativeBuildInputs = [ installShellFiles ]; 10 + meta.platforms = lib.platforms.all; 11 + } 12 + '' 13 + installManPage doc/* 14 + 15 + installManPage \ 16 + --name foo.1 <(echo foo) \ 17 + --name=bar.2 <(echo bar) \ 18 + --name baz.3 <(echo baz) 19 + 20 + echo "foo" | cmp - $out/share/man/man1/foo.1 21 + echo "bar" | cmp - $out/share/man/man2/bar.2 22 + echo "baz" | cmp - $out/share/man/man3/baz.3 23 + ''
+18
pkgs/by-name/in/installShellFiles/tests/install-manpage-named.nix
··· 1 + { 2 + lib, 3 + installShellFiles, 4 + runCommandLocal, 5 + }: 6 + 7 + runCommandLocal "install-shell-files--install-manpage" 8 + { 9 + nativeBuildInputs = [ installShellFiles ]; 10 + meta.platforms = lib.platforms.all; 11 + } 12 + '' 13 + echo foo > foo.1 14 + 15 + installManPage --name bar.1 foo.1 16 + 17 + cmp foo.1 $out/share/man/man1/bar.1 18 + ''
+3
pkgs/by-name/in/installShellFiles/tests/install-manpage.nix
··· 14 14 echo foo > doc/foo.1 15 15 echo bar > doc/bar.2.gz 16 16 echo baz > doc/baz.3 17 + echo buzz > --name.1 17 18 18 19 installManPage doc/* 20 + installManPage -- --name.1 19 21 20 22 cmp doc/foo.1 $out/share/man/man1/foo.1 21 23 cmp doc/bar.2.gz $out/share/man/man2/bar.2.gz 22 24 cmp doc/baz.3 $out/share/man/man3/baz.3 25 + cmp -- --name.1 $out/share/man/man1/--name.1 23 26 ''