Skip to content

Commit e71e7c3

Browse files
committed
address review comments with strategy from #28
1 parent 3abb809 commit e71e7c3

File tree

11 files changed

+62
-58
lines changed

11 files changed

+62
-58
lines changed

Plugins/SharedPackagePluginExtensions/PackageManager+getSymbolGraphsForDocC.swift

+15-2
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,26 @@ extension PackageManager {
3939
context: PluginContext,
4040
verbose: Bool,
4141
snippetExtractor: SnippetExtractor?,
42-
arguments: Arguments
42+
customSymbolGraphOptions: [PluginFlag]
4343
) throws -> DocCSymbolGraphResult {
4444
// First generate the primary symbol graphs containing information about the
4545
// symbols defined in the target itself.
4646

4747
var symbolGraphOptions = target.defaultSymbolGraphOptions(in: context.package)
48-
symbolGraphOptions.override(with: arguments)
48+
49+
// Modify the symbol graph options with the custom ones
50+
for customSymbolGraphOption in customSymbolGraphOptions {
51+
switch customSymbolGraphOption {
52+
case .extendedTypes:
53+
#if swift(>=5.8)
54+
symbolGraphOptions.emitExtensionBlocks = true
55+
#else
56+
print("warning: detected '--include-extended-types' option, which is incompatible with your swift version (required: 5.8)")
57+
#endif
58+
default:
59+
fatalError("error: unknown PluginFlag (\(customSymbolGraphOption.parsedValues.joined(separator: ", "))) detected in symbol graph generation - please create an issue at https://github.com/apple/swift-docc-plugin")
60+
}
61+
}
4962

5063
if verbose {
5164
print("symbol graph options: '\(symbolGraphOptions)'")

Plugins/SharedPackagePluginExtensions/SymbolGraphOptions+override.swift

-28
This file was deleted.

Plugins/SharedPackagePluginExtensions/Target+defaultSymbolGraphOptions.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ extension SwiftSourceModuleTarget {
3636

3737
#if swift(<5.8)
3838
private extension PackageManager.SymbolGraphOptions {
39-
/// A copatibility layer for lower Swift versions which discards unknown parameters.
39+
/// A compatibility layer for lower Swift versions which discards unknown parameters.
4040
init(minimumAccessLevel: PackagePlugin.PackageManager.SymbolGraphOptions.AccessLevel = .public,
4141
includeSynthesized: Bool = false,
4242
includeSPI: Bool = false,

Plugins/Swift-DocC Convert/SwiftDocCConvert.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ import PackagePlugin
7171
context: context,
7272
verbose: verbose,
7373
snippetExtractor: snippetExtractor,
74-
arguments: parsedArguments.dumpSymbolGraphArguments()
74+
customSymbolGraphOptions: parsedArguments.symbolGraphArguments
7575
)
7676

7777
if try FileManager.default.contentsOfDirectory(atPath: symbolGraphs.targetSymbolGraphsDirectory.path).isEmpty {

Plugins/Swift-DocC Preview/SwiftDocCPreview.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ import PackagePlugin
8484
context: context,
8585
verbose: verbose,
8686
snippetExtractor: snippetExtractor,
87-
arguments: parsedArguments.dumpSymbolGraphArguments()
87+
customSymbolGraphOptions: parsedArguments.symbolGraphArguments
8888
)
8989

9090
if try FileManager.default.contentsOfDirectory(atPath: symbolGraphs.targetSymbolGraphsDirectory.path).isEmpty {

Sources/SwiftDocCPluginUtilities/HelpInformation.swift

+3
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ public enum HelpInformation {
4747
PluginFlag.disableIndex,
4848
]
4949

50+
// stops 'not mutated' warning for Swift 5.7 and lower
51+
supportedPluginFlags += []
52+
5053
#if swift(>=5.8)
5154
supportedPluginFlags += [PluginFlag.extendedTypes]
5255
#endif

Sources/SwiftDocCPluginUtilities/ParsedArguments.swift

+24-20
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,6 @@ public struct ParsedArguments {
1919
return arguments.contains("--help") || arguments.contains("-h")
2020
}
2121

22-
/// Returns the arguments that could be passed to `swift package dump-symbol-graph`.
23-
///
24-
/// In practice, however we won't invoke the `dump-symbol-graph` command via the command line,
25-
/// but via the `PackagePlugin` API. Thus, these arguments have to be parsed later on and converted to
26-
/// `PackageManager.SymbolGraphOptions`.
27-
public func dumpSymbolGraphArguments() -> Arguments {
28-
var dumpSymbolGraphArguments = arguments.filter(for: .dumpSymbolGraph)
29-
30-
for argumentsTransformer in Self.argumentsTransformers {
31-
dumpSymbolGraphArguments = argumentsTransformer.transform(dumpSymbolGraphArguments)
32-
}
33-
34-
return dumpSymbolGraphArguments
35-
}
36-
3722
/// Returns the arguments that should be passed to `docc` to invoke the given plugin action.
3823
///
3924
/// Merges the arguments provided upon initialization of the parsed arguments that are relevant
@@ -168,8 +153,19 @@ public struct ParsedArguments {
168153
/// Creates a new set of parsed arguments with the given arguments.
169154
public init(_ arguments: [String]) {
170155
self.arguments = arguments
156+
157+
let symbolGraphArguments = arguments.filter(for: .dumpSymbolGraph)
158+
159+
self.symbolGraphArguments = ParsedArguments.ArgumentConsumer.dumpSymbolGraph.flags.filter { option in
160+
option.parsedValues.contains(where: symbolGraphArguments.contains)
161+
}
171162
}
172163

164+
// Build array with plugin flags that modify the symbol graph generation,
165+
// filtering from the available custom symbol graph options those
166+
// that correspond to the received flags
167+
var symbolGraphArguments: [PluginFlag]
168+
173169
/// The command-line options required by the `docc` tool.
174170
private static let requiredOptions: [CommandLineOption] = [
175171
.fallbackDisplayName,
@@ -199,16 +195,16 @@ private extension ParsedArguments {
199195

200196
/// Returns the flags applicable to an `ArgumentConsumer`.
201197
///
202-
/// If `flags` is `nil`, this `ArgumentConsumer` is assumed to
198+
/// If `flags.isEmpty` is `true`, this `ArgumentConsumer` is assumed to
203199
/// consume all flags not consumed by any of the other `ArgumentConsumer`s.
204-
var flags: [PluginFlag]? {
200+
var flags: [PluginFlag] {
205201
switch self {
206202
case .dumpSymbolGraph:
207203
return [
208204
PluginFlag.extendedTypes
209205
]
210206
case .docc:
211-
return nil
207+
return []
212208
}
213209
}
214210
}
@@ -217,14 +213,22 @@ private extension ParsedArguments {
217213
private extension Arguments {
218214
/// Returns the subset of arguments which are applicable to the given `consumer`.
219215
func filter(for consumer: ParsedArguments.ArgumentConsumer) -> Arguments {
220-
if let flagsToInclude = consumer.flags {
216+
if !consumer.flags.isEmpty {
217+
// If the consumer can provide a complete list of valid flags,
218+
// we only include elements that are included in one of these flags'
219+
// `parsedValues`, i.e. if one of these flags can be applied to the
220+
// element.
221+
let flagsToInclude = consumer.flags
221222
return self.filter { argument in
222223
flagsToInclude.contains(where: { flag in
223224
flag.parsedValues.contains(argument)
224225
})
225226
}
226227
} else {
227-
let flagsToExclude = ParsedArguments.ArgumentConsumer.allCases.compactMap(\.flags).flatMap { $0 }
228+
// If the consumer cannot provide a complete list of valid flags, (which
229+
// should only happen for the `.docc` case) we return all elements
230+
// that are not applicable to any of the other `ArgumentConsumer`s.
231+
let flagsToExclude = ParsedArguments.ArgumentConsumer.allCases.flatMap(\.flags)
228232
return self.filter { argument in
229233
!flagsToExclude.contains(where: { flag in
230234
flag.parsedValues.contains(argument)

Sources/SwiftDocCPluginUtilities/PluginFlags/ExtendedTypesFlag.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ extension PluginFlag {
2424
description: """
2525
Allows documenting symbols that a target adds to its dependencies.
2626
""",
27-
argumentTransformation: { $0 + ["--emit-extension-block-symbols"] }
27+
argumentTransformation: { $0 }
2828
)
2929
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// This source file is part of the Swift.org open source project
2+
//
3+
// Copyright (c) 2022 Apple Inc. and the Swift project authors
4+
// Licensed under Apache License v2.0 with Runtime Library Exception
5+
//
6+
// See https://swift.org/LICENSE.txt for license information
7+
// See https://swift.org/CONTRIBUTORS.txt for Swift project authors
8+
9+
extension PluginFlag: Equatable {
10+
static func ==(lhs: PluginFlag, rhs: PluginFlag) -> Bool {
11+
return lhs.parsedValues == rhs.parsedValues
12+
}
13+
}

Tests/SwiftDocCPluginUtilitiesTests/ParsedArgumentsTests.swift

+2-3
Original file line numberDiff line numberDiff line change
@@ -456,19 +456,18 @@ final class ParsedArgumentsTests: XCTestCase {
456456
)
457457

458458
XCTAssertFalse(doccArguments.contains("--include-extended-types"))
459-
XCTAssertFalse(doccArguments.contains("--emit-extension-block-symbols"))
460459
}
461460

462461
func testDumpSymbolGraphArguments() {
463462
let dumpSymbolGraphArguments = ParsedArguments(["--include-extended-types"])
464463

465-
XCTAssertEqual(dumpSymbolGraphArguments.dumpSymbolGraphArguments(), ["--emit-extension-block-symbols"])
464+
XCTAssertEqual(dumpSymbolGraphArguments.symbolGraphArguments, [.extendedTypes])
466465
}
467466

468467
func testDumpSymbolGraphArgumentsWithDocCArguments() {
469468
let dumpSymbolGraphArguments = ParsedArguments(["--fallback-default-module-kind", "Executable"])
470469

471470

472-
XCTAssertTrue(dumpSymbolGraphArguments.dumpSymbolGraphArguments().isEmpty)
471+
XCTAssertEqual(dumpSymbolGraphArguments.symbolGraphArguments, [])
473472
}
474473
}

Tests/SwiftDocCPluginUtilitiesTests/PluginFlags/ExtendedTypesFlagTests.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ final class ExtendedTypesFlagTests: XCTestCase {
1616
PluginFlag.extendedTypes.transform(
1717
["--include-extended-types", "--other-flag"]
1818
),
19-
["--other-flag", "--emit-extension-block-symbols"]
19+
["--other-flag"]
2020
)
2121
}
2222

0 commit comments

Comments
 (0)