fork/fish-shell
git clone https://git.julia.blue/fork/fish-shell
c2e2fd643
master
1function fish_add_path --description"Add paths to the PATH" 2# This is meant to be the easy one-stop shop to adding stuff to $PATH. 3# By default it'll prepend the given paths to a universal $fish_user_paths, excluding the already-included ones. 4# 5# That means it can be executed once in an interactive session, or stuffed in config.fish, 6# and it will do The Right Thing. 7# 8# The options: 9# --prepend or --append to select whether to put the new paths first or last 10# --global or --universal to decide whether to use a universal or global fish_user_paths 11# --path to set $PATH instead 12# --move to move existing entries instead of ignoring them 13# --verbose to print the set-command used 14# --dry-run to print the set-command without running it 15# We do not allow setting $PATH universally. 16# 17# It defaults to keeping $fish_user_paths or creating a universal, prepending and ignoring existing entries. 18argparse -x g,U -x P,U -x a,p g/global U/universal P/path p/prepend a/append h/help m/move v/verbose n/dry-run --$argv 19or return 20 21if set -q _flag_help 22__fish_print_help fish_add_path 23return 0 24end 25 26set -l scope$_flag_global $_flag_universal 27if not set -q scope[ 1 ] ;and not set -q fish_user_paths 28set scope -U 29end 30 31set -l var fish_user_paths 32set -q _flag_path 33and set var PATH 34# $PATH should be global 35and set scope -g 36set -l mode$_flag_prepend $_flag_append 37set -q mode[ 1 ] ;or set mode -p 38 39# Enable verbose mode if we're interactively used 40status current-command| string match -rq'^fish_add_path$' 41and isatty stdout 42and set -l _flag_verbose yes 43 44# To keep the order of our arguments, go through and save the ones we want to keep. 45set -l newpaths 46set -l indexes 47for pathin $argv 48# Realpath allows us to canonicalize the path, which is needed for deduplication. 49# We could add a non-canonical version of the given path if no duplicate exists, but tbh that's a recipe for disaster. 50 51# realpath complains if a parent directory does not exist, so we silence stderr. 52set -l p( builtin realpath -s --$path 2> /dev/null) 53 54# Ignore non-existing paths 55if not test -d " $p " 56# path does not exist 57if set -q _flag_verbose 58# print a message in verbose mode 59if test -f " $p " 60printf ( _ "Skipping path because it is a file instead of a directory: %s\n" ) " $p " 61else 62printf ( _ "Skipping non-existent path: %s\n" ) " $p " 63end 64end 65continue 66end 67 68if set -l ind( contains -i --$p $$var ) 69# In move-mode, we remove it from its current position and add it back. 70if set -q _flag_move;and not contains --$p $newpaths 71set -a indexes$ind 72set -a newpaths$p 73else if set -q _flag_verbose 74printf ( _ "Skipping already included path: %s\n" ) " $p " 75end 76else if not contains --$p $newpaths 77# Without move, we only add it if it's not in. 78set -a newpaths$p 79end 80end 81 82# Ensure the variable is only set once, by constructing a new variable before. 83# This is to stop any handlers or anything from firing more than once. 84set -l newvar$$var 85if set -q _flag_move;and set -q indexes[ 1 ] 86# We remove in one step, so the indexes don't move. 87set -e newvar[ " $indexes " ] 88end 89set $mode newvar$newpaths 90 91# Finally, only set if there is anything *to* set. 92# This saves us from setting, especially in the common case of someone putting this in config.fish 93# to ensure a path is in $PATH. 94if set -q newpaths[ 1 ] ;or set -q indexes[ 1 ] 95if set -q _flag_verbose;or set -q _flag_n 96# The escape helps make it unambiguous - so you see whether an argument includes a space or something. 97echo ( string escape -- set$scope $var $newvar ) 98end 99 100not set -q _flag_n 101and set $scope $var $newvar 102return 0 103else 104if set -q _flag_verbose 105# print a message in verbose mode 106printf ( _ "No paths to add, not setting anything.\n" ) 107end 108return 1 109end 110end