|
| 1 | +/* |
| 2 | + This source file is part of the Swift.org open source project |
| 3 | + |
| 4 | + Copyright (c) 2024 Apple Inc. and the Swift project authors |
| 5 | + Licensed under Apache License v2.0 with Runtime Library Exception |
| 6 | + |
| 7 | + See https://swift.org/LICENSE.txt for license information |
| 8 | + See https://swift.org/CONTRIBUTORS.txt for Swift project authors |
| 9 | +*/ |
| 10 | + |
| 11 | +import ArgumentParser |
| 12 | +import Foundation |
| 13 | +import SwiftDocC |
| 14 | + |
| 15 | +extension Docc.ProcessArchive { |
| 16 | + struct DiffDocCArchive: ParsableCommand { |
| 17 | + |
| 18 | + // MARK: - Configuration |
| 19 | + |
| 20 | + static var configuration = CommandConfiguration( |
| 21 | + commandName: "diff-docc-archive", |
| 22 | + abstract: "Produce a list of symbols added in the newer DocC Archive that did not exist in the initial DocC Archive.", |
| 23 | + shouldDisplay: true) |
| 24 | + |
| 25 | + // MARK: - Command Line Options & Arguments |
| 26 | + |
| 27 | + @Argument( |
| 28 | + help: ArgumentHelp( |
| 29 | + "The path to the initial DocC Archive to be compared.", |
| 30 | + valueName: "initialDocCArchive"), |
| 31 | + transform: URL.init(fileURLWithPath:)) |
| 32 | + var initialDocCArchivePath: URL |
| 33 | + |
| 34 | + @Argument( |
| 35 | + help: ArgumentHelp( |
| 36 | + "The path to the newer DocC Archive to be compared.", |
| 37 | + valueName: "newerDocCArchive"), |
| 38 | + transform: URL.init(fileURLWithPath:)) |
| 39 | + var newerDocCArchivePath: URL |
| 40 | + |
| 41 | + // MARK: - Execution |
| 42 | + |
| 43 | + public mutating func run() throws { |
| 44 | + |
| 45 | + let initialDocCArchiveAPIs: [String] = try findAllSymbols(initialPath: initialDocCArchivePath) |
| 46 | + let newDocCArchiveAPIs: [String] = try findAllSymbols(initialPath: newerDocCArchivePath) |
| 47 | + |
| 48 | + print("\ninitialDocCArchiveAPIs: ") |
| 49 | + print(initialDocCArchiveAPIs) |
| 50 | + |
| 51 | + print("\nnewDocCArchiveAPIs: ") |
| 52 | + print(newDocCArchiveAPIs) |
| 53 | + |
| 54 | + let initialSet = Set(initialDocCArchiveAPIs.map { $0.plainText }) |
| 55 | + let newSet = Set(newDocCArchiveAPIs.map { $0.plainText }) |
| 56 | + let difference = newSet.subtracting(initialSet) |
| 57 | + print("\nDifference:\n\(difference)") |
| 58 | + } |
| 59 | + |
| 60 | + func findAllSymbols(initialPath: URL) throws -> [String] { |
| 61 | + guard let enumerator = FileManager.default.enumerator( |
| 62 | + at: initialPath, |
| 63 | + includingPropertiesForKeys: [], |
| 64 | + options: .skipsHiddenFiles, |
| 65 | + errorHandler: nil |
| 66 | + ) else { |
| 67 | + return [] |
| 68 | + } |
| 69 | + |
| 70 | + var returnSymbols: [String] = [] |
| 71 | + for case let filePath as URL in enumerator { |
| 72 | + if filePath.lastPathComponent.hasSuffix(".json") { |
| 73 | + returnSymbols.append(filePath.lastPathComponent) |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + return returnSymbols |
| 78 | + } |
| 79 | + |
| 80 | + } |
| 81 | +} |
0 commit comments