mirror of
https://github.com/xtool-org/xtool.git
synced 2026-11-19 14:04:22 +03:00
xcode: tweaks to XcodeProjects API
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user