Skip to content
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ Options:
--dry-run Format files, but do not save changes to disk.
--check Terminates with a non-zero exit code if any files were formatted.
--files A comma separated list of relative file paths to format. All files are formatted if empty.
--exclude A comma separated list of relative file or folder paths to exclude from formatting.
--version Display version information
--report Writes a json file to the given directory. Defaults to 'format-report.json' if no filename given.
```
Expand Down
53 changes: 37 additions & 16 deletions src/CodeFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static async Task<WorkspaceFormatResult> FormatWorkspaceAsync(
ILogger logger,
CancellationToken cancellationToken)
{
var (workspaceFilePath, workspaceType, logLevel, saveFormattedFiles, _, filesToFormat, reportPath) = options;
var (workspaceFilePath, workspaceType, logLevel, saveFormattedFiles, _, filesToFormat, filesToIgnore, reportPath) = options;
var logWorkspaceWarnings = logLevel == LogLevel.Trace;

logger.LogInformation(string.Format(Resources.Formatting_code_files_in_workspace_0, workspaceFilePath));
Expand All @@ -60,7 +60,7 @@ public static async Task<WorkspaceFormatResult> FormatWorkspaceAsync(
logger.LogTrace(Resources.Determining_formattable_files);

var (fileCount, formatableFiles) = await DetermineFormattableFiles(
solution, projectPath, filesToFormat, logger, cancellationToken).ConfigureAwait(false);
solution, projectPath, filesToFormat, filesToIgnore, logger, cancellationToken).ConfigureAwait(false);

var determineFilesMS = workspaceStopwatch.ElapsedMilliseconds - loadWorkspaceMS;
logger.LogTrace(Resources.Complete_in_0_ms, determineFilesMS);
Expand Down Expand Up @@ -242,6 +242,7 @@ private static async Task<Solution> RunCodeFormattersAsync(
Solution solution,
string projectPath,
ImmutableHashSet<string> filesToFormat,
ImmutableHashSet<string> filesToIgnore,
ILogger logger,
CancellationToken cancellationToken)
{
Expand Down Expand Up @@ -271,7 +272,7 @@ private static async Task<Solution> RunCodeFormattersAsync(

// Get project documents and options with .editorconfig settings applied.
var getProjectDocuments = project.DocumentIds.Select(documentId => GetDocumentAndOptions(
project, documentId, filesToFormat, codingConventionsManager, optionsApplier, cancellationToken));
project, documentId, filesToFormat, filesToIgnore, codingConventionsManager, optionsApplier, cancellationToken));
getDocumentsAndOptions.AddRange(getProjectDocuments);
}

Expand Down Expand Up @@ -310,25 +311,14 @@ private static async Task<Solution> RunCodeFormattersAsync(
Project project,
DocumentId documentId,
ImmutableHashSet<string> filesToFormat,
ImmutableHashSet<string> filesToIgnore,
ICodingConventionsManager codingConventionsManager,
EditorConfigOptionsApplier optionsApplier,
CancellationToken cancellationToken)
{
var document = project.Solution.GetDocument(documentId);

// If a files list was passed in, then ignore files not present in the list.
if (!filesToFormat.IsEmpty && !filesToFormat.Contains(document.FilePath))
{
return (null, null, null, false);
}

if (!document.SupportsSyntaxTree)
{
return (null, null, null, false);
}

// Ignore generated code files.
if (await GeneratedCodeUtilities.IsGeneratedCodeAsync(document, cancellationToken).ConfigureAwait(false))
if (await ShouldIgnoreDocument(document, filesToFormat, filesToIgnore, cancellationToken))
{
return (null, null, null, false);
}
Expand All @@ -347,5 +337,36 @@ private static async Task<Solution> RunCodeFormattersAsync(
options = optionsApplier.ApplyConventions(options, context.CurrentConventions, project.Language);
return (document, options, context.CurrentConventions, true);
}

private static async Task<bool> ShouldIgnoreDocument(
Document document,
ImmutableHashSet<string> filesToFormat,
ImmutableHashSet<string> filesToIgnore,
CancellationToken cancellationToken)
{
if (!filesToFormat.IsEmpty && !filesToFormat.Contains(document.FilePath))
{
// If a files list was passed in, then ignore files not present in the list.
return true;
}
else if (!document.SupportsSyntaxTree)
{
return true;
}
else if (await GeneratedCodeUtilities.IsGeneratedCodeAsync(document, cancellationToken).ConfigureAwait(false))
{
// Ignore generated code files.
return true;
}
else if (!filesToIgnore.IsEmpty && filesToIgnore.Any(f => document.FilePath.StartsWith(f, StringComparison.OrdinalIgnoreCase)))
{
// Ignore file in, or under a folder in the list to exclude
return true;
}
else
{
return false;
}
}
}
}
5 changes: 5 additions & 0 deletions src/FormatOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ internal class FormatOptions
public bool SaveFormattedFiles { get; }
public bool ChangesAreErrors { get; }
public ImmutableHashSet<string> FilesToFormat { get; }
public ImmutableHashSet<string> FilesToIgnore { get; }
public string ReportPath { get; }

public FormatOptions(
Expand All @@ -22,6 +23,7 @@ public FormatOptions(
bool saveFormattedFiles,
bool changesAreErrors,
ImmutableHashSet<string> filesToFormat,
ImmutableHashSet<string> filesToIgnore,
string reportPath)
{
WorkspaceFilePath = workspaceFilePath;
Expand All @@ -30,6 +32,7 @@ public FormatOptions(
SaveFormattedFiles = saveFormattedFiles;
ChangesAreErrors = changesAreErrors;
FilesToFormat = filesToFormat;
FilesToIgnore = filesToIgnore;
ReportPath = reportPath;
}

Expand All @@ -40,6 +43,7 @@ public void Deconstruct(
out bool saveFormattedFiles,
out bool changesAreErrors,
out ImmutableHashSet<string> filesToFormat,
out ImmutableHashSet<string> filesToIgnore,
out string reportPath)
{
workspaceFilePath = WorkspaceFilePath;
Expand All @@ -48,6 +52,7 @@ public void Deconstruct(
saveFormattedFiles = SaveFormattedFiles;
changesAreErrors = ChangesAreErrors;
filesToFormat = FilesToFormat;
filesToIgnore = FilesToIgnore;
reportPath = ReportPath;
}
}
Expand Down
9 changes: 6 additions & 3 deletions src/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,15 @@ private static async Task<int> Main(string[] args)
.AddOption(new Option(new[] { "--dry-run" }, Resources.Format_files_but_do_not_save_changes_to_disk, new Argument<bool>()))
.AddOption(new Option(new[] { "--check" }, Resources.Terminate_with_a_non_zero_exit_code_if_any_files_were_formatted, new Argument<bool>()))
.AddOption(new Option(new[] { "--files" }, Resources.A_comma_separated_list_of_relative_file_paths_to_format_All_files_are_formatted_if_empty, new Argument<string>(() => null)))
.AddOption(new Option(new[] { "--exclude" }, Resources.A_comma_separated_list_of_relative_file_or_folder_paths_to_exclude_from_formatting, new Argument<string>(() => null)))
.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)))
.UseVersionOption()
.Build();

return await parser.InvokeAsync(args).ConfigureAwait(false);
}

public static async Task<int> Run(string folder, string workspace, string verbosity, bool dryRun, bool check, string files, string report, IConsole console = null)
public static async Task<int> Run(string folder, string workspace, string verbosity, bool dryRun, bool check, string files, string exclude, string report, IConsole console = null)
{
// Setup logging.
var serviceCollection = new ServiceCollection();
Expand Down Expand Up @@ -101,7 +102,8 @@ public static async Task<int> Run(string folder, string workspace, string verbos

Environment.CurrentDirectory = workspaceDirectory;

var filesToFormat = GetFilesToFormat(files, folder);
var filesToFormat = GetFiles(files, folder);
var filesToIgnore = GetFiles(exclude, folder);

// Since we are running as a dotnet tool we should be able to find an instance of
// MSBuild in a .NET Core SDK.
Expand All @@ -122,6 +124,7 @@ public static async Task<int> Run(string folder, string workspace, string verbos
saveFormattedFiles: !dryRun,
changesAreErrors: check,
filesToFormat,
filesToIgnore,
reportPath: report);

var formatResult = await CodeFormatter.FormatWorkspaceAsync(
Expand Down Expand Up @@ -192,7 +195,7 @@ private static void ConfigureServices(ServiceCollection serviceCollection, ICons
/// <summary>
/// Converts a comma-separated list of relative file paths to a hashmap of full file paths.
/// </summary>
internal static ImmutableHashSet<string> GetFilesToFormat(string files, string folder)
internal static ImmutableHashSet<string> GetFiles(string files, string folder)
{
if (string.IsNullOrEmpty(files))
{
Expand Down
3 changes: 3 additions & 0 deletions src/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,7 @@
<data name="Writing_formatting_report_to_0" xml:space="preserve">
<value>Writing formatting report to: '{0}'.</value>
</data>
<data name="A_comma_separated_list_of_relative_file_or_folder_paths_to_exclude_from_formatting" xml:space="preserve">
<value>A comma separated list of relative file or folder paths to exclude from formatting</value>
</data>
</root>
5 changes: 5 additions & 0 deletions src/xlf/Resources.cs.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="cs" original="../Resources.resx">
<body>
<trans-unit id="A_comma_separated_list_of_relative_file_or_folder_paths_to_exclude_from_formatting">
<source>A comma separated list of relative file or folder paths to exclude from formatting</source>
<target state="new">A comma separated list of relative file or folder paths to exclude from formatting</target>
<note />
</trans-unit>
<trans-unit id="A_comma_separated_list_of_relative_file_paths_to_format_All_files_are_formatted_if_empty">
<source>A comma separated list of relative file paths to format. All files are formatted if empty.</source>
<target state="new">A comma separated list of relative file paths to format. All files are formatted if empty.</target>
Expand Down
5 changes: 5 additions & 0 deletions src/xlf/Resources.de.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="de" original="../Resources.resx">
<body>
<trans-unit id="A_comma_separated_list_of_relative_file_or_folder_paths_to_exclude_from_formatting">
<source>A comma separated list of relative file or folder paths to exclude from formatting</source>
<target state="new">A comma separated list of relative file or folder paths to exclude from formatting</target>
<note />
</trans-unit>
<trans-unit id="A_comma_separated_list_of_relative_file_paths_to_format_All_files_are_formatted_if_empty">
<source>A comma separated list of relative file paths to format. All files are formatted if empty.</source>
<target state="new">A comma separated list of relative file paths to format. All files are formatted if empty.</target>
Expand Down
5 changes: 5 additions & 0 deletions src/xlf/Resources.es.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="es" original="../Resources.resx">
<body>
<trans-unit id="A_comma_separated_list_of_relative_file_or_folder_paths_to_exclude_from_formatting">
<source>A comma separated list of relative file or folder paths to exclude from formatting</source>
<target state="new">A comma separated list of relative file or folder paths to exclude from formatting</target>
<note />
</trans-unit>
<trans-unit id="A_comma_separated_list_of_relative_file_paths_to_format_All_files_are_formatted_if_empty">
<source>A comma separated list of relative file paths to format. All files are formatted if empty.</source>
<target state="new">A comma separated list of relative file paths to format. All files are formatted if empty.</target>
Expand Down
5 changes: 5 additions & 0 deletions src/xlf/Resources.fr.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="fr" original="../Resources.resx">
<body>
<trans-unit id="A_comma_separated_list_of_relative_file_or_folder_paths_to_exclude_from_formatting">
<source>A comma separated list of relative file or folder paths to exclude from formatting</source>
<target state="new">A comma separated list of relative file or folder paths to exclude from formatting</target>
<note />
</trans-unit>
<trans-unit id="A_comma_separated_list_of_relative_file_paths_to_format_All_files_are_formatted_if_empty">
<source>A comma separated list of relative file paths to format. All files are formatted if empty.</source>
<target state="new">A comma separated list of relative file paths to format. All files are formatted if empty.</target>
Expand Down
5 changes: 5 additions & 0 deletions src/xlf/Resources.it.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="it" original="../Resources.resx">
<body>
<trans-unit id="A_comma_separated_list_of_relative_file_or_folder_paths_to_exclude_from_formatting">
<source>A comma separated list of relative file or folder paths to exclude from formatting</source>
<target state="new">A comma separated list of relative file or folder paths to exclude from formatting</target>
<note />
</trans-unit>
<trans-unit id="A_comma_separated_list_of_relative_file_paths_to_format_All_files_are_formatted_if_empty">
<source>A comma separated list of relative file paths to format. All files are formatted if empty.</source>
<target state="new">A comma separated list of relative file paths to format. All files are formatted if empty.</target>
Expand Down
5 changes: 5 additions & 0 deletions src/xlf/Resources.ja.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="ja" original="../Resources.resx">
<body>
<trans-unit id="A_comma_separated_list_of_relative_file_or_folder_paths_to_exclude_from_formatting">
<source>A comma separated list of relative file or folder paths to exclude from formatting</source>
<target state="new">A comma separated list of relative file or folder paths to exclude from formatting</target>
<note />
</trans-unit>
<trans-unit id="A_comma_separated_list_of_relative_file_paths_to_format_All_files_are_formatted_if_empty">
<source>A comma separated list of relative file paths to format. All files are formatted if empty.</source>
<target state="new">A comma separated list of relative file paths to format. All files are formatted if empty.</target>
Expand Down
5 changes: 5 additions & 0 deletions src/xlf/Resources.ko.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="ko" original="../Resources.resx">
<body>
<trans-unit id="A_comma_separated_list_of_relative_file_or_folder_paths_to_exclude_from_formatting">
<source>A comma separated list of relative file or folder paths to exclude from formatting</source>
<target state="new">A comma separated list of relative file or folder paths to exclude from formatting</target>
<note />
</trans-unit>
<trans-unit id="A_comma_separated_list_of_relative_file_paths_to_format_All_files_are_formatted_if_empty">
<source>A comma separated list of relative file paths to format. All files are formatted if empty.</source>
<target state="new">A comma separated list of relative file paths to format. All files are formatted if empty.</target>
Expand Down
5 changes: 5 additions & 0 deletions src/xlf/Resources.pl.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="pl" original="../Resources.resx">
<body>
<trans-unit id="A_comma_separated_list_of_relative_file_or_folder_paths_to_exclude_from_formatting">
<source>A comma separated list of relative file or folder paths to exclude from formatting</source>
<target state="new">A comma separated list of relative file or folder paths to exclude from formatting</target>
<note />
</trans-unit>
<trans-unit id="A_comma_separated_list_of_relative_file_paths_to_format_All_files_are_formatted_if_empty">
<source>A comma separated list of relative file paths to format. All files are formatted if empty.</source>
<target state="new">A comma separated list of relative file paths to format. All files are formatted if empty.</target>
Expand Down
5 changes: 5 additions & 0 deletions src/xlf/Resources.pt-BR.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="pt-BR" original="../Resources.resx">
<body>
<trans-unit id="A_comma_separated_list_of_relative_file_or_folder_paths_to_exclude_from_formatting">
<source>A comma separated list of relative file or folder paths to exclude from formatting</source>
<target state="new">A comma separated list of relative file or folder paths to exclude from formatting</target>
<note />
</trans-unit>
<trans-unit id="A_comma_separated_list_of_relative_file_paths_to_format_All_files_are_formatted_if_empty">
<source>A comma separated list of relative file paths to format. All files are formatted if empty.</source>
<target state="new">A comma separated list of relative file paths to format. All files are formatted if empty.</target>
Expand Down
Loading