From 85956ac531ba78e655cdc996ca9461a0d648d03a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Wed, 25 May 2022 13:11:44 +0200 Subject: [PATCH] ???? --- Program.cs | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 72 insertions(+), 5 deletions(-) diff --git a/Program.cs b/Program.cs index 27d0ec6..cfcc7f9 100644 --- a/Program.cs +++ b/Program.cs @@ -1,6 +1,28 @@ using System.Text.RegularExpressions; -ProcessDirectory(Directory.GetCurrentDirectory()); +var DEBUG = false; + +string[] clargs = Args.ToArray(); + +var dir = Directory.GetCurrentDirectory(); + +if (clargs.Length > 0) dir = Path.Combine(dir, clargs[0]); + +dir = Path.GetFullPath(dir); + +if (File.Exists(dir)) +{ + FormatBuildFile(dir); +} +else if (Directory.Exists(dir)) +{ + ProcessDirectory(dir); +} +else +{ + Console.Error.WriteLine($"Invalid Directory/File argument: '{dir}'"); +} + void ProcessDirectory(string baseDir) { foreach (var subDir in Directory.EnumerateFileSystemEntries(baseDir)) @@ -30,6 +52,13 @@ void FormatBuildFile(string filename) string FormatFile(string filename, string content) { + if (DEBUG) + { + Console.WriteLine(""); + Console.WriteLine("============================== "+filename+" =============================="); + Console.WriteLine(""); + } + var depsRex = new Regex(@"^\s*deps\s*=\s*\[(\s*\n)?(?[^\]\)]+?)[\s\r\n]*\]", RegexOptions.Multiline); var matches = depsRex.Matches(content); @@ -43,7 +72,31 @@ string FormatFile(string filename, string content) var newDeps = FormatDeps(filename, linenum, oldDeps); - if (oldDeps != newDeps) content = content.Replace(oldDeps, newDeps); + + + if (oldDeps != newDeps) + { + if (DEBUG) + { + Console.WriteLine("+++++++++++++ OLD +++++++++++++++++"); + Console.WriteLine(oldDeps); + Console.WriteLine("++++++++++++++ NEW ++++++++++++++++"); + Console.WriteLine(newDeps); + Console.WriteLine("++++++++++++++++++++++++++++++"); + Console.WriteLine(); + } + else + { + content = content.Replace(oldDeps, newDeps); + } + } + else + { + if (DEBUG) + { + Console.WriteLine(" ( no changes )"); + } + } } return content; @@ -53,23 +106,37 @@ string FormatDeps(string filename, int linenum, string deps) { var lines = deps.Split("\n"); if (lines.Length == 0) return deps; + if (lines.Length == 1) return deps; var allindents = lines.Where(l => !string.IsNullOrWhiteSpace(l)).Select(l => l.Length - l.TrimStart().Length).Distinct().ToList(); if (allindents.Count != 1) { + foreach (var l in lines) Console.Error.WriteLine(l); throw new Exception($"Different deps-indents in file {filename}:{linenum} ( [ {string.Join(", ", allindents)} ] )"); } var indent = new string(' ', allindents[0]); - var result = lines. + var data = lines. Where(p => !string.IsNullOrWhiteSpace(p)). Select(p => GetDepGroup(filename, linenum, p)). GroupBy(p => p.group). OrderBy(p => p.Key). - SelectMany(p => p.OrderBy(q => q.clean).Select(q => $"{indent}\"{q.clean}\",").Append("")). - Aggregate((a, b) => a + "\n" + b); + ToList(); + var result = ""; + var first = true; + for (var i = 0; i < data.Count; i++) + { + foreach (var d in data[i]) + { + if (!first) result += "\n"; + result += $"{indent}\"{d.clean}\","; + first = false; + } + if (i+1 < data.Count) result += "\n"; + } + return result; }