Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d99e448

Browse files
authored
Include folder (SPM packages) in group sorting logic (yonaskolb#1466)
1 parent 274ce73 commit d99e448

File tree

2 files changed

+96
-2
lines changed

2 files changed

+96
-2
lines changed

Sources/XcodeGenKit/PBXProjGenerator.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -631,8 +631,8 @@ public class PBXProjGenerator {
631631
}
632632

633633
if let order = groupOrdering?.order {
634-
let files = group.children.filter { $0 is PBXFileReference }
635-
var groups = group.children.filter { $0 is PBXGroup }
634+
let files = group.children.filter { !$0.isGroupOrFolder }
635+
var groups = group.children.filter { $0.isGroupOrFolder }
636636

637637
var filteredGroups = [PBXFileElement]()
638638

@@ -1626,6 +1626,10 @@ extension Platform {
16261626
}
16271627

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

16301634
public func getSortOrder(groupSortPosition: SpecOptions.GroupSortPosition) -> Int {
16311635
if type(of: self).isa == "PBXGroup" {

Tests/XcodeGenKitTests/PBXProjGeneratorTests.swift

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,96 @@ class PBXProjGeneratorTests: XCTestCase {
259259
.map { $0.nameOrPath }
260260
try expect(screenGroups) == ["mainScreen1.swift", "mainScreen2.swift", "View", "Presenter", "Interactor", "Entities", "Assembly"]
261261
}
262+
263+
$0.it("sorts SPM packages") {
264+
var options = SpecOptions()
265+
options.groupSortPosition = .top
266+
options.groupOrdering = [
267+
GroupOrdering(
268+
order: [
269+
"Sources",
270+
"Resources",
271+
"Tests",
272+
"Packages",
273+
"Support files",
274+
"Configurations",
275+
]
276+
),
277+
GroupOrdering(
278+
pattern: "Packages",
279+
order: [
280+
"FeatureA",
281+
"FeatureB",
282+
"Common",
283+
]
284+
),
285+
]
286+
287+
let directories = """
288+
Configurations:
289+
- file.swift
290+
Resources:
291+
- file.swift
292+
Sources:
293+
- MainScreen:
294+
- mainScreen1.swift
295+
- mainScreen2.swift
296+
- Assembly:
297+
- file.swift
298+
- Entities:
299+
- file.swift
300+
- Interactor:
301+
- file.swift
302+
- Presenter:
303+
- file.swift
304+
- View:
305+
- file.swift
306+
Support files:
307+
- file.swift
308+
Packages:
309+
- Common:
310+
- Package.swift
311+
- FeatureA:
312+
- Package.swift
313+
- FeatureB:
314+
- Package.swift
315+
Tests:
316+
- file.swift
317+
UITests:
318+
- file.swift
319+
"""
320+
try createDirectories(directories)
321+
322+
let target = Target(name: "Test", type: .application, platform: .iOS, sources: ["Configurations", "Resources", "Sources", "Support files", "Tests", "UITests"])
323+
let project = Project(
324+
basePath: directoryPath,
325+
name: "Test",
326+
targets: [target],
327+
packages: [
328+
"Common": .local(path: "Packages/Common", group: nil),
329+
"FeatureA": .local(path: "Packages/FeatureA", group: nil),
330+
"FeatureB": .local(path: "Packages/FeatureB", group: nil),
331+
],
332+
options: options
333+
)
334+
let projGenerator = PBXProjGenerator(project: project)
335+
336+
let pbxProj = try project.generatePbxProj()
337+
let group = try pbxProj.getMainGroup()
338+
339+
projGenerator.setupGroupOrdering(group: group)
340+
341+
let mainGroups = group.children.map { $0.nameOrPath }
342+
try expect(mainGroups) == ["Sources", "Resources", "Tests", "Packages", "Support files", "Configurations", "UITests", "Products"]
343+
344+
let packages = group.children
345+
.first { $0.nameOrPath == "Packages" }
346+
.flatMap { $0 as? PBXGroup }?
347+
.children
348+
.map(\.nameOrPath)
349+
350+
try expect(packages) == ["FeatureA", "FeatureB", "Common"]
351+
}
262352
}
263353
}
264354

0 commit comments

Comments
 (0)