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

Add an option to run CodeFormatter on all projects/solutions within a directory. #93

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
88 changes: 72 additions & 16 deletions src/CodeFormatter/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ internal static class Program
private const string FileSwitch = "/file:";
private const string ConfigSwitch = "/c:";
private const string CopyrightSwitch = "/copyright:";
private const string SearchPatternSwitch = "/searchPattern:";

private static int Main(string[] args)
{
if (args.Length < 1)
{
Console.WriteLine("CodeFormatter <project or solution> [/file:<filename>] [/nocopyright] [/nounicode] [/tables] [/c:<config1,config2> [/copyright:file] [/verbose]");
Console.WriteLine("CodeFormatter <project or solution or directory> [/file:<filename>] [/nocopyright] [/nounicode] [/tables] [/c:<config1,config2> [/copyright:file] [/recursive] [/searchPattern:<searchPattern>] [/verbose]");
Console.WriteLine(" <filename> - Only apply changes to files with specified name.");
Console.WriteLine(" <configs> - Additional preprocessor configurations the formatter");
Console.WriteLine(" should run under.");
Expand All @@ -34,14 +35,30 @@ private static int Main(string[] args)
Console.WriteLine(" <verbose> - Verbose output");
Console.WriteLine(" <nounicode> - Do not convert unicode strings to escape sequences");
Console.WriteLine(" <nocopyright>- Do not update the copyright message.");
Console.WriteLine(" <searchPattern>- Pattern to look for projects/solutions within the folder. Default '*.??proj'.");
return -1;
}

var projectOrSolutionPath = args[0];
if (!File.Exists(projectOrSolutionPath))
var path = args[0];
string[] projectOrSolutionPath;

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

SearchOption searchOption = SearchOption.TopDirectoryOnly;
string searchPattern = "*.??proj";

if (!File.Exists(path))
{
Console.Error.WriteLine("Project or solution {0} doesn't exist.", projectOrSolutionPath);
return -1;
if (!Directory.Exists(path))
{
Console.Error.WriteLine("Project or solution or directory \"{0}\" doesn't exist.", path);
return -1;
}
else
{
projectOrSolutionPath = null;
}
}
else
{
projectOrSolutionPath = new[] { path };
}

var fileNamesBuilder = ImmutableArray.CreateBuilder<string>();
Expand Down Expand Up @@ -82,6 +99,15 @@ private static int Main(string[] args)
return -1;
}
}
else if (arg.StartsWith(SearchPatternSwitch, StringComparison.OrdinalIgnoreCase))
{
var pattern = arg.Substring(SearchPatternSwitch.Length);

if (pattern.Length > 0)
{
searchPattern = pattern;
}
}
else if (comparer.Equals(arg, "/nocopyright"))
{
copyrightHeader = ImmutableArray<string>.Empty;
Expand All @@ -98,30 +124,60 @@ private static int Main(string[] args)
{
allowTables = true;
}
else if (comparer.Equals(arg, "/recursive") || comparer.Equals(arg, "/r"))
{
searchOption = SearchOption.AllDirectories;
}
else
{
ruleTypeBuilder.Add(arg);
}
}

if (projectOrSolutionPath == null)
{
projectOrSolutionPath = Directory.EnumerateFiles(path, searchPattern, searchOption).ToArray();

if (projectOrSolutionPath.Length == 0)
{
Console.Error.WriteLine("Projects or solutions are not found.");
return -1;
}
}

var cts = new CancellationTokenSource();
var ct = cts.Token;

Console.CancelKeyPress += delegate { cts.Cancel(); };

try
{
RunAsync(
projectOrSolutionPath,
ruleTypeBuilder.ToImmutableArray(),
fileNamesBuilder.ToImmutableArray(),
configBuilder.ToImmutableArray(),
copyrightHeader,
allowTables,
convertUnicode,
verbose,
ct).Wait(ct);
Console.WriteLine("Completed formatting.");
var rules = ruleTypeBuilder.ToImmutableArray();
var fileNames = fileNamesBuilder.ToImmutableArray();
var config = configBuilder.ToImmutableArray();

foreach (string projectOrSolution in projectOrSolutionPath)
{
RunAsync(
projectOrSolution,
rules,
fileNames,
config,
copyrightHeader,
allowTables,
convertUnicode,
verbose,
ct).Wait(ct);
}

if (projectOrSolutionPath.Length > 1)
{
Console.WriteLine("Completed formatting {0} projects/solutions.", projectOrSolutionPath.Length);
}
else
{
Console.WriteLine("Completed formatting.");
}

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

return 0;
}
catch (AggregateException ex)
Expand Down