-
Notifications
You must be signed in to change notification settings - Fork 0
/
installer_functions.sh
111 lines (101 loc) · 2.54 KB
/
installer_functions.sh
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
#!/bin/bash
# Original source: https://stackoverflow.com/a/31236568/1530494
function relpath() {
python -c "import os, sys; print(os.path.relpath(*sys.argv[1:]))" "$@"
}
# Link the given src file into the dotfiles' bin directory using relative paths
function dotbinlink() {
src="$1"
if [[ -z "$src" ]]; then
echo 'Usage: dotbinlink executable_src'
return 2
fi
if [[ ! -e "$src" ]]; then
echo "dotbinlink: Source does not exist: '$src'"
return 2
fi
dest="$HOME/.dotfiles/bin/$src"
dest_dir=$(dirname "$dest")
src="$PWD/$src"
ln -sf "$(relpath "$src" "$dest_dir")" "$dest"
}
# Only perform the specified command if DEFINITELY_ME is 'yes'.
# This prevents "John Starich"-specific configuration files
# from showing up in someone else's git commits or something.
definitely_me() {
if [[ "$DEFINITELY_ME" == 'yes' ]]; then
"$@"
fi
}
function dotlink() {
src="$1"
dest="$2"
if [[ -z "$src" || -z "$dest" ]]; then
echo 'Usage: dotlink module_relative_src absolute_link_location'
return 2
fi
if [[ ! -e "$src" ]]; then
echo "dotlink: Source does not exist: '$src'"
return 2
fi
if [[ -e "$dest" ]]; then
if [[ -L "$dest" ]]; then
rm "$dest"
else
echo "Skipping: Dotfiles link could not be made, non-link file exists: $dest"
return 0
fi
fi
if [[ ! "$src" =~ ^/ ]]; then
src="$PWD/$src"
fi
ln -sf "$src" "$dest"
}
function dotpip() {
local packages=$(pip freeze | sed -e 's/==//')
local toinstall=()
for arg in "$@"; do
if ! grep -q "$arg" <<<"$packages"; then
toinstall+=("$arg")
fi
done
if (( ${#toinstall} > 0 )); then
pip install "${toinstall[@]}"
fi
}
function dotpip3() {
local packages=$(pip3 freeze | sed -e 's/==//')
local toinstall=()
for arg in "$@"; do
if ! grep -q "$arg" <<<"$packages"; then
toinstall+=("$arg")
fi
done
if (( ${#toinstall} > 0 )); then
pip3 install "${toinstall[@]}"
fi
}
function with_uname() {
local match=$1
shift
local args=$@
if [[ -z "${args[*]}" ]]; then
[[ "`uname`" == "$match" ]]
return $?
fi
if [[ "`uname`" == "$match" ]]; then
eval "$@"
return $?
fi
return 0
}
function macos() {
with_uname Darwin "$@"
}
function linux() {
with_uname Linux "$@"
}
# Clear the line and print the string
function printr() {
echo -en "\r\033[K$*"
}