Skip to content

Commit

Permalink
Merge pull request #9 from g-Off/add-missing-duplicates
Browse files Browse the repository at this point in the history
remove references that are duplicates and are not referenced by build files
  • Loading branch information
g-Off authored Jun 7, 2019
2 parents 9a1e1d1 + 8331382 commit de01500
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ playground.xcworkspace
# Add this line if you want to avoid checking in source code from Swift Package Manager dependencies.
# Packages/
.build/
.swiftpm

# CocoaPods
#
Expand Down
23 changes: 23 additions & 0 deletions Sources/XcodeProject/Objects+Extensions/PBXGroup+FolderSync.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public extension PBXGroup {
func sync(recursive: Bool, target: PBXTarget? = nil) {
let target = target ?? parentProject?.targets.first
removeDuplicateFiles(recursive: recursive)
removeDuplicateFilesByPath(recursive: recursive)
addMissingFiles(recursive: recursive, target: target)
removeMissingFiles(recursive: recursive)
}
Expand All @@ -65,6 +66,27 @@ public extension PBXGroup {
}
}

private func removeDuplicateFilesByPath(recursive: Bool) {
var itemsByPath: [String: [PBXReference]] = [:]
for i in 0..<children.count {
let child = children[i]
if let path = child.path {
itemsByPath[path, default: []].append(child)
}
if recursive, let group = child as? PBXGroup {
group.removeDuplicateFilesByPath(recursive: recursive)
}
}
let duplicatePathItems = itemsByPath.filter { $0.value.count > 1 }
duplicatePathItems.forEach { (path, references) in
references.forEach { reference in
if reference.buildFiles.isEmpty {
remove(child: reference)
}
}
}
}

private func removeMissingFiles(recursive: Bool) {
var newChildren: [PBXReference] = []
children.forEach {
Expand Down Expand Up @@ -103,6 +125,7 @@ public extension PBXGroup {

private func addMissingFiles(recursive: Bool, target: PBXTarget?) {
guard let url = self.url else { return }

let childPathItems: [(String, PBXReference)] = children.compactMap {
guard let childURL = $0.url else { return nil }
return (childURL.path, $0)
Expand Down
5 changes: 4 additions & 1 deletion Sources/XcodeProject/Objects/PBXGlobalID.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@

import Foundation

public struct PBXGlobalID: RawRepresentable {
public struct PBXGlobalID: RawRepresentable, CustomStringConvertible, CustomDebugStringConvertible {
public var description: String { return rawValue }
public var debugDescription: String { return rawValue }

public let rawValue: String

public init(rawValue: String) {
Expand Down

0 comments on commit de01500

Please sign in to comment.