Skip to content

sort project references as xcode16 #867

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
20 changes: 20 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,23 @@ 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 {
/// The project references are sorted alphabetically based on the name of the project it's being referenced.
project.projectReferences = project.projectReferences.sorted(by: { lhs, rhs in
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Speakus in which scenario a project contains more than one project reference. Would you mind adding a fixture under Fixtures so that we can add some tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hi. I changed company where I work and It's not so easy for me anymore to provide some example.

@mikhailmulyar may you help with Fixtures?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, will try to provide some example

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here it is, I've added example with wrong ordering and some tests for checking.
mikhailmulyar@4f282f1

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mikhailmulyar would you mind completing this PR? It'll require closing this one and opening a new one.

let lProjectRef = lhs["ProjectRef"]!
let lFile: PBXFileElement = lProjectRef.getObject()!
let rProjectRef = rhs["ProjectRef"]!
let rFile: PBXFileElement = rProjectRef.getObject()!
let lName = lFile.name!
let rName = rFile.name!
return lName.compare(rName, options: .caseInsensitive) == .orderedAscending
})
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,8 @@ class PBXProjEncoderTests: XCTestCase {
line = lines.validate(line: "/* End PBXFileSystemSynchronizedBuildFileExceptionSet section */", after: line)
}

// MARK: - Projects

// MARK: - Build phases

func test_build_phase_sources_unsorted_when_iOSProject() throws {
Expand Down
Loading