forked from swiftlang/swift-format
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add dump-effective-configuration subcommand
closes swiftlang#667 Implement the subcommand `dump-effective-configuration`, which dumps the configuration that would be used if `swift-format` was executed from the current working directory (cwd), incorporating configuration files found in the cwd or its parents, or input from the `--configuration` option. This helps when composing a configuration or with configuration debugging/verification activities.
- Loading branch information
1 parent
2f71242
commit e267558
Showing
16 changed files
with
198 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2025 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 the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import Foundation | ||
|
||
extension Configuration { | ||
/// Return the configuration as a JSON string. | ||
public func asJsonString() throws -> String { | ||
let data: Data | ||
|
||
do { | ||
let encoder = JSONEncoder() | ||
encoder.outputFormatting = [.prettyPrinted] | ||
if #available(macOS 10.13, *) { | ||
encoder.outputFormatting.insert(.sortedKeys) | ||
} | ||
|
||
data = try encoder.encode(self) | ||
} catch { | ||
throw SwiftFormatError.configurationDumpFailed("\(error)") | ||
} | ||
|
||
guard let jsonString = String(data: data, encoding: .utf8) else { | ||
// This should never happen, but let's make sure we fail more gracefully than crashing, just in case. | ||
throw SwiftFormatError.configurationDumpFailed("The JSON was not valid UTF-8") | ||
} | ||
|
||
return jsonString | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
Sources/swift-format/Frontend/DumpEffectiveConfigurationFrontend.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2025 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 the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import Foundation | ||
import SwiftFormat | ||
|
||
/// The frontend for dumping the effective configuration. | ||
class DumpEffectiveConfigurationFrontend: Frontend { | ||
private(set) var dumpResult: Result<String, Error> = .failure( | ||
SwiftFormatError.configurationDumpFailed("Configuration not resolved yet") | ||
) | ||
|
||
override func processFile(_ fileToProcess: FileToProcess) { | ||
dumpResult = Result.init(catching: fileToProcess.configuration.asJsonString) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
Sources/swift-format/Subcommands/ConfigurationOptions.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2025 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 the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import ArgumentParser | ||
|
||
/// Common arguments used by the `lint`, `format` and `dump-effective-configuration` subcommands. | ||
struct ConfigurationOptions: ParsableArguments { | ||
/// The path to the JSON configuration file that should be loaded. | ||
/// | ||
/// If not specified, the default configuration will be used. | ||
@Option( | ||
name: .customLong("configuration"), | ||
help: """ | ||
The path to a JSON file containing the configuration of the linter/formatter or a JSON string containing the \ | ||
configuration directly. | ||
""" | ||
) | ||
var configuration: String? | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
Sources/swift-format/Subcommands/DumpEffectiveConfiguration.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2025 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 the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import ArgumentParser | ||
import Foundation | ||
import SwiftFormat | ||
|
||
extension SwiftFormatCommand { | ||
/// Dumps the tool's effective configuration in JSON format to standard output. | ||
struct DumpEffectiveConfiguration: ParsableCommand { | ||
static var configuration = CommandConfiguration( | ||
abstract: "Dump the effective configuration in JSON format to standard output", | ||
discussion: """ | ||
Dumps the configuration that would be used if swift-format was executed from the current working \ | ||
directory (cwd), incorporating configuration files found in the cwd or its parents, or input from the \ | ||
--configuration option. | ||
""" | ||
) | ||
|
||
@OptionGroup() | ||
var configurationOptions: ConfigurationOptions | ||
|
||
func run() throws { | ||
// Pretend to use stdin, so that the configuration loading machinery in the Frontend base class can be used in the | ||
// next step. This produces the same results as if "format" or "lint" subcommands were called. | ||
let lintFormatOptions = try LintFormatOptions.parse(["-"]) | ||
|
||
let frontend = DumpEffectiveConfigurationFrontend( | ||
configurationOptions: configurationOptions, | ||
lintFormatOptions: lintFormatOptions | ||
) | ||
frontend.run() | ||
if frontend.diagnosticsEngine.hasErrors { | ||
throw ExitCode.failure | ||
} | ||
|
||
switch frontend.dumpResult { | ||
case .success(let configuration): | ||
print(configuration) | ||
case .failure(let error): | ||
throw error | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.