How to remove a group from Xcode project programmatically?

1.7k views Asked by At

I need to manage an Xcode project from a batch process, partially remove certain groups.

As I understand, the only tool is xcodeproj. Or writing own pbxproj file parser.

So I have installed xcodeproj.

First of all, there is no reasonable documentation, that one that is new to it could understand it easily. I assume I need to write some rb file. And execute it as ruby xcodeproj_batch.rb

Here is what I have done. But this does not work. This script finds the needed group but it does not delete it.

xcodeproj_batch.rb:

require 'rubygems'
require 'xcodeproj'


project_path = '../TestProject/TestProject.xcodeproj'
project = Xcodeproj::Project.open(project_path)
groupToDelete = Xcodeproj::Project::PBXGroup

project.groups.each do |group|
    if group.name == 'Test'
        groupToDelete = group

        puts 'cleared: '+group.name

        groupToDelete.clear
    end
end

project.groups.delete(groupToDelete)
project.save(project_path)

I assume this script has errors. I don't have xcodeproj and ruby background. I'm trying here to remove the group in two different ways. What am I missing?

1

There are 1 answers

0
Nikita On BEST ANSWER

I have found the problem, I needed to use remove_from_project and not clear:

require 'rubygems'
require 'xcodeproj'


project_path = '../TestProject/TestProject.xcodeproj'
project = Xcodeproj::Project.open(project_path)
groupToDelete = nil

project.groups.each do |group|
    if group.name == 'Test'
        groupToDelete = group

        puts 'cleared: '+group.name
        # add break;
    end
end

groupToDelete.remove_from_project
project.save(project_path)