-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwsl-setup.sh
More file actions
196 lines (172 loc) · 7.05 KB
/
wsl-setup.sh
File metadata and controls
196 lines (172 loc) · 7.05 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
#!/usr/bin/env bash
set -euo pipefail
# -----------------------------------------------------------------------------
# WSL Web‑Dev Installer v4: fully quiet, resumable, with rollback on error
# -----------------------------------------------------------------------------
# 0) Cache sudo credentials upfront
echo "🔒 Caching sudo credentials..."
sudo -v
(while true; do
sudo -n true
sleep 60
done) &
# --- On any error, reset state and exit ---
trap 'echo "⚠ Error encountered, resetting state..."; rm -f "${STATE_FILE:-$HOME/.wsl_setup_state}"; exit 1' ERR
# 1) State tracking
default_state="$HOME/.wsl_setup_state"
read -rp "State file path [default: $default_state]: " STATE_FILE
STATE_FILE=${STATE_FILE:-"$default_state"}
LAST_DONE=0
if [[ -f "$STATE_FILE" ]]; then
LAST_DONE=$(<"$STATE_FILE")
fi
done_step() {
echo "$1" >"$STATE_FILE"
}
echo
# 2) Configuration prompts
default_web="$HOME/www"
read -rp "Project root directory [default: $default_web]: " WEB_ROOT
WEB_ROOT=${WEB_ROOT:-"$default_web"}
default_ssl="$HOME/ssl"
read -rp "SSL directory [default: $default_ssl]: " SSL_DIR
SSL_DIR=${SSL_DIR:-"$default_ssl"}
default_repo="https://github.com/pedr0cazz/wsl-scripts.git"
read -rp "Scripts Git repo URL [default: $default_repo]: " SCRIPTS_REPO
SCRIPTS_REPO=${SCRIPTS_REPO:-"$default_repo"}
default_dir="$HOME/.wsl_scripts"
read -rp "Local scripts path [default: $default_dir]: " SCRIPTS_DIR
SCRIPTS_DIR=${SCRIPTS_DIR:-"$default_dir"}
# 3) Persist config for helper scripts
cat >~/.wsl_env <<EOF
export WEB_ROOT="$WEB_ROOT"
export SSL_DIR="$SSL_DIR"
export SCRIPTS_DIR="$SCRIPTS_DIR"
export SSL_SCRIPT="\$SCRIPTS_DIR/ssl-manager.sh"
EOF
# 4) Quiet runner: captures stderr, silences stdout, and displays a loading bar
run() {
local msg="$1"
shift
local tmp_log
tmp_log=$(mktemp)
# Initial print with empty progress bar (20 slots)
printf "→ %s... [%-20s] 0%%" "$msg" ""
"$@" 1>/dev/null 2>"$tmp_log" &
local pid=$!
local percent=0
while kill -0 "$pid" 2>/dev/null; do
percent=$(( percent + 5 ))
if (( percent > 90 )); then
percent=90
fi
local num_bars=$(( percent / 5 ))
local bars
bars=$(printf '%0.s=' $(seq 1 $num_bars))
local spaces
spaces=$(printf '%0.s ' $(seq 1 $(( 20 - num_bars ))))
printf "\r→ %s... [%-20s] %d%%" "$msg" "$bars$spaces" "$percent"
sleep 0.2
done
wait "$pid"
local status=$?
local output
output=$(cat "$tmp_log")
rm -f "$tmp_log"
if [ $status -eq 0 ]; then
printf "\r→ %s... [====================] 100%%\n" "$msg"
else
printf "\r→ %s... [====================] FAILED\n" "$msg"
echo "${output}"
exit "$status"
fi
}
# 5) Step 1: Create project root
if ((LAST_DONE < 1)); then
run "Create project root at $WEB_ROOT" mkdir -p "$WEB_ROOT"
done_step 1
fi
# 6) Step 2: System update & upgrade
if ((LAST_DONE < 2)); then
run "System update & upgrade" bash -c 'sudo apt-get update -qq && sudo apt-get upgrade -qq'
done_step 2
fi
# 7) Step 3: Install essentials
if ((LAST_DONE < 3)); then
run "Install essentials" sudo apt-get install -y -qq build-essential curl git unzip jq nginx mysql-server redis-server php-redis
done_step 3
fi
# 8) Step 4: Zsh & Oh My Zsh + plugins
if ((LAST_DONE < 4)); then
run "Install Zsh" sudo apt-get install -y -qq zsh
run "Install Oh My Zsh" bash -c 'RUNZSH=no CHSH=no sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"'
ZSH_CUSTOM="${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}"
run "Clone zsh-autosuggestions" git clone --depth=1 https://github.com/zsh-users/zsh-autosuggestions "$ZSH_CUSTOM/plugins/zsh-autosuggestions"
run "Clone zsh-syntax-highlighting" git clone --depth=1 https://github.com/zsh-users/zsh-syntax-highlighting "$ZSH_CUSTOM/plugins/zsh-syntax-highlighting"
run "Clone powerlevel10k theme" git clone --depth=1 https://github.com/romkatv/powerlevel10k.git "$ZSH_CUSTOM/themes/powerlevel10k"
# We avoid interactive chsh; instead we'll auto-launch zsh at the end via bashrc
done_step 4
fi
# 9) Step 5: Configure ~/.zshrc
if ((LAST_DONE < 5)); then
echo "→ Configuring ~/.zshrc"
touch ~/.zshrc
# Set ZSH theme (idempotent)
sed -i 's/^ZSH_THEME=.*/ZSH_THEME="powerlevel10k\/powerlevel10k"/' ~/.zshrc || true
# Add plugins if missing
run "Add Zsh plugins" bash -c 'grep -q "plugins=(git zsh-autosuggestions zsh-syntax-highlighting" ~/.zshrc || sed -i "s/^plugins=(/plugins=(git zsh-autosuggestions zsh-syntax-highlighting /" ~/.zshrc'
# Add smart Composer PHP version wrapper
echo 'Add smart Composer PHP version wrapper'
# Source environment variables
grep -q 'source ~/.wsl_env' ~/.zshrc || echo 'source ~/.wsl_env' >>~/.zshrc
# Append full helpers content from zshrc-helpers.sh (variables loaded via ~/.wsl_env)
grep -q '# ── Laragon SSL Manager Check' ~/.zshrc || cat "$SCRIPTS_DIR/zshrc-helpers.sh" >>~/.zshrc
# add typeset -g POWERLEVEL9K_INSTANT_PROMPT=off
#grep -q 'typeset -g POWERLEVEL9K_INSTANT_PROMPT=off' ~/.zshrc || echo 'typeset -g POWERLEVEL9K_INSTANT_PROMPT=off' >>~/.zshrc
done_step 5
fi
# 10) Step 6: NVM & Node.js LTS
if ((LAST_DONE < 6)); then
run "Install NVM" git clone --depth=1 https://github.com/nvm-sh/nvm.git "$HOME/.nvm"
run "Install Node.js LTS" bash -c '. "$HOME/.nvm/nvm.sh" >/dev/null 2>&1 && nvm install --lts >/dev/null 2>&1'
done_step 6
fi
# 11) Step 7: PHP versions & Composer
if ((LAST_DONE < 7)); then
run "Add PHP PPA" sudo add-apt-repository -y ppa:ondrej/php
run "Install PHP 8.x" bash -c 'sudo apt-get update -qq && for v in 8.2 8.3 8.4; do sudo apt-get install -y -qq php${v} php${v}-fpm php${v}-cli php${v}-common php${v}-curl php${v}-mbstring php${v}-xml php${v}-mysql php${v}-opcache php${v}-intl php${v}-zip; done'
run "Install Composer" bash -c 'curl -sS https://getcomposer.org/installer | php -- --quiet >/dev/null 2>&1 && sudo mv composer.phar /usr/local/bin/composer'
done_step 7
fi
# 12) Step 8: Secure MySQL root
if ((LAST_DONE < 8)); then
run "Secure MySQL root" bash -c "sudo mysql -e \"ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY ''; FLUSH PRIVILEGES;\""
done_step 8
fi
# 13) Step 9: Setup helper scripts
if ((LAST_DONE < 9)); then
if [ ! -d "$SCRIPTS_DIR" ]; then
run "Clone helper scripts" git clone --depth=1 "$SCRIPTS_REPO" "$SCRIPTS_DIR"
else
echo "Helper scripts already exist, skipping clone."
fi
run "Make SSL manager executable" chmod +x "$SCRIPTS_DIR/ssl-manager.sh"
done_step 9
fi
# 14) Step 10: Create SSL directory
if ((LAST_DONE < 10)); then
run "Create SSL directory" mkdir -p "$SSL_DIR"
done_step 10
fi
# 15) Final: completion message
if ((LAST_DONE < 11)); then
echo
echo "🎉 All steps complete! Please restart your terminal or run 'source ~/.zshrc'"
# Ensure new shells auto-launch zsh
if ! grep -q 'exec zsh -l' ~/.bashrc; then
echo 'exec zsh -l' >>~/.bashrc
fi
# Start zsh right now
exec zsh -l
done_step 11
fi