diff --git a/.gitignore b/.gitignore index 723ef36..77773a3 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,8 @@ -.idea \ No newline at end of file +.idea +.vscode +.vscode/launch.json + +bin + +obj + diff --git a/please_formatter.cs b/please_formatter.cs deleted file mode 100644 index e69de29..0000000 diff --git a/proj/omnisharp.json b/proj/omnisharp.json new file mode 100644 index 0000000..74b7fc1 --- /dev/null +++ b/proj/omnisharp.json @@ -0,0 +1,6 @@ +{ + "script": { + "enableScriptNuGetReferences": true, + "defaultTargetFramework": "net6.0" + } +} \ No newline at end of file diff --git a/proj/please_formatter.cs b/proj/please_formatter.cs new file mode 100644 index 0000000..503db71 --- /dev/null +++ b/proj/please_formatter.cs @@ -0,0 +1,118 @@ +#!/usr/bin/env dotnet-script + +using System.Text.RegularExpressions; + +ProcessDirectory(Directory.GetCurrentDirectory()); + +void ProcessDirectory(string baseDir) { + foreach (var subDir in Directory.EnumerateFileSystemEntries(baseDir)) + { + var fs = Path.Combine(baseDir, subDir); + if (File.Exists(fs)) + { + if (Path.GetFileName(fs) == "BUILD") + { + Console.WriteLine(); + Console.WriteLine(fs); + Console.WriteLine("======== ======== ======== ======== ======== ======== ======== ========"); + FormatBuildFile(fs); + Console.WriteLine("======== ======== ======== ======== ======== ======== ======== ========"); + Console.WriteLine(); + } + } + else if (Directory.Exists(fs)) + { + ProcessDirectory(fs); + } + } +} + +void FormatBuildFile(string filename) +{ + var oldcontent = File.ReadAllText(filename); + var newcontent = FormatFile(filename, oldcontent); + + if (oldcontent != newcontent) + { + //File.WriteAllText(filename, newcontent); + Console.WriteLine("Auto-Formatted file :: " + filename); + } +} + +string FormatFile(string filename, string content) +{ + var depsRex = new Regex(@"^\s*deps\s*=\s*\[(\s*\n)?(?[^\]\)]+?)[\s\r\n]*\]", RegexOptions.Multiline); + + var matches = depsRex.Matches(content); + + foreach (Match m in matches) + { + var oldDeps = m.Groups["deps"].Value; + if (string.IsNullOrWhiteSpace(oldDeps)) continue; + + var linenum = 1 + content[..m.Groups["deps"].Index].AsEnumerable().Count(p => p == '\n'); + + var newDeps = FormatDeps(filename, linenum, oldDeps); + + if (oldDeps != newDeps) + { + Console.WriteLine("+++++++++++++ OLD +++++++++++++++++"); + Console.WriteLine(oldDeps); + Console.WriteLine("++++++++++++++ NEW ++++++++++++++++"); + Console.WriteLine(newDeps); + Console.WriteLine("++++++++++++++++++++++++++++++"); + Console.WriteLine(); + } + } + + return content; +} + +string FormatDeps(string filename, int linenum, string deps) +{ + var lines = deps.Split("\n"); + if (lines.Length == 0) return deps; + + var allindents = lines.Where(l => !string.IsNullOrWhiteSpace(l)).Select(l => l.Length - l.TrimStart().Length).Distinct().ToList(); + if (allindents.Count != 1) + { + throw new Exception($"Different deps-indents in file {filename}:{linenum} ( [ {string.Join(", ", allindents)} ] )"); + } + + var indent = new string(' ', allindents[0]); + + var result = 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); + + return result; +} + +(int group, string clean, string raw) GetDepGroup(string filename, int linenum, string value) +{ + var v = value.Trim(); + + var rowA = v.Length > 3 && v[0] == '"' && v[^2] == '"' && v[^1] == ','; + var rowB = v.Length > 2 && v[0] == '"' && v[^1] == '"'; + + if (!rowA && !rowB) throw new Exception($"Invalid Dependency in file {filename}:{linenum} => '{v}'"); + + if (rowA) + { + v = v.Substring(1, v.Length - 4); + } + else if (rowB) + { + v = v.Substring(1, v.Length - 3); + } + + v = v.Trim(); + + //TODO group + + return (0, v, value); +} \ No newline at end of file diff --git a/proj/proj.csproj b/proj/proj.csproj new file mode 100644 index 0000000..d1ff78c --- /dev/null +++ b/proj/proj.csproj @@ -0,0 +1,11 @@ + + + + Exe + net6.0 + enable + enable + X + + +