Skip to content

Commit 032c515

Browse files
authored
Combine --check and --dry-run into a single option. (#541)
1 parent 8e09d67 commit 032c515

18 files changed

Lines changed: 89 additions & 84 deletions

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ Options:
5252
the current directory for one.
5353
--verbosity, -v Set the verbosity level. Allowed values are q[uiet], m[inimal], n[ormal], d[etailed], and
5454
diag[nostic]
55-
--dry-run Format files, but do not save changes to disk.
56-
--check Terminates with a non-zero exit code if any files were formatted.
55+
--check, --dry-run Formats files without saving changes to disk. Terminates with a non-zero exit code if any files
56+
were formatted.
5757
--include, --files A comma separated list of relative file or folder paths to include in formatting. All files are
5858
formatted if empty.
5959
--exclude A comma separated list of relative file or folder paths to exclude from formatting.
@@ -70,7 +70,7 @@ Add `format` after `dotnet` and before the command arguments that you want to ru
7070
| dotnet **format** -w <workspace> | Formats a specific project or solution. |
7171
| dotnet **format** -v diag | Formats with very verbose logging. |
7272
| dotnet **format** --include Programs.cs,Utility\Logging.cs | Formats the files Program.cs and Utility\Logging.cs |
73-
| dotnet **format** --check --dry-run | Formats but does not save. Returns a non-zero exit code if any files would have been changed. |
73+
| dotnet **format** --check | Formats but does not save. Returns a non-zero exit code if any files would have been changed. |
7474
| dotnet **format** --report <report-path> | Formats and saves a json report file to the given directory. |
7575

7676
### How To Uninstall

eng/format-verifier.ps1

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,13 @@ try {
5858
Write-Output "$(Get-Date) - $solutionFile - Formatting Workspace"
5959
$output = dotnet.exe run -p "$currentLocation\src\dotnet-format.csproj" -c Release -- -w $solution -v d --dry-run | Out-String
6060
Write-Output $output.TrimEnd()
61-
62-
if ($LastExitCode -ne 0) {
61+
62+
# Ignore CheckFailedExitCode since we don't expect these repos to be properly formatted.
63+
if ($LastExitCode -ne 0 -and $LastExitCode -ne 2) {
6364
Write-Output "$(Get-Date) - Formatting failed with error code $LastExitCode."
6465
exit -1
6566
}
66-
67+
6768
if (($output -notmatch "(?m)Formatted \d+ of (\d+) files") -or ($Matches[1] -eq "0")) {
6869
Write-Output "$(Get-Date) - No files found for solution."
6970
exit -1
@@ -78,19 +79,22 @@ try {
7879
Write-Output "$(Get-Date) - $folderName - Formatting Folder"
7980
$output = dotnet.exe run -p "$currentLocation\src\dotnet-format.csproj" -c Release -- -f $repoPath -v d --dry-run | Out-String
8081
Write-Output $output.TrimEnd()
81-
82-
if ($LastExitCode -ne 0) {
82+
83+
# Ignore CheckFailedExitCode since we don't expect these repos to be properly formatted.
84+
if ($LastExitCode -ne 0 -and $LastExitCode -ne 2) {
8385
Write-Output "$(Get-Date) - Formatting failed with error code $LastExitCode."
8486
exit -1
8587
}
86-
88+
8789
if (($output -notmatch "(?m)Formatted \d+ of (\d+) files") -or ($Matches[1] -eq "0")) {
8890
Write-Output "$(Get-Date) - No files found for solution."
8991
exit -1
9092
}
9193

9294
Write-Output "$(Get-Date) - $folderName - Complete"
9395
}
96+
97+
exit 0
9498
}
9599
catch {
96100
exit -1

src/Program.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ namespace Microsoft.CodeAnalysis.Tools
1919
internal class Program
2020
{
2121
private static readonly string[] _verbosityLevels = new[] { "q", "quiet", "m", "minimal", "n", "normal", "d", "detailed", "diag", "diagnostic" };
22+
internal const int UnhandledExceptionExitCode = 1;
23+
internal const int CheckFailedExitCode = 2;
2224

2325
private static async Task<int> Main(string[] args)
2426
{
@@ -33,8 +35,7 @@ private static async Task<int> Main(string[] args)
3335
.AddOption(new Option(new[] { "--folder", "-f" }, Resources.The_folder_to_operate_on_Cannot_be_used_with_the_workspace_option, new Argument<string>(() => null)))
3436
.AddOption(new Option(new[] { "--workspace", "-w" }, Resources.The_solution_or_project_file_to_operate_on_If_a_file_is_not_specified_the_command_will_search_the_current_directory_for_one, new Argument<string>(() => null)))
3537
.AddOption(new Option(new[] { "--verbosity", "-v" }, Resources.Set_the_verbosity_level_Allowed_values_are_quiet_minimal_normal_detailed_and_diagnostic, new Argument<string>() { Arity = ArgumentArity.ExactlyOne }.FromAmong(_verbosityLevels)))
36-
.AddOption(new Option(new[] { "--dry-run" }, Resources.Format_files_but_do_not_save_changes_to_disk, new Argument<bool>()))
37-
.AddOption(new Option(new[] { "--check" }, Resources.Terminate_with_a_non_zero_exit_code_if_any_files_were_formatted, new Argument<bool>()))
38+
.AddOption(new Option(new[] { "--check", "--dry-run" }, Resources.Formats_files_without_saving_changes_to_disk_Terminate_with_a_non_zero_exit_code_if_any_files_were_formatted, new Argument<bool>()))
3839
.AddOption(new Option(new[] { "--include", "--files" }, Resources.A_comma_separated_list_of_relative_file_or_folder_paths_to_include_in_formatting_All_files_are_formatted_if_empty, new Argument<string>(() => null)))
3940
.AddOption(new Option(new[] { "--exclude" }, Resources.A_comma_separated_list_of_relative_file_or_folder_paths_to_exclude_from_formatting, new Argument<string>(() => null)))
4041
.AddOption(new Option(new[] { "--report" }, Resources.Accepts_a_file_path_which_if_provided_will_produce_a_format_report_json_file_in_the_given_directory, new Argument<string>(() => null)))
@@ -44,7 +45,7 @@ private static async Task<int> Main(string[] args)
4445
return await parser.InvokeAsync(args).ConfigureAwait(false);
4546
}
4647

47-
public static async Task<int> Run(string folder, string workspace, string verbosity, bool dryRun, bool check, string include, string exclude, string report, IConsole console = null)
48+
public static async Task<int> Run(string folder, string workspace, string verbosity, bool check, string include, string exclude, string report, IConsole console = null)
4849
{
4950
// Setup logging.
5051
var serviceCollection = new ServiceCollection();
@@ -121,7 +122,7 @@ public static async Task<int> Run(string folder, string workspace, string verbos
121122
workspacePath,
122123
workspaceType,
123124
logLevel,
124-
saveFormattedFiles: !dryRun,
125+
saveFormattedFiles: !check,
125126
changesAreErrors: check,
126127
pathsToInclude,
127128
pathsToExclude,
@@ -137,11 +138,11 @@ public static async Task<int> Run(string folder, string workspace, string verbos
137138
catch (FileNotFoundException fex)
138139
{
139140
logger.LogError(fex.Message);
140-
return 1;
141+
return UnhandledExceptionExitCode;
141142
}
142143
catch (OperationCanceledException)
143144
{
144-
return 1;
145+
return UnhandledExceptionExitCode;
145146
}
146147
finally
147148
{
@@ -159,7 +160,7 @@ internal static int GetExitCode(WorkspaceFormatResult formatResult, bool check)
159160
return formatResult.ExitCode;
160161
}
161162

162-
return formatResult.FilesFormatted == 0 ? 0 : 1;
163+
return formatResult.FilesFormatted == 0 ? 0 : CheckFailedExitCode;
163164
}
164165

165166
internal static LogLevel GetLogLevel(string verbosity)

src/Resources.resx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,8 @@
171171
<data name="Format_files_but_do_not_save_changes_to_disk" xml:space="preserve">
172172
<value>Format files, but do not save changes to disk.</value>
173173
</data>
174-
<data name="Terminate_with_a_non_zero_exit_code_if_any_files_were_formatted" xml:space="preserve">
175-
<value>Terminates with a non-zero exit code if any files were formatted.</value>
174+
<data name="Formats_files_without_saving_changes_to_disk_Terminate_with_a_non_zero_exit_code_if_any_files_were_formatted" xml:space="preserve">
175+
<value>Formats files without saving changes to disk. Terminates with a non-zero exit code if any files were formatted.</value>
176176
</data>
177177
<data name="A_comma_separated_list_of_relative_file_or_folder_paths_to_include_in_formatting_All_files_are_formatted_if_empty" xml:space="preserve">
178178
<value>A comma separated list of relative file or folder paths to include in formatting. All files are formatted if empty.</value>

src/xlf/Resources.cs.xlf

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@
8282
<target state="translated">Soubory se naformátují, ale změny se neuloží na disk.</target>
8383
<note />
8484
</trans-unit>
85+
<trans-unit id="Formats_files_without_saving_changes_to_disk_Terminate_with_a_non_zero_exit_code_if_any_files_were_formatted">
86+
<source>Formats files without saving changes to disk. Terminates with a non-zero exit code if any files were formatted.</source>
87+
<target state="new">Formats files without saving changes to disk. Terminates with a non-zero exit code if any files were formatted.</target>
88+
<note />
89+
</trans-unit>
8590
<trans-unit id="Formatted_0_of_1_files">
8691
<source>Formatted {0} of {1} files.</source>
8792
<target state="new">Formatted {0} of {1} files.</target>
@@ -127,11 +132,6 @@
127132
<target state="translated">Přeskočí se odkazovaný projekt {0}.</target>
128133
<note />
129134
</trans-unit>
130-
<trans-unit id="Terminate_with_a_non_zero_exit_code_if_any_files_were_formatted">
131-
<source>Terminates with a non-zero exit code if any files were formatted.</source>
132-
<target state="new">Terminates with a non-zero exit code if any files were formatted.</target>
133-
<note />
134-
</trans-unit>
135135
<trans-unit id="The_file_0_does_not_appear_to_be_a_valid_project_or_solution_file">
136136
<source>The file '{0}' does not appear to be a valid project or solution file.</source>
137137
<target state="translated">Soubor {0} zřejmě není platný soubor projektu nebo řešení.</target>

src/xlf/Resources.de.xlf

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@
8282
<target state="translated">Dateien formatieren, Änderungen aber nicht auf Festplatte speichern.</target>
8383
<note />
8484
</trans-unit>
85+
<trans-unit id="Formats_files_without_saving_changes_to_disk_Terminate_with_a_non_zero_exit_code_if_any_files_were_formatted">
86+
<source>Formats files without saving changes to disk. Terminates with a non-zero exit code if any files were formatted.</source>
87+
<target state="new">Formats files without saving changes to disk. Terminates with a non-zero exit code if any files were formatted.</target>
88+
<note />
89+
</trans-unit>
8590
<trans-unit id="Formatted_0_of_1_files">
8691
<source>Formatted {0} of {1} files.</source>
8792
<target state="new">Formatted {0} of {1} files.</target>
@@ -127,11 +132,6 @@
127132
<target state="translated">Überspringen von referenziertem Projekt "{0}".</target>
128133
<note />
129134
</trans-unit>
130-
<trans-unit id="Terminate_with_a_non_zero_exit_code_if_any_files_were_formatted">
131-
<source>Terminates with a non-zero exit code if any files were formatted.</source>
132-
<target state="new">Terminates with a non-zero exit code if any files were formatted.</target>
133-
<note />
134-
</trans-unit>
135135
<trans-unit id="The_file_0_does_not_appear_to_be_a_valid_project_or_solution_file">
136136
<source>The file '{0}' does not appear to be a valid project or solution file.</source>
137137
<target state="translated">Die Datei "{0}" ist weder ein gültiges Projekt noch eine Projektmappendatei.</target>

src/xlf/Resources.es.xlf

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@
8282
<target state="translated">Formato de archivos, pero no guardar los cambios al disco.</target>
8383
<note />
8484
</trans-unit>
85+
<trans-unit id="Formats_files_without_saving_changes_to_disk_Terminate_with_a_non_zero_exit_code_if_any_files_were_formatted">
86+
<source>Formats files without saving changes to disk. Terminates with a non-zero exit code if any files were formatted.</source>
87+
<target state="new">Formats files without saving changes to disk. Terminates with a non-zero exit code if any files were formatted.</target>
88+
<note />
89+
</trans-unit>
8590
<trans-unit id="Formatted_0_of_1_files">
8691
<source>Formatted {0} of {1} files.</source>
8792
<target state="new">Formatted {0} of {1} files.</target>
@@ -127,11 +132,6 @@
127132
<target state="translated">Omitiendo projecto al que se hace referencia "{0}".</target>
128133
<note />
129134
</trans-unit>
130-
<trans-unit id="Terminate_with_a_non_zero_exit_code_if_any_files_were_formatted">
131-
<source>Terminates with a non-zero exit code if any files were formatted.</source>
132-
<target state="new">Terminates with a non-zero exit code if any files were formatted.</target>
133-
<note />
134-
</trans-unit>
135135
<trans-unit id="The_file_0_does_not_appear_to_be_a_valid_project_or_solution_file">
136136
<source>The file '{0}' does not appear to be a valid project or solution file.</source>
137137
<target state="translated">El archivo "{0}" no parece ser un proyecto o archivo de solución válido.</target>

src/xlf/Resources.fr.xlf

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@
8282
<target state="translated">Mettez en forme les fichiers, mais n'enregistrez pas les changements sur le disque.</target>
8383
<note />
8484
</trans-unit>
85+
<trans-unit id="Formats_files_without_saving_changes_to_disk_Terminate_with_a_non_zero_exit_code_if_any_files_were_formatted">
86+
<source>Formats files without saving changes to disk. Terminates with a non-zero exit code if any files were formatted.</source>
87+
<target state="new">Formats files without saving changes to disk. Terminates with a non-zero exit code if any files were formatted.</target>
88+
<note />
89+
</trans-unit>
8590
<trans-unit id="Formatted_0_of_1_files">
8691
<source>Formatted {0} of {1} files.</source>
8792
<target state="new">Formatted {0} of {1} files.</target>
@@ -127,11 +132,6 @@
127132
<target state="translated">Saut du projet référencé '{0}'.</target>
128133
<note />
129134
</trans-unit>
130-
<trans-unit id="Terminate_with_a_non_zero_exit_code_if_any_files_were_formatted">
131-
<source>Terminates with a non-zero exit code if any files were formatted.</source>
132-
<target state="new">Terminates with a non-zero exit code if any files were formatted.</target>
133-
<note />
134-
</trans-unit>
135135
<trans-unit id="The_file_0_does_not_appear_to_be_a_valid_project_or_solution_file">
136136
<source>The file '{0}' does not appear to be a valid project or solution file.</source>
137137
<target state="translated">Le fichier '{0}' ne semble pas être un fichier projet ou solution valide.</target>

src/xlf/Resources.it.xlf

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@
8282
<target state="translated">Formatta i file, ma non salvare le modifiche sul disco.</target>
8383
<note />
8484
</trans-unit>
85+
<trans-unit id="Formats_files_without_saving_changes_to_disk_Terminate_with_a_non_zero_exit_code_if_any_files_were_formatted">
86+
<source>Formats files without saving changes to disk. Terminates with a non-zero exit code if any files were formatted.</source>
87+
<target state="new">Formats files without saving changes to disk. Terminates with a non-zero exit code if any files were formatted.</target>
88+
<note />
89+
</trans-unit>
8590
<trans-unit id="Formatted_0_of_1_files">
8691
<source>Formatted {0} of {1} files.</source>
8792
<target state="new">Formatted {0} of {1} files.</target>
@@ -127,11 +132,6 @@
127132
<target state="translated">Il progetto di riferimento '{0}' verrà ignorato.</target>
128133
<note />
129134
</trans-unit>
130-
<trans-unit id="Terminate_with_a_non_zero_exit_code_if_any_files_were_formatted">
131-
<source>Terminates with a non-zero exit code if any files were formatted.</source>
132-
<target state="new">Terminates with a non-zero exit code if any files were formatted.</target>
133-
<note />
134-
</trans-unit>
135135
<trans-unit id="The_file_0_does_not_appear_to_be_a_valid_project_or_solution_file">
136136
<source>The file '{0}' does not appear to be a valid project or solution file.</source>
137137
<target state="translated">Il file '{0}' non sembra essere un file di progetto o di soluzione valido.</target>

src/xlf/Resources.ja.xlf

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@
8282
<target state="translated">ファイルを書式設定しますが、変更をディスクに保存しません。</target>
8383
<note />
8484
</trans-unit>
85+
<trans-unit id="Formats_files_without_saving_changes_to_disk_Terminate_with_a_non_zero_exit_code_if_any_files_were_formatted">
86+
<source>Formats files without saving changes to disk. Terminates with a non-zero exit code if any files were formatted.</source>
87+
<target state="new">Formats files without saving changes to disk. Terminates with a non-zero exit code if any files were formatted.</target>
88+
<note />
89+
</trans-unit>
8590
<trans-unit id="Formatted_0_of_1_files">
8691
<source>Formatted {0} of {1} files.</source>
8792
<target state="new">Formatted {0} of {1} files.</target>
@@ -127,11 +132,6 @@
127132
<target state="translated">参照プロジェクト '{0}' をスキップしています。</target>
128133
<note />
129134
</trans-unit>
130-
<trans-unit id="Terminate_with_a_non_zero_exit_code_if_any_files_were_formatted">
131-
<source>Terminates with a non-zero exit code if any files were formatted.</source>
132-
<target state="new">Terminates with a non-zero exit code if any files were formatted.</target>
133-
<note />
134-
</trans-unit>
135135
<trans-unit id="The_file_0_does_not_appear_to_be_a_valid_project_or_solution_file">
136136
<source>The file '{0}' does not appear to be a valid project or solution file.</source>
137137
<target state="translated">ファイル '{0}' が、有効なプロジェクト ファイルまたはソリューション ファイルではない可能性があります。</target>

0 commit comments

Comments
 (0)