2
2
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
3
4
4
using System ;
5
- using System . Collections . Generic ;
6
5
using System . Collections . Immutable ;
7
6
using System . IO ;
8
7
using System . Linq ;
9
8
using System . Reflection ;
10
9
using System . Threading ;
11
10
using System . Threading . Tasks ;
11
+ using Microsoft . CodeAnalysis ;
12
12
using Microsoft . CodeAnalysis . MSBuild ;
13
13
using Microsoft . DotNet . CodeFormatting ;
14
14
@@ -19,18 +19,21 @@ internal static class Program
19
19
private const string FileSwitch = "/file:" ;
20
20
private const string ConfigSwitch = "/c:" ;
21
21
private const string CopyrightSwitch = "/copyright:" ;
22
+ private const string LanguageSwitch = "/lang:" ;
22
23
23
24
private static int Main ( string [ ] args )
24
25
{
25
26
if ( args . Length < 1 )
26
27
{
27
- Console . WriteLine ( "CodeFormatter <project or solution > [/file:<filename>] [/nocopyright] [/nounicode] [/tables] [/c:<config1,config2> [/copyright:file] [/verbose]" ) ;
28
+ Console . WriteLine ( "CodeFormatter <project, solution or responsefile > [/file:<filename>] [/nocopyright] [/nounicode] [/tables] [/c:<config1,config2> [/copyright:file] [/lang:<language> ] [/verbose]" ) ;
28
29
Console . WriteLine ( " <filename> - Only apply changes to files with specified name." ) ;
29
30
Console . WriteLine ( " <configs> - Additional preprocessor configurations the formatter" ) ;
30
31
Console . WriteLine ( " should run under." ) ;
31
32
Console . WriteLine ( " <copyright> - Specifies file containing copyright header." ) ;
32
33
Console . WriteLine ( " Use ConvertTests to convert MSTest tests to xUnit." ) ;
33
34
Console . WriteLine ( " <tables> - Let tables opt out of formatting by defining DOTNET_FORMATTER" ) ;
35
+ Console . WriteLine ( " <language> - Specifies the language to use when a responsefile is specified." ) ;
36
+ Console . WriteLine ( " i.e. 'C#', 'Visual Basic', ... (default: 'C#')" ) ;
34
37
Console . WriteLine ( " <verbose> - Verbose output" ) ;
35
38
Console . WriteLine ( " <nounicode> - Do not convert unicode strings to escape sequences" ) ;
36
39
Console . WriteLine ( " <nocopyright>- Do not update the copyright message." ) ;
@@ -40,14 +43,15 @@ private static int Main(string[] args)
40
43
var projectOrSolutionPath = args [ 0 ] ;
41
44
if ( ! File . Exists ( projectOrSolutionPath ) )
42
45
{
43
- Console . Error . WriteLine ( "Project or solution {0} doesn't exist." , projectOrSolutionPath ) ;
46
+ Console . Error . WriteLine ( "Project, solution or response file {0} doesn't exist." , projectOrSolutionPath ) ;
44
47
return - 1 ;
45
48
}
46
49
47
50
var fileNamesBuilder = ImmutableArray . CreateBuilder < string > ( ) ;
48
51
var ruleTypeBuilder = ImmutableArray . CreateBuilder < string > ( ) ;
49
52
var configBuilder = ImmutableArray . CreateBuilder < string [ ] > ( ) ;
50
53
var copyrightHeader = FormattingConstants . DefaultCopyrightHeader ;
54
+ var language = LanguageNames . CSharp ;
51
55
var convertUnicode = true ;
52
56
var allowTables = false ;
53
57
var verbose = false ;
@@ -82,6 +86,10 @@ private static int Main(string[] args)
82
86
return - 1 ;
83
87
}
84
88
}
89
+ else if ( arg . StartsWith ( LanguageSwitch , StringComparison . OrdinalIgnoreCase ) )
90
+ {
91
+ language = arg . Substring ( LanguageSwitch . Length ) ;
92
+ }
85
93
else if ( comparer . Equals ( arg , "/nocopyright" ) )
86
94
{
87
95
copyrightHeader = ImmutableArray < string > . Empty ;
@@ -117,6 +125,7 @@ private static int Main(string[] args)
117
125
fileNamesBuilder . ToImmutableArray ( ) ,
118
126
configBuilder . ToImmutableArray ( ) ,
119
127
copyrightHeader ,
128
+ language ,
120
129
allowTables ,
121
130
convertUnicode ,
122
131
verbose ,
@@ -140,17 +149,17 @@ private static int Main(string[] args)
140
149
}
141
150
142
151
private static async Task RunAsync (
143
- string projectOrSolutionPath ,
152
+ string projectSolutionOrRspPath ,
144
153
ImmutableArray < string > ruleTypes ,
145
154
ImmutableArray < string > fileNames ,
146
155
ImmutableArray < string [ ] > preprocessorConfigurations ,
147
156
ImmutableArray < string > copyrightHeader ,
157
+ string language ,
148
158
bool allowTables ,
149
159
bool convertUnicode ,
150
160
bool verbose ,
151
161
CancellationToken cancellationToken )
152
162
{
153
- var workspace = MSBuildWorkspace . Create ( ) ;
154
163
var engine = FormattingEngine . Create ( ruleTypes ) ;
155
164
engine . PreprocessorConfigurations = preprocessorConfigurations ;
156
165
engine . FileNames = fileNames ;
@@ -159,18 +168,33 @@ private static async Task RunAsync(
159
168
engine . ConvertUnicodeCharacters = convertUnicode ;
160
169
engine . Verbose = verbose ;
161
170
162
- Console . WriteLine ( Path . GetFileName ( projectOrSolutionPath ) ) ;
163
- string extension = Path . GetExtension ( projectOrSolutionPath ) ;
164
- if ( StringComparer . OrdinalIgnoreCase . Equals ( extension , ".sln" ) )
171
+ Console . WriteLine ( Path . GetFileName ( projectSolutionOrRspPath ) ) ;
172
+ string extension = Path . GetExtension ( projectSolutionOrRspPath ) ;
173
+ if ( StringComparer . OrdinalIgnoreCase . Equals ( extension , ".rsp" ) )
174
+ {
175
+ using ( var workspace = ResponseFileWorkspace . Create ( ) )
176
+ {
177
+ Project project = workspace . OpenCommandLineProject ( projectSolutionOrRspPath , language ) ;
178
+ await engine . FormatProjectAsync ( project , cancellationToken ) ;
179
+ }
180
+ }
181
+ else if ( StringComparer . OrdinalIgnoreCase . Equals ( extension , ".sln" ) )
165
182
{
166
- var solution = await workspace . OpenSolutionAsync ( projectOrSolutionPath , cancellationToken ) ;
167
- await engine . FormatSolutionAsync ( solution , cancellationToken ) ;
183
+ using ( var workspace = MSBuildWorkspace . Create ( ) )
184
+ {
185
+ workspace . LoadMetadataForReferencedProjects = true ;
186
+ var solution = await workspace . OpenSolutionAsync ( projectSolutionOrRspPath , cancellationToken ) ;
187
+ await engine . FormatSolutionAsync ( solution , cancellationToken ) ;
188
+ }
168
189
}
169
190
else
170
191
{
171
- workspace . LoadMetadataForReferencedProjects = true ;
172
- var project = await workspace . OpenProjectAsync ( projectOrSolutionPath , cancellationToken ) ;
173
- await engine . FormatProjectAsync ( project , cancellationToken ) ;
192
+ using ( var workspace = MSBuildWorkspace . Create ( ) )
193
+ {
194
+ workspace . LoadMetadataForReferencedProjects = true ;
195
+ var project = await workspace . OpenProjectAsync ( projectSolutionOrRspPath , cancellationToken ) ;
196
+ await engine . FormatProjectAsync ( project , cancellationToken ) ;
197
+ }
174
198
}
175
199
}
176
200
}
0 commit comments