美文网首页
Mac OS 10.5 shell 使用defaults rea

Mac OS 10.5 shell 使用defaults rea

作者: 字节码 | 来源:发表于2019-10-14 23:23 被阅读0次

    Mac OS 10.5 shell 使用defaults read读取文件显示does not exist

    Snip20191014_4.png

    Mac os 升级到10.15后打包tweak项目失败,原因在于执行iosod这个sh时出错,以下方法为根据key读取plist中value:

    function readDefaultsValue() # args: plistPath, propertyName
    {
        $setCmd
        
        local plistPath="$1"
        local propertyName="$2"
        local value
        
        value=`defaults read "${plistPath%.*}" "$propertyName"` || \
            panic $? "Failed to read defaults property $propertyName from $plistPath"
    
        # return #
        echo "$value"
    }
    
    function getVersionFromVersionPList() # args: plistPath
    {
        $setCmd
        
        local plistPath="$1"
        local version
        local major
        local minor
        local bugFix
        local stage
        local pkgRev
        
        major=`readDefaultsValue "$plistPath" "Major"`
        minor=`readDefaultsValue "$plistPath" "Minor"`
        bugFix=`readDefaultsValue "$plistPath" "BugFix"`
        stage=`readDefaultsValue "$plistPath" "Stage"`
        pkgRev=`readDefaultsValue "$plistPath" "PackageRevision"`
    
        version="$major.$minor"
            
        [[ "$bugFix" == "" ]] || version+=".$bugFix"
        [[ "$stage" == "" ]] || version+="$stage"
        [[ "$pkgRev" == "" ]] || version+="-$pkgRev"
            
        # return #
        echo "$version"
    }
    

    上面的方法作用在于读取PackageVersion.plist中的key和value,等同于,在终端中读取plist文件

    swae@swaedeMacBook-Pro ~ % defaults read /Users/swae/Desktop/work/Tweak/tweakpro/tsprite/tsprite/PackageVersion.plist  Major
    2019-10-14 23:04:42.792 defaults[7909:308363]
    The domain/default pair of (/Users/swae/Desktop/work/Tweak/tweakpro/tsprite/tsprite/PackageVersion.plist, Major) does not exist
    

    从上面的输出可以看出,读取这个plist文件不存在,而经过我的验证使用defaults read读取大部分的plist文件都显示does not exist,但是当我使用defaults write 时,却显示exiting无法写入,尴尬,下面为输入:

    swae@swaedeMacBook-Pro ~ % defaults write /Users/swae/Desktop/work/Tweak/tweakpro/tsprite/tsprite/PackageVersion.plist  Major1 200
    2019-10-14 23:08:47.076 defaults[7976:310697] Could not write domain /Users/swae/Desktop/work/Tweak/tweakpro/tsprite/tsprite/PackageVersion.plist; exiting
    

    无奈中,我尝试使用defaults write 写入一个新的plist文件,然后再使用defaults read读取却是正常的:

    swae@swaedeMacBook-Pro ~ % defaults write /Users/swae/Desktop/work/Tweak/tweakpro/tsprite/tsprite/PackageVersion1.plist method 1000
    swae@swaedeMacBook-Pro ~ % defaults read /Users/swae/Desktop/work/Tweak/tweakpro/tsprite/tsprite/PackageVersion1.plist method
    1000
    

    以上是我在Mac os 10.15上实际操作得到的结果,我把锅甩给了新系统,但是当我在Mac os 10.14.6的终端上操作时,依旧是以上结果,目前还不清楚原因,待近一步排查。

    下面两张图为了说明这两个plist的不同


    无法通过defaults_read打开的plist.png

    通过defaluts_write创建的plist,可以通过defaults read 读取key。

    Snip20191014_3.png

    无法通过defaults_read打开的plist

    最终我使用新建的PackageVersion1 .plist替换调旧的,重新执行build for->profiling,问题暂时解决:

    Snip20191014_6.png

    感谢简友[Ninja400]提供的方法:
    default read CFBundleVersion 更换为 `/usr/libexec/PlistBuddy -c "Print CFBundleVersion" $SRCROOT/tweek/Info.plist``

    替换/opt/iOSOpenDev/bin/iosod中的readDefaultsValue方法

    function readDefaultsValue() # args: plistPath, propertyName
    {
        $setCmd
        
        local plistPath="$1"
        local propertyName="$2"
        local value
    
        value=`/usr/libexec/PlistBuddy -c "Print '$propertyName'" "$plistPath"` || \
            panic $? "Failed to read defaults property $propertyName from $plistPath"
          # 使用defaults read无法读取plist
        # value=`defaults read "${plistPath%.*}" "$propertyName"` || \
            # panic $? "Failed to read defaults property $propertyName from $plistPath"
    
        # return #
        echo "$value"
    }
    

    但是最终还是报错setControlFileVersionFieldUsingVersionPList

    Preparing to run Xcode Build Phase...
    process environment variables...
    packageVersionPListPath=/Users/xiaoyuan/Desktop/work/git/tsprite/tsprite/PackageVersion.plist
    Signing /Users/xiaoyuan/Library/Developer/Xcode/DerivedData/tsprite-cfiffgyeoaaiwjfecladlybccolg/Build/Products/Release-iphoneos/tsprite.dylib with ldid... Done.
    Copying /Users/xiaoyuan/Library/Developer/Xcode/DerivedData/tsprite-cfiffgyeoaaiwjfecladlybccolg/Build/Products/Release-iphoneos/tsprite.dylib to package directory at /Users/xiaoyuan/Desktop/work/git/tsprite/tsprite/Package/Library/MobileSubstrate/DynamicLibraries...
    Preparing to build package...
    read plist to get version...
    Setting control file /Users/xiaoyuan/Desktop/work/git/tsprite/tsprite/Package/DEBIAN/control Version field to getVersionFromVersionPList plistPath...
    getVersionFromVersionPList plistPath=/Users/xiaoyuan/Desktop/work/git/tsprite/tsprite/PackageVersion.plist
    1.0.0-1 using /Users/xiaoyuan/Desktop/work/git/tsprite/tsprite/PackageVersion.plist... sed: 1: "s/Version:.*/Version: g ...": unescaped newline inside substitute pattern
    Failed.
    

    暂时这样修改setControlFileVersionFieldUsingVersionPList

    function setControlFileVersionFieldUsingVersionPList() # args: controlFile, plistPath
    {
        local controlFile="$1"
        local plistPath="$2"
        local versionReadFromPList
        
        requireFile "$controlFile" false
        requireFile "$plistPath" false
        
        # read plist to get version #
        echo "read plist to get version..."
        versionReadFromPList=`getVersionFromVersionPList "$plistPath"`
        
        [[ $versionReadFromPList != "" ]] || \
            panic 1 "Failed to read version from $plistPath"
    
        # write version to control file #
    
        echo -n "Setting control file $controlFile Version field to $versionReadFromPList using $plistPath... "
    
        # sed -i "" "s/Version:.*/Version: $versionReadFromPList/g" "$controlFile" || \
            # panic $? "Failed."
    
        # success #
        echo "Done."
    }
    

    相关文章

      网友评论

          本文标题:Mac OS 10.5 shell 使用defaults rea

          本文链接:https://www.haomeiwen.com/subject/voodmctx.html