Skip to content

Commit

Permalink
Add an unambiguous sort order to build phase files
Browse files Browse the repository at this point in the history
  • Loading branch information
igor-makarov committed Jul 16, 2021
1 parent a5e4ee0 commit 26745f5
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
21 changes: 21 additions & 0 deletions lib/xcodeproj/project/object/build_phase.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,27 @@ def display_name
def ascii_plist_annotation
" #{display_name} "
end

# Sorts the to many attributes of the object according to the display
# name or the path.
#
# @param [Hash] options
# Not used.
#
# @return [void]
#
def sort(_options = nil)
files.sort! do |x, y|
result = File.basename(x.display_name.downcase, '.*') <=> File.basename(y.display_name.downcase, '.*')
if result.zero?
result = File.extname(x.display_name.downcase) <=> File.extname(y.display_name.downcase)
if result.zero? && x.file_ref.respond_to?(:full_path) && y.file_ref.respond_to?(:full_path)
result = x.file_ref.full_path.to_s.downcase <=> y.file_ref.full_path.to_s.downcase
end
end
result
end
end
end

#-----------------------------------------------------------------------#
Expand Down
66 changes: 66 additions & 0 deletions spec/project/object/build_phase_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,72 @@ module ProjectSpecs

#----------------------------------------#

describe '#sort' do
it 'sorts file references by name' do
files = %w(b a d c)
files.each do |filename|
file = @project.new_file(filename)
@build_phase.add_file_reference(file)
end
@build_phase.sort
@build_phase.files_references.map(&:path).should == %w(a b c d)
end

it 'sorts file references by full name if ambiguous' do
files = %w(f.b f.a f.d f.c)
files.each do |filename|
file = @project.new_file(filename)
@build_phase.add_file_reference(file)
end
@build_phase.sort
@build_phase.files_references.map(&:path).should == %w(f.a f.b f.c f.d)
end

it 'sorts file references by file paths if ambiguous' do
files = %w(
zh-Hant.lproj/InfoPlist.strings
he.lproj/InfoPlist.strings
ar.lproj/InfoPlist.strings
en.lproj/InfoPlist.strings
el.lproj/InfoPlist.strings
)

files.each do |filename|
file = @project.new_file(filename)
@build_phase.add_file_reference(file)
end
@build_phase.sort
@build_phase.files_references.map(&:path).should == %w(
ar.lproj/InfoPlist.strings
el.lproj/InfoPlist.strings
en.lproj/InfoPlist.strings
he.lproj/InfoPlist.strings
zh-Hant.lproj/InfoPlist.strings
)
end

it 'sorts file references by full file paths' do
groups = %w(b a d c)

parent_group = @project.new_group('parent', 'parent')

groups.each do |group_name|
group = parent_group.new_group(group_name, group_name)
file = group.new_file('file.json')
@build_phase.add_file_reference(file)
end
@build_phase.sort
@build_phase.files_references.map(&:full_path).map(&:to_s).should == %w(
parent/a/file.json
parent/b/file.json
parent/c/file.json
parent/d/file.json
)
end
end

#----------------------------------------#

it "returns the files it's associated with through its build files" do
file = @project.new_file('some/file')
@build_phase.add_file_reference(file)
Expand Down

0 comments on commit 26745f5

Please sign in to comment.