2

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.

Adam Jakiela
  • 2,188
  • 7
  • 30
  • 48
  • Have you managed to get this to work? I'm interested in doing something similar. – trusk Nov 12 '17 at 05:49
  • @AlexBartiş The library I was trying to integrate us updated so it was a non issue. I have not tried any of the suggested answers. Thank you everyone for your help. – Adam Jakiela Nov 13 '17 at 19:46
  • I've found the solution and updated with the correct answer below. – trusk Nov 14 '17 at 12:20

2 Answers2

3

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

trusk
  • 1,634
  • 2
  • 18
  • 32
1

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. :)

Iulian Onofrei
  • 9,188
  • 10
  • 67
  • 113
Scott McKenzie
  • 16,052
  • 8
  • 45
  • 70
  • I did this and also got the same behavior as stated by @adam above: new target is created but old one is destroyed and and the project files disappear. – trusk Nov 12 '17 at 08:14