Skip to content

Commit de01500

Browse files
authored
Merge pull request #9 from g-Off/add-missing-duplicates
remove references that are duplicates and are not referenced by build files
2 parents 9a1e1d1 + 8331382 commit de01500

3 files changed

Lines changed: 28 additions & 1 deletion

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ playground.xcworkspace
3636
# Add this line if you want to avoid checking in source code from Swift Package Manager dependencies.
3737
# Packages/
3838
.build/
39+
.swiftpm
3940

4041
# CocoaPods
4142
#

Sources/XcodeProject/Objects+Extensions/PBXGroup+FolderSync.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public extension PBXGroup {
4242
func sync(recursive: Bool, target: PBXTarget? = nil) {
4343
let target = target ?? parentProject?.targets.first
4444
removeDuplicateFiles(recursive: recursive)
45+
removeDuplicateFilesByPath(recursive: recursive)
4546
addMissingFiles(recursive: recursive, target: target)
4647
removeMissingFiles(recursive: recursive)
4748
}
@@ -65,6 +66,27 @@ public extension PBXGroup {
6566
}
6667
}
6768

69+
private func removeDuplicateFilesByPath(recursive: Bool) {
70+
var itemsByPath: [String: [PBXReference]] = [:]
71+
for i in 0..<children.count {
72+
let child = children[i]
73+
if let path = child.path {
74+
itemsByPath[path, default: []].append(child)
75+
}
76+
if recursive, let group = child as? PBXGroup {
77+
group.removeDuplicateFilesByPath(recursive: recursive)
78+
}
79+
}
80+
let duplicatePathItems = itemsByPath.filter { $0.value.count > 1 }
81+
duplicatePathItems.forEach { (path, references) in
82+
references.forEach { reference in
83+
if reference.buildFiles.isEmpty {
84+
remove(child: reference)
85+
}
86+
}
87+
}
88+
}
89+
6890
private func removeMissingFiles(recursive: Bool) {
6991
var newChildren: [PBXReference] = []
7092
children.forEach {
@@ -103,6 +125,7 @@ public extension PBXGroup {
103125

104126
private func addMissingFiles(recursive: Bool, target: PBXTarget?) {
105127
guard let url = self.url else { return }
128+
106129
let childPathItems: [(String, PBXReference)] = children.compactMap {
107130
guard let childURL = $0.url else { return nil }
108131
return (childURL.path, $0)

Sources/XcodeProject/Objects/PBXGlobalID.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77

88
import Foundation
99

10-
public struct PBXGlobalID: RawRepresentable {
10+
public struct PBXGlobalID: RawRepresentable, CustomStringConvertible, CustomDebugStringConvertible {
11+
public var description: String { return rawValue }
12+
public var debugDescription: String { return rawValue }
13+
1114
public let rawValue: String
1215

1316
public init(rawValue: String) {

0 commit comments

Comments
 (0)