Is it possible to sort the Compile Sources list in the Build Phases section of an Xcode project?

前端 未结 5 1189
梦如初夏
梦如初夏 2021-02-02 11:04

I want to sort the files in the \'Compile Sources\' section of my Xcode project according to their names. Is it possible?

5条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-02 11:55

    The accepted solution works fine, but it requires manual steps(open the project file, find the section for the target that you want etc.) so it is a little cumbersome and it can not be automated if you need to keep the section sorted each time you perform a build or commit. I faced with the same problem and I created a ruby script to sort these sections. The script sorts the 'Compile Sources', 'Copy Bundle Resources’ and all the 'Copy files' sections under Build Phase for a specified or all the targets.

    #!/usr/bin/env ruby
    require 'xcodeproj'
    require 'set'
    
    project_file, target_name = ARGV
    
    # open the project
    project = Xcodeproj::Project.open(project_file)
    
    # find the target
    targets_to_sort = project.native_targets.select { |x| x.name == target_name || target_name.nil? }
    
    phases_to_sort = [Xcodeproj::Project::Object::PBXSourcesBuildPhase, Xcodeproj::Project::Object::PBXCopyFilesBuildPhase, Xcodeproj::Project::Object::PBXResourcesBuildPhase]
    
    targets_to_sort.each do |target|
      puts "sorting files for target #{target.name}"
      phases_to_sort.each do |phase_to_sort|
        target.build_phases.select { |x| x.class == phase_to_sort }.each do |phase|
          phase.files.sort! { |l, r| l.display_name <=> r.display_name }
        end
      end
    end
    
    puts 'saving project'
    project.save
    

    To sort all targets:

    ./sort_sources.rb MyProject.xcodeproj
    

    Or to sort only one target:

    ./sort_sources.rb MyProject.xcodeproj My_Target
    

    It requires the gem xcodeproj:

    gem install xcodeproj
    

提交回复
热议问题