Skip to content

Commit e8beb53

Browse files
committed
Diff two DocC Archives to produce three lists: (1) The existing symbols within the first DocC Archive, (2) The existing symbols within the second DocC Archive, and (3) The new symbols added to the second DocC Archive that did not exist in the first DocC Archive
1 parent d323b05 commit e8beb53

File tree

3 files changed

+84
-3
lines changed

3 files changed

+84
-3
lines changed

Sources/SwiftDocCUtilities/ArgumentParsing/Subcommands/Diff.swift renamed to Sources/SwiftDocCUtilities/ArgumentParsing/Subcommands/DiffRenderJSON.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ import Foundation
1313
import SwiftDocC
1414

1515
extension Docc.ProcessArchive {
16-
struct Diff: ParsableCommand {
16+
struct DiffRenderJSON: ParsableCommand {
1717

1818
// MARK: - Configuration
1919

2020
static var configuration = CommandConfiguration(
21-
commandName: "diff",
21+
commandName: "diff-render-json",
2222
abstract: "Produce the symbol diff between two Render JSON files.",
2323
shouldDisplay: true)
2424

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
}

Sources/SwiftDocCUtilities/ArgumentParsing/Subcommands/ProcessArchive.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ extension Docc {
1818
static var configuration = CommandConfiguration(
1919
commandName: "process-archive",
2020
abstract: "Perform operations on documentation archives ('.doccarchive' directories).",
21-
subcommands: [TransformForStaticHosting.self, Index.self, Diff.self])
21+
subcommands: [TransformForStaticHosting.self, Index.self, DiffRenderJSON.self, DiffDocCArchive.self])
2222

2323
}
2424
}

0 commit comments

Comments
 (0)