SortCodeMethods
DirectoryToISO TextToJson
This commit is contained in:
@ -1,274 +0,0 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace File_Folder_Helper.Helpers;
|
||||
|
||||
internal static partial class HelperVSCodePossibleExtension
|
||||
{
|
||||
|
||||
private record Method(string Name,
|
||||
int ParameterCount,
|
||||
int StartLine,
|
||||
int EndLine,
|
||||
int FirstUsedLine);
|
||||
|
||||
[GeneratedRegex(@"(?<method>[A-Z]{1}[A-Za-z_0-9]*)\(")]
|
||||
private static partial Regex CSharpMethodName();
|
||||
|
||||
[GeneratedRegex(@"\s[a-zA-Z_]*,")]
|
||||
private static partial Regex CSharpParameter();
|
||||
|
||||
[GeneratedRegex(@"\b(public|private|internal|protected)\s\b(static)?\s?\b(partial)?\s?\b(async)?\s?[[\]<,>?a-zA-Z()\s]*\s[A-Z]{1}[a-zA-Z_]+\(.*\)")]
|
||||
private static partial Regex CSharpMethodLine();
|
||||
|
||||
private static string? GetName(string line)
|
||||
{
|
||||
string? result;
|
||||
Match match = CSharpMethodName().Match(line);
|
||||
if (!match.Success)
|
||||
result = null;
|
||||
else
|
||||
result = match.Groups["method"].Value;
|
||||
return result;
|
||||
}
|
||||
|
||||
private static int GetStartLine(string[] lines, int i)
|
||||
{
|
||||
int result = i;
|
||||
string line;
|
||||
for (int j = i - 1; j > -1; j--)
|
||||
{
|
||||
line = lines[j].Trim();
|
||||
if (!line.StartsWith('[') && !line.StartsWith("/// "))
|
||||
break;
|
||||
result--;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static int GetParameterCount(string line, string search)
|
||||
{
|
||||
int result;
|
||||
string after = line.Split(search)[^1];
|
||||
if (after.StartsWith(')'))
|
||||
result = 0;
|
||||
else
|
||||
{
|
||||
string[] segments = CSharpParameter().Split(after);
|
||||
result = segments.Length;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static int GetLineBlockCount(string line, bool isLinq)
|
||||
{
|
||||
int result = 0;
|
||||
bool ignore = false;
|
||||
for (int i = 0; i < line.Length; i++)
|
||||
{
|
||||
if (line[i] == '\'')
|
||||
i++;
|
||||
else if (!isLinq && !ignore && line[i] == '{')
|
||||
result++;
|
||||
else if (!isLinq && !ignore && line[i] == '}')
|
||||
result--;
|
||||
else if (isLinq && !ignore && line[i] == ';')
|
||||
result--;
|
||||
else if (i > 0 && line[i] == '"' && line[i - 1] != '\\')
|
||||
ignore = !ignore;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static int? GetFirstUsedLine(string[] lines, int i, string search, string searchNot, string searchWrap, int parameterCount)
|
||||
{
|
||||
int? result = null;
|
||||
string[] segments;
|
||||
string[] afterSegments;
|
||||
string lastSegmentBeforeDot;
|
||||
for (int j = 0; j < lines.Length; j++)
|
||||
{
|
||||
if (j == i)
|
||||
continue;
|
||||
segments = lines[j].Split(search);
|
||||
if (segments.Length == 1)
|
||||
{
|
||||
segments = lines[j].Split(searchNot);
|
||||
if (segments.Length == 1)
|
||||
{
|
||||
segments = lines[j].Split(searchWrap);
|
||||
if (segments.Length == 1)
|
||||
continue;
|
||||
}
|
||||
}
|
||||
lastSegmentBeforeDot = segments[^1].Split(").")[0];
|
||||
if (parameterCount == 0)
|
||||
{
|
||||
if (lastSegmentBeforeDot.Contains(','))
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
afterSegments = lastSegmentBeforeDot.Split(',');
|
||||
if (afterSegments.Length != parameterCount)
|
||||
continue;
|
||||
}
|
||||
result = j;
|
||||
break;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static ReadOnlyCollection<int> GetMethodLines(ReadOnlyCollection<Method> methods)
|
||||
{
|
||||
List<int> results = [];
|
||||
foreach (Method method in methods)
|
||||
{
|
||||
for (int i = method.StartLine; i < method.EndLine + 1; i++)
|
||||
results.Add(i);
|
||||
}
|
||||
return new(results);
|
||||
}
|
||||
|
||||
private static ReadOnlyCollection<Method> GetMethods(string cSharpFile, ILogger<Worker> logger, string[] lines)
|
||||
{
|
||||
List<Method> results = [];
|
||||
int blocks;
|
||||
bool isLinq;
|
||||
int endLine;
|
||||
string line;
|
||||
string? name;
|
||||
int startLine;
|
||||
string search;
|
||||
string innerLine;
|
||||
string searchNot;
|
||||
string searchWrap;
|
||||
int parameterCount;
|
||||
int? firstUsedLine;
|
||||
string lineSegmentFirst;
|
||||
for (int i = 0; i < lines.Length; i++)
|
||||
{
|
||||
line = lines[i].Trim();
|
||||
if (string.IsNullOrEmpty(line))
|
||||
continue;
|
||||
if (line.Length < 5)
|
||||
continue;
|
||||
if (line.EndsWith(','))
|
||||
continue;
|
||||
if (!CSharpMethodLine().Match(line).Success)
|
||||
continue;
|
||||
name = GetName(line);
|
||||
search = $" {name}(";
|
||||
searchNot = $"!{name}(";
|
||||
searchWrap = $"({name}(";
|
||||
if (string.IsNullOrEmpty(name))
|
||||
continue;
|
||||
blocks = 0;
|
||||
startLine = GetStartLine(lines, i);
|
||||
parameterCount = GetParameterCount(line, search);
|
||||
isLinq = lines[i + 1].Trim() != "{";
|
||||
if (isLinq)
|
||||
blocks++;
|
||||
for (int j = i + 1; j < lines.Length; j++)
|
||||
{
|
||||
innerLine = lines[j].Trim();
|
||||
if (isLinq && string.IsNullOrEmpty(innerLine))
|
||||
{
|
||||
if (line.EndsWith(';'))
|
||||
blocks--;
|
||||
}
|
||||
blocks += GetLineBlockCount(innerLine, isLinq);
|
||||
if (blocks == 0)
|
||||
{
|
||||
endLine = j;
|
||||
if (lines.Length > j + 1 && string.IsNullOrEmpty(lines[j + 1].Trim()))
|
||||
endLine++;
|
||||
firstUsedLine = GetFirstUsedLine(lines, i, search, searchNot, searchWrap, parameterCount);
|
||||
if (firstUsedLine is null)
|
||||
{
|
||||
lineSegmentFirst = line.Split(search)[0];
|
||||
if (!lines[i - 1].Trim().StartsWith("[Obsolete"))
|
||||
{
|
||||
if (lineSegmentFirst.StartsWith("private"))
|
||||
logger.LogWarning("<{cSharpFileName}> {name} with {parameterCount} parameter(s) <{line}>", Path.GetFileName(cSharpFile), name, parameterCount, lineSegmentFirst);
|
||||
else
|
||||
logger.LogInformation("<{cSharpFileName}> {name} with {parameterCount} parameter(s) <{line}>", Path.GetFileName(cSharpFile), name, parameterCount, lineSegmentFirst);
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (j > lines.Length - 2)
|
||||
throw new Exception();
|
||||
results.Add(new(name, parameterCount, startLine, endLine, firstUsedLine.Value));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return new(results.OrderBy(l => l.FirstUsedLine).ToArray());
|
||||
}
|
||||
|
||||
private static bool WriteAllLines(string cSharpFile, string[] lines, ReadOnlyCollection<Method> methods)
|
||||
{
|
||||
bool result;
|
||||
List<string> results = [];
|
||||
ReadOnlyCollection<int> methodLines = GetMethodLines(methods);
|
||||
int minMethodLines = methodLines.Min();
|
||||
for (int i = 0; i < minMethodLines; i++)
|
||||
results.Add(lines[i]);
|
||||
foreach (Method method in methods)
|
||||
{
|
||||
for (int i = method.StartLine; i < method.EndLine + 1; i++)
|
||||
results.Add(lines[i]);
|
||||
}
|
||||
for (int i = minMethodLines; i < lines.Length; i++)
|
||||
{
|
||||
if (methodLines.Contains(i))
|
||||
continue;
|
||||
results.Add(lines[i]);
|
||||
}
|
||||
string text = File.ReadAllText(cSharpFile);
|
||||
string join = string.Join(Environment.NewLine, results);
|
||||
if (join == text)
|
||||
result = false;
|
||||
else
|
||||
{
|
||||
result = true;
|
||||
File.WriteAllText(cSharpFile, join);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static bool SortFile(ILogger<Worker> logger, string cSharpFile, string[] lines)
|
||||
{
|
||||
bool result;
|
||||
ReadOnlyCollection<Method> methods = GetMethods(cSharpFile, logger, lines);
|
||||
if (methods.Count == 0)
|
||||
result = false;
|
||||
else
|
||||
result = WriteAllLines(cSharpFile, lines, methods);
|
||||
return result;
|
||||
}
|
||||
|
||||
internal static void Sort(ILogger<Worker> logger, List<string> args)
|
||||
{
|
||||
bool result = false;
|
||||
bool check;
|
||||
string[] lines;
|
||||
long ticks = DateTime.Now.Ticks;
|
||||
logger.LogInformation("{ticks}", ticks);
|
||||
string[] cSharpFiles = Directory.GetFiles(args[0], "*.cs", SearchOption.TopDirectoryOnly);
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
foreach (string cSharpFile in cSharpFiles)
|
||||
{
|
||||
lines = File.ReadAllLines(cSharpFile);
|
||||
check = SortFile(logger, cSharpFile, lines);
|
||||
if (check && !result)
|
||||
result = true;
|
||||
}
|
||||
if (!result)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -8,9 +8,6 @@ namespace File_Folder_Helper.Helpers;
|
||||
internal static partial class HelperZipFilesBy
|
||||
{
|
||||
|
||||
[GeneratedRegex("[a-zA-Z0-9]{1,}")]
|
||||
private static partial Regex LowerAlphaAlphaAndNumber();
|
||||
|
||||
private static DateTimeOffset? GetDateTimeOffset(string keyFileExtension, FileInfo fileInfo, FileInfo extractKeyFileInfo)
|
||||
{
|
||||
DateTimeOffset? dateTimeOffset = null;
|
||||
@ -31,6 +28,9 @@ internal static partial class HelperZipFilesBy
|
||||
return dateTimeOffset;
|
||||
}
|
||||
|
||||
[GeneratedRegex("[a-zA-Z0-9]{1,}")]
|
||||
private static partial Regex LowerAlphaAlphaAndNumber();
|
||||
|
||||
private static bool ExtractKeyFileAndSetDateFromZipEntry(ILogger<Worker> logger, string[] zipFiles, string keyFileExtension, string keyFileExtensionB, string keyFileExtensionC, bool renameToLower)
|
||||
{
|
||||
bool result = false;
|
||||
@ -120,6 +120,37 @@ internal static partial class HelperZipFilesBy
|
||||
return result;
|
||||
}
|
||||
|
||||
private static void ZipDirectory(ILogger<Worker> logger, string directory)
|
||||
{
|
||||
logger.LogInformation("{directory}", directory);
|
||||
string zipFile = $"{directory}.zip";
|
||||
int skipChars = directory.Length + 1;
|
||||
string[] files = Directory.GetFiles(directory, "*", SearchOption.AllDirectories);
|
||||
string[] directories = Directory.GetDirectories(directory, "*", SearchOption.AllDirectories);
|
||||
ZipArchiveMode zipArchiveMode = File.Exists(zipFile) ? ZipArchiveMode.Update : ZipArchiveMode.Create;
|
||||
for (int i = 1; i < 3; i++)
|
||||
{
|
||||
try
|
||||
{
|
||||
using ZipArchive zip = ZipFile.Open(zipFile, zipArchiveMode);
|
||||
for (int j = 0; j < directories.Length; j++)
|
||||
_ = zip.CreateEntry($"{directories[j][skipChars..]}/");
|
||||
foreach (string file in files)
|
||||
{
|
||||
_ = zip.CreateEntryFromFile(file, file[skipChars..]);
|
||||
File.Delete(file);
|
||||
}
|
||||
break;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
File.Delete(zipFile);
|
||||
zipArchiveMode = ZipArchiveMode.Create;
|
||||
}
|
||||
}
|
||||
Directory.Delete(directory, recursive: true);
|
||||
}
|
||||
|
||||
internal static bool ZipFilesByDate(ILogger<Worker> logger, string sourceDirectory, SearchOption searchOption = SearchOption.TopDirectoryOnly, string dayFormat = "")
|
||||
{
|
||||
bool result = false;
|
||||
@ -291,37 +322,6 @@ internal static partial class HelperZipFilesBy
|
||||
return result;
|
||||
}
|
||||
|
||||
private static void ZipDirectory(ILogger<Worker> logger, string directory)
|
||||
{
|
||||
logger.LogInformation("{directory}", directory);
|
||||
string zipFile = $"{directory}.zip";
|
||||
int skipChars = directory.Length + 1;
|
||||
string[] files = Directory.GetFiles(directory, "*", SearchOption.AllDirectories);
|
||||
string[] directories = Directory.GetDirectories(directory, "*", SearchOption.AllDirectories);
|
||||
ZipArchiveMode zipArchiveMode = File.Exists(zipFile) ? ZipArchiveMode.Update : ZipArchiveMode.Create;
|
||||
for (int i = 1; i < 3; i++)
|
||||
{
|
||||
try
|
||||
{
|
||||
using ZipArchive zip = ZipFile.Open(zipFile, zipArchiveMode);
|
||||
for (int j = 0; j < directories.Length; j++)
|
||||
_ = zip.CreateEntry($"{directories[j][skipChars..]}/");
|
||||
foreach (string file in files)
|
||||
{
|
||||
_ = zip.CreateEntryFromFile(file, file[skipChars..]);
|
||||
File.Delete(file);
|
||||
}
|
||||
break;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
File.Delete(zipFile);
|
||||
zipArchiveMode = ZipArchiveMode.Create;
|
||||
}
|
||||
}
|
||||
Directory.Delete(directory, recursive: true);
|
||||
}
|
||||
|
||||
internal static void ZipFilesByDirectoryWithFile(ILogger<Worker> logger, string sourceDirectory)
|
||||
{
|
||||
string[] files1;
|
||||
|
Reference in New Issue
Block a user