150 lines
5.4 KiB
C#
150 lines
5.4 KiB
C#
namespace View_by_Distance.Property.Models.Stateless;
|
|
|
|
internal class XPath
|
|
{
|
|
|
|
internal static string GetRelativePath(string path, int length)
|
|
{
|
|
string result = path[length..].Replace(@"\", "/");
|
|
return result;
|
|
}
|
|
|
|
internal static bool DeleteEmptyDirectories(string rootDirectory)
|
|
{
|
|
bool result;
|
|
if (!Directory.Exists(rootDirectory))
|
|
result = false;
|
|
else
|
|
{
|
|
string[] files;
|
|
string[] directories = Directory.GetDirectories(rootDirectory, "*", SearchOption.TopDirectoryOnly);
|
|
if (directories.Length > 0)
|
|
files = Array.Empty<string>();
|
|
else
|
|
files = Directory.GetFiles(rootDirectory, "*", SearchOption.AllDirectories);
|
|
if (directories.Length == 0 && files.Length == 0)
|
|
{
|
|
result = true;
|
|
Directory.Delete(rootDirectory);
|
|
}
|
|
else
|
|
{
|
|
result = false;
|
|
foreach (string directory in directories)
|
|
{
|
|
result = DeleteEmptyDirectories(directory);
|
|
if (result)
|
|
result = DeleteEmptyDirectories(directory);
|
|
}
|
|
if (files is null)
|
|
{
|
|
directories = Directory.GetDirectories(rootDirectory, "*", SearchOption.TopDirectoryOnly);
|
|
result = directories.Length == 0;
|
|
}
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
internal static bool WriteAllText(string path, string contents, bool updateDateWhenMatches, bool compareBeforeWrite, DateTime? updateToWhenMatches)
|
|
{
|
|
bool result;
|
|
string text;
|
|
if (!compareBeforeWrite)
|
|
result = true;
|
|
else
|
|
{
|
|
if (!File.Exists(path))
|
|
text = string.Empty;
|
|
else
|
|
text = File.ReadAllText(path);
|
|
result = text != contents;
|
|
if (!result && updateDateWhenMatches)
|
|
{
|
|
if (updateToWhenMatches is null)
|
|
File.SetLastWriteTime(path, DateTime.Now);
|
|
else
|
|
File.SetLastWriteTime(path, updateToWhenMatches.Value);
|
|
}
|
|
}
|
|
if (result)
|
|
{
|
|
if (path.Contains("()"))
|
|
File.WriteAllText(path, contents);
|
|
else if (path.Contains("{}") && !path.EndsWith(".json"))
|
|
File.WriteAllText(path, contents);
|
|
else if (path.Contains("[]") && !path.EndsWith(".json"))
|
|
File.WriteAllText(path, contents);
|
|
else if (path.Contains("{}") && path.EndsWith(".json") && contents[0] == '{')
|
|
File.WriteAllText(path, contents);
|
|
else if (path.Contains("[]") && path.EndsWith(".json") && contents[0] == '[')
|
|
File.WriteAllText(path, contents);
|
|
else
|
|
File.WriteAllText(path, contents);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
internal static List<string> GetDirectoryNames(string directory)
|
|
{
|
|
List<string> results = new();
|
|
string? checkDirectory = directory;
|
|
string? pathRoot = Path.GetPathRoot(directory);
|
|
string extension = Path.GetExtension(directory);
|
|
if (string.IsNullOrEmpty(pathRoot))
|
|
throw new NullReferenceException(nameof(pathRoot));
|
|
if (Directory.Exists(directory))
|
|
results.Add(Path.GetFileName(directory));
|
|
else if ((string.IsNullOrEmpty(extension) || extension.Length > 3) && !File.Exists(directory))
|
|
results.Add(Path.GetFileName(directory));
|
|
for (int i = 0; i < int.MaxValue; i++)
|
|
{
|
|
checkDirectory = Path.GetDirectoryName(checkDirectory);
|
|
if (string.IsNullOrEmpty(checkDirectory) || checkDirectory == pathRoot)
|
|
break;
|
|
results.Add(Path.GetFileName(checkDirectory));
|
|
}
|
|
results.Add(pathRoot);
|
|
results.Reverse();
|
|
return results;
|
|
}
|
|
|
|
internal static (int level, List<string> directories) Get(string rootDirectory, string sourceDirectory)
|
|
{
|
|
int result = 0;
|
|
string? directory;
|
|
string? checkDirectory;
|
|
List<string> results = new();
|
|
checkDirectory = sourceDirectory;
|
|
for (int i = 0; i < int.MaxValue; i++)
|
|
{
|
|
result += 1;
|
|
directory = Path.GetFileName(checkDirectory);
|
|
if (string.IsNullOrEmpty(directory))
|
|
break;
|
|
results.Add(directory);
|
|
checkDirectory = Path.GetDirectoryName(checkDirectory);
|
|
if (checkDirectory == rootDirectory)
|
|
break;
|
|
}
|
|
results.Reverse();
|
|
return new(result, results);
|
|
}
|
|
|
|
internal static string GetDirectory(string sourceDirectory, int level, string directoryName)
|
|
{
|
|
string result;
|
|
string? checkDirectory;
|
|
checkDirectory = Path.GetDirectoryName(sourceDirectory);
|
|
for (int i = 0; i < level; i++)
|
|
checkDirectory = Path.GetDirectoryName(checkDirectory);
|
|
if (string.IsNullOrEmpty(checkDirectory))
|
|
throw new Exception();
|
|
checkDirectory = Path.Combine(checkDirectory, directoryName);
|
|
if (!Directory.Exists(checkDirectory))
|
|
_ = Directory.CreateDirectory(checkDirectory);
|
|
result = checkDirectory;
|
|
return result;
|
|
}
|
|
|
|
} |