如何使用 PlistBuddy 添加设置而不覆盖现有设置?

How can I use PlistBuddy to add settings without overriding existing ones?

我正在尝试编写一个脚本,在构建期间将设置添加到“设置”应用程序,而不覆盖现有设置(如果存在)。这是我的脚本片段:

PLISTBUDDY="/usr/libexec/PlistBuddy"
SETTINGSBUNDLEPATH="$CODESIGNING_FOLDER_PATH/Settings.bundle/Root.plist"

{
$PLISTBUDDY -c "Add :PreferenceSpecifiers:0:Type string 'PSGroupSpecifier'" "$SETTINGSBUNDLEPATH"
$PLISTBUDDY -c "Add :PreferenceSpecifiers:0:Title string 'Version Information'" "$SETTINGSBUNDLEPATH"
} || {
$PLISTBUDDY -c "Set :PreferenceSpecifiers:0:Type 'PSGroupSpecifier'" "$SETTINGSBUNDLEPATH"
$PLISTBUDDY -c "Set :PreferenceSpecifiers:0:Title 'Version Information'" "$SETTINGSBUNDLEPATH"
}

{
$PLISTBUDDY -c "Add :PreferenceSpecifiers:1:Type string 'PSTitleValueSpecifier'" "$SETTINGSBUNDLEPATH"
$PLISTBUDDY -c "Add :PreferenceSpecifiers:1:Title string 'Version:'" "$SETTINGSBUNDLEPATH"
$PLISTBUDDY -c "Add :PreferenceSpecifiers:1:Key string 'appVersion'" "$SETTINGSBUNDLEPATH"
$PLISTBUDDY -c "Add :PreferenceSpecifiers:1:DefaultValue string '$APPVERSION'" "$SETTINGSBUNDLEPATH"
} || {
$PLISTBUDDY -c "Set :PreferenceSpecifiers:1:Type 'PSTitleValueSpecifier'" "$SETTINGSBUNDLEPATH"
$PLISTBUDDY -c "Set :PreferenceSpecifiers:1:Title 'Version:'" "$SETTINGSBUNDLEPATH"
$PLISTBUDDY -c "Set :PreferenceSpecifiers:1:Key 'appVersion'" "$SETTINGSBUNDLEPATH"
$PLISTBUDDY -c "Set :PreferenceSpecifiers:1:DefaultValue '$APPVERSION'" "$SETTINGSBUNDLEPATH"
}

不过我不想覆盖任何现有设置。 如何将这些设置添加到 PreferenceSpecifiers 的末尾?

我已经阅读了 documentation,并且我尝试使用 Merge 但没有成功。

$PLISTBUDDY -c "Merge ${PROJECT_DIR}/Settings1.bundle/Root.plist" "$SETTINGSBUNDLEPATH"

编辑:在代码中添加了“try-catch”,这修复了 运行 脚本两次时的问题。

这首先计算所有字典 PreferenceSpecifiers,如 these answers。然后它会遍历字典并检查该组是否已经存在,因此当您第二次构建应用程序时,脚本不会再次添加行。如果找不到该组,它将在最后一个索引之后添加行。我不确定的一件事是为什么计数不是数字以及如何正确修复它(当前代码有效)。

PLISTBUDDY="/usr/libexec/PlistBuddy"
SETTINGSBUNDLEPATH="$CODESIGNING_FOLDER_PATH/Settings.bundle/Root.plist"

count=`${PLISTBUDDY} -c "Print PreferenceSpecifiers:" ${SETTINGSBUNDLEPATH} | grep "Dict"|wc -l`
let count++
let count--

FOUND=false
for index in `seq 0 $count`
do
    val=`${PLISTBUDDY} -c "Print PreferenceSpecifiers:${index}:Key" ${SETTINGSBUNDLEPATH}`
    if [ "$val" == "versionInfo" ]; then
        FOUND=true
        break
    fi
done

if [ "$FOUND" == false ]; then
    $PLISTBUDDY -c "Add :PreferenceSpecifiers:$count:Type string 'PSGroupSpecifier'" "$SETTINGSBUNDLEPATH"
    $PLISTBUDDY -c "Add :PreferenceSpecifiers:$count:Title string 'Version Information'" "$SETTINGSBUNDLEPATH"
    $PLISTBUDDY -c "Add :PreferenceSpecifiers:$count:Key string 'versionInfo'" "$SETTINGSBUNDLEPATH"

    let count++
    $PLISTBUDDY -c "Add :PreferenceSpecifiers:$count:Type string 'PSTitleValueSpecifier'" "$SETTINGSBUNDLEPATH"
    $PLISTBUDDY -c "Add :PreferenceSpecifiers:$count:Title string 'Version:'" "$SETTINGSBUNDLEPATH"
    $PLISTBUDDY -c "Add :PreferenceSpecifiers:$count:Key string 'appVersion'" "$SETTINGSBUNDLEPATH"
    $PLISTBUDDY -c "Add :PreferenceSpecifiers:$count:DefaultValue string '$APPVERSION'" "$SETTINGSBUNDLEPATH"
fi