Skip to content

Commit

Permalink
Just extract existing functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
jcrookebumble committed Mar 31, 2022
1 parent e3b1fe1 commit 9f543f7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 170 deletions.
48 changes: 27 additions & 21 deletions Sources/XcodeProj/Objects/Files/PBXGroup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,16 +138,7 @@ public extension PBXGroup {
/// - sourceRoot: path to project's source root.
/// - Returns: an existing reference to that file, or `nil` if not found
func file(with path: Path, sourceRoot: Path) -> PBXFileReference? {
func absolutePath(for fileRef: PBXFileElement) -> Path? {
try? fileRef.fullPath(sourceRoot: sourceRoot)
}

guard let groupAbsPath = absolutePath(for: self) else { return nil }
let fileNormalizedAbsolutePath = (groupAbsPath + path).normalize()
return children.first {
guard let candidateAbsPath = absolutePath(for: $0) else { return false }
return candidateAbsPath.normalize() == fileNormalizedAbsolutePath
} as? PBXFileReference
try? existingFile(objects: nil, with: path, sourceRoot: sourceRoot)?.value
}

/// Creates a group with the given name and returns it.
Expand Down Expand Up @@ -211,17 +202,11 @@ public extension PBXGroup {
}
let groupPath = try fullPath(sourceRoot: sourceRoot)

if override, let existingFileReference = try projectObjects.fileReferences.first(where: {
// Optimization: compare lastComponent before fullPath compare
guard let fileRefPath = $0.value.path else {
return try filePath == $0.value.fullPath(sourceRoot: sourceRoot)
}
let fileRefLastPathComponent = fileRefPath.split(separator: "/").last!
if filePath.lastComponent == fileRefLastPathComponent {
return try filePath == $0.value.fullPath(sourceRoot: sourceRoot)
}
return false
}) {
if override, let existingFileReference = try existingFile(
objects: projectObjects,
with: filePath,
sourceRoot: sourceRoot
) {
if !childrenReferences.contains(existingFileReference.key) {
existingFileReference.value.path = groupPath.flatMap { filePath.relative(to: $0) }?.string
childrenReferences.append(existingFileReference.key)
Expand Down Expand Up @@ -257,3 +242,24 @@ public extension PBXGroup {
return fileReference
}
}

private extension PBXGroup {

func existingFile(
objects: PBXObjects?,
with filePath: Path,
sourceRoot: Path
) throws -> (key: PBXObjectReference, value: PBXFileReference)? {
try (objects ?? self.objects()).fileReferences.first(where: {
// Optimization: compare lastComponent before fullPath compare
guard let fileRefPath = $0.value.path else {
return try filePath == $0.value.fullPath(sourceRoot: sourceRoot)
}
let fileRefLastPathComponent = fileRefPath.split(separator: "/").last!
if filePath.lastComponent == fileRefLastPathComponent {
return try filePath == $0.value.fullPath(sourceRoot: sourceRoot)
}
return false
})
}
}
149 changes: 0 additions & 149 deletions Tests/XcodeProjTests/Objects/Files/PBXGroupPathSearchTests.swift

This file was deleted.

0 comments on commit 9f543f7

Please sign in to comment.