@@ -20,6 +20,9 @@ internal static class Program
20
20
private const string ConfigSwitch = "/c:" ;
21
21
private const string CopyrightSwitch = "/copyright:" ;
22
22
private const string LanguageSwitch = "/lang:" ;
23
+ private const string RuleEnabledSwitch1 = "/rule+:" ;
24
+ private const string RuleEnabledSwitch2 = "/rule:" ;
25
+ private const string RuleDisabledSwitch = "/rule-:" ;
23
26
24
27
private static int Main ( string [ ] args )
25
28
{
@@ -29,7 +32,8 @@ private static int Main(string[] args)
29
32
@"CodeFormatter <project, solution or responsefile> [/file:<filename>]
30
33
[/lang:<language>] [/c:<config>[,<config>...]>]
31
34
[/copyright:<file> | /nocopyright] [/tables] [/nounicode]
32
- [/simple|/agressive] [/verbose]
35
+ [/rule(+|-):rule1,rule2,...
36
+ [/verbose]
33
37
34
38
/file - Only apply changes to files with specified name.
35
39
/lang - Specifies the language to use when a responsefile is
@@ -42,17 +46,16 @@ Use ConvertTests to convert MSTest tests to xUnit.
42
46
/tables - Let tables opt out of formatting by defining
43
47
DOTNET_FORMATTER
44
48
/nounicode - Do not convert unicode strings to escape sequences
45
- /simple - Only run simple formatters (default)
46
- /agressive - Run agressive form
47
- /list - List the available rules
49
+ /rule(+|-) - Enable (default) or disable the specified rule
50
+ /rules - List the available rules
48
51
/verbose - Verbose output
49
52
" ) ;
50
53
return - 1 ;
51
54
}
52
55
53
56
var comparer = StringComparer . OrdinalIgnoreCase ;
54
57
var projectOrSolutionPath = args [ 0 ] ;
55
- if ( comparer . Equals ( projectOrSolutionPath , "/list " ) )
58
+ if ( comparer . Equals ( projectOrSolutionPath , "/rules " ) )
56
59
{
57
60
RunListRules ( ) ;
58
61
return 0 ;
@@ -65,14 +68,13 @@ Use ConvertTests to convert MSTest tests to xUnit.
65
68
}
66
69
67
70
var fileNamesBuilder = ImmutableArray . CreateBuilder < string > ( ) ;
68
- var ruleTypeBuilder = ImmutableArray . CreateBuilder < string > ( ) ;
69
71
var configBuilder = ImmutableArray . CreateBuilder < string [ ] > ( ) ;
70
72
var copyrightHeader = FormattingConstants . DefaultCopyrightHeader ;
73
+ var ruleMap = ImmutableDictionary < string , bool > . Empty ;
71
74
var language = LanguageNames . CSharp ;
72
75
var convertUnicode = true ;
73
76
var allowTables = false ;
74
77
var verbose = false ;
75
- var formattingLevel = FormattingLevel . Simple ;
76
78
77
79
for ( int i = 1 ; i < args . Length ; i ++ )
78
80
{
@@ -119,42 +121,88 @@ Use ConvertTests to convert MSTest tests to xUnit.
119
121
{
120
122
verbose = true ;
121
123
}
122
- else if ( comparer . Equals ( arg , "/tables" ) )
124
+ else if ( comparer . Equals ( arg , RuleEnabledSwitch1 ) )
123
125
{
124
- allowTables = true ;
126
+ UpdateRuleMap ( ref ruleMap , arg . Substring ( RuleEnabledSwitch1 . Length ) , enabled : true ) ;
125
127
}
126
- else if ( comparer . Equals ( arg , "/simple" ) )
128
+ else if ( comparer . Equals ( arg , RuleEnabledSwitch2 ) )
127
129
{
128
- formattingLevel = FormattingLevel . Simple ;
130
+ UpdateRuleMap ( ref ruleMap , arg . Substring ( RuleEnabledSwitch2 . Length ) , enabled : true ) ;
129
131
}
130
- else if ( comparer . Equals ( arg , "/aggressive" ) )
132
+ else if ( comparer . Equals ( arg , RuleDisabledSwitch ) )
131
133
{
132
- formattingLevel = FormattingLevel . Agressive ;
134
+ UpdateRuleMap ( ref ruleMap , arg . Substring ( RuleDisabledSwitch . Length ) , enabled : false ) ;
135
+ }
136
+ else if ( comparer . Equals ( arg , "/tables" ) )
137
+ {
138
+ allowTables = true ;
133
139
}
134
140
else
135
141
{
136
- ruleTypeBuilder . Add ( arg ) ;
142
+ Console . WriteLine ( "Unrecognized option: {0}" , arg ) ;
143
+ return 1 ;
137
144
}
138
145
}
139
146
147
+ return RunFormat (
148
+ projectOrSolutionPath ,
149
+ fileNamesBuilder . ToImmutableArray ( ) ,
150
+ configBuilder . ToImmutableArray ( ) ,
151
+ copyrightHeader ,
152
+ ruleMap ,
153
+ language ,
154
+ allowTables ,
155
+ convertUnicode ,
156
+ verbose ) ;
157
+ }
158
+
159
+ private static void RunListRules ( )
160
+ {
161
+ var rules = FormattingEngine . GetFormattingRules ( ) ;
162
+ Console . WriteLine ( "{0,-20} {1}" , "Name" , "Description" ) ;
163
+ Console . WriteLine ( "==============================================" ) ;
164
+ foreach ( var rule in rules )
165
+ {
166
+ Console . WriteLine ( "{0,-20} :{1}" , rule . Name , rule . Description ) ;
167
+ }
168
+ }
169
+
170
+ private static void UpdateRuleMap ( ref ImmutableDictionary < string , bool > ruleMap , string data , bool enabled )
171
+ {
172
+ foreach ( var current in data . Split ( ',' ) )
173
+ {
174
+ ruleMap = ruleMap . SetItem ( current , enabled ) ;
175
+ }
176
+ }
177
+
178
+ private static int RunFormat (
179
+ string projectSolutionOrRspPath ,
180
+ ImmutableArray < string > fileNames ,
181
+ ImmutableArray < string [ ] > preprocessorConfigurations ,
182
+ ImmutableArray < string > copyrightHeader ,
183
+ ImmutableDictionary < string , bool > ruleMap ,
184
+ string language ,
185
+ bool allowTables ,
186
+ bool convertUnicode ,
187
+ bool verbose )
188
+ {
140
189
var cts = new CancellationTokenSource ( ) ;
141
190
var ct = cts . Token ;
142
191
143
192
Console . CancelKeyPress += delegate { cts . Cancel ( ) ; } ;
144
193
145
194
try
146
195
{
147
- RunAsync (
148
- projectOrSolutionPath ,
149
- ruleTypeBuilder . ToImmutableArray ( ) ,
150
- fileNamesBuilder . ToImmutableArray ( ) ,
151
- configBuilder . ToImmutableArray ( ) ,
196
+ RunFormatAsync (
197
+ projectSolutionOrRspPath ,
198
+ fileNames ,
199
+ preprocessorConfigurations ,
152
200
copyrightHeader ,
201
+ ruleMap ,
153
202
language ,
154
203
allowTables ,
155
204
convertUnicode ,
156
205
verbose ,
157
- formattingLevel ,
158
206
ct ) . Wait ( ct ) ;
159
207
Console . WriteLine ( "Completed formatting." ) ;
160
208
return 0 ;
@@ -174,38 +222,30 @@ Use ConvertTests to convert MSTest tests to xUnit.
174
222
}
175
223
}
176
224
177
- private static void RunListRules ( )
178
- {
179
- var rules = FormattingEngine . GetFormattingRules ( ) ;
180
- Console . WriteLine ( "{0,-20} {1}" , "Name" , "Description" ) ;
181
- Console . WriteLine ( "==============================================" ) ;
182
- foreach ( var rule in rules )
183
- {
184
- Console . WriteLine ( "{0,-20} :{1}" , rule . Name , rule . Description ) ;
185
- }
186
- }
187
-
188
- private static async Task RunAsync (
225
+ private static async Task < int > RunFormatAsync (
189
226
string projectSolutionOrRspPath ,
190
- ImmutableArray < string > ruleTypes ,
191
227
ImmutableArray < string > fileNames ,
192
228
ImmutableArray < string [ ] > preprocessorConfigurations ,
193
229
ImmutableArray < string > copyrightHeader ,
230
+ ImmutableDictionary < string , bool > ruleMap ,
194
231
string language ,
195
232
bool allowTables ,
196
233
bool convertUnicode ,
197
234
bool verbose ,
198
- FormattingLevel formattingLevel ,
199
235
CancellationToken cancellationToken )
200
236
{
201
- var engine = FormattingEngine . Create ( ruleTypes ) ;
237
+ var engine = FormattingEngine . Create ( ) ;
202
238
engine . PreprocessorConfigurations = preprocessorConfigurations ;
203
239
engine . FileNames = fileNames ;
204
240
engine . CopyrightHeader = copyrightHeader ;
205
241
engine . AllowTables = allowTables ;
206
242
engine . ConvertUnicodeCharacters = convertUnicode ;
207
243
engine . Verbose = verbose ;
208
- engine . FormattingLevel = formattingLevel ;
244
+
245
+ if ( ! SetRuleMap ( engine , ruleMap ) )
246
+ {
247
+ return 1 ;
248
+ }
209
249
210
250
Console . WriteLine ( Path . GetFileName ( projectSolutionOrRspPath ) ) ;
211
251
string extension = Path . GetExtension ( projectSolutionOrRspPath ) ;
@@ -235,6 +275,26 @@ private static async Task RunAsync(
235
275
await engine . FormatProjectAsync ( project , cancellationToken ) ;
236
276
}
237
277
}
278
+
279
+ return 0 ;
280
+ }
281
+
282
+ private static bool SetRuleMap ( IFormattingEngine engine , ImmutableDictionary < string , bool > ruleMap )
283
+ {
284
+ var comparer = StringComparer . OrdinalIgnoreCase ;
285
+ foreach ( var entry in ruleMap )
286
+ {
287
+ var rule = engine . AllRules . Where ( x => comparer . Equals ( x . Name , entry . Key ) ) . FirstOrDefault ( ) ;
288
+ if ( rule == null )
289
+ {
290
+ Console . WriteLine ( "Could not find rule with name {0}" , entry . Key ) ;
291
+ return false ;
292
+ }
293
+
294
+ engine . ToggleRuleEnabled ( rule , entry . Value ) ;
295
+ }
296
+
297
+ return true ;
238
298
}
239
299
}
240
300
}
0 commit comments