fisher.fish 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. set -g fisher_version 3.1.1
  2. type source >/dev/null; or function source; . $argv; end
  3. switch (command uname)
  4. case Darwin FreeBSD
  5. function _fisher_now -a elapsed
  6. command perl -MTime::HiRes -e 'printf("%.0f\n", (Time::HiRes::time() * 1000) - $ARGV[0])' $elapsed
  7. end
  8. case \*
  9. function _fisher_now -a elapsed
  10. command date "+%s%3N" | command awk -v ELAPSED="$elapsed" '{ sub(/%?3N$/, "000") } $0 -= ELAPSED'
  11. end
  12. end
  13. function fisher -a cmd -d "fish package manager"
  14. if not command which curl >/dev/null
  15. echo "curl is required to use fisher -- install curl and try again" >&2
  16. return 1
  17. end
  18. test -z "$XDG_CACHE_HOME"; and set XDG_CACHE_HOME ~/.cache
  19. test -z "$XDG_CONFIG_HOME"; and set XDG_CONFIG_HOME ~/.config
  20. set -g fish_config $XDG_CONFIG_HOME/fish
  21. set -g fisher_cache $XDG_CACHE_HOME/fisher
  22. set -g fisher_config $XDG_CONFIG_HOME/fisher
  23. test -z "$fisher_path"; and set -g fisher_path $fish_config
  24. command mkdir -p {$fish_config,$fisher_path}/{functions,completions,conf.d} $fisher_cache
  25. if test ! -e "$fisher_path/completions/fisher.fish"
  26. echo "fisher self-complete" > $fisher_path/completions/fisher.fish
  27. _fisher_self_complete
  28. end
  29. if test -e "$fisher_path/conf.d/fisher.fish"
  30. command rm -f $fisher_path/conf.d/fisher.fish
  31. end
  32. switch "$version"
  33. case \*-\*
  34. case 2\*
  35. echo "fisher copy-user-key-bindings" > $fisher_path/conf.d/fisher.fish
  36. end
  37. switch "$cmd"
  38. case self-complete
  39. _fisher_self_complete
  40. case copy-user-key-bindings
  41. _fisher_copy_user_key_bindings
  42. case ls
  43. _fisher_ls | command sed "s|$HOME|~|"
  44. case -v {,--}version
  45. _fisher_version (status -f)
  46. case -h {,--}help
  47. _fisher_help
  48. case self-update
  49. _fisher_self_update (status -f); or return
  50. _fisher_self_complete
  51. case self-uninstall
  52. _fisher_self_uninstall
  53. case add rm ""
  54. if test ! -z "$argv"
  55. if not isatty
  56. while read -l i
  57. set argv $argv $i
  58. end
  59. end
  60. end
  61. _fisher_commit $argv; or return
  62. _fisher_self_complete
  63. case \*
  64. echo "error: unknown flag or command \"$cmd\"" >&2
  65. _fisher_help >&2
  66. return 1
  67. end
  68. end
  69. function _fisher_self_complete
  70. complete -c fisher --erase
  71. complete -xc fisher -n __fish_use_subcommand -a version -d "Show version"
  72. complete -xc fisher -n __fish_use_subcommand -a help -d "Show help"
  73. complete -xc fisher -n __fish_use_subcommand -a self-update -d "Update fisher"
  74. complete -xc fisher -n __fish_use_subcommand -a ls -d "List installed packages"
  75. complete -xc fisher -n __fish_use_subcommand -a rm -d "Remove packages"
  76. complete -xc fisher -n __fish_use_subcommand -a add -d "Add packages"
  77. for pkg in (_fisher_ls)
  78. complete -xc fisher -n "__fish_seen_subcommand_from rm" -a $pkg
  79. end
  80. end
  81. function _fisher_copy_user_key_bindings
  82. if functions -q fish_user_key_bindings
  83. functions -c fish_user_key_bindings fish_user_key_bindings_copy
  84. end
  85. function fish_user_key_bindings
  86. for file in $fisher_path/conf.d/*_key_bindings.fish
  87. source $file >/dev/null 2>/dev/null
  88. end
  89. if functions -q fish_user_key_bindings_copy
  90. fish_user_key_bindings_copy
  91. end
  92. end
  93. end
  94. function _fisher_ls
  95. set -l pkgs $fisher_config/*/*/*
  96. for pkg in $pkgs
  97. command readlink $pkg; and continue; or echo $pkg
  98. end | command sed "s|$fisher_config/*||;s|github\.com/||"
  99. end
  100. function _fisher_version -a file
  101. echo "fisher version $fisher_version $file" | command sed "s|$HOME|~|"
  102. end
  103. function _fisher_help
  104. echo "usage: "
  105. echo " fisher add <PACKAGES> add packages"
  106. echo " fisher rm <PACKAGES> remove packages"
  107. echo " fisher update installed packages"
  108. echo " fisher ls show installed packages"
  109. echo " fisher help show this help"
  110. echo " fisher version show version"
  111. echo " fisher self-update update fisher"
  112. echo " fisher self-uninstall uninstall fisher"
  113. echo
  114. echo "examples:"
  115. echo " fisher add jethrokuan/z rafaelrinaldi/pure"
  116. echo " fisher add gitlab.com/owner/foobar@v2"
  117. echo " fisher add ~/path/to/myfish/pkg"
  118. echo " fisher rm rafaelrinaldi/pure"
  119. echo " fisher ls | fisher rm"
  120. echo " fisher add < bundle"
  121. end
  122. function _fisher_self_update -a file
  123. set -l url "https://raw.githubusercontent.com/jorgebucaran/fisher/master/fisher.fish"
  124. echo "fetching $url" >&2
  125. curl -s "$url?nocache" >$file@
  126. set -l next_version (awk 'NR == 1 { print $4; exit }' < $file@)
  127. switch "$next_version"
  128. case "" $fisher_version
  129. command rm -f $file@
  130. if test -z "$next_version"
  131. echo "cannot update fisher -- are you offline?" >&2
  132. return 1
  133. end
  134. echo "fisher is already up-to-date" >&2
  135. case \*
  136. echo "linking $file" | command sed "s|$HOME|~|" >&2
  137. command mv -f $file@ $file
  138. source $file
  139. echo "updated fisher to $fisher_version -- hooray!" >&2
  140. end
  141. end
  142. function _fisher_self_uninstall
  143. set -l current_pkgs $fisher_config/*/*/*
  144. for path in $fisher_cache (_fisher_pkg_remove_all $current_pkgs) $fisher_config $fisher_path/{functions,completions,conf.d}/fisher.fish $fish_config/fishfile
  145. echo "removing $path"
  146. command rm -rf $path 2>/dev/null
  147. end | command sed "s|$HOME|~|" >&2
  148. set -e fisher_cache
  149. set -e fisher_config
  150. set -e fisher_path
  151. set -e fisher_version
  152. complete -c fisher --erase
  153. functions -e (functions -a | command awk '/^_fisher/') fisher
  154. echo "done -- see you again!" >&2
  155. end
  156. function _fisher_commit
  157. set -l elapsed (_fisher_now)
  158. set -l current_pkgs $fisher_config/*/*/*
  159. set -l removed_pkgs (_fisher_pkg_remove_all $current_pkgs)
  160. command rm -rf $fisher_config
  161. command mkdir -p $fisher_config
  162. set -l fishfile $fish_config/fishfile
  163. if test ! -e "$fishfile"
  164. command touch $fishfile
  165. echo "created empty fishfile in $fishfile" | command sed "s|$HOME|~|" >&2
  166. end
  167. printf "%s\n" (_fisher_fishfile_format (
  168. echo -s $argv\;) (
  169. echo -s $removed_pkgs\;
  170. ) < $fishfile) > $fishfile
  171. set -l expected_pkgs (_fisher_fishfile_read < $fishfile)
  172. set -l added_pkgs (_fisher_pkg_fetch_all $expected_pkgs)
  173. set -l updated_pkgs (
  174. for pkg in $removed_pkgs
  175. set pkg (echo $pkg | command sed "s|$fisher_config/*||")
  176. if contains -- $pkg $added_pkgs
  177. echo $pkg
  178. end
  179. end)
  180. if test -z "$added_pkgs$updated_pkgs$removed_pkgs$expected_pkgs"
  181. echo "nothing to commit -- try adding some packages" >&2
  182. return 1
  183. end
  184. _fisher_status (count $added_pkgs) (count $updated_pkgs) (count $removed_pkgs) (_fisher_now $elapsed) >&2
  185. end
  186. function _fisher_pkg_remove_all
  187. for pkg in $argv
  188. echo $pkg
  189. _fisher_pkg_uninstall $pkg
  190. end
  191. end
  192. function _fisher_pkg_fetch_all
  193. set -l pkg_jobs
  194. set -l local_pkgs
  195. set -l actual_pkgs
  196. set -l expected_pkgs
  197. for id in $argv
  198. switch $id
  199. case \~\* /\*
  200. set -l path (echo "$id" | command sed "s|~|$HOME|")
  201. if test -e "$path"
  202. set local_pkgs $local_pkgs $path
  203. else
  204. echo "cannot install \"$id\" -- is this a valid file?" >&2
  205. end
  206. continue
  207. end
  208. command awk -v ID=$id -v FS=/ 'BEGIN {
  209. if (split(ID, tmp, /@+|:/) > 2) {
  210. if (tmp[4]) sub("@"tmp[4], "", ID)
  211. print ID "\t" tmp[2]"/"tmp[1]"/"tmp[3] "\t" (tmp[4] ? tmp[4] : "master")
  212. } else {
  213. pkg = split(ID, _, "/") <= 2 ? "github.com/"tmp[1] : tmp[1]
  214. tag = tmp[2] ? tmp[2] : "master"
  215. print (\
  216. pkg ~ /^github/ ? "https://codeload."pkg"/tar.gz/"tag : \
  217. pkg ~ /^gitlab/ ? "https://"pkg"/-/archive/"tag"/"tmp[split(pkg, tmp, "/")]"-"tag".tar.gz" : \
  218. pkg ~ /^bitbucket/ ? "https://"pkg"/get/"tag".tar.gz" : pkg \
  219. ) "\t" pkg
  220. }
  221. }' | read -l url pkg tag
  222. if test ! -d "$fisher_config/$pkg"
  223. fish -c "
  224. echo fetching $url >&2
  225. command mkdir -p \"$fisher_config/$pkg\"
  226. if test ! -z \"$tag\"
  227. command git clone $url \"$fisher_config/$pkg\" --branch $tag --depth 1 2>/dev/null
  228. or echo cannot clone \"$url\" -- is this a valid url\? >&2
  229. else if command curl -Ss $url 2>&1 | command tar -xzf- -C \"$fisher_config/$pkg\" --strip-components=1 2>/dev/null
  230. command mkdir -p \"$fisher_cache/$pkg\"
  231. command cp -Rf \"$fisher_config/$pkg\" \"$fisher_cache/$pkg/..\"
  232. else if test -d \"$fisher_cache/$pkg\"
  233. echo cannot connect to server -- searching in \"$fisher_cache/$pkg\" | command sed 's|$HOME|~|' >&2
  234. command cp -Rf \"$fisher_cache/$pkg\" \"$fisher_config/$pkg/..\"
  235. else
  236. command rm -rf \"$fisher_config/$pkg\"
  237. echo cannot install \"$pkg\" -- is this a valid package\? >&2
  238. end
  239. " >/dev/null &
  240. set pkg_jobs $pkg_jobs (_fisher_jobs --last)
  241. set expected_pkgs $expected_pkgs "$pkg"
  242. end
  243. end
  244. if test ! -z "$pkg_jobs"
  245. _fisher_wait $pkg_jobs
  246. for pkg in $expected_pkgs
  247. if test -d "$fisher_config/$pkg"
  248. set actual_pkgs $actual_pkgs $pkg
  249. _fisher_pkg_install $fisher_config/$pkg
  250. end
  251. end
  252. end
  253. for pkg in $local_pkgs
  254. set -l path local/$USER
  255. set -l name (command basename $pkg)
  256. command mkdir -p $fisher_config/$path
  257. command ln -sf $pkg $fisher_config/$path
  258. set actual_pkgs $actual_pkgs $path/$name
  259. _fisher_pkg_install $fisher_config/$path/$name
  260. end
  261. if test ! -z "$actual_pkgs"
  262. _fisher_pkg_fetch_all (_fisher_pkg_get_deps $actual_pkgs | command sort --unique)
  263. printf "%s\n" $actual_pkgs
  264. end
  265. end
  266. function _fisher_pkg_get_deps
  267. for pkg in $argv
  268. set -l path $fisher_config/$pkg
  269. if test ! -d "$path"
  270. echo $pkg
  271. else if test -s "$path/fishfile"
  272. _fisher_pkg_get_deps (_fisher_fishfile_format < $path/fishfile | _fisher_fishfile_read)
  273. end
  274. end
  275. end
  276. function _fisher_pkg_install -a pkg
  277. set -l name (command basename $pkg)
  278. set -l files $pkg/{functions,completions,conf.d}/**.* $pkg/*.fish
  279. for source in $files
  280. set -l target (command basename $source)
  281. switch $source
  282. case $pkg/conf.d\*
  283. set target $fisher_path/conf.d/$target
  284. case $pkg/completions\*
  285. set target $fisher_path/completions/$target
  286. case $pkg/{functions,}\*
  287. switch $target
  288. case uninstall.fish
  289. continue
  290. case init.fish key_bindings.fish
  291. set target $fisher_path/conf.d/$name\_$target
  292. case \*
  293. set target $fisher_path/functions/$target
  294. end
  295. end
  296. echo "linking $target" | command sed "s|$HOME|~|" >&2
  297. command cp -f $source $target
  298. switch $target
  299. case \*.fish
  300. source $target >/dev/null 2>/dev/null
  301. end
  302. end
  303. end
  304. function _fisher_pkg_uninstall -a pkg
  305. set -l name (command basename $pkg)
  306. set -l files $pkg/{conf.d,completions,functions}/**.* $pkg/*.fish
  307. for source in $files
  308. set -l target (command basename $source)
  309. set -l filename (command basename $target .fish)
  310. switch $source
  311. case $pkg/conf.d\*
  312. test "$filename.fish" = "$target"; and emit "$filename"_uninstall
  313. set target conf.d/$target
  314. case $pkg/completions\*
  315. test "$filename.fish" = "$target"; and complete -ec $filename
  316. set target completions/$target
  317. case $pkg/{,functions}\*
  318. test "$filename.fish" = "$target"; and functions -e $filename
  319. switch $target
  320. case uninstall.fish
  321. source $source
  322. continue
  323. case init.fish key_bindings.fish
  324. set target conf.d/$name\_$target
  325. case \*
  326. set target functions/$target
  327. end
  328. end
  329. command rm -f $fisher_path/$target
  330. end
  331. if not functions -q fish_prompt
  332. source "$__fish_datadir$__fish_data_dir/functions/fish_prompt.fish"
  333. end
  334. end
  335. function _fisher_fishfile_read
  336. command awk -v FS=\# '!/^#/ && NF { print $1 }'
  337. end
  338. function _fisher_fishfile_format -a pkgs removed_pkgs
  339. command awk -v PWD=$PWD -v HOME=$HOME -v PKGS="$pkgs" -v REMOVED_PKGS="$removed_pkgs" '
  340. BEGIN {
  341. pkg_count = split(PKGS, pkgs, ";") - 1
  342. cmd = pkgs[1]
  343. for (i = 2; i <= pkg_count; i++) {
  344. pkg_ids[i - 1] = get_pkg_id( pkgs[i] = normalize(pkgs[i]) )
  345. }
  346. } {
  347. if (NF) {
  348. $0 = normalize($0)
  349. newln = newln > 0 ? "" : newln
  350. if (/^#/) print newln$0
  351. else if (!seen_pkgs[(pkg_id = get_pkg_id($0))]++) {
  352. for (i = 1; i < pkg_count; i++) {
  353. if (pkg_ids[i] == pkg_id) {
  354. if (cmd == "rm") next
  355. $0 = pkgs[i + 1]
  356. break
  357. }
  358. }
  359. print newln$0
  360. }
  361. newln = NF
  362. } else if (newln) newln = "\n"(newln > 0 ? "" : newln)
  363. }
  364. END {
  365. if (cmd == "rm" || pkg_count <= 1) {
  366. split(REMOVED_PKGS, tmp, ";")
  367. for (i in tmp) removed_pkgs[normalize(tmp[i])] = i
  368. for (i in pkg_ids) if (!(pkg_ids[i] in removed_pkgs)) {
  369. print "cannot remove \"" pkg_ids[i] "\" -- package not found" > "/dev/stderr"
  370. }
  371. exit
  372. }
  373. for (i in pkg_ids) if (!seen_pkgs[pkg_ids[i]]) print pkgs[i+1]
  374. }
  375. function normalize(s) {
  376. gsub(/^[ \t]*(https?:\/\/)?(.*github\.com\/)?|[\/ \t]*$/, "", s)
  377. sub(/^\.\//, PWD"/", s)
  378. sub(HOME, "~", s)
  379. return s
  380. }
  381. function get_pkg_id(s) {
  382. return (split(s, tmp, /@+|:/) > 2) ? tmp[2]"/"tmp[1]"/"tmp[3] : tmp[1]
  383. }
  384. '
  385. end
  386. function _fisher_status -a added updated removed elapsed
  387. command awk -v ADDED=$added -v UPDATED=$updated -v REMOVED=$removed -v ELAPSED=$elapsed '
  388. BEGIN {
  389. if (ADDED = ADDED - UPDATED) res = msg(res, "added", ADDED)
  390. if (UPDATED) res = msg(res, "updated", UPDATED)
  391. if (REMOVED = REMOVED - UPDATED) res = msg(res, "removed", REMOVED)
  392. printf((res ? res : "done") " in %.2fs\n", ELAPSED / 1000)
  393. }
  394. function msg(res, str, n) {
  395. return (res ? res ", " : "") str " " n " package" (n > 1 ? "s" : "")
  396. }
  397. '
  398. end
  399. function _fisher_jobs
  400. jobs $argv | command awk '/[0-9]+\t/ { print $1 }'
  401. end
  402. function _fisher_wait
  403. while for job in $argv
  404. contains -- $job (_fisher_jobs); and break
  405. end
  406. end
  407. end