Skip to content

Commit

Permalink
Merge pull request #2320 from crazytonyli/swift/try-multiple-swift-fo…
Browse files Browse the repository at this point in the history
…rmat-commands

Try different commands to run `swift-format`
  • Loading branch information
bendk authored Nov 18, 2024
2 parents 9c796dc + a791a6b commit f8671d1
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions uniffi_bindgen/src/bindings/swift/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,27 @@ impl BindingGenerator for SwiftBindingGenerator {
}

if settings.try_format_code {
if let Err(e) = Command::new("swiftformat")
.arg(source_file.as_str())
.output()
{
let commands_to_try = [
// Available in Xcode 16.
vec!["xcrun", "swift-format"],
// The official swift-format command name.
vec!["swift-format"],
// Shortcut for the swift-format command.
vec!["swift", "format"],
vec!["swiftformat"],
];

let successful_output = commands_to_try.into_iter().find_map(|command| {
Command::new(&command[0])
.args(&command[1..])
.arg(source_file.as_str())
.output()
.ok()
});
if successful_output.is_none() {
println!(
"Warning: Unable to auto-format {} using swiftformat: {e:?}",
source_file.file_name().unwrap(),
"Warning: Unable to auto-format {} using swift-format. Please make sure it is installed.",
source_file.as_str()
);
}
}
Expand Down

0 comments on commit f8671d1

Please sign in to comment.