mirror of
https://gitee.com/amber-ce/amber-pm
synced 2026-06-10 06:52:31 +08:00
refactor(desktop-fix): 重构桌面修复工具并支持多发行版
- 将工具重命名为更通用的 amber-pm-desktop-fix - 添加对不同发行版的支持,包括银河麒麟系统 - 实现旧链接自动迁移功能 - 改进目录创建和符号链接处理逻辑
This commit is contained in:
Executable
+98
@@ -0,0 +1,98 @@
|
||||
#!/bin/bash
|
||||
|
||||
# 确定目标目录
|
||||
if grep -q "Kylin" /etc/os-release; then
|
||||
TARGET_BASE="/usr/share"
|
||||
APP_TARGET_DIR="$TARGET_BASE/applications"
|
||||
echo "检测到麒麟系统,使用目标目录: $TARGET_BASE"
|
||||
else
|
||||
TARGET_BASE="/usr/local/share"
|
||||
APP_TARGET_DIR="$TARGET_BASE/applications/apm"
|
||||
echo "检测到非麒麟系统,使用目标目录: $APP_TARGET_DIR"
|
||||
fi
|
||||
|
||||
function ensure_dir() {
|
||||
local dir="$1"
|
||||
|
||||
# 检查目录是否为空
|
||||
if [ -z "$dir" ]; then
|
||||
echo "错误: 目录路径不能为空"
|
||||
return 1
|
||||
fi
|
||||
|
||||
# 检查目录是否存在
|
||||
if [ ! -d "$dir" ]; then
|
||||
echo "目录 '$dir' 不存在,正在创建..."
|
||||
if mkdir -p "$dir"; then
|
||||
echo "成功创建目录 '$dir'"
|
||||
return 0
|
||||
else
|
||||
echo "错误: 无法创建目录 '$dir'"
|
||||
return 1
|
||||
fi
|
||||
else
|
||||
return 0
|
||||
fi
|
||||
}
|
||||
|
||||
# 函数:检查目录并创建符号链接
|
||||
process_directory() {
|
||||
local source_dir="$1"
|
||||
local target_dir="$2"
|
||||
local name="$3"
|
||||
|
||||
if [ -d "$source_dir" ] && [ -n "$(ls -A "$source_dir")" ]; then
|
||||
ln -sv $source_dir/* "$target_dir" 2>/dev/null
|
||||
find "$target_dir" -xtype l -exec echo '{} is invalid now and going to be cleaned' \; -exec unlink {} \; 2>/dev/null &
|
||||
else
|
||||
echo "$name directory is empty or does not exist, skipping..."
|
||||
fi
|
||||
}
|
||||
|
||||
# 使用动态确定的目标目录
|
||||
ensure_dir "$APP_TARGET_DIR/"
|
||||
ensure_dir "$TARGET_BASE/icons/"
|
||||
|
||||
# 处理 applications 目录
|
||||
process_directory "/var/lib/apm/apm/files/ace-env/amber-ce-tools/data-dir/applications/" \
|
||||
"$APP_TARGET_DIR/" "Applications"
|
||||
|
||||
# 处理 icons 目录
|
||||
process_directory "/var/lib/apm/apm/files/ace-env/amber-ce-tools/data-dir/icons/" \
|
||||
"$TARGET_BASE/icons/" "Icons"
|
||||
|
||||
# 等待所有后台任务完成
|
||||
wait
|
||||
|
||||
# 迁移老链接(仅非麒麟系统需要)
|
||||
if ! grep -q "Kylin" /etc/os-release; then
|
||||
OLD_LINK_DIR="/usr/local/share/applications"
|
||||
# 检查旧目录是否存在且不是apm子目录
|
||||
if [ -d "$OLD_LINK_DIR" ] && [ "$OLD_LINK_DIR" != "$APP_TARGET_DIR" ]; then
|
||||
echo "检查并迁移老链接..."
|
||||
# 查找旧目录中指向APM数据目录的符号链接
|
||||
find "$OLD_LINK_DIR" -maxdepth 1 -type l | while read -r link; do
|
||||
target=$(readlink "$link")
|
||||
# 如果链接指向APM的数据目录
|
||||
if [[ "$target" == /var/lib/apm/apm/files/ace-env/amber-ce-tools/data-dir/applications/* ]]; then
|
||||
filename=$(basename "$link")
|
||||
# 如果新位置没有同名文件,则移动
|
||||
if [ ! -e "$APP_TARGET_DIR/$filename" ]; then
|
||||
echo "迁移老链接: $filename"
|
||||
mv -v "$link" "$APP_TARGET_DIR/"
|
||||
else
|
||||
echo "新位置已存在 $filename,删除老链接"
|
||||
rm -v "$link"
|
||||
fi
|
||||
fi
|
||||
done
|
||||
fi
|
||||
fi
|
||||
|
||||
# 更新桌面数据库
|
||||
if command -v update-desktop-database >/dev/null 2>&1; then
|
||||
echo "正在更新桌面数据库..."
|
||||
update-desktop-database "$TARGET_BASE/applications/"
|
||||
else
|
||||
echo "警告: update-desktop-database 命令未找到"
|
||||
fi
|
||||
Reference in New Issue
Block a user