@@ -31,6 +31,7 @@ const int _exitFlutterFormatFailed = 4;
3131const int _exitJavaFormatFailed = 5 ;
3232const int _exitGitFailed = 6 ;
3333const int _exitDependencyMissing = 7 ;
34+ const int _exitSwiftFormatFailed = 8 ;
3435
3536final Uri _googleFormatterUrl = Uri .https ('github.com' ,
3637 '/google/google-java-format/releases/download/google-java-format-1.3/google-java-format-1.3-all-deps.jar' );
@@ -44,18 +45,25 @@ class FormatCommand extends PackageCommand {
4445 super .platform,
4546 }) {
4647 argParser.addFlag ('fail-on-change' , hide: true );
47- argParser.addOption ('clang-format' ,
48+ argParser.addOption (_clangFormatArg ,
4849 defaultsTo: 'clang-format' , help: 'Path to "clang-format" executable.' );
49- argParser.addOption ('java' ,
50+ argParser.addOption (_javaArg ,
5051 defaultsTo: 'java' , help: 'Path to "java" executable.' );
52+ argParser.addOption (_swiftFormatArg,
53+ help: 'Path to "swift-format" executable.' );
5154 }
5255
56+ static const String _clangFormatArg = 'clang-format' ;
57+ static const String _javaArg = 'java' ;
58+ static const String _swiftFormatArg = 'swift-format' ;
59+
5360 @override
5461 final String name = 'format' ;
5562
5663 @override
5764 final String description =
58- 'Formats the code of all packages (Java, Objective-C, C++, and Dart).\n\n '
65+ 'Formats the code of all packages (Java, Objective-C, C++, Dart, and '
66+ 'optionally Swift).\n\n '
5967 'This command requires "git", "flutter" and "clang-format" v5 to be in '
6068 'your path.' ;
6169
@@ -71,6 +79,10 @@ class FormatCommand extends PackageCommand {
7179 await _formatDart (files);
7280 await _formatJava (files, googleFormatterPath);
7381 await _formatCppAndObjectiveC (files);
82+ final String ? swiftFormat = getNullableStringArg (_swiftFormatArg);
83+ if (swiftFormat != null ) {
84+ await _formatSwift (swiftFormat, files);
85+ }
7486
7587 if (getBoolArg ('fail-on-change' )) {
7688 final bool modified = await _didModifyAnything ();
@@ -141,10 +153,24 @@ class FormatCommand extends PackageCommand {
141153 }
142154 }
143155
156+ Future <void > _formatSwift (String swiftFormat, Iterable <String > files) async {
157+ final Iterable <String > swiftFiles =
158+ _getPathsWithExtensions (files, < String > {'.swift' });
159+ if (swiftFiles.isNotEmpty) {
160+ print ('Formatting .swift files...' );
161+ final int exitCode =
162+ await _runBatched (swiftFormat, < String > ['-i' ], files: swiftFiles);
163+ if (exitCode != 0 ) {
164+ printError ('Failed to format Swift files: exit code $exitCode .' );
165+ throw ToolExit (_exitSwiftFormatFailed);
166+ }
167+ }
168+ }
169+
144170 Future <String > _findValidClangFormat () async {
145- final String clangFormatArg = getStringArg ('clang-format' );
146- if (await _hasDependency (clangFormatArg )) {
147- return clangFormatArg ;
171+ final String clangFormat = getStringArg (_clangFormatArg );
172+ if (await _hasDependency (clangFormat )) {
173+ return clangFormat ;
148174 }
149175
150176 // There is a known issue where "chromium/depot_tools/clang-format"
0 commit comments