From 95b151325a8ab6f048e0fb718286c2651341e2b1 Mon Sep 17 00:00:00 2001 From: Nate Slottow Date: Wed, 14 Jan 2015 14:30:39 -0800 Subject: [PATCH] Adds support for opening project files. Before the tool could only operate on a solution. --- README.md | 2 +- src/CodeFormatter/Program.cs | 23 ++++++++++++++++------- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index ee3493df..26f0acd9 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ In order get the usage, simply invoke the tool with no arguments: ``` $ .\CodeFormatter.exe -CodeFormatter [] [/file ] +CodeFormatter [] [/file ] - Rule types to use in addition to the default ones. Use ConvertTests to convert MSTest tests to xUnit. - Only apply changes to files with specified name. diff --git a/src/CodeFormatter/Program.cs b/src/CodeFormatter/Program.cs index 018d0923..77bb9cb8 100644 --- a/src/CodeFormatter/Program.cs +++ b/src/CodeFormatter/Program.cs @@ -18,17 +18,17 @@ private static int Main(string[] args) { if (args.Length < 1) { - Console.WriteLine("CodeFormatter [] [/file ]"); + Console.WriteLine("CodeFormatter [] [/file ]"); Console.WriteLine(" - Rule types to use in addition to the default ones."); Console.WriteLine(" Use ConvertTests to convert MSTest tests to xUnit."); Console.WriteLine(" - Only apply changes to files with specified name."); return -1; } - var solutionPath = args[0]; - if (!File.Exists(solutionPath)) + var projectOrSolutionPath = args[0]; + if (!File.Exists(projectOrSolutionPath)) { - Console.Error.WriteLine("Solution {0} doesn't exist.", solutionPath); + Console.Error.WriteLine("Project or solution {0} doesn't exist.", projectOrSolutionPath); return -1; } @@ -59,15 +59,24 @@ private static int Main(string[] args) Console.CancelKeyPress += delegate { cts.Cancel(); }; - RunAsync(solutionPath, ruleTypes, filenames, ct).Wait(ct); + RunAsync(projectOrSolutionPath, ruleTypes, filenames, ct).Wait(ct); Console.WriteLine("Completed formatting."); return 0; } - private static async Task RunAsync(string solutionFilePath, IEnumerable ruleTypes, IEnumerable filenames, CancellationToken cancellationToken) + private static async Task RunAsync(string projectOrSolutionPath, IEnumerable ruleTypes, IEnumerable filenames, CancellationToken cancellationToken) { var workspace = MSBuildWorkspace.Create(); - await workspace.OpenSolutionAsync(solutionFilePath, cancellationToken); + + string extension = Path.GetExtension(projectOrSolutionPath); + if (StringComparer.OrdinalIgnoreCase.Equals(extension, ".sln")) + { + await workspace.OpenSolutionAsync(projectOrSolutionPath, cancellationToken); + } + else + { + await workspace.OpenProjectAsync(projectOrSolutionPath, cancellationToken); + } var engine = FormattingEngine.Create(ruleTypes, filenames); await engine.RunAsync(workspace, cancellationToken);