Skip to content

Commit 8172c06

Browse files
committed
Merge into single subcommand
1 parent 6e48824 commit 8172c06

7 files changed

+57
-73
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,10 @@ description of the configuration format and the settings that are available.
219219

220220
#### Viewing the Effective Configuration
221221

222-
The `dump-effective-configuration` subcommand dumps the configuration that
223-
would be used if `swift-format` was executed from the current working directory,
224-
and accounts for `.swift-format` files or `--configuration` options as outlined
225-
above.
222+
The `dump-configuration` subcommand accepts a `--effective` flag. If set, it
223+
dumps the configuration that would be used if `swift-format` was executed from
224+
the current working directory, and accounts for `.swift-format` files or
225+
`--configuration` options as outlined above.
226226

227227
### Miscellaneous
228228

Sources/swift-format/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,12 @@ add_executable(swift-format
1212
SwiftFormatCommand.swift
1313
VersionOptions.swift
1414
Frontend/ConfigurationLoader.swift
15-
Frontend/DumpEffectiveConfigurationFrontend.swift
15+
Frontend/DumpConfigurationFrontend.swift
1616
Frontend/FormatFrontend.swift
1717
Frontend/Frontend.swift
1818
Frontend/LintFrontend.swift
1919
Subcommands/ConfigurationOptions.swift
2020
Subcommands/DumpConfiguration.swift
21-
Subcommands/DumpEffectiveConfiguration.swift
2221
Subcommands/Format.swift
2322
Subcommands/Lint.swift
2423
Subcommands/LintFormatOptions.swift

Sources/swift-format/Frontend/DumpEffectiveConfigurationFrontend.swift renamed to Sources/swift-format/Frontend/DumpConfigurationFrontend.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313
import Foundation
1414
import SwiftFormat
1515

16-
/// The frontend for dumping the effective configuration.
17-
class DumpEffectiveConfigurationFrontend: Frontend {
18-
private(set) var dumpResult: Result<String, Error> = .failure(
19-
SwiftFormatError.configurationDumpFailed("Configuration not resolved yet")
16+
/// The frontend for dumping the configuration.
17+
class DumpConfigurationFrontend: Frontend {
18+
private(set) var dumpedConfiguration: Result<String, Error> = .failure(
19+
SwiftFormatError.configurationDumpFailed("Configuration not dumped yet")
2020
)
2121

2222
override func processFile(_ fileToProcess: FileToProcess) {
23-
dumpResult = Result.init(catching: fileToProcess.configuration.asJsonString)
23+
dumpedConfiguration = Result.init(catching: fileToProcess.configuration.asJsonString)
2424
}
2525
}

Sources/swift-format/Subcommands/ConfigurationOptions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
import ArgumentParser
1414

15-
/// Common arguments used by the `lint`, `format` and `dump-effective-configuration` subcommands.
15+
/// Common arguments used by the `lint`, `format` and `dump-configuration` subcommands.
1616
struct ConfigurationOptions: ParsableArguments {
1717
/// The path to the JSON configuration file that should be loaded.
1818
///

Sources/swift-format/Subcommands/DumpConfiguration.swift

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2025 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -15,16 +15,56 @@ import Foundation
1515
import SwiftFormat
1616

1717
extension SwiftFormatCommand {
18-
/// Dumps the tool's default configuration in JSON format to standard output.
18+
/// Dumps the tool's configuration in JSON format to standard output.
1919
struct DumpConfiguration: ParsableCommand {
2020
static var configuration = CommandConfiguration(
21-
abstract: "Dump the default configuration in JSON format to standard output"
21+
abstract: "Dump the configuration in JSON format to standard output",
22+
discussion: """
23+
Without any options, dumps the default configuration. When '--effective' is set, dumps the configuration that \
24+
would be used if swift-format was executed from the current working directory (cwd), incorporating \
25+
configuration files found in the cwd or its parents, or input from the '--configuration' option.
26+
"""
2227
)
2328

29+
/// Whether or not to dump the effective configuration.
30+
@Flag(name: .shortAndLong, help: "Dump the effective instead of the default configuration.")
31+
var effective: Bool = false
32+
33+
@OptionGroup()
34+
var configurationOptions: ConfigurationOptions
35+
36+
func validate() throws {
37+
if configurationOptions.configuration != nil && !effective {
38+
throw ValidationError("'--configuration' is only valid in combination with '--effective'")
39+
}
40+
}
41+
2442
func run() throws {
25-
let configuration = try Configuration().asJsonString()
43+
if !effective {
44+
let configuration = try Configuration().asJsonString()
45+
print(configuration)
46+
return
47+
}
48+
49+
// Pretend to use stdin, so that the configuration loading machinery in the Frontend base class can be used in the
50+
// next step. This produces the same results as if "format" or "lint" subcommands were called.
51+
let lintFormatOptions = try LintFormatOptions.parse(["-"])
52+
53+
let frontend = DumpConfigurationFrontend(
54+
configurationOptions: configurationOptions,
55+
lintFormatOptions: lintFormatOptions
56+
)
57+
frontend.run()
58+
if frontend.diagnosticsEngine.hasErrors {
59+
throw ExitCode.failure
60+
}
2661

27-
print(configuration)
62+
switch frontend.dumpedConfiguration {
63+
case .success(let configuration):
64+
print(configuration)
65+
case .failure(let error):
66+
throw error
67+
}
2868
}
2969
}
3070
}

Sources/swift-format/Subcommands/DumpEffectiveConfiguration.swift

Lines changed: 0 additions & 54 deletions
This file was deleted.

Sources/swift-format/SwiftFormatCommand.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2025 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -21,7 +21,6 @@ struct SwiftFormatCommand: ParsableCommand {
2121
abstract: "Format or lint Swift source code",
2222
subcommands: [
2323
DumpConfiguration.self,
24-
DumpEffectiveConfiguration.self,
2524
Format.self,
2625
Lint.self,
2726
],

0 commit comments

Comments
 (0)