Repository navigation
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·261 lines (220 loc) · 6.04 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·261 lines (220 loc) · 6.04 KB
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#!/usr/bin/env zsh
# Install shask for zsh on macOS.
#
# Remote install:
# curl -fsSL https://raw.githubusercontent.com/alexcamlo/shask/main/install.sh | zsh
#
# Local install:
# ./install.sh
set -euo pipefail
: ${SHASK_REPO_URL:=https://github.com/alexcamlo/shask.git}
: ${SHASK_REF:=main}
: ${SHASK_INSTALL_DIR:=$HOME/.local/share/shask}
: ${SHASK_BIN_DIR:=$HOME/.local/bin}
install_script_path="${0:A}"
zshrc="${ZDOTDIR:-$HOME}/.zshrc"
dry_run=0
uninstall=0
update_zshrc=1
usage() {
cat <<EOF
Usage:
install.sh [options]
Options:
--dry-run Print actions without changing files
--uninstall Remove zsh integration and move installed files to Trash
--no-zshrc Do not edit ~/.zshrc
-h, --help Show this help
Environment:
SHASK_REPO_URL Git repo URL (default: ${SHASK_REPO_URL})
SHASK_REF Git ref to install (default: ${SHASK_REF})
SHASK_INSTALL_DIR Install dir (default: ${SHASK_INSTALL_DIR})
SHASK_BIN_DIR Bin dir for shask symlink (default: ${SHASK_BIN_DIR})
EOF
}
while (( $# > 0 )); do
case "$1" in
--dry-run) dry_run=1 ;;
--uninstall) uninstall=1 ;;
--no-zshrc) update_zshrc=0 ;;
-h|--help) usage; exit 0 ;;
*) print -u2 -r -- "Unknown option: $1"; usage >&2; exit 2 ;;
esac
shift
done
say() {
print -r -- "$*"
}
run() {
if (( dry_run )); then
print -r -- "+ $*"
else
"$@"
fi
}
need() {
if ! command -v "$1" >/dev/null 2>&1; then
print -u2 -r -- "shask install: missing required command: $1"
exit 1
fi
}
source_path_for_zshrc() {
local source_file="$SHASK_INSTALL_DIR/shask.zsh"
if [[ "$source_file" == "$HOME/"* ]]; then
source_file="\$HOME/${source_file#$HOME/}"
fi
print -r -- "$source_file"
}
zshrc_block() {
local source_path
source_path="$(source_path_for_zshrc)"
cat <<EOF
# >>> shask >>>
source "$source_path"
# <<< shask <<<
EOF
}
validate_zshrc_blocks() {
awk '
$0 == "# >>> shask >>>" {
if (open) exit 1
open=1
next
}
$0 == "# <<< shask <<<" {
if (!open) exit 1
open=0
}
END { if (open) exit 1 }
' "$zshrc"
}
remove_zshrc_block() {
(( update_zshrc )) || return 0
[[ -f "$zshrc" ]] || return 0
local tmp="${zshrc}.shask.$$"
if ! validate_zshrc_blocks; then
print -u2 -r -- "shask install: malformed managed block in $zshrc; refusing to modify it"
return 1
fi
if (( dry_run )); then
say "Would remove existing shask block from $zshrc"
return 0
fi
awk '
$0 == "# >>> shask >>>" { skip=1; next }
$0 == "# <<< shask <<<" { skip=0; next }
!skip { print }
' "$zshrc" > "$tmp"
mv "$tmp" "$zshrc"
}
write_zshrc_block() {
(( update_zshrc )) || return 0
local block
block="$(zshrc_block)"
if (( dry_run )); then
say "Would replace the existing shask integration in $zshrc with:"
print -r -- "$block"
return 0
fi
touch "$zshrc"
remove_zshrc_block
{
print
print -r -- "$block"
} >> "$zshrc"
}
move_to_trash() {
local item_path="$1"
[[ -e "$item_path" || -L "$item_path" ]] || return 0
local trash_dir="$HOME/.Trash"
local name timestamp target base counter=0
timestamp="$(date +%Y%m%d%H%M%S)"
name="${item_path:t}"
base="$trash_dir/${name}.shask.$timestamp"
target="$base"
while [[ -e "$target" || -L "$target" ]]; do
(( counter += 1 ))
target="${base}.${counter}"
done
if (( dry_run )); then
say "Would move $item_path to $target"
return 0
fi
mkdir -p "$trash_dir"
mv "$item_path" "$target"
}
install_from_local_checkout() {
local source_dir="$1"
run mkdir -p "$SHASK_INSTALL_DIR/bin"
run cp "$source_dir/shask.zsh" "$SHASK_INSTALL_DIR/shask.zsh"
run cp "$source_dir/bin/shask" "$SHASK_INSTALL_DIR/bin/shask"
run chmod +x "$SHASK_INSTALL_DIR/bin/shask"
}
install_from_git() {
need git
if [[ -d "$SHASK_INSTALL_DIR/.git" ]]; then
run git -C "$SHASK_INSTALL_DIR" fetch --depth=1 origin "$SHASK_REF"
run git -C "$SHASK_INSTALL_DIR" checkout -q FETCH_HEAD
elif [[ -d "$SHASK_INSTALL_DIR" && -n "$(command ls -A "$SHASK_INSTALL_DIR" 2>/dev/null)" ]]; then
say "Moving existing non-git install aside: $SHASK_INSTALL_DIR"
move_to_trash "$SHASK_INSTALL_DIR"
run mkdir -p "${SHASK_INSTALL_DIR:h}"
run git clone --depth=1 --branch "$SHASK_REF" "$SHASK_REPO_URL" "$SHASK_INSTALL_DIR"
else
run mkdir -p "${SHASK_INSTALL_DIR:h}"
run git clone --depth=1 --branch "$SHASK_REF" "$SHASK_REPO_URL" "$SHASK_INSTALL_DIR"
fi
}
link_command() {
local name="$1"
local target="$SHASK_INSTALL_DIR/bin/$name"
local link="$SHASK_BIN_DIR/$name"
run mkdir -p "$SHASK_BIN_DIR"
if [[ -L "$link" && "$(readlink "$link")" == "$target" ]]; then
return 0
fi
if [[ -e "$link" || -L "$link" ]]; then
move_to_trash "$link"
fi
run ln -s "$target" "$link"
}
install_shask() {
local script_path="$install_script_path"
local script_dir="${script_path:h}"
if (( update_zshrc )) && [[ -f "$zshrc" ]] && ! validate_zshrc_blocks; then
print -u2 -r -- "shask install: malformed managed block in $zshrc; refusing to install"
return 1
fi
if [[ -f "$script_path" && "${script_path:t}" == "install.sh" && -f "$script_dir/shask.zsh" && -f "$script_dir/bin/shask" ]]; then
say "Installing from local checkout: $script_dir"
install_from_local_checkout "$script_dir"
else
say "Installing from git: $SHASK_REPO_URL#$SHASK_REF"
install_from_git
fi
link_command shask
write_zshrc_block
say "shask installed. Restart zsh or run:"
say " source \"$(source_path_for_zshrc)\""
say "Then try:"
say " shask list files"
}
remove_owned_command_link() {
local name="$1" link="$SHASK_BIN_DIR/$1" existing_target=""
[[ -L "$link" ]] || return 0
existing_target="$(readlink "$link")"
if [[ "$existing_target" == "$SHASK_INSTALL_DIR/bin/$name" ]]; then
move_to_trash "$link"
fi
}
uninstall_shask() {
remove_zshrc_block
remove_owned_command_link shask
move_to_trash "$SHASK_INSTALL_DIR"
say "shask uninstalled. Restart zsh or remove it from this shell session manually."
}
if (( uninstall )); then
uninstall_shask
else
install_shask
fi