Skip to content

Commit

Permalink
Add symbolGraphMinimumAccessLevel option support (#82)
Browse files Browse the repository at this point in the history
* Add symbolGraphMinimumAccessLevel option support

* Remove experimental prefix

* Fix CR suggestion
  • Loading branch information
Kyle-Ye authored Aug 12, 2024
1 parent 63f47d3 commit d80a059
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,17 @@ extension PackageManager {
context: PluginContext,
verbose: Bool,
snippetExtractor: SnippetExtractor?,
customSymbolGraphOptions: [PluginFlag]
customSymbolGraphOptions: [PluginFlag],
minimumAccessLevel: SymbolGraphOptions.AccessLevel? = nil
) throws -> DocCSymbolGraphResult {
// First generate the primary symbol graphs containing information about the
// symbols defined in the target itself.

var symbolGraphOptions = target.defaultSymbolGraphOptions(in: context.package)

if let minimumAccessLevel {
symbolGraphOptions.minimumAccessLevel = minimumAccessLevel
}

// Modify the symbol graph options with the custom ones
for customSymbolGraphOption in customSymbolGraphOptions {
switch customSymbolGraphOption {
Expand Down
3 changes: 2 additions & 1 deletion Plugins/Swift-DocC Convert/SwiftDocCConvert.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ import PackagePlugin
context: context,
verbose: verbose,
snippetExtractor: snippetExtractor,
customSymbolGraphOptions: parsedArguments.symbolGraphArguments
customSymbolGraphOptions: parsedArguments.symbolGraphArguments,
minimumAccessLevel: parsedArguments.arguments.symbolGraphMinimumAccessLevel.flatMap { .init(rawValue: $0) }
)

if target.doccCatalogPath == nil,
Expand Down
3 changes: 2 additions & 1 deletion Plugins/Swift-DocC Preview/SwiftDocCPreview.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ import PackagePlugin
context: context,
verbose: verbose,
snippetExtractor: snippetExtractor,
customSymbolGraphOptions: parsedArguments.symbolGraphArguments
customSymbolGraphOptions: parsedArguments.symbolGraphArguments,
minimumAccessLevel: parsedArguments.arguments.symbolGraphMinimumAccessLevel.flatMap { .init(rawValue: $0) }
)

if try FileManager.default.contentsOfDirectory(atPath: symbolGraphs.targetSymbolGraphsDirectory.path).isEmpty {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for Swift project authors

import Foundation

extension Arguments {
/// The symbol graph minimum access level, if any, described by this set of command-line arguments.
public var symbolGraphMinimumAccessLevel: String? {
guard let accessLevelOptionIndex = firstIndex(
where: { argument in
return CommandLineOption.symbolGraphMinimumAccessLevel.possibleNames.contains(argument)
}
) else {
return nil
}
let accessLevelIndex = index(after: accessLevelOptionIndex)
guard indices.contains(accessLevelIndex) else {
return nil
}
return self[accessLevelIndex]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ extension CommandLineOption {
defaultName: "--fallback-default-module-kind"
)

/// A DocC flag that enables support for linking to other DocC archives and enables
/// A DocC flag that enables support for linking to other DocC archives and enables
/// other documentation builds to link to the generated DocC archive.
static let enableExternalLinkSupport = CommandLineOption(
defaultName: "--enable-experimental-external-link-support"
Expand All @@ -70,4 +70,9 @@ extension CommandLineOption {
static let externalLinkDependency = CommandLineOption(
defaultName: "--dependency"
)

/// Specifies the symbol graph minimum access level.
static let symbolGraphMinimumAccessLevel = CommandLineOption(
defaultName: "--symbol-graph-minimum-access-level"
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for Swift project authors

import Foundation
import SwiftDocCPluginUtilities
import XCTest

final class ArgumentsSymbolGraphMinimumAccessLevelTests: XCTestCase {
func testArgumentsThatContainAccessLevel() {
XCTAssertEqual(
Arguments(["--symbol-graph-minimum-access-level", "internal"]).symbolGraphMinimumAccessLevel,
"internal"
)

XCTAssertEqual(
Arguments(["other-arg", "--symbol-graph-minimum-access-level", "internal", "--other-flag"]).symbolGraphMinimumAccessLevel,
"internal"
)
}

func testArgumentsThatDoNotContainAccessLevel() {
XCTAssertNil(
Arguments(["--other-option", "/test-path"]).symbolGraphMinimumAccessLevel
)

XCTAssertNil(
Arguments(["other-arg", "--other-option", "/test-path", "--other-flag"]).symbolGraphMinimumAccessLevel
)
}

func testArgumentsThatContainTrailingAccessLevelFlag() {
XCTAssertNil(
Arguments(["--symbol-graph-minimum-access-level"]).symbolGraphMinimumAccessLevel
)


XCTAssertNil(
Arguments(["other-arg", "--symbol-graph-minimum-access-level"]).symbolGraphMinimumAccessLevel
)
}
}

0 comments on commit d80a059

Please sign in to comment.