2016-04-14 00:14:50 +00:00
|
|
|
# Provide a minimalist realpath implementation to help deal with platforms that may not provide it
|
2016-11-13 21:34:19 +00:00
|
|
|
# as a command. If an external realpath or grealpath command is available simply pass all arguments
|
|
|
|
# thru to it. If not fallback to our builtin.
|
|
|
|
|
|
|
|
# The following is slightly subtle. We have to define a realpath function even if there is an
|
|
|
|
# external command by that name. That's because if we don't the parser will select our builtin.
|
|
|
|
# However, we only want our builtin if there is no external realpath command.
|
|
|
|
|
2017-02-13 16:30:38 +00:00
|
|
|
if command -sq realpath
|
2016-11-13 21:34:19 +00:00
|
|
|
function realpath -w realpath -d "print the resolved path [command realpath]"
|
|
|
|
command realpath $argv
|
2016-10-04 00:51:27 +00:00
|
|
|
end
|
2016-11-13 21:34:19 +00:00
|
|
|
exit 0
|
|
|
|
end
|
2016-10-04 00:51:27 +00:00
|
|
|
|
2016-11-13 21:34:19 +00:00
|
|
|
# If there is a HomeBrew installed version of GNU realpath named grealpath use that.
|
2017-02-13 16:30:38 +00:00
|
|
|
if command -sq grealpath
|
2016-11-13 21:34:19 +00:00
|
|
|
function realpath -w grealpath -d "print the resolved path [command grealpath]"
|
|
|
|
command grealpath $argv
|
|
|
|
end
|
|
|
|
exit 0
|
|
|
|
end
|
|
|
|
|
|
|
|
function realpath -d "return an absolute path without symlinks"
|
2020-12-26 07:55:00 +00:00
|
|
|
set -l options h/help q/quiet V-version s/no-symlinks N-strip z/zero
|
2020-03-09 18:36:12 +00:00
|
|
|
set -a options e/canonicalize-existing m/canonicalize-missing L/logical P/physical
|
2017-08-05 01:02:24 +00:00
|
|
|
set -a options 'R-relative-to=' 'B-relative-base='
|
2018-11-22 11:43:35 +00:00
|
|
|
argparse -n realpath $options -- $argv
|
2017-07-13 21:33:11 +00:00
|
|
|
or return
|
|
|
|
|
|
|
|
if set -q _flag_help
|
|
|
|
__fish_print_help realpath
|
|
|
|
return 0
|
2016-11-13 21:34:19 +00:00
|
|
|
end
|
2016-10-04 00:51:27 +00:00
|
|
|
|
2020-12-26 07:55:00 +00:00
|
|
|
set -l supported_flags
|
|
|
|
if set -el _flag_no_symlinks
|
|
|
|
set -el _flag_s
|
|
|
|
set -a supported_flags --no-symlinks
|
|
|
|
end
|
|
|
|
|
2017-07-13 21:33:11 +00:00
|
|
|
# We don't implement any of the other flags so if any are set it's an error.
|
|
|
|
if string match -q '_flag_*' -- (set -l)
|
2020-12-26 07:55:00 +00:00
|
|
|
set -l flags (set -l | string replace --filter --regex '_flag_\w+\s*' '' | sort -u)
|
2017-07-13 21:33:11 +00:00
|
|
|
printf (_ "%s: These flags are not allowed by fish realpath: '%s'") realpath "$flags" >&2
|
|
|
|
echo >&2
|
|
|
|
__fish_print_help realpath
|
|
|
|
return 1
|
|
|
|
end
|
2016-09-18 09:08:19 +00:00
|
|
|
|
2018-11-22 11:43:35 +00:00
|
|
|
if not set -q argv[1]
|
2022-04-04 03:57:55 +00:00
|
|
|
printf (_ "%ls: Expected at least %d args, got only %d\n") realpath 1 0 >&2
|
2018-11-22 11:43:35 +00:00
|
|
|
return 1
|
|
|
|
end
|
|
|
|
|
2017-07-13 21:33:11 +00:00
|
|
|
for path in $argv
|
2020-12-26 07:55:00 +00:00
|
|
|
builtin realpath $supported_flags $path
|
2016-04-14 00:14:50 +00:00
|
|
|
end
|
|
|
|
end
|