-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathfunctions.sh
More file actions
executable file
Β·172 lines (149 loc) Β· 3.93 KB
/
functions.sh
File metadata and controls
executable file
Β·172 lines (149 loc) Β· 3.93 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
#!/usr/bin/env bash
running_macos() {
[ "$(uname)" == Darwin ]
return $?
}
running_codespaces() {
[ "$CODESPACES" = true ]
return $?
}
command_available() {
which "$1" > /dev/null 2>&1
}
fzf_available() {
command_available fzf
}
fish_available() {
command_available fish
}
brew_available() {
command_available brew
}
load_brew_shellenv() {
if test -x /opt/homebrew/bin/brew; then
brew=/opt/homebrew/bin/brew
elif test -x /usr/local/bin/brew; then
brew=/usr/local/bin/brew
fi
if test -n "${brew}"; then
eval "$($brew shellenv)"
fi
}
vscode_command() {
if command_available code-insiders; then
code="code-insiders"
elif command_available code; then
code="code"
fi
echo "$code"
}
find_targets() {
local directory="$1"
# only get the top level files/directories
# also exclude the directory itself
find "$directory" -mindepth 1 -maxdepth 1
}
link_directory_contents() {
local directory="$1"
# Items managed by their own installer scripts (e.g. fish.sh handles config/fish)
local -a skip=(config home config/fish)
for linkable in $(find_targets "${directory}"); do
local should_skip=false
for s in "${skip[@]}"; do
if [[ "$linkable" = "$s" ]]; then
should_skip=true
break
fi
done
if $should_skip; then
continue
fi
if [ "$directory" = "home" ]; then
target="$HOME/$(basename "$linkable")"
elif [ "${directory}" = "config" ]; then
target="$HOME/.config/$(basename "$linkable")"
else
echo "don't know where to put ${directory} links"
return 1
fi
link "$linkable" "$target"
done
}
# Ask a y/N question. Returns 0 for yes, 1 for no.
# In auto-yes mode, always returns 0. Scripts guard against
# non-interactive use at startup, so this always has a tty or --yes.
confirm() {
local prompt="$1"
if [ "${DOTPICKLES_YES:-}" = "1" ]; then
return 0
fi
read -p "$prompt " -n 1 -r
echo
[[ $REPLY =~ ^[Yy]$ ]]
}
backup_path() {
local target="$1"
local timestamp
timestamp="$(date +%Y%m%d-%H%M%S)"
local backup="${target}.backup.${timestamp}"
local counter=2
while [ -e "$backup" ]; do
backup="${target}.backup.${timestamp}-${counter}"
counter=$((counter + 1))
done
echo "$backup"
}
link() {
local linkable="$1"
local target="$2"
local display_target="${target/$HOME/~}"
local source="${DIR}/${linkable}"
if [ -L "$target" ]; then
# Target is a symlink
if [ "$(readlink "$target")" = "$source" ]; then
echo "π $display_target -> already linked"
elif confirm "π $display_target -> linked to $(readlink "$target"). Repoint to ${linkable}? [y/N]"; then
echo "π $display_target -> linking from $linkable"
ln -Ff -s "$source" "$target"
else
echo "π $display_target -> skipped (wrong symlink)"
fi
elif [ -e "$target" ]; then
# Target exists as real file or directory
local filetype="file"
[ -d "$target" ] && filetype="directory"
if confirm "π $display_target -> exists as $filetype. Replace with symlink to ${linkable}? [y/N]"; then
local backup
backup="$(backup_path "$target")"
echo "π $display_target -> backing up to ${backup##*/}"
mv "$target" "$backup"
echo "π $display_target -> linking from $linkable"
ln -s "$source" "$target"
else
echo "π $display_target -> skipped ($filetype exists)"
fi
else
# Target doesn't exist
echo "π $display_target -> linking from $linkable"
ln -s "$source" "$target"
fi
}
brew_bundle() {
echo "π» running brew bundle"
cat Brewfile "Brewfile.${DOTPICKLES_ROLE}" 2> /dev/null | brew bundle --file=- 2>&1 | sed 's/^/ β /'
echo
}
vim_plugins() {
echo "β¨οΈοΈ configuring vim"
vim +PlugInstall +qall
echo
}
# make sure op is logged in
op_ensure_signed_in() {
if ! which op > /dev/null 2> /dev/null; then
brew install 1password-cli
fi
if ! op whoami > /dev/null 2>&1; then
op signin
fi
}