22// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33
44using System ;
5- using System . Collections . Generic ;
65using System . Collections . Immutable ;
76using System . IO ;
87using System . Linq ;
98using System . Reflection ;
109using System . Threading ;
1110using System . Threading . Tasks ;
11+ using Microsoft . CodeAnalysis ;
1212using Microsoft . CodeAnalysis . MSBuild ;
1313using Microsoft . DotNet . CodeFormatting ;
1414
@@ -19,18 +19,21 @@ internal static class Program
1919 private const string FileSwitch = "/file:" ;
2020 private const string ConfigSwitch = "/c:" ;
2121 private const string CopyrightSwitch = "/copyright:" ;
22+ private const string LanguageSwitch = "/lang:" ;
2223
2324 private static int Main ( string [ ] args )
2425 {
2526 if ( args . Length < 1 )
2627 {
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]" ) ;
2829 Console . WriteLine ( " <filename> - Only apply changes to files with specified name." ) ;
2930 Console . WriteLine ( " <configs> - Additional preprocessor configurations the formatter" ) ;
3031 Console . WriteLine ( " should run under." ) ;
3132 Console . WriteLine ( " <copyright> - Specifies file containing copyright header." ) ;
3233 Console . WriteLine ( " Use ConvertTests to convert MSTest tests to xUnit." ) ;
3334 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#')" ) ;
3437 Console . WriteLine ( " <verbose> - Verbose output" ) ;
3538 Console . WriteLine ( " <nounicode> - Do not convert unicode strings to escape sequences" ) ;
3639 Console . WriteLine ( " <nocopyright>- Do not update the copyright message." ) ;
@@ -40,14 +43,15 @@ private static int Main(string[] args)
4043 var projectOrSolutionPath = args [ 0 ] ;
4144 if ( ! File . Exists ( projectOrSolutionPath ) )
4245 {
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 ) ;
4447 return - 1 ;
4548 }
4649
4750 var fileNamesBuilder = ImmutableArray . CreateBuilder < string > ( ) ;
4851 var ruleTypeBuilder = ImmutableArray . CreateBuilder < string > ( ) ;
4952 var configBuilder = ImmutableArray . CreateBuilder < string [ ] > ( ) ;
5053 var copyrightHeader = FormattingConstants . DefaultCopyrightHeader ;
54+ var language = LanguageNames . CSharp ;
5155 var convertUnicode = true ;
5256 var allowTables = false ;
5357 var verbose = false ;
@@ -82,6 +86,10 @@ private static int Main(string[] args)
8286 return - 1 ;
8387 }
8488 }
89+ else if ( arg . StartsWith ( LanguageSwitch , StringComparison . OrdinalIgnoreCase ) )
90+ {
91+ language = arg . Substring ( LanguageSwitch . Length ) ;
92+ }
8593 else if ( comparer . Equals ( arg , "/nocopyright" ) )
8694 {
8795 copyrightHeader = ImmutableArray < string > . Empty ;
@@ -117,6 +125,7 @@ private static int Main(string[] args)
117125 fileNamesBuilder . ToImmutableArray ( ) ,
118126 configBuilder . ToImmutableArray ( ) ,
119127 copyrightHeader ,
128+ language ,
120129 allowTables ,
121130 convertUnicode ,
122131 verbose ,
@@ -140,17 +149,17 @@ private static int Main(string[] args)
140149 }
141150
142151 private static async Task RunAsync (
143- string projectOrSolutionPath ,
152+ string projectSolutionOrRspPath ,
144153 ImmutableArray < string > ruleTypes ,
145154 ImmutableArray < string > fileNames ,
146155 ImmutableArray < string [ ] > preprocessorConfigurations ,
147156 ImmutableArray < string > copyrightHeader ,
157+ string language ,
148158 bool allowTables ,
149159 bool convertUnicode ,
150160 bool verbose ,
151161 CancellationToken cancellationToken )
152162 {
153- var workspace = MSBuildWorkspace . Create ( ) ;
154163 var engine = FormattingEngine . Create ( ruleTypes ) ;
155164 engine . PreprocessorConfigurations = preprocessorConfigurations ;
156165 engine . FileNames = fileNames ;
@@ -159,18 +168,33 @@ private static async Task RunAsync(
159168 engine . ConvertUnicodeCharacters = convertUnicode ;
160169 engine . Verbose = verbose ;
161170
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" ) )
165182 {
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+ }
168189 }
169190 else
170191 {
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+ }
174198 }
175199 }
176200 }
0 commit comments