Skip to content
This repository has been archived by the owner on Jul 12, 2022. It is now read-only.

Commit

Permalink
Merge pull request #22 from nslottow/support-opening-projects
Browse files Browse the repository at this point in the history
Adds support for opening project files.
  • Loading branch information
nslottow committed Jan 14, 2015
2 parents a5b7b4a + 95b1513 commit cf4b485
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ In order get the usage, simply invoke the tool with no arguments:

```
$ .\CodeFormatter.exe
CodeFormatter <solution> [<rule types>] [/file <filename>]
CodeFormatter <project or solution> [<rule types>] [/file <filename>]
<rule types> - Rule types to use in addition to the default ones.
Use ConvertTests to convert MSTest tests to xUnit.
<filename> - Only apply changes to files with specified name.
Expand Down
23 changes: 16 additions & 7 deletions src/CodeFormatter/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ private static int Main(string[] args)
{
if (args.Length < 1)
{
Console.WriteLine("CodeFormatter <solution> [<rule types>] [/file <filename>]");
Console.WriteLine("CodeFormatter <project or solution> [<rule types>] [/file <filename>]");
Console.WriteLine(" <rule types> - Rule types to use in addition to the default ones.");
Console.WriteLine(" Use ConvertTests to convert MSTest tests to xUnit.");
Console.WriteLine(" <filename> - 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;
}

Expand Down Expand Up @@ -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<string> ruleTypes, IEnumerable<string> filenames, CancellationToken cancellationToken)
private static async Task RunAsync(string projectOrSolutionPath, IEnumerable<string> ruleTypes, IEnumerable<string> 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);
Expand Down

0 comments on commit cf4b485

Please sign in to comment.