#!/bin/bash # 代码注释清理快捷脚本 set -e SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PYTHON_SCRIPT="$SCRIPT_DIR/remove_comments.py" # 颜色定义 GREEN='\033[0;32m' YELLOW='\033[1;33m' RED='\033[0;31m' NC='\033[0m' # No Color # 打印带颜色的消息 print_info() { echo -e "${GREEN}[INFO]${NC} $1" } print_warning() { echo -e "${YELLOW}[WARNING]${NC} $1" } print_error() { echo -e "${RED}[ERROR]${NC} $1" } # 显示使用说明 show_usage() { cat << EOF 代码注释清理工具 - 快捷脚本 用法: $0 [选项] <路径> 选项: preview 预览模式(不实际修改) clean 清理注释(保留版权和重要注释) clean-all 清理所有注释(包括版权) test 在测试文件上运行 示例: $0 preview YuMi/Global/ # 预览Global目录 $0 clean YuMi/Global/ # 清理Global目录 $0 clean-all YuMi/Global/YUMIConstant.m # 清理单个文件的所有注释 $0 test # 测试模式 EOF } # 预览模式 preview_mode() { local path=$1 print_info "预览模式:查看将要删除的注释" python3 "$PYTHON_SCRIPT" "$path" -r --dry-run } # 清理模式(保留重要注释) clean_mode() { local path=$1 print_warning "即将清理注释,但会保留:" echo " ✓ 版权声明 (Copyright, Created by)" echo " ✓ 重要注释 (TODO, FIXME, NOTE)" echo " ✓ 编译指令 (#pragma mark)" echo "" read -p "是否继续? (y/N): " -n 1 -r echo if [[ $REPLY =~ ^[Yy]$ ]]; then python3 "$PYTHON_SCRIPT" "$path" -r else print_info "已取消" fi } # 清理所有注释 clean_all_mode() { local path=$1 print_warning "⚠️ 警告:将删除所有注释(包括版权声明)" echo "" read -p "是否确认? (y/N): " -n 1 -r echo if [[ $REPLY =~ ^[Yy]$ ]]; then python3 "$PYTHON_SCRIPT" "$path" -r --no-copyright --no-important --no-pragma else print_info "已取消" fi } # 测试模式 test_mode() { local test_file="$SCRIPT_DIR/test_comment_removal.m" if [ ! -f "$test_file" ]; then print_error "测试文件不存在: $test_file" exit 1 fi print_info "在测试文件上运行..." echo "" # 预览 print_info "步骤 1/3: 预览删除效果" python3 "$PYTHON_SCRIPT" "$test_file" --dry-run echo "" # 实际清理 print_info "步骤 2/3: 执行清理" python3 "$PYTHON_SCRIPT" "$test_file" echo "" # 显示结果 print_info "步骤 3/3: 查看清理后的文件" echo "----------------------------------------" cat "$test_file" echo "----------------------------------------" echo "" # 恢复测试文件 if [ -f "${test_file}.backup" ]; then mv "${test_file}.backup" "$test_file" print_info "已恢复测试文件" fi } # 主函数 main() { # 检查Python脚本是否存在 if [ ! -f "$PYTHON_SCRIPT" ]; then print_error "找不到Python脚本: $PYTHON_SCRIPT" exit 1 fi # 检查参数 if [ $# -eq 0 ]; then show_usage exit 1 fi local command=$1 case $command in preview) if [ $# -lt 2 ]; then print_error "缺少路径参数" show_usage exit 1 fi preview_mode "$2" ;; clean) if [ $# -lt 2 ]; then print_error "缺少路径参数" show_usage exit 1 fi clean_mode "$2" ;; clean-all) if [ $# -lt 2 ]; then print_error "缺少路径参数" show_usage exit 1 fi clean_all_mode "$2" ;; test) test_mode ;; help|--help|-h) show_usage ;; *) print_error "未知命令: $command" show_usage exit 1 ;; esac } main "$@"