i'm making a script that modifies an iOS project automatically with ruby. After adding some files, I need modify the project.pbxproj file to save the changes in the project.
I've find a method to parse pbxproj file to json:
json = JSON.parse(`plutil -convert json -o - "#{filename}"`)
But after modifying the json, I would like to revert the process to save the changes in the pbxproj format. Does somebody know some way to do that?
There does not seem to be a way to directly convert the JSON back to the pbxproj file format, but Xcode will read xml1
formatted data as a valid project file.
Inspired by the accepted answer to Library to read/write pbxproj/xcodeproj files?, I decided to try running Xcode with the project's converted JSON. Xcode gave me an error saying that the project was invalid. When I converted the JSON to XML using plutil
, Xcode read it in just fine. When I observed the file contents, I saw that it stayed in XML when Xcode loaded it, but the moment I changed any value in Xcode, it rewrote the file using the pbxproj format. It seemed to rearrange the order of frameworks and libraries, but that may be because the original pbxproj I was dealing with was generated by Unity3D.
Here is a rough recipe for how to automatically convert, manipulate, and subsequently use the pbxproj file:
#!/bin/sh
plutil -convert json -r -o project.json -- project.pbxproj
ruby my_script.rb < project.json > project-modified.json
plutil -convert xml1 -o project-modified.xml -- project-modified.json
cp project-modified.xml project.pbxproj
I don't know if there is an easily-automatible way to have Xcode bring the modified file back to its original format, but it should be fine to invoke xcodebuild
or xcrun
while the file is in XML format.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With