|
| 1 | +import Foundation |
| 2 | +import ToolCommon |
| 3 | +import ZippyJSON |
| 4 | + |
| 5 | +struct OutputGroupsCalculator { |
| 6 | + func calculateOutputGroups(arguments: Arguments) async throws { |
| 7 | + let pifCache = arguments.baseObjRoot |
| 8 | + .appendingPathComponent("XCBuildData/PIFCache") |
| 9 | + let projectCache = pifCache.appendingPathComponent("project") |
| 10 | + let targetCache = pifCache.appendingPathComponent("target") |
| 11 | + |
| 12 | + let fileManager = FileManager.default |
| 13 | + |
| 14 | + guard fileManager.fileExists(atPath: projectCache.path) && |
| 15 | + fileManager.fileExists(atPath: targetCache.path) |
| 16 | + else { |
| 17 | + throw UsageError(message: """ |
| 18 | +error: PIFCache (\(pifCache)) doesn't exist. If you manually cleared Derived \ |
| 19 | +Data, you need to close and re-open the project for the PIFCache to be created \ |
| 20 | +again. Using the "Clean Build Folder" command instead (⇧ ⌘ K) won't trigger \ |
| 21 | +this error. If this error still happens after re-opening the project, please \ |
| 22 | +file a bug report here: \ |
| 23 | +https://github.com/MobileNativeFoundation/rules_xcodeproj/issues/new?template=bug.md |
| 24 | +""") |
| 25 | + } |
| 26 | + |
| 27 | + let projectURL = try Self.findProjectURL(in: projectCache) |
| 28 | + let project = try Self.decodeProject(at: projectURL) |
| 29 | + let targets = |
| 30 | + try await Self.decodeTargets(project.targets, in: targetCache) |
| 31 | + |
| 32 | + dump(targets) |
| 33 | + } |
| 34 | + |
| 35 | + static func findProjectURL(in projectCache: URL) throws -> URL { |
| 36 | + let projectPIFsEnumerator = FileManager.default.enumerator( |
| 37 | + at: projectCache, |
| 38 | + includingPropertiesForKeys: [.contentModificationDateKey], |
| 39 | + options: [ |
| 40 | + .skipsHiddenFiles, |
| 41 | + .skipsPackageDescendants, |
| 42 | + .skipsSubdirectoryDescendants, |
| 43 | + ] |
| 44 | + )! |
| 45 | + |
| 46 | + var newestProjectPIF: URL? |
| 47 | + var newestProjectPIFDate = Date.distantPast |
| 48 | + for case let projectPIF as URL in projectPIFsEnumerator { |
| 49 | + guard let resourceValues = try? projectPIF.resourceValues( |
| 50 | + forKeys: [.contentModificationDateKey] |
| 51 | + ), let modificationDate = resourceValues.contentModificationDate |
| 52 | + else { |
| 53 | + continue |
| 54 | + } |
| 55 | + |
| 56 | + // TODO: The modification date is in the filename, should we use |
| 57 | + // that instead? |
| 58 | + if modificationDate > newestProjectPIFDate { |
| 59 | + newestProjectPIF = projectPIF |
| 60 | + newestProjectPIFDate = modificationDate |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + guard let projectPIF = newestProjectPIF else { |
| 65 | + throw UsageError(message: """ |
| 66 | +error: Couldn't find a Project PIF at "\(projectCache)". Please file a bug \ |
| 67 | +report here: https://github.com/MobileNativeFoundation/rules_xcodeproj/issues/new?template=bug.md |
| 68 | +""") |
| 69 | + } |
| 70 | + |
| 71 | + return projectPIF |
| 72 | + } |
| 73 | + |
| 74 | + static func decodeProject(at url: URL) throws -> ProjectPIF { |
| 75 | + let decoder = ZippyJSONDecoder() |
| 76 | + return try decoder.decode(ProjectPIF.self, from: Data(contentsOf: url)) |
| 77 | + } |
| 78 | + |
| 79 | + static func decodeTargets( |
| 80 | + _ targets: [String], |
| 81 | + in targetCache: URL |
| 82 | + ) async throws -> [TargetPIF] { |
| 83 | + return try await withThrowingTaskGroup( |
| 84 | + of: TargetPIF.self, |
| 85 | + returning: [TargetPIF].self |
| 86 | + ) { group in |
| 87 | + for target in targets { |
| 88 | + group.addTask { |
| 89 | + let url = |
| 90 | + targetCache.appendingPathComponent("\(target)-json") |
| 91 | + let decoder = ZippyJSONDecoder() |
| 92 | + return try decoder |
| 93 | + .decode(TargetPIF.self, from: Data(contentsOf: url)) |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + var targetPIFs: [TargetPIF] = [] |
| 98 | + for try await target in group { |
| 99 | + targetPIFs.append(target) |
| 100 | + } |
| 101 | + |
| 102 | + return targetPIFs |
| 103 | + } |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +struct ProjectPIF: Decodable { |
| 108 | + let targets: [String] |
| 109 | +} |
| 110 | + |
| 111 | +struct TargetPIF: Decodable { |
| 112 | + struct BuildConfiguration: Decodable { |
| 113 | + let name: String |
| 114 | + let buildSettings: [String: String] |
| 115 | + } |
| 116 | + |
| 117 | + let guid: String |
| 118 | + let buildConfigurations: [BuildConfiguration] |
| 119 | +} |
| 120 | + |
| 121 | +struct Target { |
| 122 | + let label: String |
| 123 | + |
| 124 | + // Maps Platform Name -> [Target ID] |
| 125 | + let targetIds: [String: [String]] |
| 126 | +} |
0 commit comments