person-key-to-immich-import birthday json (Day-Helper-2024-05-18)

csharp_prefer_braces = true
This commit is contained in:
2025-09-06 11:16:55 -07:00
parent 8ec89953bc
commit 6102da7266
54 changed files with 2218 additions and 1721 deletions

View File

@ -1,11 +1,10 @@
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 {
@ -40,7 +39,6 @@ internal static partial class Helper20250320 {
string result = JsonSerializer.Serialize(this, MethodCollectionCommonSourceGenerationContext.Default.Method);
return result;
}
}
[JsonSourceGenerationOptions(WriteIndented = true)]
@ -63,7 +61,6 @@ internal static partial class Helper20250320 {
string result = JsonSerializer.Serialize(this, MethodCollectionCommonSourceGenerationContext.Default.Method);
return result;
}
}
[JsonSourceGenerationOptions(WriteIndented = true)]
@ -102,8 +99,9 @@ internal static partial class Helper20250320 {
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) {
if (!gitOthersModifiedAndDeletedExcludingStandardFiles.Contains(cSharpFile))
if (!gitOthersModifiedAndDeletedExcludingStandardFiles.Contains(cSharpFile)) {
continue;
}
for (int i = 0; i < 10; i++) {
lines = File.ReadAllLines(cSharpFile);
check = SortFile(logger, logOnly, scopeSpaces, cSharpFile, lines);
@ -111,34 +109,38 @@ internal static partial class Helper20250320 {
Thread.Sleep(500);
changed.Add($"{i + 1:00}) {cSharpFile}");
}
if (logOnly || !check)
if (logOnly || !check) {
break;
}
}
}
if (changed.Count == 0)
if (changed.Count == 0) {
logger.LogInformation("No changes :)");
else {
} else {
changed.Reverse();
foreach (string c in changed)
foreach (string c in changed) {
logger.LogInformation(c);
}
}
}
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)
if (methods.Count == 0) {
result = false;
else if (methods.Any(l => l.EndLine is null))
} else if (methods.Any(l => l.EndLine is null)) {
result = false;
else if (logOnly) {
foreach (Method method in methods)
} 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 {
ReadOnlyCollection<Method> sortedMethods = GetSortedMethods(methods);
if (Debugger.IsAttached)
if (Debugger.IsAttached) {
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);
}
@ -166,18 +168,23 @@ internal static partial class Helper20250320 {
System.Text.RegularExpressions.Match regularExpressionsMatch;
for (int i = 0; i < lines.Length; i++) {
check = GetNumberOfStartSpaces(lines, i);
if (check != scopeSpaces)
if (check != scopeSpaces) {
continue;
}
line = lines[i].Trim();
if (string.IsNullOrEmpty(line))
if (string.IsNullOrEmpty(line)) {
continue;
if (line.Length < 5)
}
if (line.Length < 5) {
continue;
if (line.EndsWith(','))
}
if (line.EndsWith(',')) {
continue;
}
regularExpressionsMatch = CSharpMethodLine().Match(line);
if (!regularExpressionsMatch.Success)
if (!regularExpressionsMatch.Success) {
continue;
}
match = new(Async: regularExpressionsMatch.Groups[_Async].Value,
Name: regularExpressionsMatch.Groups[_Name].Value,
Parameters: regularExpressionsMatch.Groups[_Parameters].Value,
@ -194,50 +201,61 @@ internal static partial class Helper20250320 {
Not: $"!{match.Name}(",
Wrap: $"({match.Name}(");
logger.LogInformation("{line} {a} // {results}", line.Split(" =>")[0], "{ }", results.Count);
if (string.IsNullOrEmpty(match.Name))
if (string.IsNullOrEmpty(match.Name)) {
continue;
}
blocks = 0;
startLine = GetStartLine(lines, i);
if (!lines[startLine].StartsWith("#pragma") && !lines[startLine].StartsWith("#nullable"))
if (!lines[startLine].StartsWith("#pragma") && !lines[startLine].StartsWith("#nullable")) {
firstLine = lines[startLine].Trim();
else
} else {
firstLine = lines[startLine + 1].Trim();
}
isLinq = !lines[i + 1].StartsWith("#pragma") && !lines[i + 1].StartsWith("#nullable") && lines[i].Trim()[^1] != '{' && lines[i + 1].Trim() != "{";
if (isLinq)
if (isLinq) {
blocks++;
}
endLine = null;
if (lines[i].Trim()[^1] == '{')
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"))
if (innerLine.StartsWith("#pragma") || innerLine.StartsWith("#nullable")) {
continue;
}
if (isLinq && string.IsNullOrEmpty(innerLine)) {
if (line.EndsWith(';'))
if (line.EndsWith(';')) {
blocks--;
}
}
blocks += GetLineBlockCount(innerLine, isLinq);
if (blocks != 0)
if (blocks != 0) {
continue;
}
endLine = j;
if (lines.Length > j + 1 && string.IsNullOrEmpty(lines[j + 1].Trim()))
if (lines.Length > j + 1 && string.IsNullOrEmpty(lines[j + 1].Trim())) {
endLine++;
if (j > lines.Length - 2)
}
if (j > lines.Length - 2) {
throw new Exception();
}
break;
}
referenceToLineNumbers = GetReferenceToLineNumbers(lines: lines, start: 0, end: lines.Length, i: i, search: search, parameters: parameters);
if (referenceToLineNumbers.Count == 0) {
lineSegmentFirst = line.Split(match.Name)[0];
if (!lines[i - 1].Trim().StartsWith("[Obsolete")) {
if (lineSegmentFirst.StartsWith("private"))
if (lineSegmentFirst.StartsWith("private")) {
logger.LogWarning("// <{cSharpFileName}> {name} with {parameters} parameter(s) <{line}>", Path.GetFileName(cSharpFile), match.Name, parameters, lineSegmentFirst);
else
} else {
logger.LogInformation("// <{cSharpFileName}> {name} with {parameters} parameter(s) <{line}>", Path.GetFileName(cSharpFile), match.Name, parameters, lineSegmentFirst);
}
}
}
if (referenceToLineNumbers.Count == 0)
if (referenceToLineNumbers.Count == 0) {
referenceToLineNumbers.Add(-1);
}
logger.LogInformation("{line} {a} // {results} ~~~ {startLine} => {firstUsedLine}", line.Split(" =>")[0], "{ }", results.Count, startLine, referenceToLineNumbers.First());
method = new(EndLine: endLine,
FirstLine: firstLine,
@ -257,8 +275,9 @@ internal static partial class Helper20250320 {
private static int GetNumberOfStartSpaces(string[] lines, int i) {
int result = 0;
foreach (char @char in lines[i]) {
if (@char != ' ')
if (@char != ' ') {
break;
}
result += 1;
}
return result;
@ -289,8 +308,9 @@ internal static partial class Helper20250320 {
System.Text.RegularExpressions.Match[] matches = parameterRegex.Matches($"{match.Parameters},").ToArray();
try {
foreach (System.Text.RegularExpressions.Match m in matches) {
if (!m.Success)
if (!m.Success) {
continue;
}
value = m.Value.Trim()[..^1];
segments = value.Split(' ');
results.Add(segments[^1], value);
@ -300,8 +320,9 @@ internal static partial class Helper20250320 {
System.Text.RegularExpressions.Match m;
for (int i = 0; i < matches.Length; i++) {
m = matches[i];
if (!m.Success)
if (!m.Success) {
continue;
}
results.Add(i.ToString(), i.ToString());
}
}
@ -313,8 +334,9 @@ internal static partial class Helper20250320 {
string line;
for (int j = i - 1; j > -1; j--) {
line = lines[j].Trim();
if (!line.StartsWith('[') && !line.StartsWith('#') && !line.StartsWith("/// "))
if (!line.StartsWith('[') && !line.StartsWith('#') && !line.StartsWith("/// ")) {
break;
}
result--;
}
return result;
@ -324,16 +346,17 @@ internal static partial class Helper20250320 {
int result = 0;
bool ignore = false;
for (int i = 0; i < line.Length; i++) {
if (line[i] == '\'')
if (line[i] == '\'') {
i++;
else if (!isLinq && !ignore && line[i] == '{')
} else if (!isLinq && !ignore && line[i] == '{') {
result++;
else if (!isLinq && !ignore && line[i] == '}')
} else if (!isLinq && !ignore && line[i] == '}') {
result--;
else if (isLinq && !ignore && line[i] == ';')
} else if (isLinq && !ignore && line[i] == ';') {
result--;
else if (i > 0 && line[i] == '"' && line[i - 1] != '\\')
} else if (i > 0 && line[i] == '"' && line[i - 1] != '\\') {
ignore = !ignore;
}
}
return result;
}
@ -344,8 +367,9 @@ internal static partial class Helper20250320 {
string[] afterSegments;
string lastSegmentBeforeDot;
for (int j = start; j < end; j++) {
if (j == i)
if (j == i) {
continue;
}
segments = lines[j].Split(search.Name);
if (segments.Length == 1) {
segments = lines[j].Split(search.Not);
@ -354,23 +378,26 @@ internal static partial class Helper20250320 {
if (segments.Length == 1) {
if (!lines[j].EndsWith(search.Delegate)) {
segments = lines[j].Split(search.Constructor);
if (segments.Length == 1)
if (segments.Length == 1) {
continue;
}
}
}
}
}
if (lines[j].EndsWith(search.Delegate))
if (lines[j].EndsWith(search.Delegate)) {
results.Add(j);
else {
} else {
lastSegmentBeforeDot = segments[^1].Split(").")[0];
if (parameters.Count == 0) {
if (lastSegmentBeforeDot.Contains(','))
if (lastSegmentBeforeDot.Contains(',')) {
continue;
}
} else {
afterSegments = lastSegmentBeforeDot.Split(',');
if (afterSegments.Length != parameters.Count)
if (afterSegments.Length != parameters.Count) {
continue;
}
}
results.Add(j);
}
@ -383,10 +410,12 @@ internal static partial class Helper20250320 {
List<Method> check = sortedMethods.ToList();
foreach (Method method in sortedMethods) {
logger.LogInformation($"{method.Match.Name} => {method.Parameters.Count}");
if (method.EndLine is null)
if (method.EndLine is null) {
continue;
if (!check.Remove(method))
}
if (!check.Remove(method)) {
continue;
}
MethodWith methodWith = GetMethodWith(lines, sortedMethods, check, method, method.EndLine.Value);
results.Add(methodWith);
}
@ -399,27 +428,32 @@ internal static partial class Helper20250320 {
MethodWith[] sortedReferences;
Dictionary<int, MethodWith> references = [];
foreach (Method m in methods) {
if (m.EndLine is null)
if (m.EndLine is null) {
continue;
if (m == method)
}
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 (!check.Remove(m))
if (!check.Remove(m)) {
continue;
}
foreach (int i in referenceToLineNumbers) {
if (references.ContainsKey(i))
if (references.ContainsKey(i)) {
continue;
}
methodWith = GetMethodWith(lines, methods, check, m, m.EndLine.Value);
references.Add(i, methodWith);
break;
}
}
}
if (references.Count < 2)
if (references.Count < 2) {
sortedReferences = (from l in references select l.Value).ToArray();
else
} else {
sortedReferences = (from l in references orderby l.Key select l.Value).ToArray();
}
methodWith = new(EndLine: method.EndLine,
FirstLine: method.FirstLine,
Line: method.Line,
@ -435,29 +469,33 @@ internal static partial class Helper20250320 {
private static bool WriteAllLines(string cSharpFile, string[] lines, ReadOnlyCollection<MethodWith> collection) {
bool result;
if (Debugger.IsAttached)
if (Debugger.IsAttached) {
WriteDebug(collection);
}
List<string> results = [];
ReadOnlyCollection<int> methodLines = GetMethodLines(collection);
int maxMethodLines = methodLines.Max();
for (int i = 0; i < maxMethodLines; i++) {
if (methodLines.Contains(i))
if (methodLines.Contains(i)) {
continue;
}
results.Add(lines[i]);
}
List<bool> nests = [true];
foreach (MethodWith methodWith in collection) {
if (methodWith.EndLine is null)
if (methodWith.EndLine is null) {
continue;
}
AppendLines(results, nests, lines, methodWith, methodWith.EndLine.Value);
}
for (int i = maxMethodLines + 1; i < lines.Length; i++)
for (int i = maxMethodLines + 1; i < lines.Length; i++) {
results.Add(lines[i]);
}
string text = File.ReadAllText(cSharpFile);
string join = string.Join(Environment.NewLine, results);
if (join == text)
if (join == text) {
result = false;
else {
} else {
result = true;
File.WriteAllText(cSharpFile, join);
}
@ -467,16 +505,18 @@ internal static partial class Helper20250320 {
private static void WriteDebug(ReadOnlyCollection<MethodWith> collection) {
List<string> results = [];
List<bool> nests = [true];
foreach (MethodWith methodWith in collection)
foreach (MethodWith methodWith in collection) {
AppendLines(results, nests, methodWith);
}
File.WriteAllText(Path.Combine(".vscode", "helper", ".md"), string.Join(Environment.NewLine, results));
}
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)
foreach (MethodWith m in methodWith.References) {
AppendLines(results, nests, m);
}
nests.RemoveAt(nests.Count - 1);
}
@ -484,23 +524,27 @@ internal static partial class Helper20250320 {
List<int> results = [];
List<bool> nests = [true];
foreach (MethodWith methodWith in collection) {
if (methodWith.EndLine is null)
if (methodWith.EndLine is null) {
continue;
}
AppendLineNumbers(results, nests, methodWith, methodWith.EndLine.Value);
}
int[] distinct = results.Distinct().ToArray();
if (distinct.Length != results.Count)
if (distinct.Length != results.Count) {
throw new Exception();
}
return new(results);
}
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++)
for (int i = methodWith.StartLine; i < methodWithEndLineValue + 1; i++) {
results.Add(i);
}
foreach (MethodWith m in methodWith.References) {
if (m.EndLine is null)
if (m.EndLine is null) {
continue;
}
AppendLineNumbers(results, nests, m, m.EndLine.Value);
}
nests.RemoveAt(nests.Count - 1);
@ -508,11 +552,13 @@ internal static partial class Helper20250320 {
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++)
for (int i = methodWith.StartLine; i < methodWithEndLineValue + 1; i++) {
results.Add(lines[i]);
}
foreach (MethodWith m in methodWith.References) {
if (m.EndLine is null)
if (m.EndLine is null) {
continue;
}
AppendLines(results, nests, lines, m, m.EndLine.Value);
}
nests.RemoveAt(nests.Count - 1);