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

Adds support for opening project files. #22

Merged
merged 1 commit into from
Jan 14, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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