Repository navigation
Expand file tree
/
Copy pathaliases_Darwin.sh
More file actions
357 lines (288 loc) · 8.65 KB
/
Copy pathaliases_Darwin.sh
File metadata and controls
357 lines (288 loc) · 8.65 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
#
# Xcode aliases
#
# Open Xcode for current folder (prefers workspace to project)
xc() {
local xcode_app
if ! xcode_app="$(__get_xcode_app)"; then
xcode_app="Xcode"
fi
/usr/bin/open -a "${xcode_app}" "${1:-.}"
}
# Dammit Xcode (delete derived data twice a day for entire career as needed)
fuxcode() {
__check_dt_not_running || return 1
if [[ -d "${HOME}/Library/Developer/Xcode/DerivedData" ]]; then
rm -rf "${HOME}/Library/Developer/Xcode/DerivedData"
fi
}
# Dammit CoreSimulator (kill simulator service twice a day for entire career as needed)
fucoresim() {
__check_dt_not_running || return 1
/bin/launchctl bootout "user/${UID}/com.apple.CoreSimulator.CoreSimulatorService"
}
# Dammit Interface Builder (ibtool)
fuibtool() {
__check_dt_not_running || return 1
/usr/bin/pkill -KILL -x -U "${UID}" "ibtoold"
}
# Verify Xcode installation
haxcode() {
local xcode_app
if ! xcode_app="$(__get_xcode_app)"; then
printf 'Error: Command Line Tools selected, or Xcode not installed.\n'
return 1
fi
/usr/sbin/spctl --assess --verbose "${xcode_app}"
}
__check_dt_not_running() {
if /usr/bin/pgrep -x -U "${UID}" "Simulator" >/dev/null 2>&1; then
printf >&2 'Error: Simulator is running.\n'
return 1
fi
if /usr/bin/pgrep -x -U "${UID}" "Xcode" >/dev/null 2>&1; then
printf >&2 'Error: Xcode is running.\n'
return 1
fi
}
# Get the path of the currently selected Xcode.
__get_xcode_app() {
local developer_dir
if ! developer_dir="$(/usr/bin/xcode-select --print-path 2>/dev/null)"; then
return 1
fi
if [[ "${developer_dir}" != *".app/Contents/Developer" ]]; then
return 1
fi
printf '%s\n' "${developer_dir%"/Contents/Developer"}"
}
#
# Homebrew - fully update/upgrade, clean up the mess, optionally
# install something in between. Boop!
#
boop() {
__boop_check_pyenv
local err
brew update; err="$?"
((err == 0)) || return "${err}"
brew upgrade; err="$?"
((err == 0)) || return "${err}"
if (($# > 0)); then
brew install "$@"; err="$?"
((err == 0)) || return "${err}"
fi
brew cleanup --prune=all -s
}
boop_cask() {
(($# > 0)) || return 1
local err
brew update; err="$?"
((err == 0)) || return "${err}"
brew cask install "$@"; err="$?"
((err == 0)) || return "${err}"
brew cleanup --prune=all -s
}
__boop_check_pyenv() {
if ! __is_function mm_off; then
return
fi
if [[ -z "$(__path_select_re "${PATH}" 'rbenv|rvm|pyenv|nvm')" ]]; then
return
fi
cat >&2 <<EOT
rbenv, rvm, pyenv, and/or nvm found in PATH. This can break installing or
upgrading software from Homebrew. Run \`mm_off -a\` now to restart this
EOT
printf >&2 'shell without it/them (y/n)? '
local confirmation
read -r confirmation
if [[ "${confirmation}" =~ ^[Yy] ]]; then
printf >&2 '\nRestarting the shell. Please run this command again.\n'
mm_off -a
fi
}
# Code signing helper for Homebrew binaries (notably GDB)
if [[ -s "${DCP_CONFIG_DIR}/brew_codesign.sha1" ]]; then
brew_codesign() {
local target fingerprint
target="$1"
if __is_command realpath; then
target="$(realpath -q "${target}")"
fi
if ! __is_macho_bundle "${target}" && ! __is_macho_file "${target}"; then
printf >&2 'Error: argument is not a Mach-O bundle or binary file.\n'
return 1
fi
fingerprint="$(<"${DCP_CONFIG_DIR}/brew_codesign.sha1")"
/usr/bin/codesign --force --sign "${fingerprint}" "${target}"
}
__is_macho_bundle() {
local bundle_name
bundle_name="$(basename "$1")"
bundle_name="${bundle_name%%.*}"
if [[ ! -d "$1" ]]; then
return 1
fi
[[ -d "$1/Contents" ]] || __is_macho_file "$1/${bundle_name}"
}
__is_macho_file() {
local output
output="$(file -b "$1" 2>/dev/null)"
[[ "${output}" = "Mach-O"* ]]
}
fi
#
# Quickly compute and copy a file's hash to the clipboard
#
copy_md5() {
if [[ ! -f "$1" ]]; then
printf >&2 'Argument is not a file.\n'
return 1
fi
local sum="$(/sbin/md5 -q "$1")"
printf '%s' "${sum}" | pbcopy
printf '%s\n' "${sum}"
}
copy_sha1() { __copy_sha 1 "$1"; }
copy_sha256() { __copy_sha 256 "$1"; }
copy_sha384() { __copy_sha 384 "$1"; }
__copy_sha() {
if [[ ! -f "$2" ]]; then
printf >&2 'Argument is not a file.\n'
return 1
fi
local sum="$(/usr/bin/shasum -a "$1" "$2" | cut -d ' ' -f 1)"
printf '%s' "${sum}" | pbcopy
printf '%s\n' "${sum}"
}
# Fix obnoxious bug with macOS zsh completion for /usr/bin/du if coreutils is
# installed via Homebrew.
if __is_zsh && [[ -x "/usr/local/opt/coreutils/bin/gdu" ]]; then
du() { /usr/local/opt/coreutils/bin/gdu "$@"; }
fi
# Caddy
if __is_command caddy; then
caddyfs() {
caddy run --config "${DCP}/share/Darwin.Caddyfile" "$@"
}
fi
#
# System-level resets... these come in handy.
#
# Set computer name, hostname, and Bonjour hostname
set_computer_name() {
[[ -z "$1" ]] && return 1 || :
# Remove single quotes, replace spaces with hyphens
local hostname="$1"
hostname="${hostname//\'/}"
hostname="${hostname// /-}"
printf 'Setting ComputerName to "%s"...\n' "$1" >&2
sudo scutil --set ComputerName "$1"
printf 'Setting HostName and LocalHostName to "%s"...\n' "${hostname}" >&2
sudo scutil --set HostName "${hostname}"
sudo scutil --set LocalHostName "${hostname}"
}
# Reset "Open With..." menus after connecting a drive with applications on it
reset_launch_services() {
local framework_path="/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework"
"${framework_path}"/Support/lsregister -kill -r \
-domain local \
-domain system \
-domain user
killall Finder
}
# Flush DNS cache and reset mDNSResponder
reset_dns_cache() {
printf >&2 'Flushing the DNS cache (enter your user password if prompted)...\n'
if ! __is_file discoveryutil; then
# Sane versions of macOS
sudo -H dscacheutil -flushcache
sudo -H killall -HUP mDNSResponder
else
# OS X 10.9 - 10.10.3 (rest in pieces, discoveryd)
sudo -H discoveryutil mdnsflushcache
sudo -H discoveryutil udnsflushcache
fi
}
# Clear Quick Look's file locks (so you can empty the trash)
reset_quick_look() {
qlmanage -r
}
#
# Just launchd things (tm)
#
# Convenience wrapper for globally "exporting" a variable. Similar to setting
# environment variables in the Control Panel on Windows. Seen by apps like
# IntelliJ that pick up on certain vars: JAVA_HOME, etc.
launchd_export() {
while (($# > 0)); do
launchctl setenv "$1" "$(__valueof "$1")"
shift
done
}
# launchctl wrapper for apsd (Apple Push Service: Messages.app, etc.)
apsctl() {
"${DCP}/libexec/lctl.sh" \
apsctl \
system /System/Library/LaunchDaemons/com.apple.apsd.plist \
"$(__lctl_protected_action "$1")"
}
# launchctl wrapper for CoreAudio (because sometimes there be dragons)
coreaudioctl() {
"${DCP}/libexec/lctl.sh" \
coreaudioctl \
system /System/Library/LaunchDaemons/com.apple.audio.coreaudiod.plist \
"$1"
}
# launchctl wrapper for bluetoothd
bluetoothctl() {
"${DCP}/libexec/lctl.sh" \
bluetoothctl \
system /System/Library/LaunchDaemons/com.apple.bluetoothd.plist \
"$(__lctl_protected_action "$1")"
}
# launchctl wrapper for cfprefsd agent (clear file locks / empty the trash)
cfprefsctl() {
"${DCP}/libexec/lctl.sh" \
cfprefsctl \
user /System/Library/LaunchAgents/com.apple.cfprefsd.xpc.agent.plist \
"$(__lctl_protected_action "$1")"
}
# "action transformer" for protected services that can't be stopped traditionally
__lctl_protected_action() {
case "$1" in
stop) printf 'unstoppable\n' ;;
restart) printf 'kickstart\n' ;;
*) printf '%s\n' "$1"
esac
}
# gpgconf wrapper for gpg-agent, if installed via Homebrew
if [[ -h "/usr/local/opt/gnupg" ]]; then
gpgagentctl() {
if [[ "$1" = "stop" || "$1" = "restart" ]]; then
/usr/local/opt/gnupg/bin/gpgconf --kill "gpg-agent"
if /usr/bin/pgrep -q -x -U "${UID}" "gpg-agent" 2>/dev/null; then
/usr/bin/pkill -KILL -x -U "${UID}" "gpg-agent"
fi
fi
if [[ "$1" != "stop" ]]; then
/usr/local/opt/gnupg/bin/gpgconf --launch "gpg-agent"
fi
}
fi
# `brew services' wrappers for chunkwm, khd, and kwm if installed via Homebrew
if [[ -d "/usr/local/opt/chunkwm" ]]; then
chunkwmctl() { __brewctl chunkwm "$@"; }
fi
if [[ -d "/usr/local/opt/khd" ]]; then
khdctl() { __brewctl khd "$@"; }
fi
__brewctl() {
(($# > 1)) || return 1
local formula="$1"; shift
brew services "$@" "${formula}"
}
# htop(1) should generally be run as root on macOS
if [[ -x "/usr/local/opt/htop/bin/htop" ]]; then
htop() { sudo /usr/local/opt/htop/bin/htop "$@"; }
fi