Creating New iOS Build Target with Xcodeproj

2.2k views Asked by At

I am trying to create a new iPhone build target pragmatically with the Ruby gem Xcodeproj. Between my lack of Ruby knowlege and the poor documentation with Xcodeproj, am facing some issues. Here is my code:

require 'rubygems'
require 'xcodeproj'

#get target name from args
scheme_name = ARGV[0]
iosProjectDir = ARGV[1]

# Open the existing Xcode project
project_file = iosProjectDir + '/UserApp.xcodeproj'
project = Xcodeproj::Project.new(project_file)

#Add the target to the project. Are these parameters correct?
app_target = project.new_target(:application, scheme_name, :ios, "8.0")

# Save the project file
project.save(project_file)

When I run this code, a new scheme is made in the XCode project. However, it corrupts all my other build targets and almost all of the projects files disappear. I have to revert the project to get them back. Could this code be corrupting the iOS project?

The only documentation I have found regararding adding a new target is here. I am a bit confused by the optional variable product_group.

Any ideas as to what I am doing wrong here? I am also open to other methods of adding the target progmatically.

2

There are 2 answers

1
Scott McKenzie On

I can't say why yours doesn't work, but I was able to achieve this with:

require 'xcodeproj'

project_name = "Test"
project_path = "./Test.xcodeproj"

project = Xcodeproj::Project.new(project_path)

project.new_target(:application, "Test", :ios, "8.0")
project.save()

Which makes me think it could be the use of ARGV to input the scheme name or not saving before creation.

Let me know how you get along. :)

0
trusk On

Ok, got it to work

require 'rubygems'
require 'xcodeproj'

#get target name from args
scheme_name = ARGV[0]
iosProjectDir = ARGV[1]
iosProjName = ARGV[2]

# Open the existing Xcode project
project_file = iosProjName + '.xcodeproj'
project = Xcodeproj::Project.open(project_file)
project.save(project_file)

#Add the target to the project. Are these parameters correct?
app_target = project.new_target(:application, scheme_name, :ios, "11.0")

# Save the project file
project.save(project_file)

So the issue was Project.open and not Project.new