Skip to content

Commit 9dd86bb

Browse files
committed
Fixes based on feedback:
- change output format to an enumeration - change verbosity to log level to avoid using the global
1 parent d40dba5 commit 9dd86bb

File tree

3 files changed

+33
-23
lines changed

3 files changed

+33
-23
lines changed

Sources/Commands/SwiftPackageTool.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -564,8 +564,7 @@ extension SwiftPackageTool {
564564
skipSynthesizedMembers: skipSynthesizedMembers,
565565
minimumAccessLevel: minimumAccessLevel,
566566
skipInheritedDocs: skipInheritedDocs,
567-
includeSPISymbols: includeSPISymbols,
568-
prettyPrintOutputJSON: prettyPrint)
567+
includeSPISymbols: includeSPISymbols)
569568

570569
// Run the tool once for every library and executable target in the root package.
571570
let buildPlan = buildOp.buildPlan!
@@ -576,7 +575,7 @@ extension SwiftPackageTool {
576575
try symbolGraphExtractor.extractSymbolGraph(
577576
target: target,
578577
buildPlan: buildPlan,
579-
verbose: verbosity != .concise,
578+
logLevel: swiftTool.logLevel,
580579
outputDirectory: symbolGraphDirectory)
581580
}
582581

@@ -1276,7 +1275,7 @@ final class PluginDelegate: PluginInvocationDelegate {
12761275
target: target,
12771276
buildPlan: buildPlan,
12781277
outputRedirection: .collect,
1279-
verbose: false,
1278+
logLevel: .warning,
12801279
outputDirectory: outputDir)
12811280

12821281
// Return the results to the plugin.

Sources/Commands/SymbolGraphExtract.swift

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010

1111
import ArgumentParser
12+
import Basics
1213
import Build
1314
import PackageGraph
1415
import PackageModel
@@ -19,26 +20,31 @@ import TSCUtility
1920
/// A wrapper for swift-symbolgraph-extract tool.
2021
public struct SymbolGraphExtract {
2122
let tool: AbsolutePath
22-
var skipSynthesizedMembers: Bool = false
23-
var minimumAccessLevel: AccessLevel = .public
24-
var skipInheritedDocs: Bool = false
25-
var includeSPISymbols: Bool = false
26-
var prettyPrintOutputJSON: Bool = false
23+
24+
var skipSynthesizedMembers = false
25+
var minimumAccessLevel = AccessLevel.public
26+
var skipInheritedDocs = false
27+
var includeSPISymbols = false
28+
var outputFormat = OutputFormat.json(pretty: false)
2729

2830
/// Access control levels.
29-
public enum AccessLevel: String, RawRepresentable, CustomStringConvertible, CaseIterable, ExpressibleByArgument {
31+
public enum AccessLevel: String, RawRepresentable, CaseIterable, ExpressibleByArgument {
3032
// The cases reflect those found in `include/swift/AST/AttrKind.h` of the swift compiler (at commit 03f55d7bb4204ca54841218eb7cc175ae798e3bd)
3133
case `private`, `fileprivate`, `internal`, `public`, `open`
32-
33-
public var description: String { rawValue }
3434
}
3535

36+
/// Output format of the generated symbol graph.
37+
public enum OutputFormat {
38+
/// JSON format, optionally "pretty-printed" be more human-readable.
39+
case json(pretty: Bool)
40+
}
41+
3642
/// Creates a symbol graph for `target` in `outputDirectory` using the build information from `buildPlan`. The `outputDirection` determines how the output from the tool subprocess is handled, and `verbosity` specifies how much console output to ask the tool to emit.
3743
public func extractSymbolGraph(
3844
target: ResolvedTarget,
3945
buildPlan: BuildPlan,
4046
outputRedirection: Process.OutputRedirection = .none,
41-
verbose: Bool,
47+
logLevel: Basics.Diagnostic.Severity,
4248
outputDirectory: AbsolutePath
4349
) throws {
4450
let buildParameters = buildPlan.buildParameters
@@ -50,7 +56,7 @@ public struct SymbolGraphExtract {
5056
commandLine += try buildParameters.targetTripleArgs(for: target)
5157
commandLine += buildPlan.createAPIToolCommonArgs(includeLibrarySearchPaths: true)
5258
commandLine += ["-module-cache-path", buildParameters.moduleCache.pathString]
53-
if verbose {
59+
if logLevel <= .info {
5460
commandLine += ["-v"]
5561
}
5662
commandLine += ["-minimum-access-level", minimumAccessLevel.rawValue]
@@ -63,16 +69,19 @@ public struct SymbolGraphExtract {
6369
if includeSPISymbols {
6470
commandLine += ["-include-spi-symbols"]
6571
}
66-
if prettyPrintOutputJSON {
67-
commandLine += ["-pretty-print"]
72+
switch outputFormat {
73+
case .json(let pretty):
74+
if pretty {
75+
commandLine += ["-pretty-print"]
76+
}
6877
}
6978
commandLine += ["-output-dir", outputDirectory.pathString]
7079

7180
// Run the extraction.
7281
let process = Process(
7382
arguments: commandLine,
7483
outputRedirection: outputRedirection,
75-
verbose: verbose)
84+
verbose: logLevel <= .info)
7685
try process.launch()
7786
try process.waitUntilExit()
7887
}

Tests/CommandsTests/PackageToolTests.swift

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1508,18 +1508,20 @@ final class PackageToolTests: CommandsTestCase {
15081508
// Check that if we don't pass any target, we successfully get symbol graph information for all targets in the package, and at different paths.
15091509
do {
15101510
let result = try SwiftPMProduct.SwiftPackage.executeProcess(["generate-documentation"], packagePath: packageDir)
1511-
XCTAssertEqual(result.exitStatus, .terminated(code: 0))
1512-
XCTAssertMatch(try result.utf8Output(), .and(.contains("MyLibrary:"), .contains("mypackage/MyLibrary")))
1513-
XCTAssertMatch(try result.utf8Output(), .and(.contains("MyCommand:"), .contains("mypackage/MyCommand")))
1511+
let output = try result.utf8Output() + result.utf8stderrOutput()
1512+
XCTAssertEqual(result.exitStatus, .terminated(code: 0), "output: \(output)")
1513+
XCTAssertMatch(output, .and(.contains("MyLibrary:"), .contains("mypackage/MyLibrary")))
1514+
XCTAssertMatch(output, .and(.contains("MyCommand:"), .contains("mypackage/MyCommand")))
15141515

15151516
}
15161517

15171518
// Check that if we pass a target, we successfully get symbol graph information for just the target we asked for.
15181519
do {
15191520
let result = try SwiftPMProduct.SwiftPackage.executeProcess(["--target", "MyLibrary", "generate-documentation"], packagePath: packageDir)
1520-
XCTAssertEqual(result.exitStatus, .terminated(code: 0))
1521-
XCTAssertMatch(try result.utf8Output(), .and(.contains("MyLibrary:"), .contains("mypackage/MyLibrary")))
1522-
XCTAssertNoMatch(try result.utf8Output(), .and(.contains("MyCommand:"), .contains("mypackage/MyCommand")))
1521+
let output = try result.utf8Output() + result.utf8stderrOutput()
1522+
XCTAssertEqual(result.exitStatus, .terminated(code: 0), "output: \(output)")
1523+
XCTAssertMatch(output, .and(.contains("MyLibrary:"), .contains("mypackage/MyLibrary")))
1524+
XCTAssertNoMatch(output, .and(.contains("MyCommand:"), .contains("mypackage/MyCommand")))
15231525
}
15241526
}
15251527
}

0 commit comments

Comments
 (0)