Files
herolib_rust/scripts/install.sh
2025-08-27 13:17:52 +02:00

235 lines
7.1 KiB
Bash
Executable File

#!/bin/bash
# Herolib Rust Installation Script
# This script installs the herolib_rust repository and its dependencies
# Can be run locally or curled from remote
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Configuration
REPO_URL="https://git.ourworld.tf/herocode/herolib_rust"
REPO_NAME="herolib_rust"
MIN_RUST_VERSION="1.70.0"
# Set CODEROOT (default to ~/code unless already set)
if [ -z "$CODEROOT" ]; then
CODEROOT="$HOME/code"
fi
TARGET_DIR="$CODEROOT/git.ourworld.tf/herocode/$REPO_NAME"
echo -e "${BLUE}===============================================${NC}"
echo -e "${BLUE} Herolib Rust Installation Script${NC}"
echo -e "${BLUE}===============================================${NC}"
echo ""
echo -e "${BLUE}Repository:${NC} $REPO_URL"
echo -e "${BLUE}Target Directory:${NC} $TARGET_DIR"
echo -e "${BLUE}CODEROOT:${NC} $CODEROOT"
echo ""
# Function to check if command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Function to compare version numbers
version_ge() {
printf '%s\n%s\n' "$2" "$1" | sort -V -C
}
# Function to get Rust version
get_rust_version() {
if command_exists rustc; then
rustc --version | cut -d' ' -f2
else
echo "0.0.0"
fi
}
# Function to install Rust
install_rust() {
echo -e "${YELLOW}📦 Installing Rust...${NC}"
if command_exists curl; then
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
elif command_exists wget; then
wget -qO- https://sh.rustup.rs | sh -s -- -y
else
echo -e "${RED}❌ Error: Neither curl nor wget is available. Please install one of them first.${NC}"
exit 1
fi
# Source the cargo environment
source "$HOME/.cargo/env"
echo -e "${GREEN}✅ Rust installed successfully${NC}"
}
# Function to update Rust
update_rust() {
echo -e "${YELLOW}📦 Updating Rust...${NC}"
rustup update
echo -e "${GREEN}✅ Rust updated successfully${NC}"
}
# Check system requirements
echo -e "${YELLOW}🔍 Checking system requirements...${NC}"
# Check for git
if ! command_exists git; then
echo -e "${RED}❌ Error: git is required but not installed${NC}"
echo "Please install git first:"
echo " macOS: brew install git"
echo " Ubuntu/Debian: sudo apt-get install git"
echo " CentOS/RHEL: sudo yum install git"
exit 1
fi
echo -e "${GREEN}✅ git found${NC}"
# Check for curl or wget
if ! command_exists curl && ! command_exists wget; then
echo -e "${RED}❌ Error: Either curl or wget is required${NC}"
echo "Please install one of them first:"
echo " macOS: curl is usually pre-installed"
echo " Ubuntu/Debian: sudo apt-get install curl"
echo " CentOS/RHEL: sudo yum install curl"
exit 1
fi
if command_exists curl; then
echo -e "${GREEN}✅ curl found${NC}"
else
echo -e "${GREEN}✅ wget found${NC}"
fi
# Check Rust installation
current_rust_version=$(get_rust_version)
if [ "$current_rust_version" = "0.0.0" ]; then
echo -e "${YELLOW}⚠️ Rust not found${NC}"
install_rust
elif ! version_ge "$current_rust_version" "$MIN_RUST_VERSION"; then
echo -e "${YELLOW}⚠️ Rust version $current_rust_version is below minimum required $MIN_RUST_VERSION${NC}"
update_rust
else
echo -e "${GREEN}✅ Rust $current_rust_version found (>= $MIN_RUST_VERSION required)${NC}"
fi
# Check cargo
if ! command_exists cargo; then
echo -e "${RED}❌ Error: cargo not found after Rust installation${NC}"
exit 1
fi
echo -e "${GREEN}✅ cargo found${NC}"
echo ""
# Clone or update repository
hero git clone $REPO_URL
REPO_PATH=$(hero git path $REPO_URL)
cd "$REPO_PATH"
# Install dependencies and build
echo -e "${YELLOW}🔧 Installing dependencies and building...${NC}"
# Check if we can build the workspace
echo -e "${BLUE} 📋 Checking workspace...${NC}"
if ! cargo check --workspace; then
echo -e "${RED}❌ Error: Workspace check failed${NC}"
exit 1
fi
echo -e "${GREEN}✅ Workspace check passed${NC}"
# Build the workspace
echo -e "${BLUE} 🔨 Building workspace...${NC}"
if ! cargo build --workspace; then
echo -e "${RED}❌ Error: Build failed${NC}"
exit 1
fi
echo -e "${GREEN}✅ Workspace built successfully${NC}"
# Build herodo binary
echo -e "${BLUE} 🔨 Building herodo binary...${NC}"
if [ -d "herodo" ]; then
cd herodo
if cargo build --release; then
echo -e "${GREEN}✅ herodo binary built successfully${NC}"
# Install herodo binary
if [ "$EUID" -eq 0 ]; then
# Running as root, install to /usr/local/bin
INSTALL_DIR="/usr/local/bin"
else
# Running as user, install to ~/hero/bin
INSTALL_DIR="$HOME/hero/bin"
mkdir -p "$INSTALL_DIR"
fi
cp ../target/release/herodo "$INSTALL_DIR/"
chmod +x "$INSTALL_DIR/herodo"
echo -e "${GREEN}✅ herodo installed to $INSTALL_DIR${NC}"
# Add to PATH if not already there
if [[ ":$PATH:" != *":$INSTALL_DIR:"* ]]; then
echo -e "${YELLOW}📝 Adding $INSTALL_DIR to PATH${NC}"
echo "export PATH=\"$INSTALL_DIR:\$PATH\"" >> "$HOME/.bashrc"
echo "export PATH=\"$INSTALL_DIR:\$PATH\"" >> "$HOME/.zshrc" 2>/dev/null || true
echo -e "${BLUE} Note: Restart your shell or run 'source ~/.bashrc' to use herodo${NC}"
fi
else
echo -e "${YELLOW}⚠️ herodo build failed, but continuing...${NC}"
fi
cd ..
else
echo -e "${YELLOW}⚠️ herodo directory not found, skipping binary build${NC}"
fi
# Run tests to verify installation
echo -e "${YELLOW}🧪 Running tests to verify installation...${NC}"
if cargo test --workspace --lib; then
echo -e "${GREEN}✅ All tests passed${NC}"
else
echo -e "${YELLOW}⚠️ Some tests failed, but installation completed${NC}"
fi
echo ""
echo -e "${GREEN}===============================================${NC}"
echo -e "${GREEN} Installation Complete!${NC}"
echo -e "${GREEN}===============================================${NC}"
echo ""
echo -e "${GREEN}🎉 Herolib Rust has been successfully installed!${NC}"
echo ""
echo -e "${BLUE}Installation Details:${NC}"
echo -e " Repository: $TARGET_DIR"
echo -e " CODEROOT: $CODEROOT"
echo -e " Rust Version: $(rustc --version | cut -d' ' -f2)"
echo -e " Cargo Version: $(cargo --version | cut -d' ' -f2)"
echo ""
echo -e "${BLUE}Available Scripts:${NC}"
echo -e " Build herodo: $TARGET_DIR/build_herodo.sh"
echo -e " Run Rhai tests: $TARGET_DIR/run_rhai_tests.sh"
echo -e " Publish crates: $TARGET_DIR/scripts/publish-all.sh"
echo ""
echo -e "${BLUE}Getting Started:${NC}"
echo -e " cd $TARGET_DIR"
echo -e " ./build_herodo.sh"
echo -e " ./run_rhai_tests.sh"
echo ""
echo -e "${BLUE}Documentation:${NC}"
echo -e " README: $TARGET_DIR/README.md"
echo -e " Examples: $TARGET_DIR/examples/"
echo ""
# Set environment variable for future use
echo "export HEROLIB_RUST_PATH=\"$TARGET_DIR\"" >> "$HOME/.bashrc"
echo "export HEROLIB_RUST_PATH=\"$TARGET_DIR\"" >> "$HOME/.zshrc" 2>/dev/null || true
echo -e "${GREEN}Happy coding! 🚀${NC}"
echo ""