@@ -15,14 +15,21 @@ namespace CodeFormatter
1515{
1616 internal static class Program
1717 {
18+ private const string FileSwitch = "/file:" ;
19+ private const string ConfigSwitch = "/c:" ;
20+ private const string CopyrightSwitch = "/copyright:" ;
21+
1822 private static int Main ( string [ ] args )
1923 {
2024 if ( args . Length < 1 )
2125 {
22- Console . WriteLine ( "CodeFormatter <project or solution> [<rule types>] [/file <filename>] [/nocopyright]" ) ;
26+ Console . WriteLine ( "CodeFormatter <project or solution> [<rule types>] [/file: <filename>] [/nocopyright] [/c:<config1,config2> [/copyright:file ]" ) ;
2327 Console . WriteLine ( " <rule types> - Rule types to use in addition to the default ones." ) ;
2428 Console . WriteLine ( " Use ConvertTests to convert MSTest tests to xUnit." ) ;
25- Console . WriteLine ( " <filename> - Only apply changes to files with specified name." ) ;
29+ Console . WriteLine ( " <filename> - Only apply changes to files with specified name." ) ;
30+ Console . WriteLine ( " <configs> - Additional preprocessor configurations the formatter" ) ;
31+ Console . WriteLine ( " should run under." ) ;
32+ Console . WriteLine ( " <copyright> - Specifies file containing copyright header." )
2633 return - 1 ;
2734 }
2835
@@ -33,37 +40,48 @@ private static int Main(string[] args)
3340 return - 1 ;
3441 }
3542
36- var ruleTypes = new List < string > ( ) ;
37- var filenames = new List < string > ( ) ;
38- var disableCopyright = false ;
43+ var fileNamesBuilder = ImmutableArray . CreateBuilder < string > ( ) ;
44+ var ruleTypeBuilder = ImmutableArray . CreateBuilder < string > ( ) ;
45+ var configBuilder = ImmutableArray . CreateBuilder < string [ ] > ( ) ;
46+ var copyrightHeader = FormattingConstants . DefaultCopyrightHeader ;
3947 var comparer = StringComparer . OrdinalIgnoreCase ;
40- var preprocessorConfigurations = new List < string [ ] > ( ) ;
4148
4249 for ( int i = 1 ; i < args . Length ; i ++ )
4350 {
4451 string arg = args [ i ] ;
45- if ( comparer . Equals ( arg , "/file" ) )
52+ if ( arg . StartsWith ( FileSwitch , StringComparison . OrdinalIgnoreCase ) )
53+ {
54+ var all = arg . Substring ( FileSwitch . Length ) ;
55+ var files = all . Split ( new [ ] { ',' } , StringSplitOptions . RemoveEmptyEntries ) ;
56+ fileNamesBuilder . AddRange ( files ) ;
57+ }
58+ else if ( arg . StartsWith ( ConfigSwitch , StringComparison . OrdinalIgnoreCase ) )
4659 {
47- if ( i + 1 < args . Length )
60+ var all = arg . Substring ( ConfigSwitch . Length ) ;
61+ var configs = all . Split ( new [ ] { ',' } , StringSplitOptions . RemoveEmptyEntries ) ;
62+ configBuilder . Add ( configs ) ;
63+ }
64+ else if ( arg . StartsWith ( CopyrightSwitch , StringComparison . OrdinalIgnoreCase ) )
65+ {
66+ var fileName = arg . Substring ( CopyrightSwitch . Length ) ;
67+ try
68+ {
69+ copyrightHeader = ImmutableArray . CreateRange ( File . ReadAllLines ( fileName ) ) ;
70+ }
71+ catch ( Exception ex )
4872 {
49- string param = args [ i + 1 ] ;
50- filenames . Add ( param ) ;
51- i ++ ;
73+ Console . Error . WriteLine ( "Could not read {0}" , fileName ) ;
74+ Console . Error . WriteLine ( ex . Message ) ;
75+ return - 1 ;
5276 }
5377 }
5478 else if ( comparer . Equals ( arg , "/nocopyright" ) )
5579 {
56- disableCopyright = true ;
57- }
58- else if ( arg . StartsWith ( "/c:" , StringComparison . OrdinalIgnoreCase ) )
59- {
60- var all = arg . Substring ( 3 ) ;
61- var configs = all . Split ( new [ ] { ',' } , StringSplitOptions . RemoveEmptyEntries ) ;
62- preprocessorConfigurations . Add ( configs ) ;
80+ copyrightHeader = ImmutableArray < string > . Empty ;
6381 }
6482 else
6583 {
66- ruleTypes . Add ( arg ) ;
84+ ruleTypeBuilder . Add ( arg ) ;
6785 }
6886 }
6987
@@ -72,21 +90,30 @@ private static int Main(string[] args)
7290
7391 Console . CancelKeyPress += delegate { cts . Cancel ( ) ; } ;
7492
75- RunAsync ( projectOrSolutionPath , ruleTypes , filenames , disableCopyright , ImmutableArray . CreateRange ( preprocessorConfigurations ) , ct ) . Wait ( ct ) ;
93+ RunAsync (
94+ projectOrSolutionPath ,
95+ ruleTypeBuilder . ToImmutableArray ( ) ,
96+ fileNamesBuilder . ToImmutableArray ( ) ,
97+ configBuilder . ToImmutableArray ( ) ,
98+ copyrightHeader ,
99+ ct ) . Wait ( ct ) ;
76100 Console . WriteLine ( "Completed formatting." ) ;
77101 return 0 ;
78102 }
79103
80- private static async Task RunAsync ( string projectOrSolutionPath , IEnumerable < string > ruleTypes , IEnumerable < string > filenames , bool disableCopright , ImmutableArray < string [ ] > preprocessorConfigurations , CancellationToken cancellationToken )
104+ private static async Task RunAsync (
105+ string projectOrSolutionPath ,
106+ ImmutableArray < string > ruleTypes ,
107+ ImmutableArray < string > fileNames ,
108+ ImmutableArray < string [ ] > preprocessorConfigurations ,
109+ ImmutableArray < string > copyrightHeader ,
110+ CancellationToken cancellationToken )
81111 {
82112 var workspace = MSBuildWorkspace . Create ( ) ;
83- var engine = FormattingEngine . Create ( ruleTypes , filenames ) ;
113+ var engine = FormattingEngine . Create ( ruleTypes ) ;
84114 engine . PreprocessorConfigurations = preprocessorConfigurations ;
85-
86- if ( disableCopright )
87- {
88- engine . CopyrightHeader = ImmutableArray < string > . Empty ;
89- }
115+ engine . FileNames = fileNames ;
116+ engine . CopyrightHeader = copyrightHeader ;
90117
91118 string extension = Path . GetExtension ( projectOrSolutionPath ) ;
92119 if ( StringComparer . OrdinalIgnoreCase . Equals ( extension , ".sln" ) )
0 commit comments