17

I am using an IDL which automatically generates source files for my xcode project. Does anyone know how I can automatically have the generated files added to the project? Currently I have to delete the current files from the project and add the new ones. This gets really annoying.

Using a folder reference works for the header files but xcode doesn't want to recognize any files in a folder reference as source files. Has anyone ever found a solution to this problem?

1
  • This is still hot, especially when you think about swagger and API code generation. Got any progress on this? Commented Oct 28, 2016 at 10:20

4 Answers 4

3

I also spent a couple of days writing a solution for this problem. Here is a ruby script that you can add to your project's target as a run script build phase. This was tested with XCode 3.2.4 and ruby 1.8.7.

For this to work you will need to install rb-appscript ruby gem. ( eg: sudo gem install rb-appscript )

There is little setup to do:

  1. First it needs a list of compile targets for added files.
  2. Second it expects a project group name which it will synchronize with it's associated disk folder ('objc' in my case) . Obviously this group needs to exist and to point to a real folder.

Here is the script:

require 'rubygems'
require 'appscript'

target_names = ['MinitSample'] # Put your target names here
group_name = 'objc'   # Name of Xcode project group where to add the generated files
project_name = ENV["PROJECT_NAME"]
project_dir = ENV["PROJECT_DIR"]

xcode = Appscript.app('Xcode')
project = xcode.projects[project_name]
group = project.groups[group_name]
group_path =  group.real_path.get
generated_files = Dir.glob(group_path+"/*.m")
missing_files = Array.new(generated_files)
group.item_references.get.each {|item|
  item_path = item.real_path.get
  missing_files.delete(item_path)
  if ! generated_files.include?(item_path) then
    group.file_references[item.name.get].delete
    puts "Deleting #{File.basename(item_path)} from group #{group_name}, as it is not in generated files list"
  end
}
if missing_files.empty? then
  puts "There are no new files to add. "
  exit
end
# holds the compile targets for generated files
targets = []
project.targets.get.each{ |target|
  targets << target if target_names.include?(target.name.get)
}
if targets.empty? then
  puts "Unable to find #{target_names.inspect} in project targets ! Aborting"
  exit
end
missing_files.each{ |path|
  file_name = File.basename(path)
  msg = "Adding #{file_name} to group #{group_name} and to targets: "
  item = xcode.make(:new => :file_reference, 
                    :at => group.item_references.after, 
                    :with_properties => {:full_path => path,
                    :name => file_name})
  targets.each {|target|
      xcode.add(item,{:to=>target})
      msg += target.name.get
    }
    puts msg
}
Sign up to request clarification or add additional context in comments.

2 Comments

Is this approach now out of date? It looks like you have deleted the minit repository.
I've deleted the minit reference as it is no longer updated. This approach was designed for a specific version of xcode as mentioned. Newer versions might not work.
2

A good idea can be found here: https://stackoverflow.com/a/17894337/354018

Basically, you import the generated .m files into a known source file that is added to the compile phase.

Comments

0

I've added a radar (details at http://www.openradar.me/radar?id=4885314376040448) asking for support for this.

Comments

0

Look at xcode-editor project, an API for manipulating Xcode project files.

You can add files to a project, specify which target it belongs, add xib files and frameworks.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.