Files
QuickWrt/build.sh
2025-09-23 14:29:28 +08:00

189 lines
6.9 KiB
Bash
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
set -euo pipefail # 更严格的安全设置
# =============================================================================
# 颜色和样式配置
# =============================================================================
readonly RED_COLOR='\033[1;31m'
readonly GREEN_COLOR='\033[1;32m'
readonly YELLOW_COLOR='\033[1;33m'
readonly BLUE_COLOR='\033[1;34m'
readonly MAGENTA_COLOR='\033[1;35m'
readonly CYAN_COLOR='\033[1;36m'
readonly BOLD='\033[1m'
readonly RESET='\033[0m'
readonly BLINK='\033[5m'
# =============================================================================
# 全局常量定义
# =============================================================================
readonly SCRIPT_NAME="OpenWRT Build System"
readonly SCRIPT_VERSION="1.0.0"
readonly AUTHOR="OPPEN321"
readonly BLOG="www.kejizero.online"
readonly SUPPORTED_ARCHITECTURES=("rockchip" "x86_64")
readonly REQUIRED_USER="zhao"
# =============================================================================
# 全局变量
# =============================================================================
GROUP_FLAG=false
START_TIME=$(date +%s)
CPU_CORES=$(( $(nproc --all) + 1 ))
# =============================================================================
# 函数定义
# =============================================================================
# 打印带颜色的消息
print_color() {
local color=$1
shift
echo -e "${color}$*${RESET}"
}
# 打印错误消息并退出
error_exit() {
print_color "$RED_COLOR" "❌ 错误: $1"
exit 1
}
# 打印警告消息
print_warning() {
print_color "$YELLOW_COLOR" "⚠️ 警告: $1"
}
# 打印成功消息
print_success() {
print_color "$GREEN_COLOR" "$1"
}
# 打印信息消息
print_info() {
print_color "$BLUE_COLOR" " 信息: $1"
}
# 验证必需的环境变量
validate_environment() {
if [[ "$(whoami)" != "$REQUIRED_USER" ]] && [[ -z "${git_name:-}" || -z "${git_password:-}" ]]; then
error_exit "未授权访问。请设置认证信息后再执行此脚本。"
fi
}
# 显示使用帮助
show_usage() {
echo -e "\n${BOLD}使用方法:${RESET}"
echo -e " bash $0 <version> <architecture>"
echo -e "\n${BOLD}支持的架构:${RESET}"
for arch in "${SUPPORTED_ARCHITECTURES[@]}"; do
echo -e "${GREEN_COLOR}$arch${RESET}"
done
echo -e "\n${BOLD}示例:${RESET}"
echo -e " bash $0 v24 x86_64"
echo -e " bash $0 v24 rockchip"
}
# 验证参数
validate_arguments() {
local version="$1"
local arch="$2"
if [[ -z "$version" ]]; then
error_exit "未指定版本号"
fi
if [[ -z "$arch" ]]; then
error_exit "未指定目标架构"
fi
local valid_arch=false
for supported_arch in "${SUPPORTED_ARCHITECTURES[@]}"; do
if [[ "$arch" == "$supported_arch" ]]; then
valid_arch=true
break
fi
done
if [[ "$valid_arch" == false ]]; then
error_exit "不支持的架构: '$arch'"
fi
}
# 显示横幅
show_banner() {
clear
echo -e ""
echo -e "${BOLD}${BLUE_COLOR}╔══════════════════════════════════════════════════════════════════╗${RESET}"
echo -e "${BOLD}${BLUE_COLOR}${RESET} OPENWRT 自动化构建系统 ${BOLD}${BLUE_COLOR}${RESET}"
echo -e "${BOLD}${BLUE_COLOR}╚══════════════════════════════════════════════════════════════════╝${RESET}"
echo -e "${BOLD}${BLUE_COLOR}┌────────────────────────────────────────────────────────────────────┐${RESET}"
echo -e "${BOLD}${BLUE_COLOR}${RESET} 🛠️ ${BOLD}开发者:${RESET} $AUTHOR ${BOLD}${BLUE_COLOR}${RESET}"
echo -e "${BOLD}${BLUE_COLOR}${RESET} 🌐 ${BOLD}博客:${RESET} $BLOG ${BOLD}${BLUE_COLOR}${RESET}"
echo -e "${BOLD}${BLUE_COLOR}${RESET} 💡 ${BOLD}理念:${RESET} 开源 · 定制化 · 高性能 ${BOLD}${BLUE_COLOR}${RESET}"
echo -e "${BOLD}${BLUE_COLOR}${RESET} 📦 ${BOLD}版本:${RESET} $SCRIPT_VERSION ${BOLD}${BLUE_COLOR}${RESET}"
echo -e "${BOLD}${BLUE_COLOR}└────────────────────────────────────────────────────────────────────┘${RESET}"
echo -e "${BOLD}${BLUE_COLOR}══════════════════════════════════════════════════════════════════════${RESET}"
echo -e "${BOLD}🔧 ${GREEN_COLOR}构建开始时间:${RESET} $(date '+%Y-%m-%d %H:%M:%S')"
echo -e "${BOLD}${GREEN_COLOR}处理器核心数:${RESET} $CPU_CORES"
echo -e "${BOLD}🐧 ${GREEN_COLOR}系统用户:${RESET} $(whoami)"
echo -e "${BOLD}${BLUE_COLOR}══════════════════════════════════════════════════════════════════════${RESET}"
echo -e ""
}
# 初始化构建环境
setup_build_environment() {
if [[ "$(id -u)" == "0" ]]; then
export FORCE_UNSAFE_CONFIGURE=1
export FORCE=1
print_warning "以 root 权限运行,已启用强制不安全配置"
fi
}
# 设置下载进度条
setup_curl_progress() {
if curl --help | grep -q progress-bar; then
CURL_OPTIONS="--progress-bar"
else
CURL_OPTIONS="--silent"
fi
export CURL_OPTIONS
}
# =============================================================================
# 主程序逻辑
# =============================================================================
main() {
local version="${1:-}"
local architecture="${2:-}"
# 参数验证
validate_arguments "$version" "$architecture"
# 显示横幅
show_banner
# 环境验证
validate_environment
# 环境设置
setup_build_environment
setup_curl_progress
print_success "初始化完成,开始构建 $architecture 架构的 $version 版本"
# 记录开始时间
START_TIME=$(date +%s)
}
# 脚本入口点
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
# 如果没有提供足够的参数,显示使用帮助
if [[ $# -lt 2 ]]; then
show_usage
error_exit "参数不足,需要指定版本号和目标架构"
fi
# 执行主程序
main "$@"
fi