This commit is contained in:
Mike Schwörer 2022-05-25 13:11:44 +02:00
parent 71d36c680f
commit 85956ac531
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF
1 changed files with 72 additions and 5 deletions

View File

@ -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)?(?<deps>[^\]\)]+?)[\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;
}