Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 76 additions & 34 deletions functions/fisher.fish
Original file line number Diff line number Diff line change
@@ -1,3 +1,69 @@
function __fisher_fetch_plugin --argument-names plugin source
trap "exit 130" SIGINT

if test -e $plugin
command cp -Rf $plugin/* $source
return
end

set --local temp (command mktemp -d)
trap "command rm -rf $temp" EXIT

# Parse repo and tag (last '@' splits)
set --local repo (string replace --regex -- '@[^@]*$' '' $plugin)
set --local tag (string match --regex -- '@([^@]+)$' $plugin | string replace --regex -- '^@' '')
test -z "$tag" && set tag HEAD

# GitLab tarball
if string match -rq '^(https://)?gitlab.com/' $repo
set --local path (string replace --regex -- '^https?://gitlab.com/' '' $repo)
set --local name (string split -- / $path)[-1]
set --local url https://gitlab.com/$path/-/archive/$tag/$name-$tag.tar.gz
echo Fetching (set_color --underline)$url(set_color normal)
if command curl -q --silent -L $url | command tar -xzC $temp -f - 2>/dev/null
command cp -Rf $temp/*/* $source
return
end
echo "fisher: Invalid plugin name or host unavailable: $plugin" >&2
command rm -rf $source
return
end

# GitHub tarball
if string match -rq '^[a-zA-Z0-9_.-]+/[a-zA-Z0-9_.-]+(@[^@]+)?$' $plugin
set --local url https://api.github.com/repos/$repo/tarball/$tag
echo Fetching (set_color --underline)$url(set_color normal)
if command curl -q --silent -L $url | command tar -xzC $temp -f - 2>/dev/null
command cp -Rf $temp/*/* $source
return
end
echo "fisher: Invalid plugin name or host unavailable: $plugin" >&2
command rm -rf $source
return
end

# Git URLs (ssh, https, .git)
set --local repo_url $plugin
set --local repo_tag HEAD
if string match -rq '@[^/]+$' $plugin
set repo_url (string replace --regex -- '@[^@/]+$' '' $plugin)
set repo_tag (string replace --regex -- '^.*@' '' $plugin)
end

echo Fetching (set_color --underline)$repo_url(set_color normal)
if test "$repo_tag" = HEAD
command git clone --depth 1 --single-branch $repo_url $temp &>/dev/null
else
command git clone $repo_url --depth 1 --branch $repo_tag --single-branch $temp &>/dev/null
end
if test $status -eq 0
command cp -Rf $temp/* $source
else
echo "fisher: Invalid git repo or host unavailable: $plugin" >&2
command rm -rf $source
end
end

function fisher --argument-names cmd --description "A plugin manager for Fish"
set --query fisher_path || set --local fisher_path $__fish_config_dir
set --local fisher_version 4.4.5
Expand All @@ -18,7 +84,7 @@ function fisher --argument-names cmd --description "A plugin manager for Fish"
echo "Variables:"
echo " \$fisher_path Plugin installation path. Default: $__fish_config_dir" | string replace --regex -- $HOME \~
case ls list
string match --entire --regex -- "$argv[2]" $_fisher_plugins
string match --entire --regex -- "$argv[2]" (string replace --regex -- '^'$HOME '~' $_fisher_plugins)
case install update remove
isatty || read --local --null --array stdin && set --append argv $stdin

Expand All @@ -29,7 +95,7 @@ function fisher --argument-names cmd --description "A plugin manager for Fish"
set --local old_plugins $_fisher_plugins
set --local new_plugins

test -e $fish_plugins && set --local file_plugins (string match --regex -- '^[^\s]+$' <$fish_plugins | string replace -- \~ ~)
test -e $fish_plugins && set --local file_plugins (string match --regex -- '^[^\s]+$' <$fish_plugins | string replace --regex -- '^~' $HOME)

if ! set --query argv[2]
if test "$cmd" != update
Expand Down Expand Up @@ -83,32 +149,8 @@ function fisher --argument-names cmd --description "A plugin manager for Fish"
command mkdir -p $source/{completions,conf.d,themes,functions}

$fish_path --command "
if test -e $plugin
command cp -Rf $plugin/* $source
else
set temp (command mktemp -d)
set repo (string split -- \@ $plugin) || set repo[2] HEAD

if set path (string replace --regex -- '^(https://)?gitlab.com/' '' \$repo[1])
set name (string split -- / \$path)[-1]
set url https://gitlab.com/\$path/-/archive/\$repo[2]/\$name-\$repo[2].tar.gz
else
set url https://api.github.com/repos/\$repo[1]/tarball/\$repo[2]
end

echo Fetching (set_color --underline)\$url(set_color normal)

if command curl -q --silent -L \$url | command tar -xzC \$temp -f - 2>/dev/null
command cp -Rf \$temp/*/* $source
else
echo fisher: Invalid plugin name or host unavailable: \\\"$plugin\\\" >&2
command rm -rf $source
end

command rm -rf \$temp
end

set files $source/* && string match --quiet --regex -- .+\.fish\\\$ \$files
$(functions __fisher_fetch_plugin)
__fisher_fetch_plugin \"$plugin\" \"$source\"
" &

set --append pid_list (jobs --last --pid)
Expand All @@ -134,11 +176,11 @@ function fisher --argument-names cmd --description "A plugin manager for Fish"
for name in (string replace --filter --regex -- '.+/conf\.d/([^/]+)\.fish$' '$1' $$plugin_files_var)
emit {$name}_uninstall
end
printf "%s\n" Removing\ (set_color red --bold)$plugin(set_color normal) " "$$plugin_files_var | string replace -- \~ ~
printf "%s\n" Removing\ (set_color red --bold)$plugin(set_color normal) " "(string replace --regex -- '^'$HOME '~' $$plugin_files_var)
set --erase _fisher_plugins[$index]
end

command rm -rf (string replace -- \~ ~ $$plugin_files_var)
command rm -rf (string replace --regex -- '^~' $HOME $$plugin_files_var)

functions --erase (string replace --filter --regex -- '.+/functions/([^/]+)\.fish$' '$1' $$plugin_files_var)

Expand Down Expand Up @@ -178,12 +220,12 @@ function fisher --argument-names cmd --description "A plugin manager for Fish"

set --local plugin_files_var _fisher_(string escape --style=var -- $plugin)_files

set --query files[1] && set --universal $plugin_files_var (string replace -- $source $fisher_path $files | string replace -- ~ \~)
set --query files[1] && set --universal $plugin_files_var (string replace -- $source $fisher_path $files | string replace --regex -- '^'$HOME '~')

contains -- $plugin $_fisher_plugins || set --universal --append _fisher_plugins $plugin
contains -- $plugin $install_plugins && set --local event install || set --local event update

printf "%s\n" Installing\ (set_color --bold)$plugin(set_color normal) " "$$plugin_files_var | string replace -- \~ ~
printf "%s\n" Installing\ (set_color --bold)$plugin(set_color normal) " "(string replace --regex -- '^'$HOME '~' $$plugin_files_var)

for file in (string match --regex -- '.+/[^/]+\.fish$' $$plugin_files_var | string replace -- \~ ~)
source $file
Expand All @@ -206,7 +248,7 @@ function fisher --argument-names cmd --description "A plugin manager for Fish"
contains -- (string lower -- $plugin) (string lower -- $commit_plugins) || set --append commit_plugins $plugin
end

string replace --regex -- $HOME \~ $commit_plugins >$fish_plugins
string replace --regex -- '^'$HOME '~' $commit_plugins >$fish_plugins
else
set --erase _fisher_plugins
command rm -f $fish_plugins
Expand All @@ -233,7 +275,7 @@ if ! set --query _fisher_upgraded_to_4_4
fisher update >/dev/null 2>/dev/null
else
for var in (set --names | string match --entire --regex '^_fisher_.+_files$')
set $var (string replace -- ~ \~ $$var)
set $var (string replace --regex -- '^~' $HOME $$var)
end
functions --erase _fisher_fish_postexec
end
Expand Down