Modern Apps git

xtool.git

Public
git clone https://git.maproj.org/xtool.git
main ▾

xtool.git / Sources / XcodeProjects / XcodeProject.swift

XcodeProject.swift113 lines · 3.9 KiBRaw
1import Foundation
2private import XcodeProj
3@_exported import XcodeProjectFormat
4
5// This library is a lightweight layer on top of the XcodeProjectFormat
6// interface, providing other facilities for working with the Xcode
7// project formats.
8//
9// We currently use XcodeProj under the hood for some functionality, but this
10// should be considered an implementation detail.
11
12public struct XcodeProject: Equatable, Sendable {
13 public enum Format: Sendable {
14 case pbxproj
15 case xcproj
16 }
17
18 public var project: XCSchema.Project
19 public var format: Format
20 /// The .xcodeproj bundle's location, including for projects constructed in memory.
21 public var url: URL
22 /// For pbxproj projects, an explicit source root.
23 public var projectDirPath = ""
24
25 public var sourceRoot: URL {
26 if projectDirPath.isEmpty {
27 url.deletingLastPathComponent()
28 } else {
29 URL(
30 filePath: projectDirPath,
31 directoryHint: .isDirectory,
32 relativeTo: url.deletingLastPathComponent(),
33 ).standardizedFileURL
34 }
35 }
36
37 public init(
38 project: XCSchema.Project,
39 url: URL,
40 format: Format = .pbxproj,
41 ) {
42 self.project = project
43 self.format = format
44 self.url = url.standardizedFileURL.resolvingSymlinksInPath()
45 }
46
47 /// Reads the project from the given `.xcodeproj` path.
48 public init(url: URL) throws {
49 guard url.pathExtension == "xcodeproj" else {
50 throw Errors.notAnXcodeproj(url.path)
51 }
52 let url = url.standardizedFileURL.resolvingSymlinksInPath()
53 self.url = url
54 let xcprojURL = url.appending(path: "project.xcproj")
55 lazy var pbxprojURL = url.appending(path: "project.pbxproj")
56 if FileManager.default.fileExists(atPath: xcprojURL.path) {
57 let data = try Data(contentsOf: xcprojURL)
58 self.project = try XCSchema.Project(jsonRepresentation: data)
59 self.format = .xcproj
60 } else if FileManager.default.fileExists(atPath: pbxprojURL.path) {
61 let data = try Data(contentsOf: pbxprojURL)
62 let pbxproj = try PBXProj(data: data)
63 self.project = try XCSchema.Project(jsonRepresentation: pbxproj.xcprojData())
64 self.projectDirPath = try pbxproj.rootProject()?.projectDirPath ?? ""
65 self.format = .pbxproj
66 } else {
67 throw Errors.missingProjectFile(url.path)
68 }
69 }
70
71 /// Sets the format in which the project should be serialized.
72 public func withFormat(_ format: Format) -> XcodeProject {
73 var copy = self
74 copy.format = format
75 return copy
76 }
77
78 /// Returns a copy at a new bundle location.
79 public func withURL(_ url: URL) -> Self {
80 var copy = self
81 copy.url = url.standardizedFileURL.resolvingSymlinksInPath()
82 return copy
83 }
84
85 /// Writes the project to its current `.xcodeproj` URL.
86 public func write() throws {
87 try? FileManager.default.createDirectory(at: url, withIntermediateDirectories: true)
88
89 switch format {
90 case .pbxproj:
91 try project.pbxprojRepresentation(
92 name: url.deletingPathExtension().lastPathComponent,
93 projectDirPath: projectDirPath,
94 ).write(to: url.appendingPathComponent("project.pbxproj"))
95 case .xcproj:
96 guard projectDirPath.isEmpty else {
97 throw Errors.xcprojWithExplicitSourceRoot
98 }
99 try project
100 .jsonRepresentation()
101 .write(to: url.appendingPathComponent("project.xcproj"))
102 }
103
104 let workspaceURL = url.appendingPathComponent("project.xcworkspace")
105 try XcodeWorkspace(children: [.file(.current)], url: workspaceURL).write()
106 }
107
108 public enum Errors: Error {
109 case notAnXcodeproj(String)
110 case missingProjectFile(String)
111 case xcprojWithExplicitSourceRoot
112 }
113}