What tools support editing project.pbxproj files?

我是研究僧i 提交于 2019-12-06 08:30:08

问题


I want to edit project.pbxproj straight up using command line (for CI server script)

what tools can allow me to do this?

I used to use PlistBuddy to edit the output Info.plist; however, what i really want to do is to edit this user defined field, which is used in multiple places, and i really don't want to have to hunt that down in every plist location


回答1:


Maybe you went the wrong way. If you have a frequently changing parameter, it is easier to carry out it in * .plist as a setting. Replacement a value in the * .plist much easier than editing the project file or pass the user defined field. In my project I have the setting server_address which placed in the configuration.plist.

1) When the app starts, just load the configuration once and distribute a settings:

- (NSDictionary *)loadConfigFromPlist:(NSString *)plistName {
    NSURL *plistUrl = [[NSBundle bundleForClass:[self class]] URLForResource:plistName withExtension:@"plist"];

    if (!plistUrl) {
        @throw [NSException exceptionWithName:@"InaccessibleFileException"
                                       reason:[NSString stringWithFormat:@"Unable to read config from '%@'", plistName]
                                     userInfo:nil];
    }

    NSDictionary *config = [NSDictionary dictionaryWithContentsOfURL:plistUrl];
    return config;
}

2) When configuring the CI need to add a build step for custom script (after updating sources from the git/svn), e.g. for teamcity CI:

echo "Check a condition - change server address"

if [ "true" = "%env.is_change_serveraddress%" ]; then
  echo "env.is_change_serveraddress = %env.is_change_serveraddress%"
  echo "Setup a custom server address: %env.server_address%"
  /usr/libexec/plistbuddy -c "Set :WebClient:Constants:WebServerAddress %env.server_address%" "configuration.plist"
else
  echo "env.is_change_serveraddress = %env.is_change_serveraddress%"
fi



回答2:


project.pbxproj is an old-style ASCII property list file, too. So you can use /usr/libexec/PlistBuddy to edit it.

Print some User-Defined key's value like this,

# Get the key A83311AA20DA4A80004B8C0E in your project.pbxproj
# LZD_NOTIFICATION_SERVICE_BUNDLE_ID is defined by me,
# Replace key paths with your own.
/usr/libexec/PlistBuddy -c 'print :objects:A83311AA20DA4A80004B8C0E:buildSettings:LZD_NOTIFICATION_SERVICE_BUNDLE_ID' LAAppAdapter.xcodeproj/project.pbxproj

Set its value like this,

/usr/libexec/PlistBuddy -c 'set :objects:A83311AA20DA4A80004B8C0E:buildSettings:LZD_NOTIFICATION_SERVICE_BUNDLE_ID com.dawnsong.notification-service' LAAppAdapter.xcodeproj/project.pbxproj



回答3:


I know this has been answered for a while, but since the original question is about tools supporting the manipulation of .pbxproj files, and many other people may be looking for the same information, here's how I do it. It took me quite a while to figure this out because I was very unfamiliar with Xcode when I started attempting this, so I hope this saves others the hours of grief I had to put in.

You can use the plutil command to transform the .pbxproj file from the legacy .plist format into an XML or JSON format you will be able to manipulate more easily. I'm using JSON. To do so, just run:

plutil -convert json project.pbxproj

This will convert the format of project.pbxproj, but be aware that -contrary to common sense- the output won't be another file with a JSON extention such as project.json. What will happen is that project.pbxproj will be converted to JSON format, but retain it's cryptic .pbxproj extension. So even though the file's format has been changed, Xcode will still pick it up and use it in its new JSON format.

Then you can change project.pbxproj with ease using any JSON manipulation tool of your choosing. I'm using Groovy's JsonSlurper class in a Groovy script.

Note I also explored the XML option, but I found the project.pbxproj file in XML format to be cumbersome to parse. The elements are not properly nested to allow for traversing the tree with ease. It's plagued with:

<key>someKey</key>
<dict>
    <!--More elements which provide configuration for the key above-->
</dict>

So it's positional in nature. You have to look for the key element corresponding to the setting you want to manipulate and then jump to the dict element just after it. Which means you have to mount the children of each XML element into an array, in order to index them.



来源:https://stackoverflow.com/questions/32133576/what-tools-support-editing-project-pbxproj-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!