From 5da76f48f1c311294e247f6ce614d1be3bf6287c Mon Sep 17 00:00:00 2001 From: AlekseyTs Date: Fri, 6 Mar 2015 17:04:06 -0800 Subject: [PATCH] Add an option to run CodeFormatter on all projects/solutions within directory. --- src/CodeFormatter/Program.cs | 88 +++++++++++++++++++++++++++++------- 1 file changed, 72 insertions(+), 16 deletions(-) diff --git a/src/CodeFormatter/Program.cs b/src/CodeFormatter/Program.cs index 90335ebb..5c471c4f 100644 --- a/src/CodeFormatter/Program.cs +++ b/src/CodeFormatter/Program.cs @@ -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 [/file:] [/nocopyright] [/nounicode] [/tables] [/c: [/copyright:file] [/verbose]"); + Console.WriteLine("CodeFormatter [/file:] [/nocopyright] [/nounicode] [/tables] [/c: [/copyright:file] [/recursive] [/searchPattern:] [/verbose]"); Console.WriteLine(" - Only apply changes to files with specified name."); Console.WriteLine(" - Additional preprocessor configurations the formatter"); Console.WriteLine(" should run under."); @@ -34,14 +35,30 @@ private static int Main(string[] args) Console.WriteLine(" - Verbose output"); Console.WriteLine(" - Do not convert unicode strings to escape sequences"); Console.WriteLine(" - Do not update the copyright message."); + Console.WriteLine(" - 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; + 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(); @@ -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.Empty; @@ -98,12 +124,27 @@ 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; @@ -111,17 +152,32 @@ private static int Main(string[] args) 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."); + } return 0; } catch (AggregateException ex)