javascript methods for sequence to readable date

c# like java for PI5

Helper 2025-02-19 more updates for Compare
This commit is contained in:
2025-03-26 17:02:35 -07:00
parent 0621d0f07e
commit 919279a917
12 changed files with 1119 additions and 738 deletions

View File

@ -1,14 +1,14 @@
using Microsoft.Extensions.Logging;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using Microsoft.Extensions.Logging;
namespace File_Folder_Helper.ADO2025.PI5;
internal static partial class Helper20250320
{
internal static partial class Helper20250320 {
private record Match(string Name,
string Parameters,
@ -34,11 +34,9 @@ internal static partial class Helper20250320
ReadOnlyCollection<int> ReferenceToLineNumbers,
int? ScopeEnum,
Search Search,
int StartLine)
{
int StartLine) {
public override string ToString()
{
public override string ToString() {
string result = JsonSerializer.Serialize(this, MethodCollectionCommonSourceGenerationContext.Default.Method);
return result;
}
@ -47,8 +45,7 @@ internal static partial class Helper20250320
[JsonSourceGenerationOptions(WriteIndented = true)]
[JsonSerializable(typeof(Method[]))]
private partial class MethodCollectionCommonSourceGenerationContext : JsonSerializerContext
{
private partial class MethodCollectionCommonSourceGenerationContext : JsonSerializerContext {
}
private record MethodWith(int? EndLine,
@ -60,11 +57,9 @@ internal static partial class Helper20250320
ReadOnlyCollection<int> ReferenceToLineNumbers,
int? ScopeEnum,
Search Search,
int StartLine)
{
int StartLine) {
public override string ToString()
{
public override string ToString() {
string result = JsonSerializer.Serialize(this, MethodCollectionCommonSourceGenerationContext.Default.Method);
return result;
}
@ -73,8 +68,7 @@ internal static partial class Helper20250320
[JsonSourceGenerationOptions(WriteIndented = true)]
[JsonSerializable(typeof(MethodWith[]))]
private partial class MethodWithCollectionCommonSourceGenerationContext : JsonSerializerContext
{
private partial class MethodWithCollectionCommonSourceGenerationContext : JsonSerializerContext {
}
private const string _Name = "name";
@ -88,11 +82,14 @@ internal static partial class Helper20250320
[GeneratedRegex(@"[[\]<,>?a-zA-Z0-9_()\s]*?\s[a-z_]{1}[a-zA-Z0-9_]*?,")]
private static partial Regex CSharpParameter();
[GeneratedRegex(@"(?<scope>public|private|internal|protected|\sI[a-zA-Z0-9_]*\.)\s?\b(?<static>static)?\s?\b(?<partial>partial)?\s?\b(?<async>async)?\s?\b(?<result>[\[\]\.\?<,>a-zA-Z0-9_()\s]*?)\s?\b(?<name>[A-Z_]{1}[a-zA-Z0-9_]*)+\((?<parameters>.*)\)")]
// VSCode Search ^\s*\b(?<scope>public|private|internal|protected|\sI[a-zA-Z0-9_]*\.)\s?\b(?<static>static)?\s?\b(?<partial>partial)?\s?\b(?<async>async)?\s?\b(?<result>[\[\]\.\?<,>a-zA-Z0-9_()\s]*?)\s?\b(?<name>[A-Z_]{1}[a-zA-Z0-9_])+\((?<parameters>.*)\)\s?\{?$
[GeneratedRegex(@"^\s*\b(?<scope>public|private|internal|protected|\sI[a-zA-Z0-9_]*\.)\s?\b(?<static>static)?\s?\b(?<partial>partial)?\s?\b(?<async>async)?\s?\b(?<result>[\[\]\.\?<,>a-zA-Z0-9_()\s]*?)\s?\b(?<name>[A-Z_]{1}[a-zA-Z0-9_]*)+\((?<parameters>.*)\)\s?\{?$")]
private static partial Regex CSharpMethodLine();
internal static void SortCodeMethods(ILogger<Worker> logger, List<string> args, CancellationToken cancellationToken)
{
private static ReadOnlyCollection<Method> GetSortedMethods(ReadOnlyCollection<Method> methods) =>
(from l in methods orderby l.ScopeEnum descending, l.ReferenceToLineNumbers.Count descending, l.Line.Length, l.Match.Name.Length, l.Match.Name select l).ToArray().AsReadOnly();
internal static void SortCodeMethods(ILogger<Worker> logger, List<string> args, CancellationToken cancellationToken) {
bool check;
string[] lines;
List<string> changed = [];
@ -104,16 +101,13 @@ internal static partial class Helper20250320
string repositoryDirectory = Path.GetFullPath(args[0]);
string[] cSharpFiles = Directory.GetFiles(repositoryDirectory, "*.cs", SearchOption.AllDirectories);
ReadOnlyCollection<string> gitOthersModifiedAndDeletedExcludingStandardFiles = logOnly ? new(cSharpFiles) : Helpers.HelperGit.GetOthersModifiedAndDeletedExcludingStandardFiles(repositoryDirectory, usePathCombine, cancellationToken);
foreach (string cSharpFile in cSharpFiles)
{
foreach (string cSharpFile in cSharpFiles) {
if (!gitOthersModifiedAndDeletedExcludingStandardFiles.Contains(cSharpFile))
continue;
for (int i = 0; i < 10; i++)
{
for (int i = 0; i < 10; i++) {
lines = File.ReadAllLines(cSharpFile);
check = SortFile(logger, logOnly, scopeSpaces, cSharpFile, lines);
if (check)
{
if (check) {
Thread.Sleep(500);
changed.Add($"{i + 1:00}) {cSharpFile}");
}
@ -123,41 +117,35 @@ internal static partial class Helper20250320
}
if (changed.Count == 0)
logger.LogInformation("No changes :)");
else
{
else {
changed.Reverse();
foreach (string c in changed)
logger.LogInformation(c);
}
}
private static bool SortFile(ILogger<Worker> logger, bool logOnly, int scopeSpaces, string cSharpFile, string[] lines)
{
private static bool SortFile(ILogger<Worker> logger, bool logOnly, int scopeSpaces, string cSharpFile, string[] lines) {
bool result;
ReadOnlyCollection<Method> methods = GetMethods(logger, scopeSpaces, cSharpFile, lines);
if (methods.Count == 0)
result = false;
else if (methods.Any(l => l.EndLine is null))
result = false;
else if (logOnly)
{
else if (logOnly) {
foreach (Method method in methods)
logger.LogInformation("{cSharpFile} - {Name} has {lines} line(s)", cSharpFile, method.Match.Name, (method.EndLine is null ? 999999 : method.EndLine.Value - method.StartLine).ToString("000000"));
result = false;
}
else
{
} else {
ReadOnlyCollection<Method> sortedMethods = GetSortedMethods(methods);
if (Debugger.IsAttached)
File.WriteAllText(Path.Combine(".vscode", "helper", ".txt"), string.Join(Environment.NewLine, sortedMethods.Select(l => $"{l.Match.Name} => {l.Parameters.Count}")));
File.WriteAllText(Path.Combine(".vscode", "helper", ".json"), JsonSerializer.Serialize(sortedMethods.ToArray(), MethodCollectionCommonSourceGenerationContext.Default.MethodArray));
ReadOnlyCollection<MethodWith> collection = GetCollection(logger, lines, sortedMethods);
result = WriteAllLines(cSharpFile, lines, collection);
}
return result;
}
private static ReadOnlyCollection<Method> GetMethods(ILogger<Worker> logger, int scopeSpaces, string cSharpFile, string[] lines)
{
private static ReadOnlyCollection<Method> GetMethods(ILogger<Worker> logger, int scopeSpaces, string cSharpFile, string[] lines) {
List<Method> results = [];
int check;
int blocks;
@ -176,8 +164,7 @@ internal static partial class Helper20250320
Regex parameterRegex = CSharpParameter();
ReadOnlyDictionary<string, string> parameters;
System.Text.RegularExpressions.Match regularExpressionsMatch;
for (int i = 0; i < lines.Length; i++)
{
for (int i = 0; i < lines.Length; i++) {
check = GetNumberOfStartSpaces(lines, i);
if (check != scopeSpaces)
continue;
@ -215,17 +202,17 @@ internal static partial class Helper20250320
firstLine = lines[startLine].Trim();
else
firstLine = lines[startLine + 1].Trim();
isLinq = !lines[i + 1].StartsWith("#pragma") && !lines[i + 1].StartsWith("#nullable") && lines[i + 1].Trim() != "{";
isLinq = !lines[i + 1].StartsWith("#pragma") && !lines[i + 1].StartsWith("#nullable") && lines[i].Trim()[^1] != '{' && lines[i + 1].Trim() != "{";
if (isLinq)
blocks++;
endLine = null;
for (int j = i + 1; j < lines.Length; j++)
{
if (lines[i].Trim()[^1] == '{')
blocks++;
for (int j = i + 1; j < lines.Length; j++) {
innerLine = lines[j].Trim();
if (innerLine.StartsWith("#pragma") || innerLine.StartsWith("#nullable"))
continue;
if (isLinq && string.IsNullOrEmpty(innerLine))
{
if (isLinq && string.IsNullOrEmpty(innerLine)) {
if (line.EndsWith(';'))
blocks--;
}
@ -240,11 +227,9 @@ internal static partial class Helper20250320
break;
}
referenceToLineNumbers = GetReferenceToLineNumbers(lines: lines, start: 0, end: lines.Length, i: i, search: search, parameters: parameters);
if (referenceToLineNumbers.Count == 0)
{
if (referenceToLineNumbers.Count == 0) {
lineSegmentFirst = line.Split(match.Name)[0];
if (!lines[i - 1].Trim().StartsWith("[Obsolete"))
{
if (!lines[i - 1].Trim().StartsWith("[Obsolete")) {
if (lineSegmentFirst.StartsWith("private"))
logger.LogWarning("// <{cSharpFileName}> {name} with {parameters} parameter(s) <{line}>", Path.GetFileName(cSharpFile), match.Name, parameters, lineSegmentFirst);
else
@ -269,11 +254,9 @@ internal static partial class Helper20250320
return results.AsReadOnly();
}
private static int GetNumberOfStartSpaces(string[] lines, int i)
{
private static int GetNumberOfStartSpaces(string[] lines, int i) {
int result = 0;
foreach (char @char in lines[i])
{
foreach (char @char in lines[i]) {
if (@char != ' ')
break;
result += 1;
@ -281,11 +264,9 @@ internal static partial class Helper20250320
return result;
}
private static int GetScopeEnum(Match match)
{
private static int GetScopeEnum(Match match) {
int result;
int value = match.Scope switch
{
int value = match.Scope switch {
"public" => 8000,
"internal" => 7000,
"protected" => 6000,
@ -301,29 +282,36 @@ internal static partial class Helper20250320
return result;
}
private static ReadOnlyDictionary<string, string> GetParameters(Regex parameterRegex, Match match)
{
private static ReadOnlyDictionary<string, string> GetParameters(Regex parameterRegex, Match match) {
Dictionary<string, string> results = [];
string value;
string[] segments;
System.Text.RegularExpressions.Match[] matches = parameterRegex.Matches($"{match.Parameters},").ToArray();
foreach (System.Text.RegularExpressions.Match m in matches)
{
if (!m.Success)
continue;
value = m.Value.Trim()[..^1];
segments = value.Split(' ');
results.Add(segments[^1], value);
try {
foreach (System.Text.RegularExpressions.Match m in matches) {
if (!m.Success)
continue;
value = m.Value.Trim()[..^1];
segments = value.Split(' ');
results.Add(segments[^1], value);
}
} catch (Exception) {
results.Clear();
System.Text.RegularExpressions.Match m;
for (int i = 0; i < matches.Length; i++) {
m = matches[i];
if (!m.Success)
continue;
results.Add(i.ToString(), i.ToString());
}
}
return new(results);
}
private static int GetStartLine(string[] lines, int i)
{
private static int GetStartLine(string[] lines, int i) {
int result = i;
string line;
for (int j = i - 1; j > -1; j--)
{
for (int j = i - 1; j > -1; j--) {
line = lines[j].Trim();
if (!line.StartsWith('[') && !line.StartsWith('#') && !line.StartsWith("/// "))
break;
@ -332,12 +320,10 @@ internal static partial class Helper20250320
return result;
}
private static int GetLineBlockCount(string line, bool isLinq)
{
private static int GetLineBlockCount(string line, bool isLinq) {
int result = 0;
bool ignore = false;
for (int i = 0; i < line.Length; i++)
{
for (int i = 0; i < line.Length; i++) {
if (line[i] == '\'')
i++;
else if (!isLinq && !ignore && line[i] == '{')
@ -352,27 +338,21 @@ internal static partial class Helper20250320
return result;
}
private static List<int> GetReferenceToLineNumbers(string[] lines, int start, int end, int i, Search search, ReadOnlyDictionary<string, string> parameters)
{
private static List<int> GetReferenceToLineNumbers(string[] lines, int start, int end, int i, Search search, ReadOnlyDictionary<string, string> parameters) {
List<int> results = [];
string[] segments;
string[] afterSegments;
string lastSegmentBeforeDot;
for (int j = start; j < end; j++)
{
for (int j = start; j < end; j++) {
if (j == i)
continue;
segments = lines[j].Split(search.Name);
if (segments.Length == 1)
{
if (segments.Length == 1) {
segments = lines[j].Split(search.Not);
if (segments.Length == 1)
{
if (segments.Length == 1) {
segments = lines[j].Split(search.Wrap);
if (segments.Length == 1)
{
if (!lines[j].EndsWith(search.Delegate))
{
if (segments.Length == 1) {
if (!lines[j].EndsWith(search.Delegate)) {
segments = lines[j].Split(search.Constructor);
if (segments.Length == 1)
continue;
@ -382,16 +362,12 @@ internal static partial class Helper20250320
}
if (lines[j].EndsWith(search.Delegate))
results.Add(j);
else
{
else {
lastSegmentBeforeDot = segments[^1].Split(").")[0];
if (parameters.Count == 0)
{
if (parameters.Count == 0) {
if (lastSegmentBeforeDot.Contains(','))
continue;
}
else
{
} else {
afterSegments = lastSegmentBeforeDot.Split(',');
if (afterSegments.Length != parameters.Count)
continue;
@ -402,15 +378,10 @@ internal static partial class Helper20250320
return results;
}
private static ReadOnlyCollection<Method> GetSortedMethods(ReadOnlyCollection<Method> methods) =>
(from l in methods orderby l.ScopeEnum descending, l.ReferenceToLineNumbers.Count descending, l.Line.Length, l.Match.Name.Length, l.Match.Name select l).ToArray().AsReadOnly();
private static ReadOnlyCollection<MethodWith> GetCollection(ILogger<Worker> logger, string[] lines, ReadOnlyCollection<Method> sortedMethods)
{
private static ReadOnlyCollection<MethodWith> GetCollection(ILogger<Worker> logger, string[] lines, ReadOnlyCollection<Method> sortedMethods) {
List<MethodWith> results = [];
List<Method> check = sortedMethods.ToList();
foreach (Method method in sortedMethods)
{
foreach (Method method in sortedMethods) {
logger.LogInformation($"{method.Match.Name} => {method.Parameters.Count}");
if (method.EndLine is null)
continue;
@ -422,25 +393,21 @@ internal static partial class Helper20250320
return results.AsReadOnly();
}
private static MethodWith GetMethodWith(string[] lines, ReadOnlyCollection<Method> methods, List<Method> check, Method method, int methodEndLineValue)
{
private static MethodWith GetMethodWith(string[] lines, ReadOnlyCollection<Method> methods, List<Method> check, Method method, int methodEndLineValue) {
MethodWith methodWith;
List<int> referenceToLineNumbers;
MethodWith[] sortedReferences;
Dictionary<int, MethodWith> references = [];
foreach (Method m in methods)
{
foreach (Method m in methods) {
if (m.EndLine is null)
continue;
if (m == method)
continue;
referenceToLineNumbers = GetReferenceToLineNumbers(lines: lines, start: method.StartLine, end: methodEndLineValue, i: -1, search: m.Search, parameters: m.Parameters);
if (referenceToLineNumbers.Count > 0)
{
if (referenceToLineNumbers.Count > 0) {
if (!check.Remove(m))
continue;
foreach (int i in referenceToLineNumbers)
{
foreach (int i in referenceToLineNumbers) {
if (references.ContainsKey(i))
continue;
methodWith = GetMethodWith(lines, methods, check, m, m.EndLine.Value);
@ -466,23 +433,20 @@ internal static partial class Helper20250320
return methodWith;
}
private static bool WriteAllLines(string cSharpFile, string[] lines, ReadOnlyCollection<MethodWith> collection)
{
private static bool WriteAllLines(string cSharpFile, string[] lines, ReadOnlyCollection<MethodWith> collection) {
bool result;
if (Debugger.IsAttached)
WriteDebug(collection);
List<string> results = [];
ReadOnlyCollection<int> methodLines = GetMethodLines(collection);
int maxMethodLines = methodLines.Max();
for (int i = 0; i < maxMethodLines; i++)
{
for (int i = 0; i < maxMethodLines; i++) {
if (methodLines.Contains(i))
continue;
results.Add(lines[i]);
}
List<bool> nests = [true];
foreach (MethodWith methodWith in collection)
{
foreach (MethodWith methodWith in collection) {
if (methodWith.EndLine is null)
continue;
AppendLines(results, nests, lines, methodWith, methodWith.EndLine.Value);
@ -493,16 +457,14 @@ internal static partial class Helper20250320
string join = string.Join(Environment.NewLine, results);
if (join == text)
result = false;
else
{
else {
result = true;
File.WriteAllText(cSharpFile, join);
}
return result;
}
private static void WriteDebug(ReadOnlyCollection<MethodWith> collection)
{
private static void WriteDebug(ReadOnlyCollection<MethodWith> collection) {
List<string> results = [];
List<bool> nests = [true];
foreach (MethodWith methodWith in collection)
@ -510,8 +472,7 @@ internal static partial class Helper20250320
File.WriteAllText(Path.Combine(".vscode", "helper", ".md"), string.Join(Environment.NewLine, results));
}
private static void AppendLines(List<string> results, List<bool> nests, MethodWith methodWith)
{
private static void AppendLines(List<string> results, List<bool> nests, MethodWith methodWith) {
nests.Add(true);
results.Add($" - {new string('#', nests.Count)} {methodWith.Match.Name} => {methodWith.Parameters.Count}");
foreach (MethodWith m in methodWith.References)
@ -519,12 +480,10 @@ internal static partial class Helper20250320
nests.RemoveAt(nests.Count - 1);
}
private static ReadOnlyCollection<int> GetMethodLines(ReadOnlyCollection<MethodWith> collection)
{
private static ReadOnlyCollection<int> GetMethodLines(ReadOnlyCollection<MethodWith> collection) {
List<int> results = [];
List<bool> nests = [true];
foreach (MethodWith methodWith in collection)
{
foreach (MethodWith methodWith in collection) {
if (methodWith.EndLine is null)
continue;
AppendLineNumbers(results, nests, methodWith, methodWith.EndLine.Value);
@ -535,13 +494,11 @@ internal static partial class Helper20250320
return new(results);
}
private static void AppendLineNumbers(List<int> results, List<bool> nests, MethodWith methodWith, int methodWithEndLineValue)
{
private static void AppendLineNumbers(List<int> results, List<bool> nests, MethodWith methodWith, int methodWithEndLineValue) {
nests.Add(true);
for (int i = methodWith.StartLine; i < methodWithEndLineValue + 1; i++)
results.Add(i);
foreach (MethodWith m in methodWith.References)
{
foreach (MethodWith m in methodWith.References) {
if (m.EndLine is null)
continue;
AppendLineNumbers(results, nests, m, m.EndLine.Value);
@ -549,13 +506,11 @@ internal static partial class Helper20250320
nests.RemoveAt(nests.Count - 1);
}
private static void AppendLines(List<string> results, List<bool> nests, string[] lines, MethodWith methodWith, int methodWithEndLineValue)
{
private static void AppendLines(List<string> results, List<bool> nests, string[] lines, MethodWith methodWith, int methodWithEndLineValue) {
nests.Add(true);
for (int i = methodWith.StartLine; i < methodWithEndLineValue + 1; i++)
results.Add(lines[i]);
foreach (MethodWith m in methodWith.References)
{
foreach (MethodWith m in methodWith.References) {
if (m.EndLine is null)
continue;
AppendLines(results, nests, lines, m, m.EndLine.Value);