From 8440ab05918b38b5ee59a99f3f01ba8b9237dab7 Mon Sep 17 00:00:00 2001 From: Roald Ruiter Date: Sun, 28 Jun 2020 14:26:04 +0200 Subject: [PATCH] Closes #13. Format whole project at once using --project option. --- src/TcBlack/Program.cs | 118 +++++++++++++++++++++++++---------------- 1 file changed, 73 insertions(+), 45 deletions(-) diff --git a/src/TcBlack/Program.cs b/src/TcBlack/Program.cs index 5bbf67d..913556c 100644 --- a/src/TcBlack/Program.cs +++ b/src/TcBlack/Program.cs @@ -8,31 +8,67 @@ namespace TcBlack { class Program { + /// + /// Options for command line interface. + /// + class Options + { + [Option( + HelpText = "TcPOU file(s) to format.", + SetName = "FilesToFormat" + )] + public IEnumerable Filenames { get; set; } + [Option( + Default = "", + HelpText = "Plc project to format.", + SetName = "FilesToBuild" + )] + public string Project { get; set; } + + [Option( + Default = false, + HelpText = + "Compiles project before and after formatting, in order to check " + + "if the code has changed. WARNING: Takes > 30 seconds!" + )] + public bool Safe { get; set; } + + [Option( + Default = false, + HelpText = "Outputs build info. Has no effect in non-safe mode." + )] + public bool Verbose { get; set; } + } + static void Main(string[] args) { Parser.Default.ParseArguments(args) - .WithParsed(o => + .WithParsed(options => { - string files = string.Join( - "\n", - o.Filenames.Select(filename => $" - {filename}").ToArray() + string[] filenames = FilesToFormat(options); + + string fileListForCommandPrompt = string.Join( + "\n", + filenames.Select(filename => $" - {filename}").ToArray() ); - if (o.Safe) + if (options.Safe) { Console.WriteLine( - $"\nFormatting file(s) in safe mode:\n{files}\n" + $"\nFormatting {filenames.Length} file(s) " + + $"in safe mode:\n{fileListForCommandPrompt}\n" ); - SafeFormat(o); + SafeFormat(filenames, options.Verbose); } else { Console.WriteLine( - $"\nFormatting file(s) in fast non-safe mode:\n{files}\n" + $"\nFormatting {filenames.Length} file(s) " + + $"in fast non-safe mode:\n{fileListForCommandPrompt}\n" ); try { - CreateBackups(o.Filenames.ToArray()); + CreateBackups(options.Filenames.ToArray()); } catch (FileNotFoundException) { @@ -42,26 +78,45 @@ static void Main(string[] args) ); return; } - FormatAll(o.Filenames.ToArray()); + FormatAll(filenames); } }); } + /// + /// Return all the files which should be formatted. + /// + /// Input from the user. + /// Array with full path to the files. + static string[] FilesToFormat(Options options) + { + if (options.Project.Length > 0) + { + string projectDirectory = Path.GetDirectoryName(options.Project); + + return Directory.GetFiles( + projectDirectory, "*.TcPOU", SearchOption.AllDirectories + ); + } + else + { + return options.Filenames.ToArray(); + } + } + /// /// Compiles project before and after formatting to check if nothing changed. /// It does this by comparing the compile hash. /// /// Input from the command line. - static void SafeFormat(Options options) + static void SafeFormat(string[] filenames, bool verbose) { Console.WriteLine("Building project before formatting."); - TcProjectBuilder tcProject = new TcProjectBuilder( - options.Filenames.First() - ); + TcProjectBuilder tcProject = new TcProjectBuilder(filenames.First()); string hashBeforeFormat = string.Empty; try { - hashBeforeFormat = tcProject.Build(options.Verbose).Hash; + hashBeforeFormat = tcProject.Build(verbose).Hash; } catch(ProjectBuildFailed) { @@ -74,7 +129,7 @@ static void SafeFormat(Options options) List backups; try { - backups = CreateBackups(options.Filenames.ToArray()); + backups = CreateBackups(filenames); } catch (FileNotFoundException) { @@ -84,13 +139,13 @@ static void SafeFormat(Options options) ); return; } - FormatAll(options.Filenames.ToArray()); + FormatAll(filenames); Console.WriteLine("Building project after formatting."); string hashAfterFormat = string.Empty; try { - hashAfterFormat = tcProject.Build(options.Verbose).Hash; + hashAfterFormat = tcProject.Build(verbose).Hash; } catch(ProjectBuildFailed) { @@ -136,32 +191,5 @@ static List CreateBackups(string[] filenames) { return filenames.Select(filename => new Backup(filename)).ToList(); } - - /// - /// Options for command line interface. - /// - class Options - { - [Option( - 'f', "filenames", - HelpText = "File(s) you want to reformat.", - Required = true - )] - public IEnumerable Filenames { get; set; } - - [Option( - 's', "safe", - HelpText = - "Compiles project before and after formatting, in order to check " - + "if the code has changed. WARNING: Takes > 30 seconds!" - )] - public bool Safe { get; set; } - - [Option( - 'v', "verbose", - HelpText = "Outputs build info. Has no effect in non-safe mode." - )] - public bool Verbose { get; set; } - } } }