Repository navigation
Expand file tree
/
Copy path_utils.sh
More file actions
225 lines (199 loc) · 4.49 KB
/
Copy path_utils.sh
File metadata and controls
225 lines (199 loc) · 4.49 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
#!/bin/bash
# Cross-platform utility functions
# =============================================================================
# Logging (source-able from any script)
# =============================================================================
source "$(dirname "${BASH_SOURCE[0]}")/_logger.sh"
# =============================================================================
# OS Detection
# =============================================================================
get_os() {
if [[ "${OSTYPE}" == "darwin"* ]]; then
echo "macos"
elif [[ -f /etc/os-release ]]; then
. /etc/os-release
case "$ID" in
debian|ubuntu|pop|linuxmint|elementary)
echo "debian"
;;
fedora|rhel|centos|rocky|almalinux)
echo "fedora"
;;
*)
echo "unknown"
;;
esac
else
echo "unknown"
fi
}
OS_TYPE=$(get_os)
# =============================================================================
# Path and Command Utilities
# =============================================================================
zshrc() {
if ! [ -z "$2" ]; then
echo "" >> ~/.zshrc
echo "# $2" >> ~/.zshrc
fi
echo "$1" >> ~/.zshrc
# Note: Don't source .zshrc here - script runs in bash, zshrc has zsh-specific commands
}
has_path() {
local path="$*"
if [ -e "$HOME/$path" ]; then
return 0
fi
return 1
}
has_command() {
if [ -x "$(command -v $1)" ]; then
return 0
fi
return 1
}
# =============================================================================
# Package Manager Detection
# =============================================================================
has_brew() {
if command -v brew &> /dev/null && brew ls --versions "$1" &> /dev/null; then
return 0
fi
return 1
}
has_apt() {
if command -v dpkg &> /dev/null && dpkg -l "$1" 2>/dev/null | grep -q "^ii"; then
return 0
fi
return 1
}
has_dnf() {
if command -v rpm &> /dev/null && rpm -q "$1" &> /dev/null; then
return 0
fi
return 1
}
has_cask() {
if brew ls --cask "$1" &> /dev/null; then
return 0
fi
return 1
}
has_app() {
local name="$*"
if [ -e "/Applications/$name.app" ]; then
return 0
fi
return 1
}
has_variable() {
if [ -z "${!1}" ]; then
return 1
fi
return 0
}
# =============================================================================
# Cross-Platform Package Check
# =============================================================================
has_pkg() {
case "$OS_TYPE" in
macos) has_brew "$1" ;;
debian) has_apt "$1" ;;
fedora) has_dnf "$1" ;;
*) return 1 ;;
esac
}
# =============================================================================
# Test Functions
# =============================================================================
test_path() {
if has_path "$1"; then
e_success "$1"
else
e_failure "$1"
fi
}
test_command() {
if has_command "$1"; then
e_success "$1"
else
e_failure "$1"
fi
}
test_brew() {
if has_brew "$1"; then
e_success "$1"
else
e_failure "$1"
fi
}
test_cask() {
if has_cask "$1"; then
e_success "$1"
else
e_failure "$1"
fi
}
test_app() {
if has_app "$1"; then
e_success "$1"
else
e_failure "$1"
fi
}
test_apt() {
if has_apt "$1"; then
e_success "$1"
else
e_failure "$1"
fi
}
test_dnf() {
if has_dnf "$1"; then
e_success "$1"
else
e_failure "$1"
fi
}
# =============================================================================
# Package Installation Functions
# =============================================================================
brew_install() {
if ! has_brew "$1"; then
e_pending "Installing $1"
brew install "$1" > /dev/null
fi
test_brew "$1"
}
brew_cask_install() {
if ! has_cask "$1"; then
e_pending "Installing $1"
brew install --cask "$1" > /dev/null
fi
test_cask "$1"
}
apt_install() {
if ! has_apt "$1"; then
e_pending "Installing $1"
sudo apt-get install -y "$1" > /dev/null
fi
test_apt "$1"
}
dnf_install() {
if ! has_dnf "$1"; then
e_pending "Installing $1"
sudo dnf install -y "$1" > /dev/null
fi
test_dnf "$1"
}
# =============================================================================
# Cross-Platform Package Installation
# =============================================================================
pkg_install() {
case "$OS_TYPE" in
macos) brew_install "$1" ;;
debian) apt_install "$1" ;;
fedora) dnf_install "$1" ;;
*) e_failure "Unknown OS, cannot install $1" ;;
esac
}