Skip to content

Commit

Permalink
IOSC-12423 do sort projectReferences as Xcode 16 do
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxim Kholyavkin committed Oct 10, 2024
1 parent 013cc79 commit a22d0e2
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions Sources/XcodeProj/Objects/Project/PBXProjEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ final class PBXProjEncoder {
sort(buildPhases: proj.objects.resourcesBuildPhases, outputSettings: outputSettings)
sort(buildPhases: proj.objects.sourcesBuildPhases, outputSettings: outputSettings)
sort(navigatorGroups: proj.objects.groups, outputSettings: outputSettings)
sortProjectReferences(for: proj.projects, outputSettings: outputSettings)

var output = [String]()
var stateHolder = StateHolder()
Expand Down Expand Up @@ -468,4 +469,44 @@ final class PBXProjEncoder {
navigatorGroups.values.forEach { $0.children = $0.children.sorted(by: sort) }
}
}

private func sortProjectReferences(for projects: [PBXProject], outputSettings: PBXOutputSettings) {
guard outputSettings.projReferenceFormat == .xcode else {
return
}

for project in projects {
do {
project.projectReferences = try project.projectReferences.sorted(by: { lhs, rhs in
guard let lProjectRef = lhs["ProjectRef"] else {
throw Errors.unexpectedPbxProj()
}
guard let lFile: PBXFileElement = lProjectRef.getObject() else {
throw Errors.unexpectedPbxProj()
}
guard let rProjectRef = rhs["ProjectRef"] else {
throw Errors.unexpectedPbxProj()
}
guard let rFile: PBXFileElement = rProjectRef.getObject() else {
throw Errors.unexpectedPbxProj()
}
guard let lName = lFile.name else {
throw Errors.unexpectedPbxProj()
}
guard let rName = rFile.name else {
throw Errors.unexpectedPbxProj()
}

return lName.compare(rName, options: .caseInsensitive) == .orderedAscending
})
} catch {
print("Unable to sort as Xcode 16 do: \(error)")
fatalError("\(error)")
}
}
}
}

private enum Errors: Error {
case unexpectedPbxProj(_ id: Int = #line)
}

0 comments on commit a22d0e2

Please sign in to comment.