config_editor/build.sh
2025-12-31 09:22:08 +00:00

368 lines
9.9 KiB
Bash
Raw Permalink Blame History

This file contains ambiguous Unicode characters

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.

# config editor 一键构建脚本
set -e
# 配置变量
PACKAGE_NAME="config-editor"
VERSION="1.2"
ARCHITECTURE="amd64"
MAINTAINER="hjy <hjy@pw.com>"
echo "=========================================="
echo "Config Editor 构建脚本 - 版本 $VERSION"
echo "架构: $ARCHITECTURE"
echo "构建时间: $(date)"
echo "=========================================="
# 检查依赖
echo "检查构建依赖..."
if ! command -v dpkg-deb &> /dev/null; then
echo "错误: 需要 dpkg-deb请安装: sudo apt install dpkg-dev"
exit 1
fi
# 检查源代码是否存在
echo "检查源代码..."
if [ ! -f "config_editor.py" ]; then
echo "错误: 未找到主程序文件 config_editor.py"
exit 1
fi
# 创建构建目录
BUILD_DIR="/tmp/${PACKAGE_NAME}-build"
echo "创建构建目录: $BUILD_DIR"
rm -rf "$BUILD_DIR"
mkdir -p "$BUILD_DIR/DEBIAN"
mkdir -p "$BUILD_DIR/usr/share/applications"
mkdir -p "$BUILD_DIR/usr/share/config-editor"
mkdir -p "$BUILD_DIR/usr/bin"
# 复制应用文件
echo "复制应用文件..."
cp config_editor.py "$BUILD_DIR/usr/share/config-editor/"
# 复制配置文件
if [ -f "config_editor_rules.json" ]; then
cp config_editor_rules.json "$BUILD_DIR/usr/share/config-editor/"
fi
if [ -f "config_editor_settings.json" ]; then
cp config_editor_settings.json "$BUILD_DIR/usr/share/config-editor/"
fi
# 创建桌面文件
echo "创建桌面文件..."
cat > "$BUILD_DIR/usr/share/applications/config-editor.desktop" << 'EOF'
[Desktop Entry]
Version=1.0
Type=Application
Name=Config Editor
Comment=Configuration Management Tool
Exec=/usr/bin/config-editor
Icon=config-editor
Categories=Development;Utility;Settings;
Terminal=false
StartupNotify=true
StartupWMClass=ConfigEditor
EOF
# 创建快捷方式创建脚本
echo "创建快捷方式创建脚本..."
cat > "$BUILD_DIR/usr/share/config-editor/create_desktop_shortcut.sh" << 'EOF'
#!/bin/bash
# 桌面快捷方式创建脚本
# 检查是否有图形环境
if [ -z "$DISPLAY" ] && [ -z "$WAYLAND_DISPLAY" ]; then
echo "未检测到图形环境,跳过创建桌面快捷方式"
exit 0
fi
echo "正在创建桌面快捷方式..."
# 获取当前用户如果是sudo运行使用SUDO_USER
CURRENT_USER="${SUDO_USER:-$USER}"
USER_HOME=$(getent passwd "$CURRENT_USER" | cut -d: -f6)
if [ -z "$USER_HOME" ] || [ ! -d "$USER_HOME" ]; then
echo "无法确定用户主目录,跳过创建桌面快捷方式"
exit 0
fi
# 检测桌面目录(支持不同语言的桌面目录名)
DESKTOP_DIR=""
for possible_dir in "Desktop" "桌面" "Escritorio" "Bureau" "Schreibtisch"; do
if [ -d "$USER_HOME/$possible_dir" ]; then
DESKTOP_DIR="$USER_HOME/$possible_dir"
break
fi
done
# 如果没有找到标准桌面目录尝试创建Desktop目录
if [ -z "$DESKTOP_DIR" ]; then
DESKTOP_DIR="$USER_HOME/Desktop"
mkdir -p "$DESKTOP_DIR"
chown "$CURRENT_USER:$CURRENT_USER" "$DESKTOP_DIR"
echo "创建桌面目录: $DESKTOP_DIR"
fi
# 创建桌面快捷方式
DESKTOP_FILE="$DESKTOP_DIR/config-editor.desktop"
cat > "$DESKTOP_FILE" << 'DESKTOP_EOF'
[Desktop Entry]
Version=1.0
Type=Application
Name=Config Editor
Comment=Configuration Management Tool
Exec=/usr/bin/config-editor
Icon=config-editor
Categories=Development;Utility;Settings;
Terminal=false
StartupNotify=true
StartupWMClass=ConfigEditor
DESKTOP_EOF
# 设置权限
chown "$CURRENT_USER:$CURRENT_USER" "$DESKTOP_FILE"
chmod 755 "$DESKTOP_FILE"
echo "桌面快捷方式已创建: $DESKTOP_FILE"
echo "如果快捷方式没有立即显示,请刷新桌面或重新登录"
EOF
chmod +x "$BUILD_DIR/usr/share/config-editor/create_desktop_shortcut.sh"
# 创建虚拟环境安装脚本 - 修复版本
echo "创建虚拟环境安装脚本..."
cat > "$BUILD_DIR/usr/share/config-editor/setup_venv.sh" << 'EOF'
#!/bin/bash
# 虚拟环境安装脚本 - 修复sudo环境问题
set -e
APP_DIR="/usr/share/config-editor"
VENV_DIR="$APP_DIR/venv"
echo "设置Config Editor虚拟环境..."
# 检查是否使用sudo
if [ "$EUID" -ne 0 ]; then
echo "错误: 请使用sudo运行此脚本"
echo "命令: sudo $0"
exit 1
fi
# 清理旧的虚拟环境(如果存在)
if [ -d "$VENV_DIR" ]; then
echo "清理旧的虚拟环境..."
rm -rf "$VENV_DIR"
fi
# 创建虚拟环境
echo "创建虚拟环境..."
python3 -m venv "$VENV_DIR"
if [ $? -ne 0 ]; then
echo "错误: 无法创建虚拟环境"
echo "请确保已安装 python3-venv: sudo apt install python3-venv"
exit 1
fi
echo "✓ 虚拟环境创建成功"
# 使用虚拟环境中的pip安装PyQt6 - 关键修复:使用绝对路径
echo "安装PyQt6..."
if ! "$VENV_DIR/bin/python" -m pip install PyQt6 2>/dev/null; then
echo "尝试使用国内镜像安装..."
"$VENV_DIR/bin/python" -m pip install PyQt6 -i https://pypi.tuna.tsinghua.edu.cn/simple/
fi
# 验证安装
if ! "$VENV_DIR/bin/python" -c "import PyQt6" 2>/dev/null; then
echo "错误: PyQt6安装失败"
echo "请尝试手动安装:"
echo " sudo $VENV_DIR/bin/pip install PyQt6"
exit 1
fi
echo "✓ PyQt6安装成功"
# 设置权限
chmod -R 755 "$VENV_DIR"
echo ""
echo "虚拟环境设置完成!"
echo "虚拟环境位置: $VENV_DIR"
echo ""
echo "可以测试运行:"
echo " $VENV_DIR/bin/python $APP_DIR/config_editor.py"
EOF
chmod +x "$BUILD_DIR/usr/share/config-editor/setup_venv.sh"
# 创建启动脚本
echo "创建启动脚本..."
cat > "$BUILD_DIR/usr/bin/config-editor" << 'EOF'
#!/bin/bash
# Config Editor 启动脚本
APP_DIR="/usr/share/config-editor"
VENV_DIR="$APP_DIR/venv"
# 日志文件
LOG_DIR="${HOME}/.config/config-editor"
mkdir -p "$LOG_DIR"
LOG_FILE="$LOG_DIR/startup.log"
# 记录启动信息
{
echo "========================================"
echo "启动时间: $(date)"
echo "用户: $(whoami)"
echo "DISPLAY: $DISPLAY"
} >> "$LOG_FILE"
# 检查虚拟环境
if [ ! -d "$VENV_DIR" ]; then
echo "错误: 虚拟环境未找到。请先运行:"
echo " sudo /usr/share/config-editor/setup_venv.sh"
exit 1
fi
# 检查Python可执行文件
PYTHON_EXEC="$VENV_DIR/bin/python"
if [ ! -x "$PYTHON_EXEC" ]; then
PYTHON_EXEC="$VENV_DIR/bin/python3"
if [ ! -x "$PYTHON_EXEC" ]; then
echo "错误: 虚拟环境中找不到Python可执行文件"
exit 1
fi
fi
# 检查PyQt6是否安装
if ! "$PYTHON_EXEC" -c "import PyQt6" 2>/dev/null; then
echo "错误: PyQt6未在虚拟环境中安装。请运行:"
echo " sudo /usr/share/config-editor/setup_venv.sh"
echo "如果仍然失败,请手动安装:"
echo " sudo $VENV_DIR/bin/pip install PyQt6"
exit 1
fi
# 切换到应用目录
cd "$APP_DIR"
# 启动应用
exec "$PYTHON_EXEC" config_editor.py "$@"
EOF
chmod +x "$BUILD_DIR/usr/bin/config-editor"
# 创建控制文件
echo "创建DEBIAN控制文件..."
cat > "$BUILD_DIR/DEBIAN/control" << EOF
Package: $PACKAGE_NAME
Version: $VERSION
Architecture: $ARCHITECTURE
Depends: python3, python3-venv
Maintainer: $MAINTAINER
Description: Config Editor with one-click hide feature
A PyQt6-based configuration management editor.
Features include one-click hide all configuration items.
EOF
# 创建安装后脚本
echo "创建安装后脚本..."
cat > "$BUILD_DIR/DEBIAN/postinst" << 'EOF'
#!/bin/bash
# 安装后脚本
echo "Config Editor 安装完成!"
echo ""
echo "重要: 需要设置虚拟环境才能运行应用"
echo "请执行以下命令完成安装:"
echo " sudo /usr/share/config-editor/setup_venv.sh"
echo ""
echo "注意: 如果之前安装失败,新版本修复了虚拟环境安装问题"
echo " 请确保运行上述命令安装PyQt6到正确的虚拟环境中"
# 更新桌面数据库
update-desktop-database /usr/share/applications 2>/dev/null || true
# 设置权限
chmod 755 /usr/bin/config-editor
chmod 755 /usr/share/config-editor/setup_venv.sh
chmod 755 /usr/share/config-editor/create_desktop_shortcut.sh
# 尝试创建桌面快捷方式(如果图形环境可用)
echo "正在尝试创建桌面快捷方式..."
/usr/share/config-editor/create_desktop_shortcut.sh 2>/dev/null || true
echo ""
echo "安装完成!您可以通过以下方式启动:"
echo "1. 桌面快捷方式 (如果已创建)"
echo "2. 应用程序菜单中的 'Config Editor'"
echo "3. 命令行: config-editor"
echo ""
echo "如需手动创建桌面快捷方式,请运行:"
echo " /usr/share/config-editor/create_desktop_shortcut.sh"
exit 0
EOF
chmod +x "$BUILD_DIR/DEBIAN/postinst"
# 创建卸载前脚本
echo "创建卸载前脚本..."
cat > "$BUILD_DIR/DEBIAN/prerm" << 'EOF'
#!/bin/bash
# 卸载前脚本
echo "清理桌面快捷方式..."
# 获取当前用户
CURRENT_USER="${SUDO_USER:-$USER}"
USER_HOME=$(getent passwd "$CURRENT_USER" | cut -d: -f6 2>/dev/null || echo "")
if [ -n "$USER_HOME" ] && [ -d "$USER_HOME" ]; then
# 删除可能的桌面快捷方式
for possible_dir in "Desktop" "桌面" "Escritorio" "Bureau" "Schreibtisch"; do
DESKTOP_FILE="$USER_HOME/$possible_dir/config-editor.desktop"
if [ -f "$DESKTOP_FILE" ]; then
rm -f "$DESKTOP_FILE"
echo "移除桌面快捷方式: $DESKTOP_FILE"
fi
done
fi
exit 0
EOF
chmod +x "$BUILD_DIR/DEBIAN/prerm"
# 构建deb包
echo "构建deb包..."
dpkg-deb --build "$BUILD_DIR" "${PACKAGE_NAME}_${VERSION}_${ARCHITECTURE}.deb"
# 验证包
if dpkg -I "${PACKAGE_NAME}_${VERSION}_${ARCHITECTURE}.deb" > /dev/null; then
echo "✓ 包验证成功"
else
echo "✗ 包验证失败"
exit 1
fi
# 清理
rm -rf "$BUILD_DIR"
echo ""
echo "=========================================="
echo "构建完成!"
echo "生成的deb包: ${PACKAGE_NAME}_${VERSION}_${ARCHITECTURE}.deb"
echo ""
echo "安装步骤:"
echo "1. 卸载旧版本: sudo dpkg -r config-editor"
echo "2. 安装新版本: sudo dpkg -i ${PACKAGE_NAME}_${VERSION}_${ARCHITECTURE}.deb"
echo "3. 修复虚拟环境: sudo /usr/share/config-editor/setup_venv.sh"
echo ""
echo "新功能: 自动创建桌面快捷方式"
echo "重要: 新版本修复了虚拟环境安装问题"
echo "=========================================="