Skip to content

Commit

Permalink
Include folder (SPM packages) in group sorting logic (#1466)
Browse files Browse the repository at this point in the history
  • Loading branch information
jflan-dd authored May 17, 2024
1 parent 274ce73 commit d99e448
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 2 deletions.
8 changes: 6 additions & 2 deletions Sources/XcodeGenKit/PBXProjGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -631,8 +631,8 @@ public class PBXProjGenerator {
}

if let order = groupOrdering?.order {
let files = group.children.filter { $0 is PBXFileReference }
var groups = group.children.filter { $0 is PBXGroup }
let files = group.children.filter { !$0.isGroupOrFolder }
var groups = group.children.filter { $0.isGroupOrFolder }

var filteredGroups = [PBXFileElement]()

Expand Down Expand Up @@ -1626,6 +1626,10 @@ extension Platform {
}

extension PBXFileElement {
/// - returns: `true` if the element is a group or a folder reference. Likely an SPM package.
var isGroupOrFolder: Bool {
self is PBXGroup || (self as? PBXFileReference)?.lastKnownFileType == "folder"
}

public func getSortOrder(groupSortPosition: SpecOptions.GroupSortPosition) -> Int {
if type(of: self).isa == "PBXGroup" {
Expand Down
90 changes: 90 additions & 0 deletions Tests/XcodeGenKitTests/PBXProjGeneratorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,96 @@ class PBXProjGeneratorTests: XCTestCase {
.map { $0.nameOrPath }
try expect(screenGroups) == ["mainScreen1.swift", "mainScreen2.swift", "View", "Presenter", "Interactor", "Entities", "Assembly"]
}

$0.it("sorts SPM packages") {
var options = SpecOptions()
options.groupSortPosition = .top
options.groupOrdering = [
GroupOrdering(
order: [
"Sources",
"Resources",
"Tests",
"Packages",
"Support files",
"Configurations",
]
),
GroupOrdering(
pattern: "Packages",
order: [
"FeatureA",
"FeatureB",
"Common",
]
),
]

let directories = """
Configurations:
- file.swift
Resources:
- file.swift
Sources:
- MainScreen:
- mainScreen1.swift
- mainScreen2.swift
- Assembly:
- file.swift
- Entities:
- file.swift
- Interactor:
- file.swift
- Presenter:
- file.swift
- View:
- file.swift
Support files:
- file.swift
Packages:
- Common:
- Package.swift
- FeatureA:
- Package.swift
- FeatureB:
- Package.swift
Tests:
- file.swift
UITests:
- file.swift
"""
try createDirectories(directories)

let target = Target(name: "Test", type: .application, platform: .iOS, sources: ["Configurations", "Resources", "Sources", "Support files", "Tests", "UITests"])
let project = Project(
basePath: directoryPath,
name: "Test",
targets: [target],
packages: [
"Common": .local(path: "Packages/Common", group: nil),
"FeatureA": .local(path: "Packages/FeatureA", group: nil),
"FeatureB": .local(path: "Packages/FeatureB", group: nil),
],
options: options
)
let projGenerator = PBXProjGenerator(project: project)

let pbxProj = try project.generatePbxProj()
let group = try pbxProj.getMainGroup()

projGenerator.setupGroupOrdering(group: group)

let mainGroups = group.children.map { $0.nameOrPath }
try expect(mainGroups) == ["Sources", "Resources", "Tests", "Packages", "Support files", "Configurations", "UITests", "Products"]

let packages = group.children
.first { $0.nameOrPath == "Packages" }
.flatMap { $0 as? PBXGroup }?
.children
.map(\.nameOrPath)

try expect(packages) == ["FeatureA", "FeatureB", "Common"]
}
}
}

Expand Down

0 comments on commit d99e448

Please sign in to comment.