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
Changes from 1 commit
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
45 changes: 45 additions & 0 deletions Sources/xcodeproj/Objects/Files/PBXGroup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,51 @@ public extension PBXGroup {
}
}

/// Adds file at the give path to the project.
///
/// - Parameters:
/// - filePath: path to the file.
/// - sourceTree: file sourceTree, default is `.group`
/// - sourceRoot: path to project's source root.
/// - Returns: new file and its reference.
@discardableResult
func addNewFile(
at filePath: Path,
sourceTree: PBXSourceTree = .group,
sourceRoot: Path
) throws -> PBXFileReference {
let projectObjects = try objects()
guard filePath.exists else {
throw XcodeprojEditingError.unexistingFile(filePath)
}
let groupPath = try fullPath(sourceRoot: sourceRoot)

let path: String?
switch sourceTree {
case .group:
path = groupPath.map { filePath.relative(to: $0) }?.string
case .sourceRoot:
path = filePath.relative(to: sourceRoot).string
case .absolute:
path = filePath.string
default:
path = nil
}
let fileReference = PBXFileReference(
sourceTree: sourceTree,
name: filePath.lastComponent,
explicitFileType: filePath.extension.flatMap(Xcode.filetype),
lastKnownFileType: filePath.extension.flatMap(Xcode.filetype),
path: path
)
projectObjects.add(object: fileReference)
fileReference.parent = self
if !childrenReferences.contains(fileReference.reference) {
childrenReferences.append(fileReference.reference)
}
return fileReference
}

/// Adds file at the give path to the project or returns existing file and its reference.
///
/// - Parameters:
Expand Down