feat: file metadata
This commit is contained in:
102
CarManagerV3/Classes/FileUpdate.cs
Normal file
102
CarManagerV3/Classes/FileUpdate.cs
Normal file
@@ -0,0 +1,102 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using CarManagerV3.Manager;
|
||||
|
||||
namespace CarManagerV3.Classes
|
||||
{
|
||||
internal class FileUpdate
|
||||
{
|
||||
public string version;
|
||||
public bool needUpdate;
|
||||
public Func<string, string> updateAction;
|
||||
|
||||
public static List<FileUpdate> GetUpdates()
|
||||
{
|
||||
return new List<FileUpdate>
|
||||
{
|
||||
new FileUpdate {
|
||||
version = "1.4.3.0",
|
||||
needUpdate = true,
|
||||
updateAction = (fileContent) =>
|
||||
{
|
||||
// Implement the logic to update the file content for version
|
||||
List<string> parts = fileContent.Split(";").ToList();
|
||||
parts[4] = "Black";
|
||||
return string.Join(";", parts);
|
||||
}
|
||||
},
|
||||
new FileUpdate {
|
||||
version = "1.4.4.0",
|
||||
needUpdate = false
|
||||
}
|
||||
// Add more FileUpdate instances for other versions as needed
|
||||
};
|
||||
}
|
||||
|
||||
public static List<FileUpdate> GetRequiredUpdates(string fromVersion)
|
||||
{
|
||||
string currentVersion = Updater.GetCurrentVersion();
|
||||
// Get all updates whos version is greater than fromVersion
|
||||
return GetUpdates().Where(update => string.Compare(update.version, fromVersion) > 0 && string.Compare(update.version, currentVersion) <= 0 && update.needUpdate && update.updateAction != null).ToList();
|
||||
}
|
||||
|
||||
public static string ApplyUpdates(string fileContent, string fileVersion)
|
||||
{
|
||||
List<FileUpdate> updates = GetRequiredUpdates(fileVersion);
|
||||
foreach (var update in updates)
|
||||
{
|
||||
if (update.needUpdate && update.updateAction != null)
|
||||
{
|
||||
fileContent = update.updateAction(fileContent);
|
||||
}
|
||||
}
|
||||
return fileContent;
|
||||
}
|
||||
|
||||
public static List<string> ApplyUpdatesToALl(List<string> fileContents, string fileVersion)
|
||||
{
|
||||
List<FileUpdate> updates = GetRequiredUpdates(fileVersion);
|
||||
List<string> updatedContents = new List<string>();
|
||||
foreach (var content in fileContents)
|
||||
{
|
||||
string updatedContent = content;
|
||||
foreach (var update in updates)
|
||||
{
|
||||
if (update.needUpdate && update.updateAction != null)
|
||||
{
|
||||
updatedContent = update.updateAction(updatedContent);
|
||||
}
|
||||
}
|
||||
updatedContents.Add(updatedContent);
|
||||
}
|
||||
return updatedContents;
|
||||
}
|
||||
|
||||
|
||||
public static string PerformOldLegacyUpdate(string fileContent)
|
||||
{
|
||||
string[] parts = fileContent.Split(';');
|
||||
// is part 7 a valid int? if not set it to 0 and log a warning.
|
||||
if (parts.Length == 7)
|
||||
{
|
||||
Console.Error.WriteLine($"Warning: CSV string has only 7 fields, expected 8. Setting Order to 0. CSV: {fileContent}");
|
||||
if (!StateManager.askForMigration())
|
||||
{
|
||||
throw new Exception("User declined migration. Cannot parse CSV string with missing Order field.");
|
||||
}
|
||||
Array.Resize(ref parts, 8);
|
||||
parts[7] = "0";
|
||||
return string.Join(";", parts);
|
||||
}
|
||||
else if (parts.Length != 8)
|
||||
{
|
||||
throw new FormatException($"CSV string has {parts.Length} fields, expected 8. CSV: {fileContent}");
|
||||
}
|
||||
return fileContent;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user