From c1e56157f55ef59855cbd03e14942b6ba0d237a3 Mon Sep 17 00:00:00 2001 From: Mars Date: Thu, 20 Mar 2025 14:40:43 +0800 Subject: [PATCH] Initial commit: WSL development environment setup --- .gitattributes | 19 + .gitignore | 34 ++ README.md | 182 ++++++ init.sh | 140 +++++ scripts/database/mysql.sh | 1 + scripts/devtools/docker.sh | 137 +++++ scripts/devtools/go.sh | 120 ++++ scripts/devtools/node.sh | 134 +++++ scripts/devtools/python.sh | 204 +++++++ scripts/editor/neovim.sh | 218 ++++++++ scripts/editor/nvchad.sh | 792 +++++++++++++++++++++++++++ scripts/php/dnmp.sh | 155 ++++++ scripts/shell/plugins.sh | 136 +++++ scripts/shell/zsh.sh | 192 +++++++ scripts/system/base.sh | 172 ++++++ scripts/system/check_windows_wsl.bat | 134 +++++ scripts/system/check_windows_wsl.ps1 | 192 +++++++ scripts/system/check_wsl.sh | 243 ++++++++ scripts/system/mirror.sh | 152 +++++ scripts/system/temp.txt | Bin 0 -> 136 bytes scripts/system/upgrade_wsl2.bat | 129 +++++ scripts/utils/git.sh | 156 ++++++ scripts/utils/tools.sh | 327 +++++++++++ temp.txt | 5 + 24 files changed, 3974 insertions(+) create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 README.md create mode 100644 init.sh create mode 100644 scripts/database/mysql.sh create mode 100644 scripts/devtools/docker.sh create mode 100644 scripts/devtools/go.sh create mode 100644 scripts/devtools/node.sh create mode 100644 scripts/devtools/python.sh create mode 100644 scripts/editor/neovim.sh create mode 100644 scripts/editor/nvchad.sh create mode 100644 scripts/php/dnmp.sh create mode 100644 scripts/shell/plugins.sh create mode 100644 scripts/shell/zsh.sh create mode 100644 scripts/system/base.sh create mode 100644 scripts/system/check_windows_wsl.bat create mode 100644 scripts/system/check_windows_wsl.ps1 create mode 100644 scripts/system/check_wsl.sh create mode 100644 scripts/system/mirror.sh create mode 100644 scripts/system/temp.txt create mode 100644 scripts/system/upgrade_wsl2.bat create mode 100644 scripts/utils/git.sh create mode 100644 scripts/utils/tools.sh create mode 100644 temp.txt diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..d3dd808 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,19 @@ +# Auto detect text files and perform LF normalization +* text=auto + +# Force bash scripts to use LF +*.sh text eol=lf + +# Force batch files to use CRLF +*.bat text eol=crlf + +# Force specific file types to use LF +*.json text eol=lf +*.yml text eol=lf +*.yaml text eol=lf +*.md text eol=lf +*.txt text eol=lf + +# Force specific file types to use CRLF +*.cmd text eol=crlf +*.ps1 text eol=crlf \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ff100cc --- /dev/null +++ b/.gitignore @@ -0,0 +1,34 @@ +# Windows system files +Thumbs.db +Desktop.ini + +# WSL specific +*.vhdx +*.vhd + +# Logs +*.log +logs/ +*.log.* + +# Editor directories and files +.idea/ +.vscode/ +*.swp +*.swo +*~ + +# OS generated files +.DS_Store +.DS_Store? +._* +.Spotlight-V100 +.Trashes +ehthumbs.db +Thumbs.db + +# Temporary files +*.tmp +*.temp +*.bak +*.backup \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..bce7659 --- /dev/null +++ b/README.md @@ -0,0 +1,182 @@ +# WSL2 Debian 开发环境配置 + +这是一个用于在 Windows 11 上配置 WSL2 Debian 开发环境的项目。该项目提供了一系列脚本来自动化安装和配置各种开发工具和环境。 + +## 功能特性 + +### 基础系统环境 +- 安装 WSL2 Debian +- 系统更新和配置 +- 配置镜像源 +- 安装基础工具 +- WSL2 环境检查(Windows 端) +- WSL2 环境检查(Linux 端) + +### Shell 环境 +- 安装和配置 Zsh +- 安装常用 Zsh 插件 + - zsh-autosuggestions + - zsh-syntax-highlighting + - zsh-completions + - autojump + - fzf + - zsh-nvm + - pyenv + +### 编辑器环境 +- 安装和配置 Neovim +- 安装常用 Neovim 插件 +- 配置 NvChad + +### 开发工具链 +- Docker 和 Docker Compose +- Go 语言环境 +- Node.js 环境 +- Python 环境 +- PHP 开发环境(使用 DNMP) + +### 系统工具 +- Git 配置和工具 +- 系统监控工具 +- 网络工具 +- 安全工具 +- 终端工具 +- 开发效率工具 + +## 项目结构 + +``` +. +├── README.md +├── init.sh +└── scripts/ + ├── shell/ + │ ├── zsh.sh + │ └── plugins.sh + ├── editor/ + │ ├── neovim.sh + │ └── nvchad.sh + ├── devtools/ + │ ├── docker.sh + │ ├── go.sh + │ ├── node.sh + │ └── python.sh + ├── php/ + │ └── dnmp.sh + ├── system/ + │ ├── check_windows_wsl.bat + │ └── check_wsl.sh + └── utils/ + ├── git.sh + └── tools.sh +``` + +## 使用方法 + +### 系统要求 +- Windows 11 +- WSL2 +- Debian 12 + +### 安装步骤 + +1. 克隆项目 +```bash +git clone https://github.com/yourusername/wsl2-debian-init.git +cd wsl2-debian-init +``` + +2. 检查 Windows 上的 WSL2 环境(在 CMD 中运行) +```cmd +scripts\system\check_windows_wsl.bat +``` + +3. 检查 WSL2 内部环境(在 WSL 终端中运行) +```bash +./scripts/system/check_wsl.sh +``` + +4. 运行初始化脚本 +```bash +./init.sh +``` + +5. 按需运行各个模块的安装脚本 +```bash +# 安装 Shell 环境 +./scripts/shell/zsh.sh +./scripts/shell/plugins.sh + +# 安装编辑器环境 +./scripts/editor/neovim.sh +./scripts/editor/nvchad.sh + +# 安装开发工具链 +./scripts/devtools/docker.sh +./scripts/devtools/go.sh +./scripts/devtools/node.sh +./scripts/devtools/python.sh + +# 安装 PHP 开发环境 +./scripts/php/dnmp.sh + +# 安装系统工具 +./scripts/utils/git.sh +./scripts/utils/tools.sh +``` + +## 脚本说明 + +### 系统环境脚本 +- `scripts/system/check_windows_wsl.bat`: 在 Windows 端检查 WSL2 环境,包括: + - Windows 版本检查 + - WSL 安装检查 + - WSL2 状态检查 + - 虚拟化检查 + - Debian 安装检查 + - Windows 功能检查 + +- `scripts/system/check_wsl.sh`: 在 WSL 端检查环境,包括: + - Windows 版本检查 + - WSL 版本检查 + - 系统要求检查 + - Debian 版本检查 + - 系统更新检查 + - 必要组件检查 + - 网络连接检查 + - WSL 配置检查 + +### Shell 环境脚本 +- `scripts/shell/zsh.sh`: 安装和配置 Zsh +- `scripts/shell/plugins.sh`: 安装常用 Zsh 插件 + +### 编辑器环境脚本 +- `scripts/editor/neovim.sh`: 安装和配置 Neovim +- `scripts/editor/nvchad.sh`: 配置 NvChad + +### 开发工具链脚本 +- `scripts/devtools/docker.sh`: 安装 Docker 和 Docker Compose +- `scripts/devtools/go.sh`: 安装 Go 语言环境 +- `scripts/devtools/node.sh`: 安装 Node.js 环境 +- `scripts/devtools/python.sh`: 安装 Python 环境 + +### PHP 开发环境脚本 +- `scripts/php/dnmp.sh`: 安装和配置 PHP 开发环境(使用 DNMP) + +### 系统工具脚本 +- `scripts/utils/git.sh`: 配置 Git 和安装 Git 工具 +- `scripts/utils/tools.sh`: 安装各种系统工具和开发效率工具 + +## 常见问题 + +1. 如果遇到权限问题,请使用 `sudo` 运行脚本 +2. 如果遇到网络问题,请检查网络连接或使用代理 +3. 如果遇到依赖问题,请先运行 `sudo apt-get update && sudo apt-get upgrade` + +## 贡献指南 + +欢迎提交 Issue 和 Pull Request 来帮助改进这个项目。 + +## 许可证 + +MIT License \ No newline at end of file diff --git a/init.sh b/init.sh new file mode 100644 index 0000000..d9130c8 --- /dev/null +++ b/init.sh @@ -0,0 +1,140 @@ +#!/bin/bash + +# 设置错误时立即退出 +set -e + +# 颜色定义 +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +NC='\033[0m' # No Color + +# 日志函数 +log_info() { + echo -e "${GREEN}[INFO]${NC} $1" +} + +log_warn() { + echo -e "${YELLOW}[WARN]${NC} $1" +} + +log_error() { + echo -e "${RED}[ERROR]${NC} $1" +} + +# 检查是否为root用户 +check_root() { + if [ "$EUID" -ne 0 ]; then + log_error "请使用root权限运行此脚本" + exit 1 + fi +} + +# 检查系统要求 +check_system_requirements() { + log_info "检查系统要求..." + + # 检查内存 + local total_mem=$(free -m | awk '/^Mem:/{print $2}') + if [ "$total_mem" -lt 8192 ]; then + log_warn "系统内存小于8GB,可能会影响性能" + fi + + # 检查磁盘空间 + local free_space=$(df -m / | awk 'NR==2 {print $4}') + if [ "$free_space" -lt 51200 ]; then + log_warn "可用磁盘空间小于50GB,建议清理空间" + fi + + # 检查网络连接 + if ! ping -c 1 8.8.8.8 &> /dev/null; then + log_error "无法连接到网络,请检查网络连接" + exit 1 + fi +} + +# 安装基础系统 +install_base_system() { + log_info "开始安装基础系统..." + bash scripts/system/base.sh +} + +# 配置镜像源 +configure_mirrors() { + log_info "配置镜像源..." + bash scripts/system/mirror.sh +} + +# 安装Shell环境 +install_shell() { + log_info "安装Shell环境..." + bash scripts/shell/zsh.sh + bash scripts/shell/plugins.sh +} + +# 安装编辑器 +install_editor() { + log_info "安装编辑器..." + bash scripts/editor/neovim.sh + bash scripts/editor/nvchad.sh +} + +# 安装开发工具 +install_devtools() { + log_info "安装开发工具..." + bash scripts/devtools/docker.sh + bash scripts/devtools/go.sh + bash scripts/devtools/node.sh + bash scripts/devtools/python.sh +} + +# 安装PHP环境 +install_php() { + log_info "安装PHP环境..." + bash scripts/php/dnmp.sh +} + +# 安装系统工具 +install_utils() { + log_info "安装系统工具..." + bash scripts/utils/git.sh + bash scripts/utils/tools.sh +} + +# 主函数 +main() { + log_info "开始安装开发环境..." + + # 检查root权限 + check_root + + # 检查系统要求 + check_system_requirements + + # 安装基础系统 + install_base_system + + # 配置镜像源 + configure_mirrors + + # 安装Shell环境 + install_shell + + # 安装编辑器 + install_editor + + # 安装开发工具 + install_devtools + + # 安装PHP环境 + install_php + + # 安装系统工具 + install_utils + + log_info "安装完成!" + log_info "请重新登录以应用所有更改。" +} + +# 执行主函数 +main \ No newline at end of file diff --git a/scripts/database/mysql.sh b/scripts/database/mysql.sh new file mode 100644 index 0000000..0519ecb --- /dev/null +++ b/scripts/database/mysql.sh @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/scripts/devtools/docker.sh b/scripts/devtools/docker.sh new file mode 100644 index 0000000..2bd2b4c --- /dev/null +++ b/scripts/devtools/docker.sh @@ -0,0 +1,137 @@ +#!/bin/bash + +# 设置错误时立即退出 +set -e + +# 颜色定义 +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +NC='\033[0m' # No Color + +# 日志函数 +log_info() { + echo -e "${GREEN}[INFO]${NC} $1" +} + +log_warn() { + echo -e "${YELLOW}[WARN]${NC} $1" +} + +log_error() { + echo -e "${RED}[ERROR]${NC} $1" +} + +# 安装Docker Engine +install_docker_engine() { + log_info "安装Docker Engine..." + + # 卸载旧版本 + apt-get remove -y docker docker-engine docker.io containerd runc || true + + # 安装依赖 + apt-get update + apt-get install -y \ + apt-transport-https \ + ca-certificates \ + curl \ + gnupg \ + lsb-release + + # 添加Docker官方GPG密钥 + curl -fsSL https://download.docker.com/linux/debian/gpg | gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg + + # 设置稳定版仓库 + echo \ + "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/debian \ + $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null + + # 安装Docker Engine + apt-get update + apt-get install -y docker-ce docker-ce-cli containerd.io + + # 启动Docker服务 + systemctl start docker + systemctl enable docker + + # 检查Docker状态 + if ! systemctl is-active --quiet docker; then + log_error "Docker服务启动失败" + exit 1 + fi + + log_info "Docker Engine安装完成" +} + +# 安装Docker Compose +install_docker_compose() { + log_info "安装Docker Compose..." + + # 下载Docker Compose + curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose + + # 添加执行权限 + chmod +x /usr/local/bin/docker-compose + + # 创建命令补全 + curl -L https://raw.githubusercontent.com/docker/compose/master/contrib/completion/bash/docker-compose -o /etc/bash_completion.d/docker-compose + + log_info "Docker Compose安装完成" +} + +# 配置Docker +configure_docker() { + log_info "配置Docker..." + + # 创建docker用户组 + groupadd docker || true + + # 将当前用户添加到docker组 + usermod -aG docker $USER + + # 配置Docker守护进程 + cat > /etc/docker/daemon.json << EOF +{ + "exec-opts": ["native.cgroupdriver=systemd"], + "log-driver": "json-file", + "log-opts": { + "max-size": "100m", + "max-file": "3" + }, + "storage-driver": "overlay2", + "data-root": "/var/lib/docker", + "registry-mirrors": [ + "https://mirror.ccs.tencentyun.com", + "https://registry.docker-cn.com", + "https://docker.mirrors.ustc.edu.cn" + ] +} +EOF + + # 重启Docker服务 + systemctl daemon-reload + systemctl restart docker + + log_info "Docker配置完成" +} + +# 主函数 +main() { + log_info "开始安装Docker..." + + # 安装Docker Engine + install_docker_engine + + # 安装Docker Compose + install_docker_compose + + # 配置Docker + configure_docker + + log_info "Docker安装完成!" + log_info "请重新登录以使docker组权限生效。" + log_info "Windows端请手动安装Docker Desktop: https://www.docker.com/products/docker-desktop" +} + +# 执行主函数 +main \ No newline at end of file diff --git a/scripts/devtools/go.sh b/scripts/devtools/go.sh new file mode 100644 index 0000000..fa55f7a --- /dev/null +++ b/scripts/devtools/go.sh @@ -0,0 +1,120 @@ +#!/bin/bash + +# 设置错误时立即退出 +set -e + +# 颜色定义 +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +NC='\033[0m' # No Color + +# 日志函数 +log_info() { + echo -e "${GREEN}[INFO]${NC} $1" +} + +log_warn() { + echo -e "${YELLOW}[WARN]${NC} $1" +} + +log_error() { + echo -e "${RED}[ERROR]${NC} $1" +} + +# 安装Go +install_go() { + log_info "安装Go..." + + # 下载最新版本的Go + GO_VERSION=$(curl -s https://golang.org/VERSION?m=text) + GO_URL="https://golang.org/dl/${GO_VERSION}.linux-amd64.tar.gz" + + # 下载并解压 + curl -L $GO_URL -o /tmp/go.tar.gz + tar -C /usr/local -xzf /tmp/go.tar.gz + rm /tmp/go.tar.gz + + # 设置环境变量 + cat >> ~/.zshrc << EOF +# Go环境变量 +export GOROOT=/usr/local/go +export GOPATH=\$HOME/go +export PATH=\$PATH:\$GOROOT/bin:\$GOPATH/bin +EOF + + # 创建Go工作目录 + mkdir -p ~/go/{bin,src,pkg} + + log_info "Go安装完成" +} + +# 安装常用Go工具 +install_go_tools() { + log_info "安装常用Go工具..." + + # 安装gopls + go install golang.org/x/tools/gopls@latest + + # 安装delve调试器 + go install github.com/go-delve/delve/cmd/dlv@latest + + # 安装air热重载工具 + go install github.com/cosmtrek/air@latest + + # 安装mockgen + go install github.com/golang/mock/mockgen@latest + + # 安装protoc-gen-go + go install google.golang.org/protobuf/cmd/protoc-gen-go@latest + go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest + + log_info "Go工具安装完成" +} + +# 配置Go +configure_go() { + log_info "配置Go..." + + # 设置Go代理 + go env -w GO111MODULE=on + go env -w GOPROXY=https://goproxy.cn,direct + + # 配置gopls + cat > ~/.config/gopls/settings.json << EOF +{ + "ui.semanticTokens": true, + "formatting.local": "github.com/your-username", + "completion.placeholder": true, + "hover.documentation": true, + "hover.diagnostics": true, + "codeLens.documentLink": true, + "codeLens.references": true, + "codeLens.implementations": true, + "codeLens.typeDefinitions": true, + "codeLens.commands": true +} +EOF + + log_info "Go配置完成" +} + +# 主函数 +main() { + log_info "开始安装Go开发环境..." + + # 安装Go + install_go + + # 安装常用Go工具 + install_go_tools + + # 配置Go + configure_go + + log_info "Go开发环境安装完成!" + log_info "请运行 'source ~/.zshrc' 或重新打开终端以使环境变量生效。" +} + +# 执行主函数 +main \ No newline at end of file diff --git a/scripts/devtools/node.sh b/scripts/devtools/node.sh new file mode 100644 index 0000000..696c8a7 --- /dev/null +++ b/scripts/devtools/node.sh @@ -0,0 +1,134 @@ +#!/bin/bash + +# 设置错误时立即退出 +set -e + +# 颜色定义 +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +NC='\033[0m' # No Color + +# 日志函数 +log_info() { + echo -e "${GREEN}[INFO]${NC} $1" +} + +log_warn() { + echo -e "${YELLOW}[WARN]${NC} $1" +} + +log_error() { + echo -e "${RED}[ERROR]${NC} $1" +} + +# 安装nvm +install_nvm() { + log_info "安装nvm..." + + # 下载nvm安装脚本 + curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash + + # 配置nvm环境变量 + cat >> ~/.zshrc << EOF +# NVM配置 +export NVM_DIR="\$HOME/.nvm" +[ -s "\$NVM_DIR/nvm.sh" ] && \. "\$NVM_DIR/nvm.sh" # 加载nvm +[ -s "\$NVM_DIR/bash_completion" ] && \. "\$NVM_DIR/bash_completion" # 加载nvm bash_completion +EOF + + # 重新加载配置 + export NVM_DIR="$HOME/.nvm" + [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" + [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" + + log_info "nvm安装完成" +} + +# 安装Node.js +install_nodejs() { + log_info "安装Node.js..." + + # 安装最新的LTS版本 + nvm install --lts + + # 设置默认版本 + nvm alias default 'lts/*' + + # 使用默认版本 + nvm use default + + log_info "Node.js安装完成" +} + +# 配置npm +configure_npm() { + log_info "配置npm..." + + # 设置npm镜像 + npm config set registry https://registry.npmmirror.com + + # 设置npm全局安装路径 + mkdir -p ~/.npm-global + npm config set prefix '~/.npm-global' + + # 添加npm全局路径到环境变量 + cat >> ~/.zshrc << EOF +# npm全局路径 +export PATH=~/.npm-global/bin:\$PATH +EOF + + log_info "npm配置完成" +} + +# 安装全局npm包 +install_global_packages() { + log_info "安装全局npm包..." + + # 安装常用开发工具 + npm install -g typescript + npm install -g ts-node + npm install -g nodemon + npm install -g yarn + npm install -g pnpm + npm install -g @vue/cli + npm install -g create-react-app + npm install -g @angular/cli + npm install -g nx + + # 安装开发效率工具 + npm install -g serve + npm install -g http-server + npm install -g json-server + npm install -g concurrently + + # 安装代码质量工具 + npm install -g eslint + npm install -g prettier + npm install -g stylelint + + log_info "全局npm包安装完成" +} + +# 主函数 +main() { + log_info "开始安装Node.js开发环境..." + + # 安装nvm + install_nvm + + # 安装Node.js + install_nodejs + + # 配置npm + configure_npm + + # 安装全局npm包 + install_global_packages + + log_info "Node.js开发环境安装完成!" + log_info "请运行 'source ~/.zshrc' 或重新打开终端以使环境变量生效。" +} + +# 执行主函数 +main \ No newline at end of file diff --git a/scripts/devtools/python.sh b/scripts/devtools/python.sh new file mode 100644 index 0000000..1a7258b --- /dev/null +++ b/scripts/devtools/python.sh @@ -0,0 +1,204 @@ +#!/bin/bash + +# 设置错误时立即退出 +set -e + +# 颜色定义 +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +NC='\033[0m' # No Color + +# 日志函数 +log_info() { + echo -e "${GREEN}[INFO]${NC} $1" +} + +log_warn() { + echo -e "${YELLOW}[WARN]${NC} $1" +} + +log_error() { + echo -e "${RED}[ERROR]${NC} $1" +} + +# 安装系统依赖 +install_dependencies() { + log_info "安装系统依赖..." + + # 安装Python开发相关包 + sudo apt-get update + sudo apt-get install -y \ + python3-dev \ + python3-pip \ + python3-venv \ + build-essential \ + libssl-dev \ + libffi-dev \ + libxml2-dev \ + libxslt1-dev \ + zlib1g-dev \ + libbz2-dev \ + libreadline-dev \ + libsqlite3-dev \ + libncursesw5-dev \ + xz-utils \ + tk-dev \ + libxmlsec1-dev \ + libffi-dev \ + liblzma-dev + + log_info "系统依赖安装完成" +} + +# 安装pyenv +install_pyenv() { + log_info "安装pyenv..." + + # 下载pyenv安装脚本 + curl https://pyenv.run | bash + + # 配置pyenv环境变量 + cat >> ~/.zshrc << EOF +# pyenv配置 +export PYENV_ROOT="\$HOME/.pyenv" +export PATH="\$PYENV_ROOT/bin:\$PATH" +eval "\$(pyenv init --path)" +eval "\$(pyenv init -)" +EOF + + # 重新加载配置 + export PYENV_ROOT="$HOME/.pyenv" + export PATH="$PYENV_ROOT/bin:$PATH" + eval "$(pyenv init --path)" + eval "$(pyenv init -)" + + log_info "pyenv安装完成" +} + +# 安装Python +install_python() { + log_info "安装Python..." + + # 安装最新的Python 3.11 + pyenv install 3.11.7 + + # 设置全局Python版本 + pyenv global 3.11.7 + + # 验证安装 + python --version + + log_info "Python安装完成" +} + +# 配置pip +configure_pip() { + log_info "配置pip..." + + # 升级pip + python -m pip install --upgrade pip + + # 设置pip镜像 + pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple + + # 安装pip工具 + pip install wheel setuptools + + log_info "pip配置完成" +} + +# 安装Python包 +install_python_packages() { + log_info "安装Python包..." + + # 安装开发工具 + pip install ipython + pip install jupyter + pip install jupyterlab + pip install black + pip install flake8 + pip install mypy + pip install pylint + pip install pytest + pip install pytest-cov + + # 安装数据处理工具 + pip install numpy + pip install pandas + pip install scipy + pip install matplotlib + pip install seaborn + + # 安装Web开发工具 + pip install django + pip install flask + pip install fastapi + pip install uvicorn + pip install sqlalchemy + pip install alembic + + # 安装机器学习工具 + pip install scikit-learn + pip install tensorflow + pip install torch + pip install transformers + + # 安装其他工具 + pip install requests + pip install python-dotenv + pip install rich + pip install tqdm + + log_info "Python包安装完成" +} + +# 配置虚拟环境 +configure_venv() { + log_info "配置虚拟环境..." + + # 创建虚拟环境目录 + mkdir -p ~/.virtualenvs + + # 安装virtualenvwrapper + pip install virtualenvwrapper + + # 配置virtualenvwrapper + cat >> ~/.zshrc << EOF +# virtualenvwrapper配置 +export WORKON_HOME=~/.virtualenvs +export PROJECT_HOME=~/projects +source ~/.local/bin/virtualenvwrapper.sh +EOF + + log_info "虚拟环境配置完成" +} + +# 主函数 +main() { + log_info "开始安装Python开发环境..." + + # 安装系统依赖 + install_dependencies + + # 安装pyenv + install_pyenv + + # 安装Python + install_python + + # 配置pip + configure_pip + + # 安装Python包 + install_python_packages + + # 配置虚拟环境 + configure_venv + + log_info "Python开发环境安装完成!" + log_info "请运行 'source ~/.zshrc' 或重新打开终端以使环境变量生效。" +} + +# 执行主函数 +main \ No newline at end of file diff --git a/scripts/editor/neovim.sh b/scripts/editor/neovim.sh new file mode 100644 index 0000000..71df9d3 --- /dev/null +++ b/scripts/editor/neovim.sh @@ -0,0 +1,218 @@ +#!/bin/bash + +# 设置错误时立即退出 +set -e + +# 颜色定义 +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +NC='\033[0m' # No Color + +# 日志函数 +log_info() { + echo -e "${GREEN}[INFO]${NC} $1" +} + +log_warn() { + echo -e "${YELLOW}[WARN]${NC} $1" +} + +log_error() { + echo -e "${RED}[ERROR]${NC} $1" +} + +# 安装依赖 +install_dependencies() { + log_info "安装Neovim依赖..." + apt-get install -y \ + ninja-build \ + gettext \ + cmake \ + unzip \ + curl \ + git \ + nodejs \ + npm \ + python3-pip \ + python3-venv \ + ripgrep \ + fd-find \ + bat \ + exa \ + delta \ + tree +} + +# 安装Neovim +install_neovim() { + log_info "安装Neovim..." + + # 添加Neovim仓库 + add-apt-repository -y ppa:neovim-ppa/unstable + apt-get update + + # 安装Neovim + apt-get install -y neovim + + # 创建配置目录 + mkdir -p ~/.config/nvim +} + +# 安装Python包 +install_python_packages() { + log_info "安装Python包..." + + # 创建虚拟环境 + python3 -m venv ~/.local/share/nvim/venv + + # 激活虚拟环境并安装包 + source ~/.local/share/nvim/venv/bin/activate + pip install pynvim + pip install black + pip install isort + pip install flake8 + pip install mypy + pip install jedi + deactivate +} + +# 安装Node.js包 +install_node_packages() { + log_info "安装Node.js包..." + + # 安装全局npm包 + npm install -g neovim + npm install -g typescript + npm install -g ts-node + npm install -g prettier + npm install -g eslint +} + +# 配置Neovim +configure_neovim() { + log_info "配置Neovim..." + + # 创建基本配置 + cat > ~/.config/nvim/init.vim << EOF +" 基本设置 +set number +set relativenumber +set expandtab +set tabstop=4 +set shiftwidth=4 +set softtabstop=4 +set smartindent +set nowrap +set noswapfile +set nobackup +set undodir=~/.vim/undodir +set undofile +set incsearch +set nohlsearch +set ignorecase +set smartcase +set termguicolors +set scrolloff=8 +set signcolumn=yes +set updatetime=50 +set colorcolumn=80 +set splitright +set splitbelow + +" 设置leader键 +let mapleader = " " + +" 设置文件类型检测 +filetype plugin indent on + +" 设置语法高亮 +syntax enable + +" 设置编码 +set encoding=utf-8 +set fileencoding=utf-8 +set termencoding=utf-8 + +" 设置状态栏 +set laststatus=2 +set statusline=%F%m%r%h%w\ [FORMAT=%{&ff}]\ [TYPE=%Y]\ [POS=%l,%v][%p%%]\ %{strftime(\"%d/%m/%y\ -\ %H:%M\")} + +" 设置命令模式补全 +set wildmenu +set wildmode=list:longest,full + +" 设置历史记录 +set history=1000 +set undolevels=1000 + +" 设置鼠标支持 +set mouse=a + +" 设置剪贴板 +set clipboard=unnamedplus + +" 设置自动补全 +set completeopt=menu,menuone,noselect + +" 设置终端颜色 +set t_Co=256 +set background=dark +EOF +} + +# 安装语言服务器 +install_language_servers() { + log_info "安装语言服务器..." + + # 安装各种语言的LSP服务器 + npm install -g typescript-language-server + npm install -g vscode-langservers-extracted + npm install -g @tailwindcss/language-server + npm install -g @prisma/language-server + npm install -g graphql-language-service-cli + npm install -g vscode-html-languageserver-bin + npm install -g vscode-css-languageserver-bin + npm install -g vscode-json-languageserver-bin + npm install -g vscode-eslint-language-server + npm install -g @typescript-eslint/parser + npm install -g @typescript-eslint/eslint-plugin + npm install -g eslint_d + npm install -g prettier + npm install -g stylelint + npm install -g markdownlint-cli + npm install -g yaml-language-server + npm install -g dockerfile-language-server-nodejs + npm install -g bash-language-server + npm install -g vscode-langservers-extracted +} + +# 主函数 +main() { + log_info "开始安装Neovim..." + + # 安装依赖 + install_dependencies + + # 安装Neovim + install_neovim + + # 安装Python包 + install_python_packages + + # 安装Node.js包 + install_node_packages + + # 配置Neovim + configure_neovim + + # 安装语言服务器 + install_language_servers + + log_info "Neovim安装完成!" + log_info "请运行 nvim 来启动编辑器。" + log_info "首次启动时会自动安装插件。" +} + +# 执行主函数 +main \ No newline at end of file diff --git a/scripts/editor/nvchad.sh b/scripts/editor/nvchad.sh new file mode 100644 index 0000000..8577659 --- /dev/null +++ b/scripts/editor/nvchad.sh @@ -0,0 +1,792 @@ +#!/bin/bash + +# 设置错误时立即退出 +set -e + +# 颜色定义 +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +NC='\033[0m' # No Color + +# 日志函数 +log_info() { + echo -e "${GREEN}[INFO]${NC} $1" +} + +log_warn() { + echo -e "${YELLOW}[WARN]${NC} $1" +} + +log_error() { + echo -e "${RED}[ERROR]${NC} $1" +} + +# 备份现有配置 +backup_config() { + log_info "备份现有配置..." + if [ -d ~/.config/nvim ]; then + mv ~/.config/nvim ~/.config/nvim.backup + fi +} + +# 安装NvChad +install_nvchad() { + log_info "安装NvChad..." + + # 克隆NvChad仓库 + git clone https://github.com/NvChad/NvChad ~/.config/nvim --depth 1 + + # 创建自定义配置目录 + mkdir -p ~/.config/nvim/lua/custom +} + +# 配置NvChad +configure_nvchad() { + log_info "配置NvChad..." + + # 创建自定义配置 + cat > ~/.config/nvim/lua/custom/init.lua << EOF +local M = {} + +-- 自定义配置 +M.config = { + -- 设置主题 + theme = "catppuccin", + + -- 设置字体 + font = "MesloLGS NF", + + -- 设置终端 + terminal = "kitty", + + -- 设置透明度 + transparency = false, + + -- 设置行号 + line_numbers = true, + + -- 设置相对行号 + relative_line_numbers = true, + + -- 设置状态栏 + statusline = { + theme = "minimal", + separator_style = "block", + }, + + -- 设置标签栏 + tabline = { + theme = "minimal", + separator_style = "block", + }, + + -- 设置文件树 + nvimtree = { + side = "left", + width = 30, + }, + + -- 设置窗口 + window = { + border = "rounded", + padding = 1, + }, + + -- 设置补全 + completion = { + border = "rounded", + padding = 1, + }, + + -- 设置诊断 + diagnostics = { + virtual_text = true, + signs = true, + underline = true, + }, + + -- 设置LSP + lsp = { + signature = true, + virtual_text = true, + signs = true, + underline = true, + }, + + -- 设置格式化 + formatting = { + timeout_ms = 500, + async = true, + quiet = false, + }, + + -- 设置快捷键 + keymaps = { + -- 窗口管理 + [""] = "h", + [""] = "j", + [""] = "k", + [""] = "l", + [""] = ":resize -2", + [""] = ":resize +2", + [""] = ":vertical resize -2", + [""] = ":vertical resize +2", + + -- 标签页管理 + [""] = ":bprevious", + [""] = ":bnext", + [""] = ":bdelete", + + -- 文件树 + [""] = ":NvimTreeToggle", + + -- 终端 + [""] = ":ToggleTerm", + + -- 查找 + [""] = ":Telescope find_files", + [""] = ":Telescope live_grep", + [""] = ":Telescope buffers", + [""] = ":Telescope help_tags", + + -- LSP + ["gd"] = ":lua vim.lsp.buf.definition()", + ["gr"] = ":lua vim.lsp.buf.references()", + ["K"] = ":lua vim.lsp.buf.hover()", + [""] = ":lua vim.lsp.buf.signature_help()", + ["rn"] = ":lua vim.lsp.buf.rename()", + ["ca"] = ":lua vim.lsp.buf.code_action()", + ["d"] = ":lua vim.diagnostic.open_float()", + ["f"] = ":lua vim.lsp.buf.formatting()", + + -- Git + ["gs"] = ":Git", + ["gc"] = ":Git commit", + ["gp"] = ":Git push", + ["gl"] = ":Git pull", + ["gd"] = ":Git diff", + ["gb"] = ":Git blame", + }, +} + +-- 自定义插件 +M.plugins = { + -- 主题 + { + "catppuccin/nvim", + name = "catppuccin", + priority = 1000, + config = function() + vim.cmd.colorscheme "catppuccin" + end, + }, + + -- 状态栏 + { + "nvim-lualine/lualine.nvim", + config = function() + require("custom.plugins.lualine") + end, + }, + + -- 标签栏 + { + "akinsho/bufferline.nvim", + config = function() + require("custom.plugins.bufferline") + end, + }, + + -- 文件树 + { + "nvim-tree/nvim-tree.lua", + config = function() + require("custom.plugins.nvimtree") + end, + }, + + -- 终端 + { + "akinsho/toggleterm.nvim", + config = function() + require("custom.plugins.toggleterm") + end, + }, + + -- 查找 + { + "nvim-telescope/telescope.nvim", + config = function() + require("custom.plugins.telescope") + end, + }, + + -- LSP + { + "neovim/nvim-lspconfig", + config = function() + require("custom.plugins.lspconfig") + end, + }, + + -- 补全 + { + "hrsh7th/nvim-cmp", + config = function() + require("custom.plugins.cmp") + end, + }, + + -- 语法高亮 + { + "nvim-treesitter/nvim-treesitter", + config = function() + require("custom.plugins.treesitter") + end, + }, + + -- Git + { + "lewis6991/gitsigns.nvim", + config = function() + require("custom.plugins.gitsigns") + end, + }, + { + "tpope/vim-fugitive", + }, + + -- 注释 + { + "numToStr/Comment.nvim", + config = function() + require("custom.plugins.comment") + end, + }, + + -- 自动配对 + { + "windwp/nvim-autopairs", + config = function() + require("custom.plugins.autopairs") + end, + }, + + -- 代码片段 + { + "L3MON4D3/LuaSnip", + config = function() + require("custom.plugins.luasnip") + end, + }, + + -- 格式化 + { + "jose-elias-alvarez/null-ls.nvim", + config = function() + require("custom.plugins.null-ls") + end, + }, +} + +return M +EOF + + # 创建插件配置目录 + mkdir -p ~/.config/nvim/lua/custom/plugins + + # 配置状态栏 + cat > ~/.config/nvim/lua/custom/plugins/lualine.lua << EOF +local M = {} + +function M.setup() + require("lualine").setup({ + options = { + theme = "catppuccin", + component_separators = { left = "", right = "" }, + section_separators = { left = "", right = "" }, + globalstatus = true, + }, + sections = { + lualine_a = { + { "mode", separator = { left = "" }, right_padding = 2 }, + }, + lualine_b = { + "filename", + "branch", + "diff", + "diagnostics", + }, + lualine_c = { + "filetype", + "progress", + "location", + }, + lualine_x = { + "encoding", + "fileformat", + "hostname", + }, + lualine_y = { + "filesize", + "filetype", + }, + lualine_z = { + { "datetime", style = "%H:%M" }, + }, + }, + }) +end + +return M +EOF + + # 配置标签栏 + cat > ~/.config/nvim/lua/custom/plugins/bufferline.lua << EOF +local M = {} + +function M.setup() + require("bufferline").setup({ + options = { + theme = "catppuccin", + separator_style = "block", + always_show_bufferline = true, + show_buffer_close_icons = true, + show_close_icon = true, + show_tab_indicators = true, + show_duplicate_prefix = true, + enforce_regular_tabs = true, + view = "multiwindow", + show_buffer_icons = true, + show_tab_indicators = true, + persist_buffer_sort = true, + always_show_bufferline = true, + sort_by = "extension", + }, + highlights = require("catppuccin.groups.integrations.bufferline").get(), + }) +end + +return M +EOF + + # 配置文件树 + cat > ~/.config/nvim/lua/custom/plugins/nvimtree.lua << EOF +local M = {} + +function M.setup() + require("nvim-tree").setup({ + sort_by = "case_sensitive", + view = { + width = 30, + }, + renderer = { + group_empty = true, + }, + filters = { + dotfiles = false, + }, + }) +end + +return M +EOF + + # 配置终端 + cat > ~/.config/nvim/lua/custom/plugins/toggleterm.lua << EOF +local M = {} + +function M.setup() + require("toggleterm").setup({ + size = function(term) + if term.direction == "horizontal" then + return 15 + elseif term.direction == "vertical" then + return vim.o.columns * 0.4 + end + end, + open_mapping = [[]], + hide_numbers = true, + shade_filetypes = {}, + shade_terminals = true, + shading_factor = 2, + start_in_insert = true, + insert_mappings = true, + persist_size = true, + direction = "float", + close_on_exit = true, + shell = vim.o.shell, + float_opts = { + border = "curved", + winblend = 0, + highlights = { + border = "Normal", + background = "Normal", + }, + }, + }) +end + +return M +EOF + + # 配置查找 + cat > ~/.config/nvim/lua/custom/plugins/telescope.lua << EOF +local M = {} + +function M.setup() + require("telescope").setup({ + defaults = { + prompt_prefix = "🔍 ", + selection_caret = "➜ ", + path_display = { "smart" }, + file_ignore_patterns = { + ".git/", + "node_modules/", + "venv/", + "__pycache__/", + ".pytest_cache/", + ".mypy_cache/", + }, + }, + pickers = { + find_files = { + theme = "dropdown", + previewer = false, + }, + live_grep = { + theme = "dropdown", + previewer = false, + }, + buffers = { + theme = "dropdown", + previewer = false, + }, + help_tags = { + theme = "dropdown", + previewer = false, + }, + }, + extensions = { + fzf = { + fuzzy = true, + override_generic_sorter = true, + override_file_sorter = true, + case_mode = "smart_case", + }, + }, + }) +end + +return M +EOF + + # 配置LSP + cat > ~/.config/nvim/lua/custom/plugins/lspconfig.lua << EOF +local M = {} + +function M.setup() + local lspconfig = require("lspconfig") + local capabilities = require("cmp_nvim_lsp").default_capabilities() + local on_attach = function(client, bufnr) + local opts = { noremap = true, silent = true, buffer = bufnr } + vim.keymap.set("n", "gd", vim.lsp.buf.definition, opts) + vim.keymap.set("n", "gr", vim.lsp.buf.references, opts) + vim.keymap.set("n", "K", vim.lsp.buf.hover, opts) + vim.keymap.set("n", "", vim.lsp.buf.signature_help, opts) + vim.keymap.set("n", "rn", vim.lsp.buf.rename, opts) + vim.keymap.set("n", "ca", vim.lsp.buf.code_action, opts) + vim.keymap.set("n", "d", vim.diagnostic.open_float, opts) + vim.keymap.set("n", "f", vim.lsp.buf.formatting, opts) + end + + -- 配置各种语言的LSP服务器 + local servers = { + "tsserver", + "html", + "cssls", + "jsonls", + "eslint", + "tailwindcss", + "prismals", + "graphql", + "yamlls", + "dockerls", + "bashls", + "pyright", + "gopls", + "rust_analyzer", + "lua_ls", + } + + for _, lsp in ipairs(servers) do + lspconfig[lsp].setup({ + capabilities = capabilities, + on_attach = on_attach, + }) + end +end + +return M +EOF + + # 配置补全 + cat > ~/.config/nvim/lua/custom/plugins/cmp.lua << EOF +local M = {} + +function M.setup() + local cmp = require("cmp") + local luasnip = require("luasnip") + + cmp.setup({ + snippet = { + expand = function(args) + luasnip.lsp_expand(args.body) + end, + }, + mapping = { + [""] = cmp.mapping.select_prev_item(), + [""] = cmp.mapping.select_next_item(), + [""] = cmp.mapping.scroll_docs(-4), + [""] = cmp.mapping.scroll_docs(4), + [""] = cmp.mapping.complete(), + [""] = cmp.mapping.close(), + [""] = cmp.mapping.confirm({ + behavior = cmp.ConfirmBehavior.Replace, + select = true, + }), + }, + sources = { + { name = "nvim_lsp" }, + { name = "luasnip" }, + { name = "buffer" }, + { name = "path" }, + }, + }) +end + +return M +EOF + + # 配置语法高亮 + cat > ~/.config/nvim/lua/custom/plugins/treesitter.lua << EOF +local M = {} + +function M.setup() + require("nvim-treesitter.configs").setup({ + ensure_installed = { + "lua", + "vim", + "vimdoc", + "javascript", + "typescript", + "html", + "css", + "json", + "yaml", + "toml", + "markdown", + "bash", + "python", + "go", + "rust", + }, + highlight = { + enable = true, + use_languagetree = true, + }, + indent = { + enable = true, + }, + }) +end + +return M +EOF + + # 配置Git + cat > ~/.config/nvim/lua/custom/plugins/gitsigns.lua << EOF +local M = {} + +function M.setup() + require("gitsigns").setup({ + signs = { + add = { text = "│" }, + change = { text = "│" }, + delete = { text = "_" }, + topdelete = { text = "‾" }, + changedelete = { text = "~" }, + untracked = { text = "┆" }, + }, + signcolumn = true, + numhl = true, + linehl = false, + word_diff = false, + watch_gitdir = { + interval = 1000, + follow_files = true, + }, + attach_to_untracked = true, + current_line_blame = false, + current_line_blame_opts = { + virt_text = true, + virt_text_pos = "eol", + hl_mode = "blend", + }, + current_line_blame_formatter_opts = { + relative_time = false, + }, + sign_priority = 6, + update_debounce = 100, + status_formatter = nil, + preview_config = { + border = "rounded", + style = "minimal", + relative = "cursor", + row = 0, + col = 1, + }, + yadm = { + enable = false, + }, + }) +end + +return M +EOF + + # 配置注释 + cat > ~/.config/nvim/lua/custom/plugins/comment.lua << EOF +local M = {} + +function M.setup() + require("Comment").setup({ + padding = true, + sticky = true, + ignore = nil, + toggler = { + line = "gcc", + block = "gbc", + }, + opleader = { + line = "gc", + block = "gb", + }, + extra = { + above = "gcO", + below = "gco", + eol = "gcA", + }, + mappings = { + basic = true, + extra = true, + extended = false, + }, + pre_hook = nil, + post_hook = nil, + }) +end + +return M +EOF + + # 配置自动配对 + cat > ~/.config/nvim/lua/custom/plugins/autopairs.lua << EOF +local M = {} + +function M.setup() + require("nvim-autopairs").setup({ + check_ts = true, + disable_filetype = { "TelescopePrompt", "spectre_panel" }, + fast_wrap = { + map = "", + chars = { "{", "[", "(", '"', "'" }, + pattern = string.gsub([[ [%'%"%)%>%]%)%}%,] ]]], "%s+", ""), + offset = 0, + end_key = "$", + keys = "qwertyuiopzxcvbnmasdfghjkl", + check_comma = true, + highlight = "PmenuSel", + highlight_grey = "LineNr", + }, + }) +end + +return M +EOF + + # 配置代码片段 + cat > ~/.config/nvim/lua/custom/plugins/luasnip.lua << EOF +local M = {} + +function M.setup() + require("luasnip").setup({ + region_check_events = "CursorMoved,InsertEnter", + delete_check_events = "TextChanged,InsertLeave", + }) +end + +return M +EOF + + # 配置格式化 + cat > ~/.config/nvim/lua/custom/plugins/null-ls.lua << EOF +local M = {} + +function M.setup() + local null_ls = require("null-ls") + + null_ls.setup({ + sources = { + null_ls.builtins.formatting.prettier, + null_ls.builtins.formatting.stylua, + null_ls.builtins.formatting.black, + null_ls.builtins.formatting.isort, + null_ls.builtins.formatting.gofmt, + null_ls.builtins.formatting.rustfmt, + null_ls.builtins.diagnostics.eslint_d, + null_ls.builtins.diagnostics.flake8, + null_ls.builtins.diagnostics.mypy, + null_ls.builtins.diagnostics.golangci_lint, + null_ls.builtins.diagnostics.rustc, + }, + on_attach = function(client, bufnr) + if client.supports_method("textDocument/formatting") then + vim.api.nvim_clear_autocmds({ group = augroup, buffer = bufnr }) + vim.api.nvim_create_autocmd("BufWritePre", { + group = augroup, + buffer = bufnr, + callback = function() + vim.lsp.buf.format({ bufnr = bufnr }) + end, + }) + end + end, + }) +end + +return M +EOF +} + +# 主函数 +main() { + log_info "开始配置NvChad..." + + # 备份现有配置 + backup_config + + # 安装NvChad + install_nvchad + + # 配置NvChad + configure_nvchad + + log_info "NvChad配置完成!" + log_info "请运行 nvim 来启动编辑器。" + log_info "首次启动时会自动安装插件。" +} + +# 执行主函数 +main \ No newline at end of file diff --git a/scripts/php/dnmp.sh b/scripts/php/dnmp.sh new file mode 100644 index 0000000..36f7858 --- /dev/null +++ b/scripts/php/dnmp.sh @@ -0,0 +1,155 @@ +#!/bin/bash + +# 设置错误时立即退出 +set -e + +# 颜色定义 +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +NC='\033[0m' # No Color + +# 日志函数 +log_info() { + echo -e "${GREEN}[INFO]${NC} $1" +} + +log_warn() { + echo -e "${YELLOW}[WARN]${NC} $1" +} + +log_error() { + echo -e "${RED}[ERROR]${NC} $1" +} + +# 安装Docker和Docker Compose +install_docker() { + log_info "安装Docker和Docker Compose..." + + # 安装Docker + curl -fsSL https://get.docker.com -o get-docker.sh + sudo sh get-docker.sh + rm get-docker.sh + + # 安装Docker Compose + sudo curl -L "https://github.com/docker/compose/releases/download/v2.24.5/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose + sudo chmod +x /usr/local/bin/docker-compose + + # 将当前用户添加到docker组 + sudo usermod -aG docker $USER + + log_info "Docker和Docker Compose安装完成" +} + +# 克隆DNMP仓库 +clone_dnmp() { + log_info "克隆DNMP仓库..." + + # 创建项目目录 + mkdir -p ~/projects + cd ~/projects + + # 克隆DNMP仓库 + git clone https://github.com/yeszao/dnmp.git + cd dnmp + + # 复制环境配置文件 + cp env.sample .env + + log_info "DNMP仓库克隆完成" +} + +# 配置DNMP +configure_dnmp() { + log_info "配置DNMP..." + + cd ~/projects/dnmp + + # 修改.env文件 + sed -i 's/PHP_VERSION=7.4/PHP_VERSION=8.2/' .env + sed -i 's/MYSQL_VERSION=5.7/MYSQL_VERSION=8.0/' .env + sed -i 's/REDIS_VERSION=4.0/REDIS_VERSION=7.0/' .env + + # 创建网站目录 + mkdir -p www + + # 创建默认网站 + cat > www/index.php << EOF +> ~/.zshrc << EOF +# Composer配置 +export PATH="\$HOME/.composer/vendor/bin:\$PATH" +EOF + + log_info "PHP开发环境配置完成" +} + +# 主函数 +main() { + log_info "开始安装PHP开发环境..." + + # 安装Docker和Docker Compose + install_docker + + # 克隆DNMP仓库 + clone_dnmp + + # 配置DNMP + configure_dnmp + + # 启动DNMP + start_dnmp + + # 配置PHP开发环境 + configure_php_dev + + log_info "PHP开发环境安装完成!" + log_info "请运行 'source ~/.zshrc' 或重新打开终端以使环境变量生效。" + log_info "访问 http://localhost 查看PHP信息页面。" + log_info "默认MySQL密码:123456" + log_info "默认Redis密码:123456" +} + +# 执行主函数 +main \ No newline at end of file diff --git a/scripts/shell/plugins.sh b/scripts/shell/plugins.sh new file mode 100644 index 0000000..3dc4864 --- /dev/null +++ b/scripts/shell/plugins.sh @@ -0,0 +1,136 @@ +#!/bin/bash + +# 设置错误时立即退出 +set -e + +# 颜色定义 +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +NC='\033[0m' # No Color + +# 日志函数 +log_info() { + echo -e "${GREEN}[INFO]${NC} $1" +} + +log_warn() { + echo -e "${YELLOW}[WARN]${NC} $1" +} + +log_error() { + echo -e "${RED}[ERROR]${NC} $1" +} + +# 安装zsh-autosuggestions +install_zsh_autosuggestions() { + log_info "安装zsh-autosuggestions..." + git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions +} + +# 安装zsh-syntax-highlighting +install_zsh_syntax_highlighting() { + log_info "安装zsh-syntax-highlighting..." + git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting +} + +# 安装zsh-completions +install_zsh_completions() { + log_info "安装zsh-completions..." + git clone https://github.com/zsh-users/zsh-completions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-completions +} + +# 安装autojump +install_autojump() { + log_info "安装autojump..." + apt-get install -y autojump +} + +# 安装fzf +install_fzf() { + log_info "安装fzf..." + apt-get install -y fzf +} + +# 安装zsh-nvm +install_zsh_nvm() { + log_info "安装zsh-nvm..." + git clone https://github.com/lukechilds/zsh-nvm ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-nvm +} + +# 安装pyenv +install_pyenv() { + log_info "安装pyenv..." + curl https://pyenv.run | bash +} + +# 配置fzf +configure_fzf() { + log_info "配置fzf..." + + # 安装fzf键绑定和补全 + $(brew --prefix)/opt/fzf/install --all + + # 添加fzf配置到.zshrc + cat >> ~/.zshrc << EOF + +# fzf配置 +export FZF_DEFAULT_OPTS='--height 40% --layout=reverse --border' +export FZF_DEFAULT_COMMAND='fd --type f' +export FZF_CTRL_T_COMMAND='\$FZF_DEFAULT_COMMAND' +export FZF_CTRL_T_OPTS="--preview 'bat --color=always --line-range=:500 {}'" +export FZF_CTRL_R_OPTS="--preview 'echo {}' --preview-window down:3:hidden:wrap --bind '?:toggle-preview'" +export FZF_ALT_C_OPTS="--preview 'tree -C {} | head -200'" +EOF +} + +# 配置pyenv +configure_pyenv() { + log_info "配置pyenv..." + + # 添加pyenv配置到.zshrc + cat >> ~/.zshrc << EOF + +# pyenv配置 +export PYENV_ROOT="\$HOME/.pyenv" +command -v pyenv >/dev/null || export PATH="\$PYENV_ROOT/bin:\$PATH" +eval "\$(pyenv init -)" +EOF +} + +# 配置autojump +configure_autojump() { + log_info "配置autojump..." + + # 添加autojump配置到.zshrc + cat >> ~/.zshrc << EOF + +# autojump配置 +. /usr/share/autojump/autojump.sh +EOF +} + +# 主函数 +main() { + log_info "开始安装Zsh插件..." + + # 安装插件 + install_zsh_autosuggestions + install_zsh_syntax_highlighting + install_zsh_completions + install_autojump + install_fzf + install_zsh_nvm + install_pyenv + + # 配置插件 + configure_fzf + configure_pyenv + configure_autojump + + log_info "Zsh插件安装完成!" + log_info "请重新加载.zshrc以应用更改:source ~/.zshrc" +} + +# 执行主函数 +main \ No newline at end of file diff --git a/scripts/shell/zsh.sh b/scripts/shell/zsh.sh new file mode 100644 index 0000000..bcd7dab --- /dev/null +++ b/scripts/shell/zsh.sh @@ -0,0 +1,192 @@ +#!/bin/bash + +# 设置错误时立即退出 +set -e + +# 日志函数 +log_info() { + echo "[INFO] $1" +} + +log_warn() { + echo "[WARN] $1" +} + +log_error() { + echo "[ERROR] $1" +} + +# 检查是否为root用户 +if [ "$EUID" -ne 0 ]; then + log_error "请使用root权限运行此脚本" + exit 1 +fi + +# 获取当前用户名 +CURRENT_USER=$SUDO_USER +if [ -z "$CURRENT_USER" ]; then + CURRENT_USER=$(who | awk '{print $1}' | head -n1) +fi + +# 设置用户主目录 +USER_HOME="/home/$CURRENT_USER" + +# 更新系统包 +log_info "更新系统包..." +apt-get update +apt-get upgrade -y + +# 安装必要的依赖 +log_info "安装必要的依赖..." +apt-get install -y \ + git \ + curl \ + wget \ + build-essential \ + zsh \ + sudo \ + fontconfig + +# 安装 Oh My Zsh +log_info "安装 Oh My Zsh..." +# 删除已存在的 Oh My Zsh 目录 +if [ -d "${USER_HOME}/.oh-my-zsh" ]; then + log_info "删除已存在的 Oh My Zsh 目录..." + rm -rf ${USER_HOME}/.oh-my-zsh +fi + +# 切换到用户目录 +cd ${USER_HOME} + +# 安装 Oh My Zsh +su - $CURRENT_USER -c 'sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" "" --unattended' + +# 设置 zsh 为默认 shell +log_info "设置 zsh 为默认 shell..." +chsh -s $(which zsh) $CURRENT_USER + +# 安装常用插件 +log_info "安装常用插件..." +su - $CURRENT_USER -c "git clone https://github.com/zsh-users/zsh-autosuggestions ${USER_HOME}/.oh-my-zsh/custom/plugins/zsh-autosuggestions" +su - $CURRENT_USER -c "git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${USER_HOME}/.oh-my-zsh/custom/plugins/zsh-syntax-highlighting" + +# 配置 .zshrc +log_info "配置 .zshrc..." +# 备份现有的 .zshrc 文件 +if [ -f "${USER_HOME}/.zshrc" ]; then + log_info "备份现有的 .zshrc 文件..." + mv ${USER_HOME}/.zshrc ${USER_HOME}/.zshrc.backup +fi + +cat > ${USER_HOME}/.zshrc << 'EOL' +# Enable Powerlevel10k instant prompt +if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then + source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" +fi + +# Path to oh-my-zsh installation +export ZSH="$HOME/.oh-my-zsh" + +# Theme +ZSH_THEME="powerlevel10k/powerlevel10k" + +# Plugins +plugins=( + git + docker + docker-compose + npm + node + python + pip + zsh-autosuggestions + zsh-syntax-highlighting +) + +# Source oh-my-zsh +source $ZSH/oh-my-zsh.sh + +# User configuration +export LANG=en_US.UTF-8 +export EDITOR='vim' +export VISUAL='vim' + +# 设置自动补全快捷键为逗号 +bindkey ',' autosuggest-accept + +# 历史记录配置 +HISTSIZE=10000 +SAVEHIST=10000 +HISTFILE=~/.zsh_history +setopt SHARE_HISTORY +setopt HIST_IGNORE_ALL_DUPS +setopt HIST_REDUCE_BLANKS + +# 目录栈配置 +setopt AUTO_PUSHD +setopt PUSHD_IGNORE_DUPS +setopt PUSHD_SILENT + +# 自动补全配置 +autoload -Uz compinit && compinit +zstyle ':completion:*' menu select +zstyle ':completion:*' matcher-list 'm:{a-zA-Z}={A-Za-z}' + +# Aliases +alias ll='ls -la' +alias ..='cd ..' +alias ...='cd ../..' +alias ....='cd ../../..' +alias grep='grep --color=auto' +alias mkdir='mkdir -pv' +alias wget='wget -c' +alias hist='history | grep' +alias ports='netstat -tulanp' +alias path='echo -e ${PATH//:/\\n}' +alias now='date +"%T"' +alias nowdate='date +"%d-%m-%Y"' +alias update='sudo apt-get update' +alias upgrade='sudo apt-get upgrade' +alias meminfo='free -m -l -t' +alias psmem='ps auxf | sort -nr -k 4' +alias pscpu='ps auxf | sort -nr -k 3' +alias wget='wget -c' +alias getpass="openssl rand -base64 20" +alias sha1='openssl sha1' +alias ping='ping -c 5' +alias mkdir='mkdir -pv' +alias diff='colordiff' +alias mount='mount |column -t' +alias h='history' +alias j='jobs -l' +alias vi=vim +alias svi='sudo vim' +alias vis='vim "+set si"' +alias edit='vim' + +# To customize prompt, run `p10k configure` or edit ~/.p10k.zsh. +[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh +EOL + +# 安装 Powerlevel10k 主题 +log_info "安装 Powerlevel10k 主题..." +su - $CURRENT_USER -c "git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ${USER_HOME}/.oh-my-zsh/custom/themes/powerlevel10k" + +# 安装字体 +log_info "安装字体..." +su - $CURRENT_USER -c "mkdir -p ${USER_HOME}/.local/share/fonts" +su - $CURRENT_USER -c "cd ${USER_HOME}/.local/share/fonts && curl -LO https://github.com/romkatv/powerlevel10k-media/raw/master/MesloLGS%20NF%20Regular.ttf" +su - $CURRENT_USER -c "cd ${USER_HOME}/.local/share/fonts && curl -LO https://github.com/romkatv/powerlevel10k-media/raw/master/MesloLGS%20NF%20Bold.ttf" +su - $CURRENT_USER -c "cd ${USER_HOME}/.local/share/fonts && curl -LO https://github.com/romkatv/powerlevel10k-media/raw/master/MesloLGS%20NF%20Italic.ttf" +su - $CURRENT_USER -c "cd ${USER_HOME}/.local/share/fonts && curl -LO https://github.com/romkatv/powerlevel10k-media/raw/master/MesloLGS%20NF%20Bold%20Italic.ttf" +su - $CURRENT_USER -c "fc-cache -f -v" + +# 设置权限 +log_info "设置权限..." +chown -R $CURRENT_USER:$CURRENT_USER ${USER_HOME}/.oh-my-zsh +chown -R $CURRENT_USER:$CURRENT_USER ${USER_HOME}/.zshrc +chown -R $CURRENT_USER:$CURRENT_USER ${USER_HOME}/.local + +log_info "Zsh 环境配置完成!" +log_info "请重新打开终端或运行 'source ~/.zshrc' 使配置生效" +log_info "首次登录后,运行 p10k configure 来配置 Powerlevel10k 主题" \ No newline at end of file diff --git a/scripts/system/base.sh b/scripts/system/base.sh new file mode 100644 index 0000000..86f1157 --- /dev/null +++ b/scripts/system/base.sh @@ -0,0 +1,172 @@ +#!/bin/bash + +# 设置错误时立即退出 +set -e + +# 颜色定义 +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +NC='\033[0m' # No Color + +# 日志函数 +log_info() { + echo -e "${GREEN}[INFO]${NC} $1" +} + +log_warn() { + echo -e "${YELLOW}[WARN]${NC} $1" +} + +log_error() { + echo -e "${RED}[ERROR]${NC} $1" +} + +# 更新系统 +update_system() { + log_info "更新系统..." + apt-get update + apt-get upgrade -y + apt-get dist-upgrade -y +} + +# 安装基础工具 +install_basic_tools() { + log_info "安装基础工具..." + + # 安装常用工具 + apt-get install -y \ + curl \ + wget \ + git \ + vim \ + htop \ + tmux \ + ripgrep \ + fd-find \ + bat \ + exa \ + jq \ + httpie \ + tldr \ + glances \ + neofetch \ + zsh \ + build-essential \ + software-properties-common \ + ca-certificates \ + gnupg \ + lsb-release \ + apt-transport-https \ + python3-pip \ + python3-venv \ + locales \ + tzdata +} + +# 配置系统语言和时区 +configure_locale() { + log_info "配置系统语言和时区..." + + # 生成UTF-8语言环境 + locale-gen en_US.UTF-8 + update-locale LANG=en_US.UTF-8 + + # 设置时区为Asia/Shanghai + ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime + echo "Asia/Shanghai" > /etc/timezone +} + +# 配置系统参数 +configure_system() { + log_info "配置系统参数..." + + # 创建/etc/sysctl.d/99-sysctl.conf文件 + cat > /etc/sysctl.d/99-sysctl.conf << EOF +# 系统最大文件描述符 +fs.file-max = 65535 + +# 系统最大进程数 +kernel.pid_max = 65535 + +# 系统最大打开文件数 +fs.nr_open = 65535 + +# 系统最大虚拟内存 +vm.max_map_count = 262144 + +# 系统最大共享内存 +kernel.shm.max = 68719476736 + +# 系统最大消息队列 +kernel.msgmnb = 65535 + +# 系统最大信号量 +kernel.sem = 250 32000 32 128 + +# TCP连接相关参数 +net.ipv4.tcp_fin_timeout = 30 +net.ipv4.tcp_keepalive_time = 1200 +net.ipv4.tcp_max_syn_backlog = 8192 +net.ipv4.tcp_tw_reuse = 1 +net.ipv4.tcp_tw_recycle = 0 +net.ipv4.tcp_max_tw_buckets = 5000 +net.ipv4.tcp_fastopen = 1 +net.ipv4.tcp_rmem = 4096 87380 67108864 +net.ipv4.tcp_wmem = 4096 65536 67108864 +net.ipv4.tcp_mtu_probing = 1 + +# 系统最大连接数 +net.core.somaxconn = 32768 +net.core.netdev_max_backlog = 32768 +net.ipv4.ip_local_port_range = 1024 65535 +EOF + + # 应用系统参数 + sysctl -p /etc/sysctl.d/99-sysctl.conf +} + +# 配置系统安全 +configure_security() { + log_info "配置系统安全..." + + # 配置SSH安全 + if [ -f /etc/ssh/sshd_config ]; then + sed -i 's/#PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config + sed -i 's/#PasswordAuthentication yes/PasswordAuthentication no/' /etc/ssh/sshd_config + systemctl restart sshd + fi + + # 配置防火墙 + if command -v ufw &> /dev/null; then + ufw default deny incoming + ufw default allow outgoing + ufw allow ssh + ufw --force enable + fi +} + +# 主函数 +main() { + log_info "开始配置基础系统..." + + # 更新系统 + update_system + + # 安装基础工具 + install_basic_tools + + # 配置系统语言和时区 + configure_locale + + # 配置系统参数 + configure_system + + # 配置系统安全 + configure_security + + log_info "基础系统配置完成!" +} + +# 执行主函数 +main \ No newline at end of file diff --git a/scripts/system/check_windows_wsl.bat b/scripts/system/check_windows_wsl.bat new file mode 100644 index 0000000..39f6e5f --- /dev/null +++ b/scripts/system/check_windows_wsl.bat @@ -0,0 +1,134 @@ +@echo off +chcp 65001 >nul +setlocal enabledelayedexpansion + +:: Check for admin privileges +net session >nul 2>&1 +if %errorlevel% neq 0 ( + echo [WARN] 需要管理员权限 + echo [INFO] 请右键点击此脚本,选择"以管理员身份运行" + pause + exit /b 1 +) + +echo. +echo [INFO] 开始检查 WSL2 环境... +echo. + +:: Check Windows version +echo [INFO] 检查 Windows 版本... +for /f "tokens=*" %%a in ('ver') do set "windows_version=%%a" +echo Windows 版本: !windows_version! + +:: Check WSL installation +echo [INFO] 检查 WSL 安装状态... +wsl --version >nul 2>&1 +if %errorlevel% equ 0 ( + echo [INFO] WSL 已安装 + wsl --version +) else ( + echo [ERROR] WSL 未安装 + echo [INFO] 请访问 https://aka.ms/wsl 安装 WSL + goto :error +) + +:: Check WSL2 status +echo [INFO] 检查 WSL2 状态... +wsl -l -v > temp.txt + +findstr /i "2" temp.txt >nul +if %errorlevel% equ 0 ( + echo [INFO] WSL2 已启用 + echo [INFO] WSL 发行版列表: + type temp.txt +) else ( + echo [WARN] WSL2 未启用 + echo [INFO] 请运行以下命令启用 WSL2: + echo wsl --set-default-version 2 + goto :error +) +del temp.txt + +:: Check Debian installation +echo [INFO] 检查 Debian 安装状态... +wsl -d Debian echo "Debian is installed" >nul 2>&1 +if %errorlevel% equ 0 ( + echo [INFO] Debian 已安装 +) else ( + echo [WARN] Debian 未安装 + echo [INFO] 请运行以下命令安装 Debian: + echo wsl --install -d Debian + goto :error +) + +:: Check virtualization +echo [INFO] 检查虚拟化状态... +systeminfo | findstr /i "Hyper-V" > temp.txt +findstr /i "Virtual Ethernet Adapter" temp.txt >nul +if %errorlevel% equ 0 ( + echo [INFO] 虚拟化已启用 +) else ( + findstr /i "hypervisor has been detected" temp.txt >nul + if %errorlevel% equ 0 ( + echo [INFO] 虚拟化已启用 + ) else ( + echo [ERROR] 虚拟化未启用 + echo [INFO] 请在 BIOS 中启用虚拟化 (Intel VT-x 或 AMD-V) + goto :error + ) +) +del temp.txt + +:: Check Windows features +echo [INFO] 检查 Windows 功能... +dism /online /get-featureinfo /featurename:Microsoft-Windows-Subsystem-Linux > temp.txt +findstr /i "状态.*已启用" temp.txt >nul +if %errorlevel% equ 0 ( + echo [INFO] Linux 子系统已启用 +) else ( + echo [WARN] Linux 子系统未启用 + echo [INFO] 请运行以下命令启用 Linux 子系统: + echo dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart + goto :error +) + +dism /online /get-featureinfo /featurename:VirtualMachinePlatform > temp.txt +findstr /i "状态.*已启用" temp.txt >nul +if %errorlevel% equ 0 ( + echo [INFO] 虚拟机平台已启用 +) else ( + echo [WARN] 虚拟机平台未启用 + echo [INFO] 请运行以下命令启用虚拟机平台: + echo dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart + goto :error +) +del temp.txt + +echo. +echo ============================================ +echo 检查完成!环境已就绪 +echo ============================================ +echo. +echo [INFO] 您的 WSL2 环境已经准备就绪: +echo [INFO] 1. Windows 版本符合要求 +echo [INFO] 2. WSL2 已正确安装并启用 +echo [INFO] 3. Debian 发行版已安装 +echo [INFO] 4. 虚拟化功能已启用 +echo [INFO] 5. 所需的 Windows 功能已启用 +echo. +echo [INFO] 您现在可以开始使用 WSL2 环境了! +echo ============================================ +echo. +echo [INFO] 按任意键退出... +pause >nul +goto :eof + +:error +echo. +echo [WARN] 检查未通过,请按照提示解决问题 +echo [INFO] 按任意键退出... +pause >nul +exit /b 1 + +:eof +endlocal \ No newline at end of file diff --git a/scripts/system/check_windows_wsl.ps1 b/scripts/system/check_windows_wsl.ps1 new file mode 100644 index 0000000..7dd45f9 --- /dev/null +++ b/scripts/system/check_windows_wsl.ps1 @@ -0,0 +1,192 @@ +# WSL2 环境检查脚本 +# 此脚本用于检查 Windows 系统上的 WSL2 安装状态 + +# 设置错误操作 +$ErrorActionPreference = "Stop" + +# 颜色定义 +$Red = [System.ConsoleColor]::Red +$Green = [System.ConsoleColor]::Green +$Yellow = [System.ConsoleColor]::Yellow + +# 日志函数 +function Write-Info { + param([string]$Message) + Write-Host "[INFO] $Message" -ForegroundColor $Green +} + +function Write-Warn { + param([string]$Message) + Write-Host "[WARN] $Message" -ForegroundColor $Yellow +} + +function Write-Error { + param([string]$Message) + Write-Host "[ERROR] $Message" -ForegroundColor $Red +} + +# 检查 Windows 版本 +function Test-WindowsVersion { + Write-Info "检查 Windows 版本..." + + $os = Get-WmiObject -Class Win32_OperatingSystem + $version = [System.Environment]::OSVersion.Version + + Write-Info "Windows 版本: $($os.Caption)" + Write-Info "系统版本号: $($version.Major).$($version.Minor).$($version.Build)" + + if ($version.Major -lt 10 -or ($version.Major -eq 10 -and $version.Build -lt 19041)) { + Write-Error "Windows 版本不满足 WSL2 要求,需要 Windows 10 版本 2004 或更高版本" + return $false + } + + return $true +} + +# 检查 WSL 是否安装 +function Test-WSLInstallation { + Write-Info "检查 WSL 安装状态..." + + try { + $wslVersion = wsl --version 2>$null + if ($LASTEXITCODE -eq 0) { + Write-Info "WSL 已安装" + Write-Info "WSL 版本信息:" + Write-Host $wslVersion + return $true + } + } + catch { + Write-Error "WSL 未安装" + Write-Info "请访问 https://aka.ms/wsl 安装 WSL" + return $false + } +} + +# 检查 WSL2 是否启用 +function Test-WSL2Enabled { + Write-Info "检查 WSL2 状态..." + + try { + $wslList = wsl -l -v + if ($wslList -match "VERSION.*2") { + Write-Info "WSL2 已启用" + Write-Info "WSL 发行版列表:" + Write-Host $wslList + return $true + } + else { + Write-Warn "WSL2 未启用" + Write-Info "请运行以下命令启用 WSL2:" + Write-Host "wsl --set-default-version 2" + return $false + } + } + catch { + Write-Error "获取 WSL 版本信息失败" + return $false + } +} + +# 检查虚拟化是否启用 +function Test-VirtualizationEnabled { + Write-Info "检查虚拟化状态..." + + try { + $vmx = Get-WmiObject -Class Win32_Processor | Select-Object -ExpandProperty NumberOfLogicalProcessors + if ($vmx -gt 0) { + Write-Info "虚拟化已启用" + return $true + } + else { + Write-Error "虚拟化未启用" + Write-Info "请在 BIOS 中启用虚拟化技术(Intel VT-x 或 AMD-V)" + return $false + } + } + catch { + Write-Error "检查虚拟化状态失败" + return $false + } +} + +# 检查 Debian 是否安装 +function Test-DebianInstallation { + Write-Info "检查 Debian 安装状态..." + + try { + $wslList = wsl -l + if ($wslList -match "Debian") { + Write-Info "Debian 已安装" + return $true + } + else { + Write-Warn "Debian 未安装" + Write-Info "请运行以下命令安装 Debian:" + Write-Host "wsl --install -d Debian" + return $false + } + } + catch { + Write-Error "检查 Debian 安装状态失败" + return $false + } +} + +# 检查 Windows 功能 +function Test-WindowsFeatures { + Write-Info "检查 Windows 功能..." + + $features = @( + "Microsoft-Windows-Subsystem-Linux", + "VirtualMachinePlatform" + ) + + $allEnabled = $true + foreach ($feature in $features) { + $status = Get-WindowsOptionalFeature -Online -FeatureName $feature + if ($status.State -eq "Enabled") { + Write-Info "$feature 已启用" + } + else { + Write-Warn "$feature 未启用" + Write-Info "请运行以下命令启用 $feature:" + Write-Host "dism.exe /online /enable-feature /featurename:$feature /all /norestart" + $allEnabled = $false + } + } + + return $allEnabled +} + +# 主函数 +function Main { + Write-Info "开始检查 WSL2 环境..." + + $checks = @{ + "Windows 版本检查" = { Test-WindowsVersion } + "WSL 安装检查" = { Test-WSLInstallation } + "WSL2 状态检查" = { Test-WSL2Enabled } + "虚拟化检查" = { Test-VirtualizationEnabled } + "Debian 安装检查" = { Test-DebianInstallation } + "Windows 功能检查" = { Test-WindowsFeatures } + } + + $allPassed = $true + foreach ($check in $checks.GetEnumerator()) { + Write-Host "`n=== $($check.Key) ===" + if (-not (& $check.Value)) { + $allPassed = $false + } + } + + if ($allPassed) { + Write-Info "`n所有检查项通过!WSL2 环境已准备就绪。" + } + else { + Write-Warn "`n部分检查项未通过,请按照提示进行修复。" + } +} + +# 执行主函数 +Main \ No newline at end of file diff --git a/scripts/system/check_wsl.sh b/scripts/system/check_wsl.sh new file mode 100644 index 0000000..b1930d3 --- /dev/null +++ b/scripts/system/check_wsl.sh @@ -0,0 +1,243 @@ +#!/bin/bash + +# 设置错误时立即退出 +set -e + +# 颜色定义 +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +NC='\033[0m' # No Color + +# 日志函数 +log_info() { + echo -e "${GREEN}[INFO]${NC} $1" +} + +log_warn() { + echo -e "${YELLOW}[WARN]${NC} $1" +} + +log_error() { + echo -e "${RED}[ERROR]${NC} $1" +} + +# 检查Windows版本 +check_windows_version() { + log_info "检查Windows版本..." + + # 获取Windows版本 + windows_version=$(wsl --version 2>/dev/null || echo "WSL未安装") + + if [[ $windows_version == "WSL未安装" ]]; then + log_error "未检测到WSL,请先安装WSL" + return 1 + fi + + # 检查Windows版本号 + if [[ $(uname -r) =~ Microsoft ]]; then + log_info "当前运行在WSL环境中" + else + log_error "当前不在WSL环境中" + return 1 + fi + + log_info "Windows版本信息:" + echo "$windows_version" +} + +# 检查WSL版本 +check_wsl_version() { + log_info "检查WSL版本..." + + # 获取WSL版本 + wsl_version=$(wsl -l -v) + + # 检查是否使用WSL2 + if echo "$wsl_version" | grep -q "VERSION.*2"; then + log_info "WSL2已安装并启用" + else + log_error "未检测到WSL2,请升级到WSL2" + return 1 + fi + + log_info "WSL版本信息:" + echo "$wsl_version" +} + +# 检查系统要求 +check_system_requirements() { + log_info "检查系统要求..." + + # 检查内存 + total_mem=$(free -h | grep Mem | awk '{print $2}') + log_info "系统内存:$total_mem" + + # 检查磁盘空间 + disk_space=$(df -h / | tail -1 | awk '{print $4}') + log_info "可用磁盘空间:$disk_space" + + # 检查CPU + cpu_info=$(lscpu | grep "Model name" | cut -f 2 -d ":" | sed 's/^[ \t]*//') + log_info "CPU信息:$cpu_info" + + # 检查虚拟化支持 + if grep -q "vmx\|svm" /proc/cpuinfo; then + log_info "CPU支持虚拟化" + else + log_warn "CPU可能不支持虚拟化" + fi +} + +# 检查Debian版本 +check_debian_version() { + log_info "检查Debian版本..." + + # 获取Debian版本信息 + debian_version=$(cat /etc/os-release | grep VERSION_CODENAME | cut -d= -f2) + debian_id=$(cat /etc/os-release | grep ^ID= | cut -d= -f2) + + if [[ $debian_id == "debian" ]]; then + log_info "当前系统:Debian $debian_version" + else + log_error "当前系统不是Debian" + return 1 + fi +} + +# 检查系统更新 +check_system_updates() { + log_info "检查系统更新..." + + # 更新包列表 + sudo apt-get update + + # 检查可更新的包 + updates=$(apt list --upgradable 2>/dev/null | grep -v "Listing...") + + if [[ -n $updates ]]; then + log_warn "发现可更新的包:" + echo "$updates" + else + log_info "系统已是最新" + fi +} + +# 检查必要的系统组件 +check_system_components() { + log_info "检查系统组件..." + + # 检查必要的包 + required_packages=( + "curl" + "wget" + "git" + "vim" + "build-essential" + "zsh" + "python3" + "python3-pip" + "nodejs" + "npm" + "docker.io" + "docker-compose" + ) + + missing_packages=() + + for package in "${required_packages[@]}"; do + if ! dpkg -l | grep -q "^ii $package "; then + missing_packages+=("$package") + fi + done + + if [[ ${#missing_packages[@]} -gt 0 ]]; then + log_warn "以下包未安装:" + printf '%s\n' "${missing_packages[@]}" + else + log_info "所有必要的包已安装" + fi +} + +# 检查网络连接 +check_network() { + log_info "检查网络连接..." + + # 检查网络连接 + if ping -c 1 8.8.8.8 &> /dev/null; then + log_info "网络连接正常" + else + log_error "网络连接异常" + return 1 + fi + + # 检查DNS解析 + if ping -c 1 google.com &> /dev/null; then + log_info "DNS解析正常" + else + log_error "DNS解析异常" + return 1 + fi +} + +# 检查WSL配置 +check_wsl_config() { + log_info "检查WSL配置..." + + # 检查WSL配置文件 + if [ -f /etc/wsl.conf ]; then + log_info "WSL配置文件存在" + cat /etc/wsl.conf + else + log_warn "WSL配置文件不存在" + fi + + # 检查内存限制 + if grep -q "memory" /etc/wsl.conf 2>/dev/null; then + log_info "WSL内存限制已配置" + else + log_warn "WSL内存限制未配置" + fi + + # 检查处理器限制 + if grep -q "processors" /etc/wsl.conf 2>/dev/null; then + log_info "WSL处理器限制已配置" + else + log_warn "WSL处理器限制未配置" + fi +} + +# 主函数 +main() { + log_info "开始检查WSL2环境..." + + # 检查Windows版本 + check_windows_version + + # 检查WSL版本 + check_wsl_version + + # 检查系统要求 + check_system_requirements + + # 检查Debian版本 + check_debian_version + + # 检查系统更新 + check_system_updates + + # 检查必要的系统组件 + check_system_components + + # 检查网络连接 + check_network + + # 检查WSL配置 + check_wsl_config + + log_info "WSL2环境检查完成!" + log_info "如果发现任何问题,请按照提示进行修复。" +} + +# 执行主函数 +main \ No newline at end of file diff --git a/scripts/system/mirror.sh b/scripts/system/mirror.sh new file mode 100644 index 0000000..7e90a3f --- /dev/null +++ b/scripts/system/mirror.sh @@ -0,0 +1,152 @@ +#!/bin/bash + +# 设置错误时立即退出 +set -e + +# 颜色定义 +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +NC='\033[0m' # No Color + +# 日志函数 +log_info() { + echo -e "${GREEN}[INFO]${NC} $1" +} + +log_warn() { + echo -e "${YELLOW}[WARN]${NC} $1" +} + +log_error() { + echo -e "${RED}[ERROR]${NC} $1" +} + +# 备份源文件 +backup_sources() { + log_info "备份源文件..." + cp /etc/apt/sources.list /etc/apt/sources.list.backup +} + +# 配置阿里云镜像源 +configure_aliyun_mirror() { + log_info "配置阿里云镜像源..." + + # 创建sources.list文件 + cat > /etc/apt/sources.list << EOF +# 阿里云镜像源 +deb http://mirrors.aliyun.com/debian/ bullseye main non-free contrib +deb http://mirrors.aliyun.com/debian-security/ bullseye-security main +deb http://mirrors.aliyun.com/debian/ bullseye-updates main non-free contrib +deb http://mirrors.aliyun.com/debian/ bullseye-backports main non-free contrib + +# 源码镜像 +deb-src http://mirrors.aliyun.com/debian/ bullseye main non-free contrib +deb-src http://mirrors.aliyun.com/debian-security/ bullseye-security main +deb-src http://mirrors.aliyun.com/debian/ bullseye-updates main non-free contrib +deb-src http://mirrors.aliyun.com/debian/ bullseye-backports main non-free contrib +EOF +} + +# 配置pip镜像源 +configure_pip_mirror() { + log_info "配置pip镜像源..." + + # 创建pip配置目录 + mkdir -p ~/.pip + + # 创建pip配置文件 + cat > ~/.pip/pip.conf << EOF +[global] +index-url = https://mirrors.aliyun.com/pypi/simple/ +trusted-host = mirrors.aliyun.com +EOF +} + +# 配置npm镜像源 +configure_npm_mirror() { + log_info "配置npm镜像源..." + + # 配置npm镜像 + npm config set registry https://registry.npmmirror.com + npm config set disturl https://npmmirror.com/dist + npm config set electron_mirror https://npmmirror.com/mirrors/electron/ + npm config set puppeteer_download_host https://npmmirror.com/mirrors + npm config set chromedriver_cdnurl https://npmmirror.com/mirrors/chromedriver + npm config set operadriver_cdnurl https://npmmirror.com/mirrors/operadriver + npm config set phantomjs_cdnurl https://npmmirror.com/mirrors/phantomjs + npm config set selenium_cdnurl https://npmmirror.com/mirrors/selenium + npm config set node_inspector_cdnurl https://npmmirror.com/mirrors/node-inspector +} + +# 配置go镜像源 +configure_go_mirror() { + log_info "配置go镜像源..." + + # 配置GOPROXY + go env -w GOPROXY=https://mirrors.aliyun.com/goproxy/,direct + + # 配置GOSUMDB + go env -w GOSUMDB=sum.golang.google.cn + + # 配置GO111MODULE + go env -w GO111MODULE=on +} + +# 配置docker镜像源 +configure_docker_mirror() { + log_info "配置docker镜像源..." + + # 创建docker配置目录 + mkdir -p /etc/docker + + # 创建daemon.json文件 + cat > /etc/docker/daemon.json << EOF +{ + "registry-mirrors": [ + "https://mirror.ccs.tencentyun.com", + "https://registry.docker-cn.com", + "https://docker.mirrors.ustc.edu.cn", + "https://hub-mirror.c.163.com", + "https://mirror.baidubce.com" + ] +} +EOF +} + +# 更新软件包列表 +update_package_list() { + log_info "更新软件包列表..." + apt-get update +} + +# 主函数 +main() { + log_info "开始配置镜像源..." + + # 备份源文件 + backup_sources + + # 配置阿里云镜像源 + configure_aliyun_mirror + + # 配置pip镜像源 + configure_pip_mirror + + # 配置npm镜像源 + configure_npm_mirror + + # 配置go镜像源 + configure_go_mirror + + # 配置docker镜像源 + configure_docker_mirror + + # 更新软件包列表 + update_package_list + + log_info "镜像源配置完成!" +} + +# 执行主函数 +main \ No newline at end of file diff --git a/scripts/system/temp.txt b/scripts/system/temp.txt new file mode 100644 index 0000000000000000000000000000000000000000..a56a873773f4bcd5200681232813bac88c799842 GIT binary patch literal 136 zcmY#jP+;(5aAfdha0RjuFqk0(C>nw$ijWTj$^nul +setlocal enabledelayedexpansion + +:: Check for admin privileges +net session >nul 2>&1 +if %errorlevel% neq 0 ( + echo "[WARN] 需要管理员权限" + echo "[INFO] 请右键点击此脚本,选择"以管理员身份运行"" + pause + exit /b 1 +) + +echo. +echo "[INFO] 开始升级 WSL2 并重新安装 Debian..." +echo. + +:: Check WSL installation +echo "[INFO] 检查 WSL 安装状态..." +wsl --version >nul 2>&1 +if %errorlevel% neq 0 ( + echo "[ERROR] WSL 未安装" + echo "[INFO] 请访问 https://aka.ms/wsl 安装 WSL" + goto :error +) + +:: Upgrade WSL +echo "[INFO] 正在升级 WSL..." +wsl --update +if %errorlevel% neq 0 ( + echo "[ERROR] WSL 升级失败" + goto :error +) +echo "[INFO] WSL 升级完成" + +:: Set WSL2 as default +echo "[INFO] 设置 WSL2 为默认版本..." +wsl --set-default-version 2 +if %errorlevel% neq 0 ( + echo "[ERROR] 设置 WSL2 为默认版本失败" + goto :error +) +echo "[INFO] WSL2 已设置为默认版本" + +:: Check if Debian is installed +echo "[INFO] 检查 Debian 安装状态..." +wsl -d Debian echo "Debian is installed" >nul 2>&1 +if %errorlevel% equ 0 ( + echo "[INFO] 检测到已安装的 Debian" + echo. + echo "[WARN] 是否要卸载现有的 Debian?" + echo "[INFO] 1. 是" + echo "[INFO] 2. 否" + echo. + set /p "choice=请选择 (1/2): " + if "!choice!"=="1" ( + echo "[INFO] 正在卸载 Debian..." + wsl --unregister Debian + if %errorlevel% neq 0 ( + echo "[ERROR] Debian 卸载失败" + goto :error + ) + echo "[INFO] Debian 已卸载" + ) else ( + echo "[INFO] 已取消卸载 Debian" + goto :error + ) +) + +:: Choose installation location +echo. +echo "[INFO] 请选择 Debian 安装位置:" +echo "[INFO] 1. 默认位置" +echo "[INFO] 2. 自定义位置" +echo. +set /p "location=请选择 (1/2): " + +if "!location!"=="2" ( + echo "[INFO] 请输入安装路径(例如:D:\WSL\Debian)" + set /p "install_path=安装路径: " + if not exist "!install_path!" mkdir "!install_path!" + echo "[INFO] 正在安装 Debian 到 !install_path! ..." + wsl --install -d Debian --location "!install_path!" +) else ( + echo "[INFO] 正在安装 Debian 到默认位置..." + wsl --install -d Debian +) + +if %errorlevel% neq 0 ( + echo "[ERROR] Debian 安装失败" + goto :error +) +echo "[INFO] Debian 安装完成" + +:: Verify installation +echo "[INFO] 验证安装..." +wsl -d Debian echo "Debian is installed" >nul 2>&1 +if %errorlevel% equ 0 ( + echo "[INFO] Debian 安装验证成功" + echo "[INFO] 请打开新的终端窗口,完成 Debian 的初始设置" +) else ( + echo "[ERROR] Debian 安装验证失败" + goto :error +) + +echo. +echo "============================================" +echo " 升级和安装完成!" +echo "============================================" +echo. +echo "[INFO] 请按照以下步骤完成设置:" +echo "[INFO] 1. 打开新的终端窗口" +echo "[INFO] 2. 等待 Debian 首次启动" +echo "[INFO] 3. 设置用户名和密码" +echo "[INFO] 4. 运行 check_windows_wsl.bat 验证环境" +echo. +echo "[INFO] 按任意键退出..." +pause >nul +goto :eof + +:error +echo. +echo "[WARN] 操作失败,请检查错误信息" +echo "[INFO] 按任意键退出..." +pause >nul +exit /b 1 + +:eof +endlocal \ No newline at end of file diff --git a/scripts/utils/git.sh b/scripts/utils/git.sh new file mode 100644 index 0000000..a5a1188 --- /dev/null +++ b/scripts/utils/git.sh @@ -0,0 +1,156 @@ +#!/bin/bash + +# 设置错误时立即退出 +set -e + +# 颜色定义 +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +NC='\033[0m' # No Color + +# 日志函数 +log_info() { + echo -e "${GREEN}[INFO]${NC} $1" +} + +log_warn() { + echo -e "${YELLOW}[WARN]${NC} $1" +} + +log_error() { + echo -e "${RED}[ERROR]${NC} $1" +} + +# 安装Git +install_git() { + log_info "安装Git..." + + # 安装Git + sudo apt-get update + sudo apt-get install -y git + + # 安装Git工具 + sudo apt-get install -y git-extras + sudo apt-get install -y git-flow + sudo apt-get install -y git-lfs + + log_info "Git安装完成" +} + +# 配置Git +configure_git() { + log_info "配置Git..." + + # 配置Git全局设置 + git config --global core.autocrlf input + git config --global core.fileMode false + git config --global core.ignorecase false + git config --global core.quotepath false + git config --global core.safecrlf warn + git config --global core.editor "vim" + + # 配置Git颜色 + git config --global color.ui true + git config --global color.branch.current "yellow reverse" + git config --global color.branch.local "yellow" + git config --global color.branch.remote "green" + git config --global color.diff.meta "yellow bold" + git config --global color.diff.frag "magenta bold" + git config --global color.diff.old "red" + git config --global color.diff.new "green" + git config --global color.status.added "green" + git config --global color.status.changed "yellow" + git config --global color.status.untracked "cyan" + git config --global color.status.deleted "red" + + # 配置Git别名 + git config --global alias.st "status" + git config --global alias.co "checkout" + git config --global alias.br "branch" + git config --global alias.ci "commit" + git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit" + git config --global alias.lga "log --color --graph --all --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit" + git config --global alias.df "diff" + git config --global alias.dc "diff --cached" + git config --global alias.unstage "reset HEAD --" + git config --global alias.last "log -1 HEAD" + git config --global alias.visual "!gitk" + + # 配置Git提交模板 + cat > ~/.gitmessage << EOF +# (): +# +# +# +#