From 2be5aabaffaaed0b92a8c2ac13c28d2aff5b267a Mon Sep 17 00:00:00 2001 From: Yonas Kolb Date: Mon, 23 Jul 2018 21:57:17 +1000 Subject: [PATCH] sort Projects and Frameworks groups at the end --- Sources/XcodeGenKit/PBXProjGenerator.swift | 19 +++++++++++-------- .../Project.xcodeproj/project.pbxproj | 4 ++-- .../SourceGeneratorTests.swift | 19 +++++++++++++++++++ 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/Sources/XcodeGenKit/PBXProjGenerator.swift b/Sources/XcodeGenKit/PBXProjGenerator.swift index 776a171b..b796d766 100644 --- a/Sources/XcodeGenKit/PBXProjGenerator.swift +++ b/Sources/XcodeGenKit/PBXProjGenerator.swift @@ -16,7 +16,7 @@ public class PBXProjGenerator { var targetAggregateObjects: [String: ObjectReference] = [:] var targetBuildFiles: [String: ObjectReference] = [:] var targetFileReferences: [String: String] = [:] - var topLevelGroups: Set = [] + var carthageFrameworksByPlatform: [String: Set] = [:] var frameworkFiles: [String] = [] @@ -80,6 +80,8 @@ public class PBXProjGenerator { ) ) + var derivedGroups: [ObjectReference] = [] + let mainGroup = createObject( id: "Project", PBXGroup( @@ -161,7 +163,7 @@ public class PBXProjGenerator { name: "Products" ) ) - topLevelGroups.insert(productGroup.reference) + derivedGroups.append(productGroup) if !carthageFrameworksByPlatform.isEmpty { var platforms: [PBXGroup] = [] @@ -199,15 +201,16 @@ public class PBXProjGenerator { name: "Frameworks" ) ) - topLevelGroups.insert(group.reference) + derivedGroups.append(group) } - for rootGroup in sourceGenerator.rootGroups { - topLevelGroups.insert(rootGroup) - } - - mainGroup.object.children = Array(topLevelGroups) + mainGroup.object.children = Array(sourceGenerator.rootGroups) sortGroups(group: mainGroup) + // add derived groups at the end + derivedGroups.forEach(sortGroups) + mainGroup.object.children += derivedGroups + .sorted { $0.object.nameOrPath.localizedStandardCompare($1.object.nameOrPath) == .orderedAscending } + .map { $0.reference } let projectAttributes: [String: Any] = ["LastUpgradeCheck": project.xcodeVersion] .merged(project.attributes) diff --git a/Tests/Fixtures/TestProject/Project.xcodeproj/project.pbxproj b/Tests/Fixtures/TestProject/Project.xcodeproj/project.pbxproj index 22249f1e..3df37908 100644 --- a/Tests/Fixtures/TestProject/Project.xcodeproj/project.pbxproj +++ b/Tests/Fixtures/TestProject/Project.xcodeproj/project.pbxproj @@ -559,10 +559,8 @@ G_8340618952527 /* Configs */, G_3234630030493 /* FileGroup */, G_4661500274312 /* Framework */, - G_1952740716080 /* Frameworks */, G_8268950006174 /* iMessage */, G_1646573205915 /* iMessage MessagesExtension */, - G_8620238527590 /* Products */, G_7189434949822 /* Resources */, G_6651250437419 /* StandaloneFiles */, G_3997550084026 /* StaticLibrary_ObjC */, @@ -571,6 +569,8 @@ FR_232605427418 /* Mintfile */, FR_257073931060 /* ResourceFolder */, FR_775316160345 /* SomeFile */, + G_1952740716080 /* Frameworks */, + G_8620238527590 /* Products */, ); indentWidth = 2; sourceTree = ""; diff --git a/Tests/XcodeGenKitTests/SourceGeneratorTests.swift b/Tests/XcodeGenKitTests/SourceGeneratorTests.swift index 106f2f0d..a1d7b7d8 100644 --- a/Tests/XcodeGenKitTests/SourceGeneratorTests.swift +++ b/Tests/XcodeGenKitTests/SourceGeneratorTests.swift @@ -459,6 +459,25 @@ class SourceGeneratorTests: XCTestCase { try expect(sourcesBuildPhase.files.count) == 1 } + + $0.it("derived directories are sorted last") { + let directories = """ + A: + - file.swift + P: + - file.swift + S: + - file.swift + """ + try createDirectories(directories) + + let target = Target(name: "Test", type: .application, platform: .iOS, sources: ["A", "P", "S"], dependencies: [Dependency(type: .carthage, reference: "Alamofire")]) + let project = Project(basePath: directoryPath, name: "Test", targets: [target]) + + let pbxProj = try project.generatePbxProj() + let groups = try pbxProj.getMainGroup().children.compactMap { pbxProj.objects.getFileElement(reference: $0)?.nameOrPath } + try expect(groups) == ["A", "P", "S", "Frameworks", "Products"] + } } } }