btype
btype

Reputation: 1576

Create Xcode Project with Ruby XcodeProj

I had this idea of making a command line utility similar to the rails setup application. It should automatically create a Xcode project, setup unit test, setup Frank, install most used Cocoapods and setup a project structure.

There is no such thing at the moment and I would really like to have it and open source it. I struggled around the other questions here yesterday but found nothing up to date.

I came up with the idea using XcodeProj which is used by CocoaPods to generate a project containing the Pods. So it should not be impossible to do. I also found XcodeProject but this seems to be read-only.

Can someone (maybe of the Cocoapods developers) give me a hint where to start, because the Xcodeproj gem is very undocumented.

Upvotes: 3

Views: 6494

Answers (1)

Fabio
Fabio

Reputation: 3516

To the best place to start to use Xcodeproj is the Xcodeproj::Project class.

Once you open a project the Xcodeproj API allows to edit all the known attributes easily. However you need to keep in mind the following concepts:

  • If an attribute is a String you can edit it directly.
  • If an attribute stores an object it is necessary to ask the project to create a new one so it is has the chance to assign an UUID.
  • If you are editing an attribute which stores a list of objects it is safe only to use the method exposed by the ObjectList.
  • Xcodeproj performs type checking when you assign values to attributes.

The following lightly tested code should help to get you started by creating a new project named Test with a target named App for a deployment target of iOS 6.0 that adds Class.h and Class.m file to the project, ensuring Class.m is included in the target.

require 'xcodeproj'

proj = Xcodeproj::Project.new
app_target = proj.new_target(:application, 'App', :ios, '6.0')
header_ref = proj.main_group.new_file('./Class.h')
implm_ref = proj.main_group.new_file('./Class.m')
app_target.add_file_references([implm_ref])

proj.save_as('Test.xcodeproj')

Please let us know, opening an issue, which parts of the documentation you find confusing. For more karma use a pull request.

Upvotes: 16

Related Questions