Skip to content
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

Filter out empty auxiliary groups #6142

Merged
merged 1 commit into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ final class ProjectDescriptorGenerator: ProjectDescriptorGenerating {
graphTraverser: graphTraverser
)

groups.removeEmptyAuxiliaryGroups()

let xcodeProj = XcodeProj(workspace: workspace, pbxproj: pbxproj)
return ProjectDescriptor(
path: project.path,
Expand Down
18 changes: 18 additions & 0 deletions Sources/TuistGenerator/Generator/ProjectGroups.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ class ProjectGroups {
private let pbxproj: PBXProj
private let projectGroups: [String: PBXGroup]

private var auxiliaryGroups: [PBXGroup] {
// List of groups Xcode doesn't have by default if empty
//
// Note: The`Products` group is always included in the pbxproj but Xcode
// hides it in the UI if its empty.
[
frameworks,
cachedFrameworks,
]
}

// MARK: - Init

private init(
Expand Down Expand Up @@ -67,6 +78,13 @@ class ProjectGroups {
return group
}

func removeEmptyAuxiliaryGroups() {
for emptyGroup in auxiliaryGroups.filter(\.children.isEmpty) {
sortedMain.children.removeAll(where: { $0 == emptyGroup })
pbxproj.delete(object: emptyGroup)
}
}

static func generate(
project: Project,
pbxproj: PBXProj
Expand Down
51 changes: 51 additions & 0 deletions Tests/TuistGeneratorTests/Generator/ProjectGroupsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,49 @@ final class ProjectGroupsTests: XCTestCase {
])
}

func test_removeEmptyAuxiliaryGroups_removesEmptyGroups() throws {
// Given
let project = Project.test()
subject = ProjectGroups.generate(
project: project,
pbxproj: pbxproj
)

// When
subject.removeEmptyAuxiliaryGroups()

// Then
let paths = subject.sortedMain.children.map(\.nameOrPath)
XCTAssertEqual(paths, [
"Project",
"Products",
])
}

func test_removeEmptyAuxiliaryGroups_preservesNonEmptyGroups() throws {
// Given
let project = Project.test()
subject = ProjectGroups.generate(
project: project,
pbxproj: pbxproj
)

addFile(to: subject.frameworks)
addFile(to: subject.cachedFrameworks)

// When
subject.removeEmptyAuxiliaryGroups()

// Then
let paths = subject.sortedMain.children.map(\.nameOrPath)
XCTAssertEqual(paths, [
"Project",
"Frameworks",
"Cache",
"Products",
])
}

func test_targetFrameworks() throws {
subject = ProjectGroups.generate(
project: project,
Expand Down Expand Up @@ -213,4 +256,12 @@ final class ProjectGroupsTests: XCTestCase {
XCTAssertNil(main.tabWidth)
XCTAssertNil(main.wrapsLines)
}

// MARK: - Helpers

private func addFile(to group: PBXGroup) {
let file = PBXFileReference()
pbxproj.add(object: file)
group.children.append(file)
}
}