Skip to content
This repository was archived by the owner on Jul 12, 2022. It is now read-only.

Commit a390bb6

Browse files
committed
Fixed up command line handling
Change makes a couple of small updates: 1. Standardized the command line argument handling to be /option:data vs. /option data. I chose the colon version because it seemed more consistent with other .Net tools out there. I don't have a strong opinion here though. 2. Changed FilenameFilter to go through Options vs. custom handling the command line argument.
1 parent 20595c9 commit a390bb6

File tree

7 files changed

+59
-37
lines changed

7 files changed

+59
-37
lines changed

src/CodeFormatter/Program.cs

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ private static int Main(string[] args)
1919
{
2020
if (args.Length < 1)
2121
{
22-
Console.WriteLine("CodeFormatter <project or solution> [<rule types>] [/file <filename>] [/nocopyright]");
22+
Console.WriteLine("CodeFormatter <project or solution> [<rule types>] [/file:<filename>] [/nocopyright] [/c:<config1,config2>");
2323
Console.WriteLine(" <rule types> - Rule types to use in addition to the default ones.");
2424
Console.WriteLine(" Use ConvertTests to convert MSTest tests to xUnit.");
25-
Console.WriteLine(" <filename> - Only apply changes to files with specified name.");
25+
Console.WriteLine(" <filename> - Only apply changes to files with specified name.");
26+
Console.WriteLine(" <configs> - Additional preprocessor configurations the formatter")
27+
Console.WriteLine(" should run under");
2628
return -1;
2729
}
2830

@@ -33,37 +35,34 @@ private static int Main(string[] args)
3335
return -1;
3436
}
3537

36-
var ruleTypes = new List<string>();
37-
var filenames = new List<string>();
38-
var disableCopyright = false;
38+
var fileNamesBuilder = ImmutableArray.CreateBuilder<string>();
39+
var ruleTypeBuilder = ImmutableArray.CreateBuilder<string>();
40+
var configBuilder = ImmutableArray.CreateBuilder<string[]>();
3941
var comparer = StringComparer.OrdinalIgnoreCase;
40-
var preprocessorConfigurations = new List<string[]>();
42+
var disableCopyright = false;
4143

4244
for (int i = 1; i < args.Length; i++)
4345
{
4446
string arg = args[i];
45-
if (comparer.Equals(arg, "/file"))
46-
{
47-
if (i + 1 < args.Length)
48-
{
49-
string param = args[i + 1];
50-
filenames.Add(param);
51-
i++;
52-
}
53-
}
54-
else if (comparer.Equals(arg, "/nocopyright"))
47+
if (arg.StartsWith("/file:", StringComparison.OrdinalIgnoreCase))
5548
{
56-
disableCopyright = true;
49+
var all = arg.Substring(6);
50+
var files = all.Split(new[] { ','}, StringSplitOptions.RemoveEmptyEntries);
51+
fileNamesBuilder.AddRange(files);
5752
}
5853
else if (arg.StartsWith("/c:", StringComparison.OrdinalIgnoreCase))
5954
{
6055
var all = arg.Substring(3);
6156
var configs = all.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
62-
preprocessorConfigurations.Add(configs);
57+
configBuilder.Add(configs);
58+
}
59+
else if (comparer.Equals(arg, "/nocopyright"))
60+
{
61+
disableCopyright = true;
6362
}
6463
else
6564
{
66-
ruleTypes.Add(arg);
65+
ruleTypeBuilder.Add(arg);
6766
}
6867
}
6968

@@ -72,16 +71,29 @@ private static int Main(string[] args)
7271

7372
Console.CancelKeyPress += delegate { cts.Cancel(); };
7473

75-
RunAsync(projectOrSolutionPath, ruleTypes, filenames, disableCopyright, ImmutableArray.CreateRange(preprocessorConfigurations), ct).Wait(ct);
74+
RunAsync(
75+
projectOrSolutionPath,
76+
ruleTypeBuilder.ToImmutableArray(),
77+
fileNamesBuilder.ToImmutableArray(),
78+
configBuilder.ToImmutableArray(),
79+
disableCopyright,
80+
ct).Wait(ct);
7681
Console.WriteLine("Completed formatting.");
7782
return 0;
7883
}
7984

80-
private static async Task RunAsync(string projectOrSolutionPath, IEnumerable<string> ruleTypes, IEnumerable<string> filenames, bool disableCopright, ImmutableArray<string[]> preprocessorConfigurations, CancellationToken cancellationToken)
85+
private static async Task RunAsync(
86+
string projectOrSolutionPath,
87+
ImmutableArray<string> ruleTypes,
88+
ImmutableArray<string> fileNames,
89+
ImmutableArray<string[]> preprocessorConfigurations,
90+
bool disableCopright,
91+
CancellationToken cancellationToken)
8192
{
8293
var workspace = MSBuildWorkspace.Create();
83-
var engine = FormattingEngine.Create(ruleTypes, filenames);
94+
var engine = FormattingEngine.Create(ruleTypes);
8495
engine.PreprocessorConfigurations = preprocessorConfigurations;
96+
engine.FileNames = fileNames;
8597

8698
if (disableCopright)
8799
{

src/Microsoft.DotNet.CodeFormatting.Tests/Rules/CombinationTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public sealed class CombinationTest : CodeFormattingTestBase
1919

2020
static CombinationTest()
2121
{
22-
s_formattingEngine = (FormattingEngineImplementation)FormattingEngine.Create(Enumerable.Empty<string>(), Enumerable.Empty<string>());
22+
s_formattingEngine = (FormattingEngineImplementation)FormattingEngine.Create(ImmutableArray<string>.Empty);
2323
}
2424

2525
public CombinationTest()

src/Microsoft.DotNet.CodeFormatting/Filters/FilenameFilter.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,29 @@
1212

1313
namespace Microsoft.DotNet.CodeFormatting.Filters
1414
{
15+
[Export(typeof(IFormattingFilter))]
1516
internal sealed class FilenameFilter : IFormattingFilter
1617
{
17-
private IEnumerable<string> _filenames;
18+
private readonly Options _options;
1819

19-
public FilenameFilter(IEnumerable<string> filenames)
20+
[ImportingConstructor]
21+
public FilenameFilter(Options options)
2022
{
21-
_filenames = filenames;
23+
_options = options;
2224
}
2325

2426
public bool ShouldBeProcessed(Document document)
2527
{
26-
if (!_filenames.Any())
28+
var fileNames = _options.FileNames;
29+
if (fileNames.IsDefaultOrEmpty)
2730
{
2831
return true;
2932
}
3033

3134
string docFilename = Path.GetFileName(document.FilePath);
32-
33-
foreach (var filename in _filenames)
35+
foreach (var filename in fileNames)
3436
{
35-
if (filename.Equals(docFilename, StringComparison.InvariantCultureIgnoreCase))
37+
if (filename.Equals(docFilename, StringComparison.OrdinalIgnoreCase))
3638
{
3739
return true;
3840
}

src/Microsoft.DotNet.CodeFormatting/FormattingEngine.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88
using System.Collections.Generic;
99
using Microsoft.DotNet.CodeFormatting.Rules;
1010
using Microsoft.DotNet.CodeFormatting.Filters;
11+
using System.Collections.Immutable;
1112

1213
namespace Microsoft.DotNet.CodeFormatting
1314
{
1415
public static class FormattingEngine
1516
{
16-
public static IFormattingEngine Create(IEnumerable<string> ruleTypes, IEnumerable<string> filenames)
17+
public static IFormattingEngine Create(ImmutableArray<string> ruleTypes)
1718
{
1819
var catalog = new AssemblyCatalog(typeof(FormattingEngine).Assembly);
1920

@@ -46,12 +47,6 @@ public static IFormattingEngine Create(IEnumerable<string> ruleTypes, IEnumerabl
4647
});
4748

4849
var container = new CompositionContainer(filteredCatalog);
49-
50-
if (filenames.Any())
51-
{
52-
container.ComposeExportedValue<IFormattingFilter>(new FilenameFilter(filenames));
53-
}
54-
5550
var engine = container.GetExportedValue<IFormattingEngine>();
5651
var consoleFormatLogger = new ConsoleFormatLogger();
5752

src/Microsoft.DotNet.CodeFormatting/FormattingEngineImplementation.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ public ImmutableArray<string[]> PreprocessorConfigurations
4242
set { _options.PreprocessorConfigurations = value; }
4343
}
4444

45+
public ImmutableArray<string> FileNames
46+
{
47+
get { return _options.FileNames; }
48+
set { _options.FileNames = value; }
49+
}
50+
4551
public IFormatLogger FormatLogger
4652
{
4753
get { return _options.FormatLogger; }

src/Microsoft.DotNet.CodeFormatting/IFormattingEngine.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public interface IFormattingEngine
1414
{
1515
ImmutableArray<string> CopyrightHeader { get; set; }
1616
ImmutableArray<string[]> PreprocessorConfigurations { get; set; }
17+
ImmutableArray<string> FileNames { get; set; }
1718
Task FormatSolutionAsync(Solution solution, CancellationToken cancellationToken);
1819
Task FormatProjectAsync(Project porject, CancellationToken cancellationToken);
1920
}

src/Microsoft.DotNet.CodeFormatting/Options.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ internal sealed class Options
2222

2323
internal ImmutableArray<string> CopyrightHeader { get; set; }
2424
internal ImmutableArray<string[]> PreprocessorConfigurations { get; set; }
25+
26+
/// <summary>
27+
/// When non-empty the formatter will only process files with the specified name.
28+
/// </summary>
29+
internal ImmutableArray<string> FileNames { get; set; }
30+
2531
internal IFormatLogger FormatLogger { get; set; }
2632

2733
[ImportingConstructor]

0 commit comments

Comments
 (0)