tfffs - fffs - fast and simple shell plugin manager
(HTM) git clone git://src.adamsgaard.dk/fffs
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
(DIR) LICENSE
---
tfffs (4611B)
---
1 #!/bin/sh
2
3 set -e
4
5 version='0.1.0'
6
7 confsubdir=".config/fffs"
8 confdir="$HOME/$confsubdir"
9 plugindir="$confdir/lib"
10 pluginfile="$confdir/plugins"
11
12 show_help() {
13 echo "usage: ${0##*/} [OPTION] SHELL COMMAND"
14 echo "${0##*/} is a fast and simple manager for shell plugins"
15 echo "where SHELL can be 'bash' or 'zsh'."
16 echo "Valid COMMANDs are:"
17 echo " init fetch repositories specified in plugin file and"
18 echo " create configuration for parent shell. Call this"
19 echo " function after every update to the plugin file"
20 echo " update update all local plugin content"
21 echo " clean remove all local plugin content"
22 echo "Valid OPTIONs are:"
23 echo " -h, --help show this message"
24 echo " -v, --version show version and license information"
25 echo
26 echo "Set the plugin sources as git repository URLs in "
27 echo "$pluginfile-zsh or $pluginfile-bash"
28 echo "before running 'init'. Afterwards, add the generated sources file in "
29 echo "the shell-rc file. For ~/.zshrc:"
30 echo " if [ -f \"\$HOME/.config/fffs/lib/zsh/sources\" ]; then"
31 echo " . \"\$HOME/.config/fffs/lib/zsh/sources\""
32 echo " else"
33 echo " fffs zsh init"
34 echo " . \"\$HOME/.config/fffs/lib/zsh/sources\""
35 echo " fi"
36 }
37
38 show_version() {
39 echo "${0##*/} version $version"
40 echo "Licensed under the GNU Public License, v3+"
41 echo "written by Anders Damsgaard, anders@adamsgaard.dk"
42 echo "https://gitlab.com/admesg/fffs"
43 }
44
45 die() {
46 printf '%s\n' "$1" >&2
47 exit 1
48 }
49
50 clone_repositories() {
51 if [ ! -f "$pluginfile-$shell" ] || [ "$(wc -l <"$pluginfile-$shell")" -lt 1 ]; then
52 (>&2 echo "No plugins specified in $pluginfile-$shell" )
53 fi
54
55 # loop over all repositories in $configfile, and clone them if they do not
56 # exist in $plugindir
57 while read -r line; do
58 target="$plugindir/$shell/${line##*/}"
59 if [ -d "$target" ]; then
60 echo "$target already exists, skipping."
61 else
62 echo "$line > $target"
63 git clone "$line" "$target"
64 fi
65 done < "$pluginfile-$shell"
66 }
67
68 update_repositories() {
69 [ ! -d "$plugindir/$shell" ] && return
70
71 for dir in "$plugindir/$shell"/*/; do
72 echo "Updating $dir..."
73 (cd "$dir" && git pull)
74 done
75 init_sources
76 }
77
78 check_if_source_in_shellrc() {
79 if ! grep -r "$confsubdir/lib/$shell/sources" "$HOME/.${shell}rc" \
80 >/dev/null 2>&1; then
81 echo
82 echo "Make sure to source the following line in your ~/.${shell}rc:"
83 echo " $plugindir/$shell/sources"
84 fi
85 }
86
87 init_sources() {
88 echo "Generating source file $plugindir/$shell/sources"
89 mkdir -p "$plugindir/$shell"
90 echo "#!/usr/bin/env $shell" > "$plugindir/$shell/sources"
91
92 if [ "$shell" = "zsh" ]; then
93 find "$plugindir/$shell/" -maxdepth 2 -type f \
94 -iname '*.zsh' \
95 | sed 's/^/. /' >> "$plugindir/$shell/sources"
96 elif [ "$shell" = "bash" ]; then
97 find "$plugindir/$shell/" -maxdepth 2 -type f \
98 -iname '*.sh' -iname '*.bash' \
99 | sed 's/^/. /' >> "$plugindir/$shell/sources"
100 else
101 die "Unknown shell $shell"
102 fi
103
104 check_if_source_in_shellrc
105 }
106
107 init() {
108 clone_repositories
109 init_sources
110 }
111
112 clean() {
113 echo rm -rf "$plugindir"
114 rm -rf "$plugindir"
115 }
116
117
118 ## Parse command-line arguments
119
120 [ $# -lt 1 ] && (show_help && exit 1)
121
122 shell=""
123 while :; do
124 case "$1" in
125 zsh)
126 [ ! "$2" ] && die 'Error: No command specified'
127 shell='zsh'
128 ;;
129 bash)
130 [ ! "$2" ] && die 'Error: No command specified'
131 shell='bash'
132 ;;
133 init)
134 [ -z "$shell" ] && die 'Error: No SHELL specified'
135 (init && exit 0)
136 ;;
137 update)
138 [ -z "$shell" ] && die 'Error: No SHELL specified'
139 (update_repositories && exit 0)
140 ;;
141 upgrade)
142 die 'Did you mean "update"?'
143 ;;
144 clean)
145 [ -z "$shell" ] && die 'Error: No SHELL specified'
146 (clean && exit 0)
147 ;;
148 clear)
149 die 'Did you mean "clean"?'
150 ;;
151 -h|-\?|--help)
152 (show_help && exit 0)
153 ;;
154 -v|--version)
155 (show_version && exit 0)
156 ;;
157 ?*)
158 die 'Error: Unknown option specified'
159 ;;
160 *) # No more options
161 break
162 esac
163 shift
164 done