Automate work with Xcode projects using Ruby

Hi, Habr. There are a number of tasks for which there is no need to use the Xcode GUI. The execution time of such tasks can be reduced at least by the start time of Xcode. Saving time may seem insignificant when working with one project, but if there are many projects, the process of editing them can be very tedious. But most importantly, this approach opens up opportunities for the automation of work with projects.

I want to talk about a new tool whose purpose is to solve the described problem. XcodeProject is a Ruby language library that can be used to read and modify Xcode project data, as well as to build projects, archive and perform a number of tasks to automate working with them.



The library is a gem; you can install it from the terminal with the following command:

gem install xcodeproject

Beginning of work

The simplest example of using a library that displays all the goals of a project:

require 'rubygems'
require 'xcodeproject'
proj ='path/to/example.xcodeproj') do |target|

First you need to create an object of type XcodeProject :: Project:

proj ='path/to/example.xcodeproj')

Or you can find all Xcode projects in a given directory:

projs = XcodeProject::Project.find('path/to/dir')

Or according to the given pattern:

projs = XcodeProject::Project.find('*/**')

After the project object is created, you can read the data:

data =

Or change the data:

proj.change do |data|'example').config('Release').build_settings['GCC_VERSION'] = ''

Files, Groups, and Directories

We display all the groups of the highest level:

data.main_group.children.each do |child|

We display only the files from the given group:

group ='path/from/main_group')
group.files.each do |file|

You can get GroupPath groups at any time. GroupPath is the path to a group relative to the root of the project (the root of the project is the Main Group, a hidden group that is the parent of all project groups) or the current group.


Directories are groups that are represented on your file system. For them, you can get FilePath - the real path in the file system to the directory with which the object is associated.


You can add a group to the project by specifying GroupPath to it, relative to the root of the project:


Or relative to the current group:


To add a directory to the project, you must specify FilePath:

data.add_dir('group_path/to/parent', '/file_path/to/dir')

You can add a file to the project in the same way:

data.add_file('group_path/to/parent', '/file_path/to/file')

You can also delete files and groups from the project:



Getting the target object is simple:

target ='example')

After the file is added to the project, you can use it to build the target:

file = main_group.add_file('/file_path/to/file')

You can exclude a file from the assembly as follows:


Proct assembly

XcodeProject uses XcodeBuilder to build projects.

To get started, you need to create a rakefile. In a simple case, it might look like this:

require 'rubygems'
require 'xcodeproject'
proj ='path/to/example.xcodeproj')

A number of tasks are now available, including building a project. A complete list of tasks can be obtained by running rake -T.

$ rake -T
rake example:archive            # Creates an archive build of the specified target(s).
rake example:build              # Builds the specified target(s).
rake example:clean              # Cleans the build using the same build settings.
rake example:cleanbuild         # Builds the specified target(s) from a clean slate.

Change build parameters: do |t| = "libexample"
    t.configuration = "Release"

For more information on XcodeBuilder, click here .

In conclusion, a few words on the XcodeProject project. The project is under development and has only a little functionality presented in Xcode. The emergence of new opportunities will be associated with real portability in them. The project is available under the MIT license, you can use the project code as you wish. Any help in developing the project is appreciated.

Project address on github .

Also popular now: