Updating
This commit is contained in:
+18
@@ -0,0 +1,18 @@
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace LCU.Library.Internal.FileOperationsModels.Shared
|
||||
{
|
||||
public static class Clone
|
||||
{
|
||||
public static T DeepClone<T>(this T source)
|
||||
{
|
||||
if (ReferenceEquals(source, null))
|
||||
{
|
||||
return default;
|
||||
}
|
||||
|
||||
var deserializeSettings = new JsonSerializerSettings { ObjectCreationHandling = ObjectCreationHandling.Replace };
|
||||
return JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(source), deserializeSettings);
|
||||
}
|
||||
}
|
||||
}
|
||||
+130
@@ -0,0 +1,130 @@
|
||||
using LCU.Library.Internal.FileOperationsModels.Models;
|
||||
using LCU.Library.Internal.FileOperationsModels.Models.NameSettings;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using LCU.Library.Internal.FileOperationsModels.Models.Requests;
|
||||
|
||||
namespace LCU.Library.Internal.FileOperationsModels.Shared
|
||||
{
|
||||
public static class DynamicNameBuilder
|
||||
{
|
||||
public static List<TRequest> BuildListOfRequest<TRequest>(TRequest originalRequest) where TRequest : FileRequest
|
||||
{
|
||||
var nameList = RegexNameHandler.BuildFileList(originalRequest.SourcePath,
|
||||
originalRequest.FinalSourceFile,
|
||||
originalRequest.FileNameSetting);
|
||||
var requests = new List<TRequest>();
|
||||
|
||||
foreach (var name in nameList)
|
||||
{
|
||||
var newRequest = originalRequest.DeepClone();
|
||||
newRequest.SourceFile = name;
|
||||
|
||||
requests.Add(newRequest);
|
||||
}
|
||||
|
||||
return requests;
|
||||
}
|
||||
|
||||
public static List<TRequest> BuildListOfRequests<TRequest>(TRequest originalRequest) where TRequest : IFileSourceRequest
|
||||
{
|
||||
var nameList = RegexNameHandler.BuildFileList(originalRequest.FileSourceRequest.FinalSourcePath,
|
||||
originalRequest.FileSourceRequest.FinalSourceFile,
|
||||
originalRequest.FileSourceRequest.SourceFileSettings);
|
||||
|
||||
var requests = new List<TRequest>();
|
||||
|
||||
foreach (var name in nameList)
|
||||
{
|
||||
var newRequest = originalRequest.DeepClone();
|
||||
newRequest.FileSourceRequest.SourceFile = name;
|
||||
|
||||
requests.Add(newRequest);
|
||||
}
|
||||
|
||||
return requests;
|
||||
}
|
||||
|
||||
public static List<ModifyFileRequest> BuildListOfRequest(ModifyFileRequest originalRequest)
|
||||
{
|
||||
var nameList = RegexNameHandler.BuildFileList(originalRequest.SourcePath,
|
||||
originalRequest.OriginalSourceFile,
|
||||
originalRequest.FileNameSetting);
|
||||
|
||||
var requests = new List<ModifyFileRequest>();
|
||||
|
||||
foreach (var name in nameList)
|
||||
{
|
||||
var newRequest = originalRequest.DeepClone();
|
||||
newRequest.SourceFile = ModifyFileRequest.GetNameWithoutExtension(name, newRequest.SourceExtension);
|
||||
|
||||
requests.Add(newRequest);
|
||||
}
|
||||
|
||||
return requests;
|
||||
}
|
||||
|
||||
[Obsolete ("As of 7/7/2025, this method is obsolete. Use BuildListOfRequests instead.")]
|
||||
public static List<ExtractIndexFileDataRequest> BuildListOfRequest(ExtractIndexFileDataRequest originalRequest)
|
||||
{
|
||||
var nameList = RegexNameHandler.BuildFileList(originalRequest.FinalIndexFileDirectory,
|
||||
originalRequest.FinalIndexFileName,
|
||||
originalRequest.IndexFileNameSetting);
|
||||
|
||||
var requests = new List<ExtractIndexFileDataRequest>();
|
||||
|
||||
foreach (var name in nameList)
|
||||
{
|
||||
var newRequest = originalRequest.DeepClone();
|
||||
newRequest.IndexFileName = name;
|
||||
|
||||
requests.Add(newRequest);
|
||||
}
|
||||
|
||||
return requests;
|
||||
}
|
||||
|
||||
public static string BuildDynamicName(string name, DynamicNameSetting nameSetting)
|
||||
{
|
||||
if (nameSetting == null ||
|
||||
nameSetting.DateFormatSettings == null)
|
||||
{
|
||||
return name;
|
||||
}
|
||||
|
||||
name = AddFormattedDateSettings(name, nameSetting);
|
||||
return name;
|
||||
}
|
||||
|
||||
public static string AddFormattedDateSettings(string name, DynamicNameSetting nameSettings)
|
||||
{
|
||||
foreach (var formatSetting in nameSettings.DateFormatSettings)
|
||||
{
|
||||
var convertedFormat = DateTimeStringBuilder(formatSetting.DateFormat, nameSettings.DaysFromToday);
|
||||
name = ReplaceTagWithFormattedSTring(name, formatSetting.Tag, convertedFormat);
|
||||
}
|
||||
|
||||
return name;
|
||||
}
|
||||
|
||||
public static string DateTimeStringBuilder(string format, int daysFromToday)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(format))
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
return DateTime.Now.AddDays(daysFromToday).ToString(format);
|
||||
}
|
||||
|
||||
public static string ReplaceTagWithFormattedSTring(string name, string tag, string convertedFormat)
|
||||
{
|
||||
if (string.IsNullOrEmpty(tag) || string.IsNullOrWhiteSpace(convertedFormat))
|
||||
{
|
||||
return name.Trim();
|
||||
}
|
||||
|
||||
return name.Replace(tag, convertedFormat);
|
||||
}
|
||||
}
|
||||
}
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
using LCU.Library.Internal.FileOperationsModels.Models.NameSettings;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace LCU.Library.Internal.FileOperationsModels.Shared
|
||||
{
|
||||
public static class RegexNameHandler
|
||||
{
|
||||
public static ICollection<string> BuildFileList(string path, string name, DynamicNameSetting nameSetting)
|
||||
{
|
||||
var fileList = new List<string>();
|
||||
|
||||
if (nameSetting == null ||
|
||||
nameSetting.RegexSettings == null ||
|
||||
!nameSetting.RegexSettings.Any())
|
||||
{
|
||||
fileList.Add(name);
|
||||
return fileList;
|
||||
}
|
||||
|
||||
DirectoryInfo localDirectory = new DirectoryInfo(path);
|
||||
var regexName = BuildRegexName(name, nameSetting);
|
||||
fileList = localDirectory.GetFiles().Where(file => regexName.IsMatch(file.Name)).Select(file => file.Name).ToList();
|
||||
|
||||
return fileList;
|
||||
}
|
||||
|
||||
public static ICollection<string> BuildDirectoryList(string path, string name, DynamicNameSetting nameSetting)
|
||||
{
|
||||
var directoriesList = new List<string>();
|
||||
|
||||
if (nameSetting == null ||
|
||||
nameSetting.RegexSettings == null ||
|
||||
string.IsNullOrWhiteSpace(name))
|
||||
{
|
||||
directoriesList.Add(name);
|
||||
return directoriesList;
|
||||
}
|
||||
|
||||
DirectoryInfo localDirectory = new DirectoryInfo(path);
|
||||
var regexName = BuildRegexName(name, nameSetting);
|
||||
directoriesList = localDirectory.GetDirectories().Where(directory => regexName.IsMatch(directory.Name)).Select(directory => directory.Name).ToList();
|
||||
|
||||
return directoriesList;
|
||||
}
|
||||
|
||||
public static Regex BuildRegexName(string name, DynamicNameSetting nameSettings)
|
||||
{
|
||||
foreach (var formatSetting in nameSettings.RegexSettings)
|
||||
{
|
||||
name = ReplaceTagWithRegex(name, formatSetting.Tag, formatSetting.Regex);
|
||||
}
|
||||
|
||||
return new Regex(name);
|
||||
}
|
||||
|
||||
public static string ReplaceTagWithRegex(string name, string tag, string regex)
|
||||
{
|
||||
if (string.IsNullOrEmpty(tag) || string.IsNullOrWhiteSpace(regex))
|
||||
{
|
||||
return name.Trim();
|
||||
}
|
||||
|
||||
return name.Replace(tag, regex);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user