-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·442 lines (366 loc) · 12.1 KB
/
install.sh
File metadata and controls
executable file
·442 lines (366 loc) · 12.1 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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
#!/bin/zsh
# Obsidian Plugin Installer
# Discovers vaults and plugins, builds and installs interactively
set -e
# Colours
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
BOLD='\033[1m'
NC='\033[0m' # No colour
SCRIPT_DIR="${0:A:h}"
CONFIG_FILE="$SCRIPT_DIR/.install-vaults"
# Spinner frames
SPINNER_FRAMES=('⠋' '⠙' '⠹' '⠸' '⠼' '⠴' '⠦' '⠧' '⠇' '⠏')
# Global to return selections (avoiding subshell issues with read)
typeset -ga SELECTED_ITEMS
# Command line options
USE_ALL_PLUGINS=false
USE_PREVIOUS_VAULTS=false
SEARCH_DEPTH=""
show_help() {
echo "Usage: ./install.sh [options]"
echo ""
echo "Options:"
echo " -a, --all Install all plugins (skip plugin prompt)"
echo " -p, --previous Use previously selected vaults (skip vault prompt)"
echo " -d, --depth N Override vault search depth (default: 3-5 depending on location)"
echo " -h, --help Show this help message"
echo ""
echo "Examples:"
echo " ./install.sh -a -p Quick reinstall: all plugins to cached vaults"
echo " ./install.sh -d 2 Shallow search (faster, finds fewer vaults)"
echo " ./install.sh -d 8 Deep search (slower, finds nested vaults)"
echo ""
echo "Interactive prompts:"
echo " 0 Select all items"
echo " 1 2 3 Space-separated numbers for specific items"
}
# --- Helper functions ---
print_header() {
echo "\n${BOLD}${BLUE}$1${NC}"
}
print_success() {
echo "${GREEN}✓${NC} $1"
}
print_error() {
echo "${RED}✗${NC} $1" >&2
}
print_warn() {
echo "${YELLOW}!${NC} $1"
}
print_spinner() {
local frame=$1
local message=$2
printf "\r\033[K ${CYAN}${SPINNER_FRAMES[$frame]}${NC} %s" "$message" >&2
}
clear_spinner() {
printf "\r\033[K" >&2
}
# --- Discovery functions ---
find_plugins() {
local -a plugins
for dir in "$SCRIPT_DIR"/*/; do
if [[ -f "${dir}manifest.json" ]]; then
plugins+=("${dir%/}")
fi
done
print -l "${plugins[@]}"
}
get_plugin_info() {
local plugin_dir="$1"
local name=$(basename "$plugin_dir")
local version=$(grep -o '"version"[[:space:]]*:[[:space:]]*"[^"]*"' "$plugin_dir/manifest.json" | head -1 | sed 's/.*"\([^"]*\)"$/\1/')
echo "$name (v$version)"
}
find_vaults() {
local -a vaults
local -a searched_paths
local frame=0
# Default depths per location (can be overridden with -d flag)
local -a search_configs=(
"$HOME/Documents:4"
"$HOME/Desktop:3"
"$HOME/Library/Mobile Documents/iCloud~md~obsidian/Documents:4"
"$HOME:5"
)
for config in "${search_configs[@]}"; do
local search_path="${config%:*}"
local max_depth="${config#*:}"
# Override with user-specified depth if provided
[[ -n "$SEARCH_DEPTH" ]] && max_depth="$SEARCH_DEPTH"
[[ -d "$search_path" ]] || continue
local skip=false
for searched in "${searched_paths[@]}"; do
if [[ "$search_path" == "$searched"/* ]]; then
skip=true
break
fi
done
$skip && continue
local display_path="${search_path/#$HOME/~}"
print_spinner $frame "Searching ${display_path}..."
frame=$(( (frame + 1) % ${#SPINNER_FRAMES[@]} ))
while IFS= read -r obsidian_dir; do
[[ -z "$obsidian_dir" ]] && continue
local vault_dir="${obsidian_dir:h}"
local is_dup=false
for existing in "${vaults[@]}"; do
if [[ "$existing" == "$vault_dir" ]]; then
is_dup=true
break
fi
done
$is_dup || vaults+=("$vault_dir")
done < <(find "$search_path" -maxdepth "$max_depth" -type d -name ".obsidian" 2>/dev/null)
searched_paths+=("$search_path")
done
clear_spinner
print -l "${vaults[@]}"
}
load_cached_vaults() {
[[ -f "$CONFIG_FILE" ]] || return 1
local -a cached
while IFS= read -r line; do
[[ -d "$line/.obsidian" ]] && cached+=("$line")
done < "$CONFIG_FILE"
[[ ${#cached[@]} -gt 0 ]] || return 1
print -l "${cached[@]}"
}
save_vaults_to_cache() {
print -l "$@" > "$CONFIG_FILE"
}
# --- Selection functions ---
# Sets SELECTED_ITEMS global array with user selections
# Args: prompt, items...
# Returns 0 on success, 1 if no items provided
select_items() {
local prompt="$1"
shift
local -a items=("$@")
local count=${#items[@]}
SELECTED_ITEMS=()
if [[ $count -eq 0 ]]; then
return 1
fi
echo " ${BOLD}0)${NC} ${CYAN}All${NC}"
for i in {1..$count}; do
echo " ${BOLD}$i)${NC} ${items[$i]}"
done
echo ""
echo -n "$prompt (0 for all, or space-separated numbers): "
read -r selection </dev/tty
if [[ "$selection" == "0" || "$selection" == "all" ]]; then
SELECTED_ITEMS=("${items[@]}")
else
for num in ${=selection}; do
if [[ "$num" =~ ^[0-9]+$ ]] && [[ $num -ge 1 ]] && [[ $num -le $count ]]; then
SELECTED_ITEMS+=("${items[$num]}")
fi
done
fi
return 0
}
# --- Build and install functions ---
# Display error output in a bordered block
show_error_block() {
local title="$1"
local output="$2"
echo ""
echo " ${RED}┌─ ${title} ─────────────────────────────────${NC}"
echo "$output" | while IFS= read -r line; do
echo " ${RED}│${NC} $line"
done
echo " ${RED}└────────────────────────────────────────────${NC}"
}
build_plugin() {
local plugin_dir="$1"
local plugin_name=$(basename "$plugin_dir")
local output
echo -n "Building ${BOLD}$plugin_name${NC}... "
if [[ ! -f "$plugin_dir/package.json" ]]; then
print_error "No package.json found"
return 1
fi
output=$( (cd "$plugin_dir" && npm install 2>&1) ) || {
print_error "npm install failed"
show_error_block "npm install" "$output"
return 1
}
output=$( (cd "$plugin_dir" && npm run build 2>&1) ) || {
print_error "Build failed"
show_error_block "npm run build" "$output"
return 1
}
print_success "Done"
}
install_plugin() {
local plugin_dir="$1"
local vault_dir="$2"
local plugin_name=$(basename "$plugin_dir")
local target_dir="$vault_dir/.obsidian/plugins/$plugin_name"
mkdir -p "$target_dir"
local files_copied=0
for file in main.js manifest.json styles.css; do
if [[ -f "$plugin_dir/$file" ]]; then
cp "$plugin_dir/$file" "$target_dir/"
((files_copied++)) || true
fi
done
if [[ $files_copied -eq 0 ]]; then
print_error "No files to install (missing main.js?)"
return 1
fi
local display_path="${target_dir/#$HOME/~}"
print_success "$display_path"
}
# --- Main ---
main() {
# Parse command line arguments
while [[ $# -gt 0 ]]; do
case "$1" in
-a|--all)
USE_ALL_PLUGINS=true
shift
;;
-p|--previous)
USE_PREVIOUS_VAULTS=true
shift
;;
-d|--depth)
if [[ -z "$2" || "$2" == -* ]]; then
print_error "Option -d requires a numeric argument"
exit 1
fi
if ! [[ "$2" =~ ^[0-9]+$ ]]; then
print_error "Depth must be a positive integer"
exit 1
fi
SEARCH_DEPTH="$2"
shift 2
;;
-h|--help)
show_help
exit 0
;;
*)
print_error "Unknown option: $1"
show_help
exit 1
;;
esac
done
echo "${BOLD}${BLUE}Obsidian Plugin Installer${NC}"
# Find plugins
print_header "Searching for plugins..."
local -a plugin_dirs
while IFS= read -r line; do
[[ -n "$line" ]] && plugin_dirs+=("$line")
done < <(find_plugins)
if [[ ${#plugin_dirs[@]} -eq 0 ]]; then
print_error "No plugins found (looking for directories with manifest.json)"
exit 1
fi
local -a plugin_names
for dir in "${plugin_dirs[@]}"; do
plugin_names+=("$(get_plugin_info "$dir")")
done
local -a plugins_to_install
if [[ "$USE_ALL_PLUGINS" == "true" ]]; then
print_header "Installing all ${#plugin_dirs[@]} plugins:"
plugins_to_install=("${plugin_dirs[@]}")
for name in "${plugin_names[@]}"; do
echo " ${CYAN}•${NC} $name"
done
else
print_header "Found ${#plugin_dirs[@]} plugins:"
select_items "Select plugins to install" "${plugin_names[@]}"
local -a selected_plugins=("${SELECTED_ITEMS[@]}")
if [[ ${#selected_plugins[@]} -eq 0 ]]; then
print_warn "No plugins selected"
exit 0
fi
# Map selected names back to directories
for selected in "${selected_plugins[@]}"; do
for i in {1..${#plugin_names[@]}}; do
if [[ "${plugin_names[$i]}" == "$selected" ]]; then
plugins_to_install+=("${plugin_dirs[$i]}")
break
fi
done
done
fi
# Build selected plugins
print_header "Building..."
for plugin_dir in "${plugins_to_install[@]}"; do
build_plugin "$plugin_dir" || exit 1
done
# Handle vaults
local -a vaults_to_install
if [[ "$USE_PREVIOUS_VAULTS" == "true" ]]; then
# Use cached vaults from --previous flag
while IFS= read -r line; do
[[ -n "$line" ]] && vaults_to_install+=("$line")
done < <(load_cached_vaults 2>/dev/null)
if [[ ${#vaults_to_install[@]} -eq 0 ]]; then
print_error "No cached vaults found. Run without --previous first."
exit 1
fi
print_header "Using ${#vaults_to_install[@]} cached vaults:"
for vault in "${vaults_to_install[@]}"; do
echo " ${vault/#$HOME/~}"
done
else
# Discover and prompt for vaults
print_header "Searching for Obsidian vaults..."
local -a vault_dirs
while IFS= read -r line; do
[[ -n "$line" ]] && vault_dirs+=("$line")
done < <(find_vaults)
if [[ ${#vault_dirs[@]} -eq 0 ]]; then
print_error "No Obsidian vaults found"
if [[ -n "$SEARCH_DEPTH" ]]; then
print_warn "Searched with depth $SEARCH_DEPTH. Try a higher value with -d."
else
print_warn "Searched: ~/Documents (4), ~/Desktop (3), ~ (5), iCloud (4)"
fi
exit 1
fi
local -a vault_names
for dir in "${vault_dirs[@]}"; do
vault_names+=("${dir/#$HOME/~}")
done
print_header "Found ${#vault_dirs[@]} vaults:"
select_items "Select vaults" "${vault_names[@]}"
# Map selected names back to full paths
for selected in "${SELECTED_ITEMS[@]}"; do
for i in {1..${#vault_names[@]}}; do
if [[ "${vault_names[$i]}" == "$selected" ]]; then
vaults_to_install+=("${vault_dirs[$i]}")
break
fi
done
done
# Save selection for next time
[[ ${#vaults_to_install[@]} -gt 0 ]] && save_vaults_to_cache "${vaults_to_install[@]}"
fi
if [[ ${#vaults_to_install[@]} -eq 0 ]]; then
print_warn "No vaults selected"
exit 0
fi
# Install
print_header "Installing..."
for plugin_dir in "${plugins_to_install[@]}"; do
local plugin_name=$(basename "$plugin_dir")
echo "${BOLD}$plugin_name${NC} →"
for vault_dir in "${vaults_to_install[@]}"; do
echo -n " "
install_plugin "$plugin_dir" "$vault_dir"
done
done
echo ""
print_success "${BOLD}Done.${NC} Reload Obsidian to activate changes."
}
main "$@"