-
Notifications
You must be signed in to change notification settings - Fork 1
/
layx(macOS).sh
281 lines (238 loc) · 9.14 KB
/
layx(macOS).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
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
#!/bin/bash
# Color definitions
COLOR_RED='\033[0;31m'
COLOR_GREEN='\033[0;32m'
COLOR_YELLOW='\033[0;33m'
COLOR_CYAN='\033[0;36m'
COLOR_RESET='\033[0m'
# Error messages
STRING_NODE_FAIL="${COLOR_RED}Failed to execute Node.js. Please check the path and installation.${COLOR_RESET}"
STRING_DIR_ERROR="${COLOR_RED}Cannot perform this action here ${FR_CURRENT_DIR}${COLOR_RESET}"
# Directory settings
CURRENT_DIR="$(pwd)/"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/"
CONFIG_DIR="config/"
IMAGES_DIR="assets/image/"
NODE_EXE="${CURRENT_DIR}${CONFIG_DIR}node"
WEBP_EXE="${CURRENT_DIR}${CONFIG_DIR}webp"
PROGRAM_DIR="/Applications/LayX/"
FR_CURRENT_DIR="${CURRENT_DIR}"
if [ "${SCRIPT_DIR}" == "${PROGRAM_DIR}" ]; then
USE_DIR="${SCRIPT_DIR}"
else
USE_DIR="${CURRENT_DIR}"
fi
echo "LayX version 0.1.0 alpha"
# Check for Node.js
if [ ! -f "${NODE_EXE}" ]; then
if [ -f "${PROGRAM_DIR}${CONFIG_DIR}node" ]; then
NODE_EXE="${PROGRAM_DIR}${CONFIG_DIR}node"
WEBP_EXE="${PROGRAM_DIR}${CONFIG_DIR}webp"
else
echo "Program Node.js also not found."
if ! command -v node &> /dev/null; then
echo "Node.js is not installed."
else
echo "Node.js found globally."
NODE_EXE="node"
fi
fi
fi
build() {
echo "Building..."
echo "Using Node: ${NODE_EXE}"
if ! "${NODE_EXE}" -v; then
echo "${STRING_NODE_FAIL}"
exit 1
fi
if [ "${CURRENT_DIR}" != "${PROGRAM_DIR}" ]; then
"${NODE_EXE}" "${USE_DIR}${CONFIG_DIR}config.mjs" "build"
else
echo "${STRING_DIR_ERROR}"
fi
}
unbuild() {
echo "Unbuilding..."
echo "Using Node: ${NODE_EXE}"
if ! "${NODE_EXE}" -v; then
echo "${STRING_NODE_FAIL}"
exit 1
fi
if [ "${CURRENT_DIR}" != "${PROGRAM_DIR}" ]; then
"${NODE_EXE}" "${USE_DIR}${CONFIG_DIR}config.mjs" "unbuild"
else
echo "${STRING_DIR_ERROR}"
fi
}
create() {
if [ -d "${PROGRAM_DIR}" ]; then
if [ "${CURRENT_DIR}" != "${PROGRAM_DIR}" ]; then
if [ -d "${CURRENT_DIR}/layx" ]; then
read -p "There may be an existing LayX project. Do you want to replace it? (y/N): " choice
case "${choice}" in
[Yy]* ) echo -e "${COLOR_CYAN}Continuing...${COLOR_RESET}";;
[Nn]* | "" ) return;;
* ) echo -e "${COLOR_YELLOW}Please choose a valid option.${COLOR_RESET}"; create; return;;
esac
fi
echo -e "${COLOR_CYAN}Copying LayX files...${COLOR_RESET}"
cp -R "${PROGRAM_DIR}"* "${CURRENT_DIR}"
if [ -d "${CURRENT_DIR}${CONFIG_DIR}" ]; then
echo -e "${COLOR_CYAN}Cleaning up unnecessary files...${COLOR_RESET}"
rm -rf "${CURRENT_DIR}${CONFIG_DIR}"
fi
if [ -f "${CURRENT_DIR}/layx.sh" ]; then
rm "${CURRENT_DIR}/layx.sh"
fi
echo -e "${COLOR_GREEN}LayX project created in the current directory.${COLOR_RESET}"
else
echo -e "${COLOR_RED}Error: You are already in the LayX program directory. Please change to a different directory.${COLOR_RESET}"
fi
else
echo -e "${COLOR_YELLOW}LayX is not installed. Please install it first.${COLOR_RESET}"
fi
}
optimize_images() {
echo -e "${COLOR_CYAN}Optimizing images in ${IMAGES_DIR}${COLOR_RESET}"
find "${CURRENT_DIR}${IMAGES_DIR}" -type f \( -name "*.png" -o -name "*.jpg" \) -not -path "*/orginal_images_dir/*" | while read -r image; do
dir=$(dirname "${image}")
name=$(basename "${image%.*}")
echo "Processing $(basename "${image}")"
"${WEBP_EXE}" "${image}" -o "${dir}/${name}.webp" -q 90 -af -progress -short
mkdir -p "${dir}/orginal_images_dir"
mv "${image}" "${dir}/orginal_images_dir/"
echo "Processed: $(basename "${image}") at ${dir}"
done
}
install() {
# Check for root privileges using sudo
if [ "$(id -u)" != "0" ]; then
if [ ! -f "${CURRENT_DIR}/layx.sh" ]; then
echo -e "${COLOR_RED}layx.sh not found in the current directory.${COLOR_RESET}"
exit 1
fi
echo -e "${COLOR_YELLOW}Requesting administrator privileges...${COLOR_RESET}"
sudo "$0" install
exit $?
fi
if [ -d "${PROGRAM_DIR}" ]; then
if [ "${CURRENT_DIR}" == "${PROGRAM_DIR}" ]; then
echo -e "${COLOR_YELLOW}Program already installed.${COLOR_RESET}"
return
fi
read -p "Program directory already exists, would you like to update or replace? (y/N): " choice
case "${choice}" in
[Yy]* ) echo -e "${COLOR_CYAN}Continuing...${COLOR_RESET}";;
[Nn]* | "" ) return;;
* ) echo -e "${COLOR_YELLOW}Please choose a valid option.${COLOR_RESET}"; install; return;;
esac
fi
echo -e "${COLOR_CYAN}Installing...${COLOR_RESET}"
echo "Copying Files."
mkdir -p "${PROGRAM_DIR}"
cp -R "${SCRIPT_DIR}"* "${PROGRAM_DIR}"
# VS Code snippets installation for macOS
mkdir -p "$HOME/Library/Application Support/Code/User/snippets/"
cp -R "${SCRIPT_DIR}${CONFIG_DIR}syntax/layx.code-snippets" "$HOME/Library/Application Support/Code/User/snippets/"
# Create and configure alias in macOS shell
SHELL_RC="$HOME/.zshrc"
if [ ! -f "$SHELL_RC" ]; then
# Fallback to bash if zsh is not the default shell
SHELL_RC="$HOME/.bash_profile"
fi
# Add LayX alias to shell config
if ! grep -q "alias layx=" "$SHELL_RC"; then
echo "alias layx='${PROGRAM_DIR}layx.sh'" >> "$SHELL_RC"
echo -e "${COLOR_CYAN}Added layx alias to $SHELL_RC${COLOR_RESET}"
else
echo -e "${COLOR_YELLOW}layx alias already exists${COLOR_RESET}"
fi
# Add to PATH
if ! grep -q "${PROGRAM_DIR}" "$SHELL_RC"; then
echo "export PATH=\$PATH:${PROGRAM_DIR}" >> "$SHELL_RC"
echo -e "${COLOR_CYAN}Added ${PROGRAM_DIR} to PATH in $SHELL_RC${COLOR_RESET}"
else
echo -e "${COLOR_YELLOW}${PROGRAM_DIR} is already in the PATH${COLOR_RESET}"
fi
# Set executable permissions and ownership
echo -e "${COLOR_CYAN}Setting permissions...${COLOR_RESET}"
chmod +x "${PROGRAM_DIR}layx.sh"
chmod +x "${PROGRAM_DIR}${CONFIG_DIR}node"
chmod +x "${PROGRAM_DIR}config/webp"
chmod -R 755 "${PROGRAM_DIR}"
chown -R $(whoami):admin "${PROGRAM_DIR}"
echo -e "${COLOR_GREEN}Installation completed.${COLOR_RESET}"
echo -e "${COLOR_YELLOW}Please restart your terminal or run 'source $SHELL_RC' to use the layx command${COLOR_RESET}"
}
uninstall() {
if [ ! -d "${PROGRAM_DIR}" ]; then
echo -e "${COLOR_YELLOW}LayX is not installed on your system.${COLOR_RESET}"
exit 1
fi
if [ "$(id -u)" != "0" ]; then
echo -e "${COLOR_YELLOW}Requesting administrator privileges...${COLOR_RESET}"
sudo "$0" uninstall
exit $?
fi
echo -e "${COLOR_CYAN}Uninstalling...${COLOR_RESET}"
if [ -d "${PROGRAM_DIR}" ]; then
rm -rf "${PROGRAM_DIR}"
else
echo "${PROGRAM_DIR} not found."
fi
# Remove from shell config
SHELL_RC="$HOME/.zshrc"
if [ ! -f "$SHELL_RC" ]; then
SHELL_RC="$HOME/.bash_profile"
fi
if [ -f "$SHELL_RC" ]; then
sed -i '' "\|export PATH=\$PATH:${PROGRAM_DIR}|d" "$SHELL_RC"
sed -i '' "\|alias layx=|d" "$SHELL_RC"
fi
echo "Uninstallation completed."
echo -e "${COLOR_YELLOW}Please restart your terminal or run 'source $SHELL_RC' to update your PATH.${COLOR_RESET}"
}
show_options() {
echo -e "${COLOR_CYAN}Please choose an option:${COLOR_RESET}"
echo "1. Build"
echo "2. Unbuild"
echo "3. Create"
echo "4. Optimize Images"
echo "5. Install"
echo "6. Uninstall"
echo "7. Exit"
read -p "Enter your choice (1-7): " choice
case $choice in
1) build ;;
2) unbuild ;;
3) create ;;
4) optimize_images ;;
5) install ;;
6) uninstall ;;
7) exit 0 ;;
*) echo -e "${COLOR_YELLOW}Please choose a valid option.${COLOR_RESET}"; show_options ;;
esac
}
# Main script execution
if [ $# -eq 0 ]; then
show_options
else
case "$1" in
"build") build ;;
"unbuild") unbuild ;;
"create") create ;;
"optimage") optimize_images ;;
"install") install ;;
"uninstall") uninstall ;;
"add") "${NODE_EXE}" "${USE_DIR}${CONFIG_DIR}config.mjs" "$@" ;;
*)
echo -e "Available options are ${COLOR_YELLOW}\"build\"${COLOR_RESET}, ${COLOR_YELLOW}\"unbuild\"${COLOR_RESET}, ${COLOR_YELLOW}\"create\"${COLOR_RESET}, ${COLOR_YELLOW}\"add\"${COLOR_RESET}, ${COLOR_YELLOW}\"optimage\"${COLOR_RESET}, ${COLOR_YELLOW}\"install\"${COLOR_RESET} and ${COLOR_YELLOW}\"uninstall\"${COLOR_RESET}."
if [ "${CURRENT_DIR}" != "${PROGRAM_DIR}" ]; then
echo "Forwarding cmd to \"config.mjs\""
"${NODE_EXE}" "${USE_DIR}${CONFIG_DIR}config.mjs" "$@"
else
echo "${STRING_DIR_ERROR}"
fi
;;
esac
fi