-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup-unix-functions.sh
174 lines (147 loc) · 4.17 KB
/
setup-unix-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
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
# ------------------------------------------------------------------------------
# Dirs
# ------------------------------------------------------------------------------
DIR_DOWNLOADS=~/downloads
DIR_SCRIPTS=~/scripts
DIR_TOOLS=~/tools
DIR_PROJECTS=~/projects
# ------------------------------------------------------------------------------
# Checks / Dirs
# ------------------------------------------------------------------------------
# Checks if a command/program exists.
function installed() {
command -v "$1" >/dev/null 2>&1
}
# Checks if a command/program not exists.
function not_installed() {
! installed "$1"
}
# Checks if script is running on Linux.
function is_linux() {
[[ "$(uname -s)" == "Linux" ]]
}
# Checks if script is running on Mac.
function is_mac() {
is_mac_intel || is_mac_arm
}
# Checks if script is running on Mac with Intel processor.
function is_mac_intel() {
[[ "$(uname -s)" == "Darwin" && "$(uname -m)" == "x86_64" ]]
}
# Checks if script is running on Mac with ARM processor.
function is_mac_arm() {
[[ "$(uname -s)" == "Darwin" && "$(uname -m)" == "arm64" ]]
}
# Returns Homebrew directory.
function brew_dir() {
if is_mac_intel; then
echo "/usr/local"
elif is_mac_arm; then
echo "/opt/homebrew"
elif is_linux; then
echo "/home/linuxbrew/.linuxbrew"
else
brew --prefix
fi
}
# ------------------------------------------------------------------------------
# Installation
# ------------------------------------------------------------------------------
# Install something with APT.
function install_apt() {
if ! dpkg -l | cut -d' ' -f3 | grep -qw $1; then
log "APT installing: $1"
sudo apt-get install -y $1
else
log "APT skipping: $1"
fi
}
# Install something with brew.
function install_brew() {
if ! brew list -1 | grep -q "^$1\$"; then
log "Homebrew installing: $1"
brew install $1
else
log "Homebrew skipping: $1"
fi
}
# Install something with ASDF.
function install_asdf() {
lang=$1
version=$2
# install plugin and lang
log "ASDF installing: $lang $version"
asdf plugin add $lang
asdf install $lang $version
# add to .tool-versions
echo "$lang $version" >> ~/.tool-versions
reload
}
# Install something for VSCode or Cursor
function install_vscode() {
if installed "code"; then
if ! code --list-extensions | grep -qi "^$1\$"; then
log "Installing VSCode extension: $1"
code --install-extension $1
else
log "VSCode skipping: $1"
fi
fi
if installed "cursor"; then
if ! cursor --list-extensions | grep -qi "^$1\$"; then
log "Installing Cursor extension: $1"
cursor --install-extension $1
else
log "Cursor skipping: $1"
fi
fi
}
# ------------------------------------------------------------------------------
# Download / Extract
# ------------------------------------------------------------------------------
# Download a file from an URL to a target file.
function download() {
local url=$1
local target=$2
local extract_to=$3
# download
if [[ $target == /* ]]; then
target=$target
else
target=$DIR_DOWNLOADS/$target
fi
log "Downloading: $url -> $target"
curl -o "$target" -fsSL "$url"
# make executable
if [[ $target == /usr/local/bin/* ]]; then
executable $target
fi
# extract if necessary
if [ -n "$extract_to" ]; then
extract $target $extract_to
fi
}
# Extract a compressed file to a target directory/file.
function extract() {
# extract
local source=$1
local target=$2
log "Extracting: $source => $target"
case "$source" in
*.tar.gz)
tar -C $target -xzvf $source
;;
esac
}
# ------------------------------------------------------------------------------
# Helpers
# ------------------------------------------------------------------------------
# Log a formatted message to stderr.
log() {
echo "" 1>&2
echo -e "* [$(date +"%Y-%m-%d %H:%M:%S")] $@" 1>&2;
}
# Reload .bashrc
function reload() {
source ~/.bashrc
}