preface

Script copying files, adding file references to Xcode, creating a new group, adding files to Build Phase, and adding -fno-objC-ARC identifiers can all be implemented through scripts.

The preparatory work

Install the Ruby library Xcodeproj

Understand a few concepts

  • Target: Specifies a command for the product and contains commands to build the product from files in the project.
  • Groups: Groups are a way of organizing files in Xcode. They have no effect on the file system. Creating or deleting a Group does not cause folders to be added or removed. Of course, if you select Move to Trash when you delete it, it is another word. However, Xcode9 already synchronizes the Group and Folder, and the New Group creates the corresponding Folder by default.
  • FileRef:FileRef is short for File Reference. When you Remove a File from Xcode, it will prompt you to Remove Reference. The Remove Reference option does not Remove the file to the trash, but simply removes the Reference to the file from the Xcode project file.

Start operation

1. Find *. Xcodeproj and open it

xcodeproj/project.rb

project = Xcodeproj::Project.open('./*.xcodeproj')

2. Access to the Target

target = project.targets.first

3. Create a Group

xcodeproj/project/object/group.rb

Group = project.main_group.find_subpath(group path)

4. Add the file to the Group

Xcodeproj/project/object/group. The rb to avoid repeated add references, determine whether before you add reference already exists

ifGroup.find_file_by_path (file_path) // The reference already existselse// Add reference file_ref = group.new_reference(file_path) endCopy the code

After this operation, the file is added to the Group and appears in the navigation bar of the project.

However, the file is not added to the Build Phases, and Xcode will tell you that the file cannot be found whether you compile it or use it as a resource. We also need to add this file to the Build Phases.

5. Add the file to Build Phases and set the -fno-objc-ARC flag for MRC

xcodeproj/project/object/native_target.rb

If the file is ARC, execute the command

target.add_file_references([file_ref])

If the file is MRC, execute the instruction

target.add_file_references([file_ref],'-fno-objc-arc')

6. Save the project

project.save

The complete code is as follows:

def add_file_reference_to_group(target, project, directory_path, to_group, need_mrc)
    ifto_group and File::exist? (directory_path)then
        Dir.foreach(directory_path) do |entry|
            ifentry ! ="."and entry ! =".."and entry ! =".DS_Store"
                pb_gen_file_path = entry
                if to_group.find_file_by_path(pb_gen_file_path)
                    puts pb_gen_file_path + " reference exist"
                else
                    file_reference = to_group.new_reference(pb_gen_file_path)
                    ifneed_mrc and entry.include? ("pbobjc.m")
                        target.add_file_references([file_reference],'-fno-objc-arc')
                    else
                        target.add_file_references([file_reference])
                    end
                end
            end
        end
        project.save
    end
end

project = Xcodeproj::Project.open('./*.xcodeproj') target = project.targets. First group = project.main_group. Find_subpath (path of the group to be added to) Add_file_reference_to_group (target, project, path of file that you want to add to the specified Group,true)
Copy the code

Refer to the article