ScreenLockDetector/make_deb.sh

266 lines
8.0 KiB
Bash
Raw Normal View History

2025-11-07 14:57:13 +08:00
#!/bin/bash
set -e
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
2025-11-07 14:57:13 +08:00
# 配置变量
APP_NAME="ScreenLockDetector"
PACKAGE_NAME="ScreenLockDetector"
2025-11-11 17:00:01 +08:00
# 从 CMakeLists.txt 中提取版本号
VERSION=$(grep -oP '(?<=project\(ScreenLockDetector VERSION )[0-9.]+' CMakeLists.txt)
if [ -z "$VERSION" ]; then
echo -e "${RED}错误: 无法从 CMakeLists.txt 中提取版本号${NC}"
exit 1
fi
echo -e "${GREEN}✓ 从 CMakeLists.txt 读取版本号: ${VERSION}${NC}"
2025-11-07 14:57:13 +08:00
ARCH="amd64"
QT_DIR="$HOME/sdk/qt-5.15.2"
LINUXDEPLOYQT="${LINUXDEPLOYQT:-$HOME/tools/sunvpack-py/bin/linuxdeployqt}"
2025-11-07 14:57:13 +08:00
BUILD_DIR="$(pwd)/build"
PACKAGE_DIR="$(pwd)/package"
DEB_DIR="${PACKAGE_DIR}/${PACKAGE_NAME}_${VERSION}_${ARCH}"
2025-11-07 14:57:13 +08:00
echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE}Qt Screen Lock Demo - DEB 打包脚本${NC}"
echo -e "${BLUE}========================================${NC}"
2025-11-07 14:57:13 +08:00
# 检查 linuxdeployqt 是否存在
2025-11-07 14:57:13 +08:00
if [ ! -f "$LINUXDEPLOYQT" ]; then
echo -e "${RED}错误: linuxdeployqt 未找到: $LINUXDEPLOYQT${NC}"
2025-11-07 14:57:13 +08:00
exit 1
fi
# 检查 Qt 目录是否存在
2025-11-07 14:57:13 +08:00
if [ ! -d "$QT_DIR" ]; then
echo -e "${RED}错误: Qt 目录未找到: $QT_DIR${NC}"
2025-11-07 14:57:13 +08:00
exit 1
fi
# 清理之前的打包目录
echo -e "${YELLOW}[1/8] 清理之前的打包目录...${NC}"
2025-11-07 14:57:13 +08:00
rm -rf "$PACKAGE_DIR"
mkdir -p "$PACKAGE_DIR"
# 编译项目
echo -e "${YELLOW}[2/8] 编译项目...${NC}"
2025-11-07 14:57:13 +08:00
if [ ! -f "$BUILD_DIR/bin/$APP_NAME" ]; then
echo -e "${BLUE}运行编译脚本...${NC}"
2025-11-07 14:57:13 +08:00
./build.sh
fi
if [ ! -f "$BUILD_DIR/bin/$APP_NAME" ]; then
echo -e "${RED}错误: 编译失败,可执行文件未找到${NC}"
2025-11-07 14:57:13 +08:00
exit 1
fi
echo -e "${GREEN}✓ 编译成功${NC}"
# 创建 AppDir 结构
echo -e "${YELLOW}[3/8] 创建 AppDir 结构...${NC}"
2025-11-07 14:57:13 +08:00
APPDIR="${PACKAGE_DIR}/AppDir"
mkdir -p "${APPDIR}/usr/bin"
mkdir -p "${APPDIR}/usr/lib"
2025-11-07 14:57:13 +08:00
mkdir -p "${APPDIR}/usr/share/applications"
mkdir -p "${APPDIR}/usr/share/icons/hicolor/256x256/apps"
# 复制可执行文件
2025-11-07 14:57:13 +08:00
cp "$BUILD_DIR/bin/$APP_NAME" "${APPDIR}/usr/bin/"
chmod +x "${APPDIR}/usr/bin/$APP_NAME"
# 复制libstdc++.so.6
cp -r "/usr/local/lib64/libstdc++.so.6" "${APPDIR}/usr/lib/"
# 创建简单图标(如果没有的话)
if [ ! -f "icon.png" ]; then
echo -e "${BLUE}创建默认图标...${NC}"
# 这里只是创建一个占位符,实际应该有真实的图标
touch "${APPDIR}/usr/share/icons/hicolor/256x256/apps/${PACKAGE_NAME}.png"
else
cp icon.png "${APPDIR}/usr/share/icons/hicolor/256x256/apps/${PACKAGE_NAME}.png"
fi
# 复制 desktop 文件
cp screenlockdetector.desktop "${APPDIR}/usr/share/applications/"
2025-11-07 14:57:13 +08:00
echo -e "${GREEN}✓ AppDir 结构创建完成${NC}"
2025-11-07 14:57:13 +08:00
# 使用 linuxdeployqt 收集依赖
echo -e "${YELLOW}[4/8] 使用 linuxdeployqt 收集所有依赖...${NC}"
2025-11-07 14:57:13 +08:00
export PATH="${QT_DIR}/bin:$PATH"
export LD_LIBRARY_PATH="/usr/local/lib:/usr/local/lib64:${QT_DIR}/lib:$LD_LIBRARY_PATH"
cd "$PACKAGE_DIR"
"$LINUXDEPLOYQT" "${APPDIR}/usr/bin/$APP_NAME" \
-verbose=2 \
-bundle-non-qt-libs \
-no-translations \
-qmake="${QT_DIR}/bin/qmake"
2025-11-07 14:57:13 +08:00
cd - > /dev/null
echo -e "${GREEN}✓ 依赖收集完成${NC}"
# 创建 DEB 包目录结构
echo -e "${YELLOW}[5/8] 创建 DEB 包目录结构...${NC}"
2025-11-07 14:57:13 +08:00
mkdir -p "${DEB_DIR}/opt/${PACKAGE_NAME}"
mkdir -p "${DEB_DIR}/usr/bin"
mkdir -p "${DEB_DIR}/usr/share/applications"
mkdir -p "${DEB_DIR}/usr/share/icons/hicolor/256x256/apps"
mkdir -p "${DEB_DIR}/DEBIAN"
# 复制 AppDir 内容到 /opt
2025-11-07 14:57:13 +08:00
cp -r "${APPDIR}/usr/bin" "${DEB_DIR}/opt/${PACKAGE_NAME}/"
cp -r "${APPDIR}/usr/lib" "${DEB_DIR}/opt/${PACKAGE_NAME}/"
# 如果有 Qt plugins
2025-11-07 14:57:13 +08:00
if [ -d "${APPDIR}/usr/plugins" ]; then
cp -r "${APPDIR}/usr/plugins" "${DEB_DIR}/opt/${PACKAGE_NAME}/"
fi
# 如果有 Qt 库目录
if [ -d "${APPDIR}/usr/lib" ]; then
mkdir -p "${DEB_DIR}/opt/${PACKAGE_NAME}/lib"
cp -r "${APPDIR}/usr/lib"/* "${DEB_DIR}/opt/${PACKAGE_NAME}/lib/" 2>/dev/null || true
fi
# 创建启动脚本
cat > "${DEB_DIR}/opt/${PACKAGE_NAME}/bin/${APP_NAME}.sh" << 'EOF'
2025-11-07 14:57:13 +08:00
#!/bin/bash
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
APP_DIR="$(dirname "$SCRIPT_DIR")"
export LD_LIBRARY_PATH="/usr/local/lib:/usr/local/lib64:${APP_DIR}/lib:$LD_LIBRARY_PATH"
export QT_PLUGIN_PATH="${APP_DIR}/plugins"
exec "${SCRIPT_DIR}/ScreenLockDetector" "$@"
EOF
2025-11-07 14:57:13 +08:00
chmod +x "${DEB_DIR}/opt/${PACKAGE_NAME}/bin/${APP_NAME}.sh"
2025-11-07 14:57:13 +08:00
# 创建 /usr/bin 软链接
cat > "${DEB_DIR}/usr/bin/${PACKAGE_NAME}" << 'EOF'
2025-11-07 14:57:13 +08:00
#!/bin/bash
exec /opt/ScreenLockDetector/bin/ScreenLockDetector.sh "$@"
EOF
2025-11-07 14:57:13 +08:00
chmod +x "${DEB_DIR}/usr/bin/${PACKAGE_NAME}"
# 复制 desktop 文件和图标
sed "s|Exec=.*|Exec=/usr/bin/${PACKAGE_NAME}|g" screenlockdetector.desktop > "${DEB_DIR}/usr/share/applications/${PACKAGE_NAME}.desktop"
2025-11-07 14:57:13 +08:00
sed -i "s|Icon=.*|Icon=${PACKAGE_NAME}|g" "${DEB_DIR}/usr/share/applications/${PACKAGE_NAME}.desktop"
if [ -f "${APPDIR}/usr/share/icons/hicolor/256x256/apps/${PACKAGE_NAME}.png" ]; then
cp "${APPDIR}/usr/share/icons/hicolor/256x256/apps/${PACKAGE_NAME}.png" \
"${DEB_DIR}/usr/share/icons/hicolor/256x256/apps/"
fi
echo -e "${GREEN}✓ DEB 包目录结构创建完成${NC}"
# 创建 DEBIAN/control 文件
echo -e "${YELLOW}[6/8] 创建控制文件...${NC}"
# 计算安装大小 (KB)
2025-11-07 14:57:13 +08:00
INSTALLED_SIZE=$(du -sk "${DEB_DIR}/opt" | cut -f1)
cat > "${DEB_DIR}/DEBIAN/control" << EOF
2025-11-07 14:57:13 +08:00
Package: ${PACKAGE_NAME}
Version: ${VERSION}
Section: utils
Priority: optional
Architecture: ${ARCH}
Installed-Size: ${INSTALLED_SIZE}
Maintainer: Qt Screen Lock Demo <developer@example.com>
Depends: dbus, libx11-6, libxext6, libxrender1, libfontconfig1, libfreetype6, libglib2.0-0, libgl1
Description: Qt Screen Lock Detection Demo Application
A Qt5-based Linux application that detects Ubuntu system lock/unlock
events and automatically stops Paint events when the screen is locked.
.
Features:
- Automatic screen lock detection via DBus
- Automatic paint event control
- Real-time animation demonstration
- Support for multiple desktop environments (GNOME, KDE, XFCE)
- Manual control and status monitoring
.
This package includes all required Qt5 libraries and dependencies.
EOF
echo -e "${GREEN}✓ 控制文件创建完成${NC}"
# 创建 postinst 脚本(安装后脚本)
echo -e "${YELLOW}[7/8] 创建安装脚本...${NC}"
2025-11-07 14:57:13 +08:00
cat > "${DEB_DIR}/DEBIAN/postinst" << 'EOF'
2025-11-07 14:57:13 +08:00
#!/bin/bash
set -e
# 更新桌面数据库
2025-11-07 14:57:13 +08:00
if [ -x /usr/bin/update-desktop-database ]; then
update-desktop-database -q
fi
# 更新图标缓存
2025-11-07 14:57:13 +08:00
if [ -x /usr/bin/gtk-update-icon-cache ]; then
gtk-update-icon-cache -q -f /usr/share/icons/hicolor 2>/dev/null || true
fi
echo "Screen Lock Demo 安装完成!"
echo "可以通过命令 'ScreenLockDetector' 启动,或在应用程序菜单中找到它。"
2025-11-07 14:57:13 +08:00
exit 0
EOF
2025-11-07 14:57:13 +08:00
chmod 755 "${DEB_DIR}/DEBIAN/postinst"
# 创建 prerm 脚本(卸载前脚本)
cat > "${DEB_DIR}/DEBIAN/prerm" << 'EOF'
#!/bin/bash
set -e
exit 0
EOF
chmod 755 "${DEB_DIR}/DEBIAN/prerm"
echo -e "${GREEN}✓ 安装脚本创建完成${NC}"
# 构建 DEB 包
echo -e "${YELLOW}[8/8] 构建 DEB 包...${NC}"
# 设置正确的权限
2025-11-07 14:57:13 +08:00
find "${DEB_DIR}" -type d -exec chmod 755 {} \;
find "${DEB_DIR}/opt/${PACKAGE_NAME}/bin" -type f -exec chmod 755 {} \;
2025-11-07 14:57:13 +08:00
DEB_FILE="${PACKAGE_DIR}/${PACKAGE_NAME}_${VERSION}_${ARCH}.deb"
2025-11-07 14:57:13 +08:00
dpkg-deb --build "${DEB_DIR}" "$DEB_FILE"
if [ -f "$DEB_FILE" ]; then
echo -e "${GREEN}========================================${NC}"
echo -e "${GREEN}✓ DEB 包构建成功!${NC}"
echo -e "${GREEN}========================================${NC}"
echo -e "${BLUE}包位置: ${DEB_FILE}${NC}"
echo -e "${BLUE}包大小: $(du -h "$DEB_FILE" | cut -f1)${NC}"
echo ""
echo -e "${YELLOW}安装命令:${NC}"
echo -e " sudo dpkg -i $DEB_FILE"
echo ""
echo -e "${YELLOW}如果有依赖问题,运行:${NC}"
echo -e " sudo apt-get install -f"
echo ""
echo -e "${YELLOW}卸载命令:${NC}"
echo -e " sudo dpkg -r ${PACKAGE_NAME}"
2025-11-07 14:57:13 +08:00
echo ""
echo -e "${YELLOW}查看包信息:${NC}"
echo -e " dpkg -I $DEB_FILE"
echo -e " dpkg -c $DEB_FILE"
2025-11-07 14:57:13 +08:00
echo ""
else
echo -e "${RED}错误: DEB 包构建失败${NC}"
2025-11-07 14:57:13 +08:00
exit 1
fi