Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an unambiguous sort order to build phase files #839

Merged
merged 2 commits into from
Jul 18, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 build files of the phase 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