@@ -15,15 +15,19 @@ namespace CodeFormatter
15
15
{
16
16
internal static class Program
17
17
{
18
+ private const string FileSwitch = "/file:" ;
19
+ private const string ConfigSwitch = "/c:" ;
20
+ private const string CopyrightSwitch = "/copyright:" ;
21
+
18
22
private static int Main ( string [ ] args )
19
23
{
20
24
if ( args . Length < 1 )
21
25
{
22
- Console . WriteLine ( "CodeFormatter <project or solution> [<rule types>] [/file:<filename>] [/nocopyright] [/c:<config1,config2>" ) ;
26
+ Console . WriteLine ( "CodeFormatter <project or solution> [<rule types>] [/file:<filename>] [/nocopyright] [/c:<config1,config2> [/copyright:file] " ) ;
23
27
Console . WriteLine ( " <rule types> - Rule types to use in addition to the default ones." ) ;
24
28
Console . WriteLine ( " Use ConvertTests to convert MSTest tests to xUnit." ) ;
25
29
Console . WriteLine ( " <filename> - Only apply changes to files with specified name." ) ;
26
- Console . WriteLine ( " <configs> - Additional preprocessor configurations the formatter" )
30
+ Console . WriteLine ( " <configs> - Additional preprocessor configurations the formatter" ) ;
27
31
Console . WriteLine ( " should run under" ) ;
28
32
return - 1 ;
29
33
}
@@ -38,27 +42,41 @@ private static int Main(string[] args)
38
42
var fileNamesBuilder = ImmutableArray . CreateBuilder < string > ( ) ;
39
43
var ruleTypeBuilder = ImmutableArray . CreateBuilder < string > ( ) ;
40
44
var configBuilder = ImmutableArray . CreateBuilder < string [ ] > ( ) ;
45
+ var copyrightHeader = FormattingConstants . DefaultCopyrightHeader ;
41
46
var comparer = StringComparer . OrdinalIgnoreCase ;
42
- var disableCopyright = false ;
43
47
44
48
for ( int i = 1 ; i < args . Length ; i ++ )
45
49
{
46
50
string arg = args [ i ] ;
47
- if ( arg . StartsWith ( "/file:" , StringComparison . OrdinalIgnoreCase ) )
51
+ if ( arg . StartsWith ( FileSwitch , StringComparison . OrdinalIgnoreCase ) )
48
52
{
49
- var all = arg . Substring ( 6 ) ;
53
+ var all = arg . Substring ( FileSwitch . Length ) ;
50
54
var files = all . Split ( new [ ] { ',' } , StringSplitOptions . RemoveEmptyEntries ) ;
51
55
fileNamesBuilder . AddRange ( files ) ;
52
56
}
53
- else if ( arg . StartsWith ( "/c:" , StringComparison . OrdinalIgnoreCase ) )
57
+ else if ( arg . StartsWith ( ConfigSwitch , StringComparison . OrdinalIgnoreCase ) )
54
58
{
55
- var all = arg . Substring ( 3 ) ;
59
+ var all = arg . Substring ( ConfigSwitch . Length ) ;
56
60
var configs = all . Split ( new [ ] { ',' } , StringSplitOptions . RemoveEmptyEntries ) ;
57
61
configBuilder . Add ( configs ) ;
58
62
}
63
+ else if ( arg . StartsWith ( CopyrightSwitch , StringComparison . OrdinalIgnoreCase ) )
64
+ {
65
+ var fileName = arg . Substring ( CopyrightSwitch . Length ) ;
66
+ try
67
+ {
68
+ copyrightHeader = ImmutableArray . CreateRange ( File . ReadAllLines ( fileName ) ) ;
69
+ }
70
+ catch ( Exception ex )
71
+ {
72
+ Console . Error . WriteLine ( "Could not read {0}" , fileName ) ;
73
+ Console . Error . WriteLine ( ex . Message ) ;
74
+ return - 1 ;
75
+ }
76
+ }
59
77
else if ( comparer . Equals ( arg , "/nocopyright" ) )
60
78
{
61
- disableCopyright = true ;
79
+ copyrightHeader = ImmutableArray < string > . Empty ;
62
80
}
63
81
else
64
82
{
@@ -76,7 +94,7 @@ private static int Main(string[] args)
76
94
ruleTypeBuilder . ToImmutableArray ( ) ,
77
95
fileNamesBuilder . ToImmutableArray ( ) ,
78
96
configBuilder . ToImmutableArray ( ) ,
79
- disableCopyright ,
97
+ copyrightHeader ,
80
98
ct ) . Wait ( ct ) ;
81
99
Console . WriteLine ( "Completed formatting." ) ;
82
100
return 0 ;
@@ -87,18 +105,14 @@ private static async Task RunAsync(
87
105
ImmutableArray < string > ruleTypes ,
88
106
ImmutableArray < string > fileNames ,
89
107
ImmutableArray < string [ ] > preprocessorConfigurations ,
90
- bool disableCopright ,
108
+ ImmutableArray < string > copyrightHeader ,
91
109
CancellationToken cancellationToken )
92
110
{
93
111
var workspace = MSBuildWorkspace . Create ( ) ;
94
112
var engine = FormattingEngine . Create ( ruleTypes ) ;
95
113
engine . PreprocessorConfigurations = preprocessorConfigurations ;
96
114
engine . FileNames = fileNames ;
97
-
98
- if ( disableCopright )
99
- {
100
- engine . CopyrightHeader = ImmutableArray < string > . Empty ;
101
- }
115
+ engine . CopyrightHeader = copyrightHeader ;
102
116
103
117
string extension = Path . GetExtension ( projectOrSolutionPath ) ;
104
118
if ( StringComparer . OrdinalIgnoreCase . Equals ( extension , ".sln" ) )
0 commit comments