Files added via ruby xcodeproj have incorrect Full Path file reference

26 Views Asked by At

I'm trying to copy files from an external directory into Xcode via ruby script:

def find_or_create_group(project, group_path)
  group = project.main_group
  group_path.each do |name|
    group = group.find_subpath(name, true) || group.new_group(name)
  end
  group
end

base_folder = '/Path/To/MyApp/'
project_path = base_folder + 'MyApp.xcodeproj'
target_group_name = 'MyApp/Data/TEST'
source_folder = 'test_files_to_add'
destination_folder = base_folder + target_group_name

unless File.directory?(destination_folder)
  FileUtils.mkdir_p(destination_folder)
end

project = Xcodeproj::Project.open(project_path)
target = project.targets.find { |t| t.name == 'MyApp' }

resources_phase = target.build_phases.find { |phase|
  phase.is_a?(Xcodeproj::Project::Object::PBXResourcesBuildPhase)
}

group_path = ["MyApp", "Data", "TEST"]
data_group = find_or_create_group(project, trivia_group_path)


group = project.main_group.find_subpath(File.join(*group_path), true) #this results in "TEST"

# Copy JSON files from source_folder to the destination_folder within the Xcode project
Dir.glob("#{source_folder}/*.json").each do |file|
  puts "copying file: #{file}"
  FileUtils.cp(file, destination_folder) #this part works - the file ends up in the correct directory on disk

  file_reference = group.new_file(file)
  target.add_file_references([file_reference]) #this part is broken
end

project.save(project_path)

But the file ends up with a red color in Xcode and has the wrong "full path": /.../MyApp/Data/test_files_to_add/dummy.json

The correct path should be /.../MyApp/Data/dummy.json

How can I fix?

0

There are 0 best solutions below