|
| 1 | +import ArgumentParser |
| 2 | +import Foundation |
| 3 | + |
| 4 | +import PADCore |
| 5 | +import PADLogging |
| 6 | + |
| 7 | +import PADSwiftInterfaceDiff |
| 8 | +import PADProjectBuilder |
| 9 | +import PADOutputGenerator |
| 10 | +import PADPackageFileAnalyzer |
| 11 | + |
| 12 | +/// Command that analyzes the differences between an old and new project and produces a human readable output |
| 13 | +struct ProjectToOutputCommand: AsyncParsableCommand { |
| 14 | + |
| 15 | + static var configuration: CommandConfiguration = .init(commandName: "project") |
| 16 | + |
| 17 | + /// The representation of the new/updated project source |
| 18 | + @Option(help: "Specify the updated version to compare to") |
| 19 | + public var new: String |
| 20 | + |
| 21 | + /// The representation of the old/reference project source |
| 22 | + @Option(help: "Specify the old version to compare to") |
| 23 | + public var old: String |
| 24 | + |
| 25 | + /// The (optional) scheme to build |
| 26 | + /// |
| 27 | + /// Needed when comparing 2 xcode projects |
| 28 | + @Option(help: "[Optional] Which scheme to build (Needed when comparing 2 xcode projects)") |
| 29 | + public var scheme: String? |
| 30 | + |
| 31 | + @Option(help: "[Optional] Specify the type of .swiftinterface you want to compare (public/private)") |
| 32 | + public var swiftInterfaceType: SwiftInterfaceType = .public |
| 33 | + |
| 34 | + /// The (optional) output file path |
| 35 | + /// |
| 36 | + /// If not defined the output will be printed to the console |
| 37 | + @Option(help: "[Optional] Where to output the result (File path)") |
| 38 | + public var output: String? |
| 39 | + |
| 40 | + /// The (optional) path to the log output file |
| 41 | + @Option(help: "[Optional] Where to output the logs (File path)") |
| 42 | + public var logOutput: String? |
| 43 | + |
| 44 | + @Option(help: "[Optional] The log level to use during execution") |
| 45 | + public var logLevel: LogLevel = .default |
| 46 | + |
| 47 | + /// Entry point of the command line tool |
| 48 | + public func run() async throws { |
| 49 | + |
| 50 | + let projectType: ProjectType = { |
| 51 | + if let scheme { return .xcodeProject(scheme: scheme) } |
| 52 | + return .swiftPackage |
| 53 | + }() |
| 54 | + |
| 55 | + let logger = PublicApiDiff.logger(with: logLevel, logOutputFilePath: logOutput) |
| 56 | + |
| 57 | + do { |
| 58 | + var warnings = [String]() |
| 59 | + var projectChanges = [Change]() |
| 60 | + |
| 61 | + let oldSource: ProjectSource = try .from(old) |
| 62 | + let newSource: ProjectSource = try .from(new) |
| 63 | + |
| 64 | + // MARK: - Producing .swiftinterface files |
| 65 | + |
| 66 | + let projectBuilderResult = try await Self.buildProject( |
| 67 | + oldSource: oldSource, |
| 68 | + newSource: newSource, |
| 69 | + projectType: projectType, |
| 70 | + swiftInterfaceType: swiftInterfaceType, |
| 71 | + logger: logger |
| 72 | + ) |
| 73 | + |
| 74 | + // MARK: - Analyzing .swiftinterface files |
| 75 | + |
| 76 | + let swiftInterfaceChanges = try await Self.analyzeSwiftInterfaceFiles( |
| 77 | + swiftInterfaceFiles: projectBuilderResult.swiftInterfaceFiles, |
| 78 | + logger: logger |
| 79 | + ) |
| 80 | + |
| 81 | + // MARK: - Analyzing Package.swift |
| 82 | + |
| 83 | + try Self.analyzeProject( |
| 84 | + ofType: projectType, |
| 85 | + projectDirectories: projectBuilderResult.projectDirectories, |
| 86 | + changes: &projectChanges, |
| 87 | + warnings: &warnings, |
| 88 | + logger: logger |
| 89 | + ) |
| 90 | + |
| 91 | + // MARK: - Merging Changes |
| 92 | + |
| 93 | + var changes = swiftInterfaceChanges |
| 94 | + if !projectChanges.isEmpty { |
| 95 | + changes["Package.swift"] = projectChanges |
| 96 | + } |
| 97 | + |
| 98 | + // MARK: - Generate Output |
| 99 | + |
| 100 | + let generatedOutput = try Self.generateOutput( |
| 101 | + for: changes, |
| 102 | + warnings: warnings, |
| 103 | + allTargets: projectBuilderResult.swiftInterfaceFiles.map(\.name).sorted(), |
| 104 | + oldVersionName: oldSource.description, |
| 105 | + newVersionName: newSource.description |
| 106 | + ) |
| 107 | + |
| 108 | + // MARK: - |
| 109 | + |
| 110 | + if let output { |
| 111 | + try FileManager.default.write(generatedOutput, to: output) |
| 112 | + } else { |
| 113 | + // We're not using a logger here as we always want to have it printed if no output was specified |
| 114 | + print(generatedOutput) |
| 115 | + } |
| 116 | + |
| 117 | + logger.log("✅ Success", from: "Main") |
| 118 | + } catch { |
| 119 | + logger.log("💥 \(error.localizedDescription)", from: "Main") |
| 120 | + } |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +// MARK: - Privates |
| 125 | + |
| 126 | +private extension ProjectToOutputCommand { |
| 127 | + |
| 128 | + static func buildProject( |
| 129 | + oldSource: ProjectSource, |
| 130 | + newSource: ProjectSource, |
| 131 | + projectType: ProjectType, |
| 132 | + swiftInterfaceType: SwiftInterfaceType, |
| 133 | + logger: any Logging |
| 134 | + ) async throws -> ProjectBuilder.Result { |
| 135 | + |
| 136 | + let projectBuilder = ProjectBuilder( |
| 137 | + projectType: projectType, |
| 138 | + swiftInterfaceType: swiftInterfaceType, |
| 139 | + logger: logger |
| 140 | + ) |
| 141 | + |
| 142 | + return try await projectBuilder.build( |
| 143 | + oldSource: oldSource, |
| 144 | + newSource: newSource |
| 145 | + ) |
| 146 | + } |
| 147 | + |
| 148 | + static func analyzeProject( |
| 149 | + ofType projectType: ProjectType, |
| 150 | + projectDirectories: (old: URL, new: URL), |
| 151 | + changes: inout [Change], |
| 152 | + warnings: inout [String], |
| 153 | + logger: any Logging |
| 154 | + ) throws { |
| 155 | + switch projectType { |
| 156 | + case .swiftPackage: |
| 157 | + let swiftPackageFileAnalyzer = SwiftPackageFileAnalyzer( |
| 158 | + logger: logger |
| 159 | + ) |
| 160 | + let swiftPackageAnalysis = try swiftPackageFileAnalyzer.analyze( |
| 161 | + oldProjectUrl: projectDirectories.old, |
| 162 | + newProjectUrl: projectDirectories.new |
| 163 | + ) |
| 164 | + |
| 165 | + warnings = swiftPackageAnalysis.warnings |
| 166 | + changes = swiftPackageAnalysis.changes |
| 167 | + case .xcodeProject: |
| 168 | + warnings = [] |
| 169 | + changes = [] |
| 170 | + break // Nothing to do |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + static func analyzeSwiftInterfaceFiles( |
| 175 | + swiftInterfaceFiles: [SwiftInterfaceFile], |
| 176 | + logger: any Logging |
| 177 | + ) async throws -> [String: [Change]] { |
| 178 | + let swiftInterfaceDiff = SwiftInterfaceDiff(logger: logger) |
| 179 | + |
| 180 | + return try await swiftInterfaceDiff.run( |
| 181 | + with: swiftInterfaceFiles |
| 182 | + ) |
| 183 | + } |
| 184 | + |
| 185 | + static func generateOutput( |
| 186 | + for changes: [String: [Change]], |
| 187 | + warnings: [String], |
| 188 | + allTargets: [String], |
| 189 | + oldVersionName: String, |
| 190 | + newVersionName: String |
| 191 | + ) throws -> String { |
| 192 | + let outputGenerator: any OutputGenerating<String> = MarkdownOutputGenerator() |
| 193 | + |
| 194 | + return try outputGenerator.generate( |
| 195 | + from: changes, |
| 196 | + allTargets: allTargets, |
| 197 | + oldVersionName: oldVersionName, |
| 198 | + newVersionName: newVersionName, |
| 199 | + warnings: warnings |
| 200 | + ) |
| 201 | + } |
| 202 | +} |
0 commit comments