lib/nanaimo/writer/pbxproj.rb in nanaimo-0.2.5 vs lib/nanaimo/writer/pbxproj.rb in nanaimo-0.2.6
- old
+ new
@@ -26,11 +26,11 @@
objects_by_isa = value.group_by { |_k, v| isa_for(v) }
objects_by_isa.each do |isa, kvs|
write_newline
output << "/* Begin #{isa} section */"
write_newline
- sort_dictionary(kvs).each do |k, v|
+ sort_dictionary(kvs, key_can_be_isa: false).each do |k, v|
write_dictionary_key_value_pair(k, v)
end
output << "/* End #{isa} section */"
write_newline
end
@@ -38,34 +38,37 @@
ensure
@newlines = n
end
def write_dictionary_key_value_pair(k, v)
- @objects_section = true if value_for(k) == 'objects'
+ # since the objects section is always at the top-level,
+ # we can avoid checking if we're starting the 'objects'
+ # section if we're further "indented" (aka deeper) in the project
+ @objects_section = true if indent == 1 && value_for(k) == 'objects'
+
super
end
- def sort_dictionary(dictionary)
+ def sort_dictionary(dictionary, key_can_be_isa: true)
hash = value_for(dictionary)
- hash.to_a.sort do |(k1, v1), (k2, v2)|
- v2_isa = isa_for(v2)
- v1_isa = v2_isa && isa_for(v1)
- comp = v1_isa <=> v2_isa
- next comp if !comp.zero? && v1_isa
-
- key1 = value_for(k1)
- key2 = value_for(k2)
- next -1 if key1 == 'isa'
- next 1 if key2 == 'isa'
- key1 <=> key2
+ hash.sort_by do |k, _v|
+ k = value_for(k)
+ if key_can_be_isa
+ k == 'isa' ? '' : k
+ else
+ k
+ end
end
end
def isa_for(dictionary)
dictionary = value_for(dictionary)
return unless dictionary.is_a?(Hash)
- isa = dictionary.values_at('isa', ISA).map(&method(:value_for)).compact.first
- isa && value_for(isa)
+ if isa = dictionary['isa']
+ value_for(isa)
+ elsif isa = dictionary[ISA]
+ value_for(isa)
+ end
end
def flat_dictionary?(dictionary)
case isa_for(dictionary)
when 'PBXBuildFile', 'PBXFileReference'