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

Add -addNewFile method to PBXGroup to support adding file reference duplicates #410

Merged
merged 3 commits into from
May 2, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Changed

- **Breaking** Renamed module from `xcodeproj` to `XcodeProj` https://github.com/tuist/xcodeproj/pull/398 by @pepibumur.
- Add `override` flag to `PBXGroup.addFile(at:,sourceTree:,sourceRoot:)` https://github.com/tuist/xcodeproj/pull/410 by @mrylmz

### Added

Expand Down
12 changes: 8 additions & 4 deletions Sources/xcodeproj/Objects/Files/PBXGroup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -144,24 +144,28 @@ public extension PBXGroup {
///
/// - Parameters:
/// - filePath: path to the file.
/// - sourceTree: file sourceTree, default is `.group`
/// - sourceTree: file sourceTree, default is `.group`.
/// - sourceRoot: path to project's source root.
/// - override: flag to enable overriding of existing file references, default is `true`.
/// - Returns: new or existing file and its reference.
@discardableResult
func addFile(
at filePath: Path,
sourceTree: PBXSourceTree = .group,
sourceRoot: Path
sourceRoot: Path,
override: Bool = true
) throws -> PBXFileReference {
let projectObjects = try objects()
guard filePath.exists else {
throw XcodeprojEditingError.unexistingFile(filePath)
}
let groupPath = try fullPath(sourceRoot: sourceRoot)

if let existingFileReference = try projectObjects.fileReferences.first(where: {
let isFileReferencePathEqual: (Dictionary<PBXObjectReference, PBXFileReference>.Element) throws -> Bool = {
try filePath == $0.value.fullPath(sourceRoot: sourceRoot)
}) {
}

if let existingFileReference = try projectObjects.fileReferences.first(where: isFileReferencePathEqual), override {
if !childrenReferences.contains(existingFileReference.key) {
existingFileReference.value.path = groupPath.flatMap { filePath.relative(to: $0) }?.string
childrenReferences.append(existingFileReference.key)
Expand Down