2022-05-12 09:35:40 +02:00
|
|
|
|
using System.Text.RegularExpressions;
|
2022-05-11 15:19:16 +02:00
|
|
|
|
|
|
|
|
|
ProcessDirectory(Directory.GetCurrentDirectory());
|
|
|
|
|
|
|
|
|
|
void ProcessDirectory(string baseDir) {
|
|
|
|
|
foreach (var subDir in Directory.EnumerateFileSystemEntries(baseDir))
|
|
|
|
|
{
|
|
|
|
|
var fs = Path.Combine(baseDir, subDir);
|
|
|
|
|
if (File.Exists(fs))
|
|
|
|
|
{
|
2022-05-12 10:06:48 +02:00
|
|
|
|
if (Path.GetFileName(fs) == "BUILD") FormatBuildFile(fs);
|
2022-05-11 15:19:16 +02:00
|
|
|
|
}
|
|
|
|
|
else if (Directory.Exists(fs))
|
|
|
|
|
{
|
|
|
|
|
ProcessDirectory(fs);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FormatBuildFile(string filename)
|
|
|
|
|
{
|
|
|
|
|
var oldcontent = File.ReadAllText(filename);
|
|
|
|
|
var newcontent = FormatFile(filename, oldcontent);
|
|
|
|
|
|
2022-05-12 10:06:48 +02:00
|
|
|
|
if (oldcontent == newcontent) return;
|
|
|
|
|
|
|
|
|
|
File.WriteAllText(filename, newcontent);
|
|
|
|
|
Console.WriteLine("Auto-Formatted file :: " + filename);
|
2022-05-11 15:19:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string FormatFile(string filename, string content)
|
|
|
|
|
{
|
|
|
|
|
var depsRex = new Regex(@"^\s*deps\s*=\s*\[(\s*\n)?(?<deps>[^\]\)]+?)[\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);
|
|
|
|
|
|
2022-05-12 10:06:48 +02:00
|
|
|
|
if (oldDeps != newDeps) content = content.Replace(oldDeps, newDeps);
|
2022-05-11 15:19:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-12 10:06:48 +02:00
|
|
|
|
(int group, string sgroup, string clean, string raw) GetDepGroup(string filename, int linenum, string value)
|
2022-05-11 15:19:16 +02:00
|
|
|
|
{
|
|
|
|
|
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)
|
|
|
|
|
{
|
2022-05-12 10:06:48 +02:00
|
|
|
|
v = v.Substring(1, v.Length - 3);
|
2022-05-11 15:19:16 +02:00
|
|
|
|
}
|
|
|
|
|
else if (rowB)
|
|
|
|
|
{
|
2022-05-12 10:06:48 +02:00
|
|
|
|
v = v.Substring(1, v.Length - 2);
|
2022-05-11 15:19:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
v = v.Trim();
|
2022-05-12 10:06:48 +02:00
|
|
|
|
|
|
|
|
|
if (v.StartsWith(":")) return (100, "local", v, value);
|
|
|
|
|
|
|
|
|
|
if (v.StartsWith("//common")) return (200, "common", v, value);
|
|
|
|
|
if (v.StartsWith("//apcommon")) return (201, "common", v, value);
|
|
|
|
|
|
|
|
|
|
if (v.StartsWith("//accessor")) return (220, "accessor", v, value);
|
|
|
|
|
if (v.StartsWith("//models")) return (221, "models", v, value);
|
|
|
|
|
if (v.StartsWith("//routing")) return (222, "routing", v, value);
|
|
|
|
|
if (v.StartsWith("//service")) return (223, "service", v, value);
|
|
|
|
|
if (v.StartsWith("//artifacts")) return (224, "artifacts", v, value);
|
2022-05-11 15:19:16 +02:00
|
|
|
|
|
2022-05-12 10:06:48 +02:00
|
|
|
|
if (v.StartsWith("//services")) return (251, "services", v, value);
|
|
|
|
|
if (v.StartsWith("//gateways")) return (252, "gateways", v, value);
|
|
|
|
|
if (v.StartsWith("//jobs")) return (253, "jobs", v, value);
|
|
|
|
|
|
|
|
|
|
if (v.StartsWith("//proto/common:")) return (401, "commonproto", v, value);
|
|
|
|
|
if (v.StartsWith("//proto")) return (402, "proto", v, value);
|
|
|
|
|
|
|
|
|
|
if (v.StartsWith("//library/go/private")) return (500, "privatelib", v, value);
|
|
|
|
|
|
|
|
|
|
if (v.StartsWith("//library/go:")) return (501, "golib", v, value);
|
|
|
|
|
|
|
|
|
|
if (v.StartsWith("//library/go/")) return (502, "sublib", v, value);
|
|
|
|
|
|
|
|
|
|
throw new Exception($"unknown dep group in file {filename}:{linenum} => '{v}'");
|
2022-05-11 15:19:16 +02:00
|
|
|
|
}
|