From 92309742b0e2a5fa64fb00fb7e21820e4e6e6821 Mon Sep 17 00:00:00 2001 From: John Flanagan Date: Thu, 11 Apr 2024 09:52:15 -0500 Subject: [PATCH] Include folder (SPM packages) in group sorting logic --- Sources/XcodeGenKit/PBXProjGenerator.swift | 8 +- .../PBXProjGeneratorTests.swift | 90 +++++++++++++++++++ 2 files changed, 96 insertions(+), 2 deletions(-) diff --git a/Sources/XcodeGenKit/PBXProjGenerator.swift b/Sources/XcodeGenKit/PBXProjGenerator.swift index 82e87f2c..73ea0339 100644 --- a/Sources/XcodeGenKit/PBXProjGenerator.swift +++ b/Sources/XcodeGenKit/PBXProjGenerator.swift @@ -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]() @@ -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" { diff --git a/Tests/XcodeGenKitTests/PBXProjGeneratorTests.swift b/Tests/XcodeGenKitTests/PBXProjGeneratorTests.swift index 7ffa5242..b067fa56 100644 --- a/Tests/XcodeGenKitTests/PBXProjGeneratorTests.swift +++ b/Tests/XcodeGenKitTests/PBXProjGeneratorTests.swift @@ -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"] + } } }