-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstowsh
executable file
·222 lines (200 loc) · 5.36 KB
/
stowsh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/usr/bin/env bash
_runcommands() {
if [[ $DRYRUN == 1 ]] || [[ $VERBOSE -gt 1 ]]; then
echo "$@"
fi
if [[ $DRYRUN != 1 ]]; then
eval "$@"
fi
}
echoerr() {
printf "%s\n" "$*" >&2
}
deperr() {
echoerr "stowsh requires $1"
}
isgnu() {
if $1 --version >/dev/null 2>&1; then
return 1
else
return 0
fi
}
stowsh_setpaths() {
if command -v grealpath >/dev/null 2>&1; then
rpcmd="grealpath"
elif command -v realpath >/dev/null 2>&1; then
rpcmd="realpath"
else
deperr "GNU coreutils"
return 1
fi
if isgnu $rpcmd; then
deperr "GNU coreutils"
return 1
fi
if command -v gfind >/dev/null 2>&1; then
findcmd="gfind"
elif command -v find >/dev/null 2>&1; then
findcmd="find"
else
deperr "GNU findutils"
return 1
fi
if isgnu $findcmd; then
deperr "GNU findutils"
return 1
fi
if [[ "${USEGIT}" -eq 1 ]]; then
flist="list_git_files"
else
flist="list_files"
fi
}
list_files() {
if [[ "$1" == "-d" ]]; then
$findcmd . -mindepth 1 -type d -printf "%P\0"
else
$findcmd . -type f -printf "%P\0" -or -type l -printf "%P\0"
fi
}
list_git_files() {
gitfiles=($(git ls-files))
if [[ "${#gitfiles[@]}" == 0 ]]; then return; fi
if [[ "$1" == "-d" ]]; then
printf "%s\n" "${gitfiles[@]}" |
xargs -n1 dirname |
sort | uniq |
sed '/^\.$/d' |
tr '\n' '\0'
else
printf "%s\0" "${gitfiles[@]}"
fi
}
stowsh_install() {
stowsh_setpaths || return 1
local pkg=$1
local target
target=$("$rpcmd" "${2-$PWD}")
local commands=()
cd "$pkg" || return 1
mkdir -p "$target"
while IFS= read -r -d '' d; do
commands+=("mkdir -p '$target/$d'")
done < <($flist -d)
while IFS= read -r -d '' f; do
local targetf="$target/$f"
local thisdir
thisdir=$(dirname "$targetf")
local relative
relative=$($rpcmd "$f" --relative-to="$thisdir" --canonicalize-missing)
if [[ ! -f "$targetf" ]]; then
commands+=("ln -s '$relative' '$targetf'")
else
echoerr "$targetf already exists."
if [[ ! $SKIP -eq 1 ]]; then
echoerr "Aborting. Rerun with the -s flag to skip errors."
return 1
fi
fi
done < <($flist)
for cmd in "${commands[@]}"; do
_runcommands "$cmd"
done
}
stowsh_uninstall() {
stowsh_setpaths || return 1
local pkg=$1
local target
target=$("$rpcmd" "${2-$PWD}")
local commands=()
cd "$pkg" || return 1
while IFS= read -r -d '' f; do
local targetf="$target/$f"
if [[ $($rpcmd "$targetf") == $($rpcmd "$f") ]]; then
commands+=("rm '$targetf'")
elif [[ -f "$targetf" ]]; then
echoerr "$targetf does not point to to $($rpcmd "$f")."
if [[ ! $SKIP -eq 1 ]]; then
echoerr "Aborting. Rerun with the -s flag to skip errors."
return 1
fi
elif [[ ! -f "$targetf" ]]; then
echoerr "$targetf does not exist. Nothing to do."
if [[ ! $SKIP -eq 1 ]]; then
echoerr "Aborting. Rerun with the -s flag to skip errors."
return 1
fi
fi
done < <($flist)
while IFS= read -r -d '' d; do
commands+=("[[ -d '$target/$d' ]] && $findcmd '$target/$d' -type d -empty -delete")
done < <($flist -d)
for cmd in "${commands[@]}"; do
_runcommands "$cmd"
done
}
stowsh_help() {
echo "Usage: $0 [-D] [-n] [-s] [-g] [-v[v]] [-t TARGET] PACKAGES..."
}
if [ "$0" = "$BASH_SOURCE" ]; then
UNINSTALL=0
DRYRUN=0
SKIP=0
USEGIT=0
TARGET="$PWD"
PACKAGES=()
while [[ "$@" ]]; do
if [[ $1 =~ ^- ]]; then
OPTIND=1
while getopts ":vhDsngt:" opt; do
case $opt in
h)
stowsh_help
exit 0
;;
D)
UNINSTALL=1
;;
n)
DRYRUN=1
;;
s)
SKIP=1
;;
v)
VERBOSE=$((VERBOSE + 1))
;;
g)
USEGIT=1
;;
t)
TARGET="$OPTARG"
;;
*)
echo "'$OPTARG' is an invalid option/flag"
exit 1
;;
esac
done
shift $((OPTIND - 1))
else
PACKAGES+=("$1")
shift
fi
done
if [[ ${#PACKAGES[@]} -eq 0 ]]; then
stowsh_help
exit 1
fi
for i in ${!PACKAGES[*]}; do
pkg=${PACKAGES[$i]}
if [[ $UNINSTALL -eq 1 ]]; then
if [[ $VERBOSE -gt 0 ]]; then echoerr "Uninstalling $pkg from $TARGET"; fi
stowsh_uninstall "$pkg" "$TARGET"
else
if [[ $VERBOSE -gt 0 ]]; then echoerr "Installing $pkg to $TARGET"; fi
stowsh_install "$pkg" "$TARGET"
fi
done
fi