xcode: track paths on XcodeProject (#388)
- Commit
f1b455b4c52305fb073e44f191b01c9e9db19784- Author
- Kabir Oberai <[email protected]>
- When
- 2026-10-28 23:22 UTC (1 month ago)
- Parents
c34e924
## What does this PR do? An XcodeProject/XcodeWorkspace now knows its own location on disk, which is important when we start using these types to build PIF. ## How was it tested? - [x] Used in future PRs to build various Xcode projects ## AI tool usage How much of this PR was AI-assisted? (check one) - [ ] **0** - No AI was used to write code - [x] **1** - I was assisted by AI. I reviewed the finished result. - [ ] **2** - I set the AI going and left it to it; nobody has read the result - no review, or AI review only <!-- If an AI agent is filling this in: declare the level honestly, and open as a draft if it is #2. Do not lower the declared level to get the PR reviewed. --> <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit * **Improvements** * Projects and workspaces now retain their associated locations and save directly to those locations. * Generated project files preserve the configured project directory path, helping keep file references consistent when projects are written or reopened. * Projects and workspaces can be associated with a new location while retaining their existing contents. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
Diff
Sources/PackLib/XcodePacker.swift
diff --git a/Sources/PackLib/XcodePacker.swift b/Sources/PackLib/XcodePacker.swiftindex d679ea7..ecac7d4 100644--- a/Sources/PackLib/XcodePacker.swift+++ b/Sources/PackLib/XcodePacker.swift@@ -220,16 +220,16 @@ public struct XcodePacker { importedProducts: [], ) - let project = XcodeProject(project: schema) let projectURL = projectDir.appending(path: "\(plan.app.product).xcodeproj")- try project.write(to: projectURL)+ let project = XcodeProject(project: schema, url: projectURL)+ try project.write() + let workspaceURL = xtoolDir.appending(path: "\(plan.app.product).xcworkspace") let workspace = XcodeWorkspace(children: [ .file(XcodeWorkspace.Location(base: .container, path: "..")), .file(XcodeWorkspace.Location(base: .group, path: ".xtool-tmp/\(projectURL.lastPathComponent)"))- ])- let workspaceURL = xtoolDir.appending(path: "\(plan.app.product).xcworkspace")- try workspace.write(to: workspaceURL)+ ], url: workspaceURL)+ try workspace.write() return workspaceURL }
Sources/XcodeProjects/XCSchema+PBXProj.swift
diff --git a/Sources/XcodeProjects/XCSchema+PBXProj.swift b/Sources/XcodeProjects/XCSchema+PBXProj.swiftindex 5849060..42c971a 100644--- a/Sources/XcodeProjects/XCSchema+PBXProj.swift+++ b/Sources/XcodeProjects/XCSchema+PBXProj.swift@@ -2,9 +2,10 @@ import Foundation private import XcodeProj extension XCSchema.Project {- public func pbxprojRepresentation(name: String) throws -> Data {+ public func pbxprojRepresentation(name: String, projectDirPath: String = "") throws -> Data { let xcprojData = try self.jsonRepresentation() let pbxproj = try PBXProj(xcprojData: xcprojData, projectName: name)+ try pbxproj.rootProject()?.projectDirPath = projectDirPath // dataRepresentation should never return nil return try pbxproj.dataRepresentation() ?? Data() }
Sources/XcodeProjects/XcodeProject.swift
diff --git a/Sources/XcodeProjects/XcodeProject.swift b/Sources/XcodeProjects/XcodeProject.swiftindex 99cb1df..1a8f449 100644--- a/Sources/XcodeProjects/XcodeProject.swift+++ b/Sources/XcodeProjects/XcodeProject.swift@@ -1,4 +1,5 @@ import Foundation+private import XcodeProj @_exported import XcodeProjectFormat // This library is a lightweight layer on top of the XcodeProjectFormat@@ -16,13 +17,31 @@ public struct XcodeProject: Equatable, Sendable { public var project: XCSchema.Project public var format: Format+ /// The .xcodeproj bundle's location, including for projects constructed in memory.+ public var url: URL+ /// For pbxproj projects, an explicit source root.+ public var projectDirPath = ""++ public var sourceRoot: URL {+ if projectDirPath.isEmpty {+ url.deletingLastPathComponent()+ } else {+ URL(+ filePath: projectDirPath,+ directoryHint: .isDirectory,+ relativeTo: url.deletingLastPathComponent(),+ ).standardizedFileURL+ }+ } public init( project: XCSchema.Project,+ url: URL, format: Format = .pbxproj, ) { self.project = project self.format = format+ self.url = url.standardizedFileURL.resolvingSymlinksInPath() } /// Reads the project from the given `.xcodeproj` path.@@ -30,6 +49,8 @@ public struct XcodeProject: Equatable, Sendable { guard url.pathExtension == "xcodeproj" else { throw Errors.notAnXcodeproj(url.path) }+ let url = url.standardizedFileURL.resolvingSymlinksInPath()+ self.url = url let xcprojURL = url.appending(path: "project.xcproj") lazy var pbxprojURL = url.appending(path: "project.pbxproj") if FileManager.default.fileExists(atPath: xcprojURL.path) {@@ -38,7 +59,9 @@ public struct XcodeProject: Equatable, Sendable { self.format = .xcproj } else if FileManager.default.fileExists(atPath: pbxprojURL.path) { let data = try Data(contentsOf: pbxprojURL)- self.project = try XCSchema.Project(pbxprojRepresentation: data)+ let pbxproj = try PBXProj(data: data)+ self.project = try XCSchema.Project(jsonRepresentation: pbxproj.xcprojData())+ self.projectDirPath = try pbxproj.rootProject()?.projectDirPath ?? "" self.format = .pbxproj } else { throw Errors.missingProjectFile(url.path)@@ -52,27 +75,39 @@ public struct XcodeProject: Equatable, Sendable { return copy } - /// Writes the project to the given `.xcodeproj` path.- public func write(to url: URL) throws {+ /// Returns a copy at a new bundle location.+ public func withURL(_ url: URL) -> Self {+ var copy = self+ copy.url = url.standardizedFileURL.resolvingSymlinksInPath()+ return copy+ }++ /// Writes the project to its current `.xcodeproj` URL.+ public func write() throws { try? FileManager.default.createDirectory(at: url, withIntermediateDirectories: true) switch format { case .pbxproj:- try project- .pbxprojRepresentation(name: url.deletingPathExtension().lastPathComponent)- .write(to: url.appendingPathComponent("project.pbxproj"))+ try project.pbxprojRepresentation(+ name: url.deletingPathExtension().lastPathComponent,+ projectDirPath: projectDirPath,+ ).write(to: url.appendingPathComponent("project.pbxproj")) case .xcproj:+ guard projectDirPath.isEmpty else {+ throw Errors.xcprojWithExplicitSourceRoot+ } try project .jsonRepresentation() .write(to: url.appendingPathComponent("project.xcproj")) } - try XcodeWorkspace(children: [.file(.current)])- .write(to: url.appendingPathComponent("project.xcworkspace"))+ let workspaceURL = url.appendingPathComponent("project.xcworkspace")+ try XcodeWorkspace(children: [.file(.current)], url: workspaceURL).write() } public enum Errors: Error { case notAnXcodeproj(String) case missingProjectFile(String)+ case xcprojWithExplicitSourceRoot } }
Sources/XcodeProjects/XcodeWorkspace+XML.swift
diff --git a/Sources/XcodeProjects/XcodeWorkspace+XML.swift b/Sources/XcodeProjects/XcodeWorkspace+XML.swiftindex b390a95..c0e08a3 100644--- a/Sources/XcodeProjects/XcodeWorkspace+XML.swift+++ b/Sources/XcodeProjects/XcodeWorkspace+XML.swift@@ -4,14 +4,15 @@ private import XcodeProj extension XcodeWorkspace { public init(url: URL) throws { let workspace = try XCWorkspace(pathString: url.path)- self.init(workspace.data)+ self.init(workspace.data, url: url) } public func dataRepresentation() throws -> Data { try xcWorkspaceData.dataRepresentation() ?? Data() } - public func write(to url: URL) throws {+ /// Writes the workspace to its current URL.+ public func write() throws { try? FileManager.default.createDirectory(at: url, withIntermediateDirectories: true) try self.dataRepresentation() .write(to: url.appendingPathComponent("contents.xcworkspacedata"))@@ -21,8 +22,8 @@ extension XcodeWorkspace { // MARK: - Conversion extension XcodeWorkspace {- fileprivate init(_ xcWorkspaceData: XCWorkspaceData) {- self.init(children: xcWorkspaceData.children.map { .init(from: $0) })+ fileprivate init(_ xcWorkspaceData: XCWorkspaceData, url: URL) {+ self.init(children: xcWorkspaceData.children.map { .init(from: $0) }, url: url) } fileprivate var xcWorkspaceData: XCWorkspaceData {
Sources/XcodeProjects/XcodeWorkspace.swift
diff --git a/Sources/XcodeProjects/XcodeWorkspace.swift b/Sources/XcodeProjects/XcodeWorkspace.swiftindex 13b6258..e00fcf4 100644--- a/Sources/XcodeProjects/XcodeWorkspace.swift+++ b/Sources/XcodeProjects/XcodeWorkspace.swift@@ -63,9 +63,18 @@ public struct XcodeWorkspace: Hashable, Sendable { } public var children: [Element]+ public var url: URL - public init(children: [Element]) {+ public init(children: [Element], url: URL) { self.children = children+ self.url = url.standardizedFileURL.resolvingSymlinksInPath()+ }++ /// Returns a copy at a new workspace location.+ public func withURL(_ url: URL) -> Self {+ var copy = self+ copy.url = url.standardizedFileURL.resolvingSymlinksInPath()+ return copy } }