-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathrust.sh
126 lines (110 loc) · 4 KB
/
rust.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
#!/bin/bash
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
MAGENTA='\033[0;35m'
NC='\033[0m' # No Color
if [[ "${BASH_SOURCE[0]}" == "${0}" ]] || [[ -n "${ZSH_VERSION}" && "${ZSH_EVAL_CONTEXT}" == "toplevel" ]]; then
echo -e "${RED}❌ This script must be sourced! Use:${NC}"
echo -e "source ${(q-)0}\n"
exit 1
fi
error() { echo -e "${RED}❌ [ERROR]${NC} $1"; }
warn() { echo -e "${YELLOW}⚠️ [WARN]${NC} $1"; }
info() { echo -e "${BLUE}ℹ️ [INFO]${NC} $1"; }
success() { echo -e "${GREEN}✅ [SUCCESS]${NC} $1"; }
task() { echo -e "${MAGENTA}🔧 [TASK]${NC} $1"; }
run_with_spinner() {
local msg="$1"
shift
local spin_chars='🕐🕑🕒🕓🕔🕕🕖🕗🕘🕙🕚🕛'
local pid delay=0.1
"$@" >/dev/null 2>&1 &
pid=$!
printf "${MAGENTA}🔧 [TASK]${NC} %s... " "$msg"
while kill -0 $pid 2>/dev/null; do
for i in {0..11}; do
printf "\b${spin_chars:$i:1}"
sleep 0.1
done
done
wait $pid
local exit_status=$?
printf "\r\033[K"
return $exit_status
}
setup_environment() {
export RUSTUP_HOME="${HOME}/.rustup"
export CARGO_HOME="${HOME}/.cargo"
export PATH="${CARGO_HOME}/bin:${PATH}"
if [[ -f "${CARGO_HOME}/env" ]]; then
source "${CARGO_HOME}/env"
fi
}
install_dependencies() {
task "Checking system dependencies"
if command -v apt &>/dev/null; then
run_with_spinner "Updating package lists" sudo apt update &&
run_with_spinner "Installing build essentials" sudo apt install -y build-essential curl libssl-dev pkg-config
elif command -v dnf &>/dev/null; then
run_with_spinner "Installing development tools" sudo dnf groupinstall -y "Development Tools" &&
run_with_spinner "Installing system dependencies" sudo dnf install -y curl openssl-devel
elif command -v yum &>/dev/null; then
run_with_spinner "Installing development tools" sudo yum groupinstall -y "Development Tools" &&
run_with_spinner "Installing system dependencies" sudo yum install -y curl openssl-devel
elif command -v pacman &>/dev/null; then
run_with_spinner "Updating system" sudo pacman -Syu --noconfirm &&
run_with_spinner "Installing base-devel" sudo pacman -S --noconfirm base-devel curl openssl
else
error "Unsupported package manager"
return 1
fi
}
force_update_path() {
local shell_rc=("${HOME}/.bashrc" "${HOME}/.zshrc" "${HOME}/.profile")
local env_line="[[ -f \"${CARGO_HOME}/env\" ]] && source \"${CARGO_HOME}/env\""
for rc in "${shell_rc[@]}"; do
if [[ -f "${rc}" ]] && ! grep -qF "${env_line}" "${rc}"; then
echo -e "\n# Added by Rust setup script\n${env_line}" >> "${rc}"
fi
done
}
manage_rust() {
if command -v rustup &>/dev/null; then
task "Updating Rust toolchain"
if run_with_spinner "Checking for updates" rustup update; then
success "Rust toolchain updated"
else
error "Failed to update Rust toolchain"
return 1
fi
else
task "Installing Rust"
run_with_spinner "Downloading rustup" curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs -o /tmp/rustup-init &&
run_with_spinner "Installing rustup" sh /tmp/rustup-init -y --no-modify-path &&
rm -f /tmp/rustup-init
fi
}
verify_rust() {
task "Verifying installation"
if command -v rustc &>/dev/null && command -v cargo &>/dev/null; then
success "Rust components verified:"
echo -e "${CYAN}Rustc: $(rustc --version)${NC}"
echo -e "${CYAN}Cargo: $(cargo --version)${NC}"
else
error "Rust installation verification failed"
return 1
fi
}
main() {
setup_environment
install_dependencies || return 1
manage_rust || return 1
setup_environment # Re-set environment after installation
force_update_path
verify_rust || return 1
success "Rust is ready to use! Current PATH: ${BLUE}${CARGO_HOME}/bin${NC}"
}
main