From 34bf088c7806ea501a13d9a999d3e09ea2cea5dd Mon Sep 17 00:00:00 2001 From: Robert Morrison Date: Mon, 3 Jun 2024 02:54:38 +0100 Subject: [PATCH] refactor: Merge Project/Runtime Settings Merge the RuntimeSettings class into ProjectSettings, Minimising the duplication of stored data --- Program.cs | 39 +++++++++---------------- ProjectSettings/ProjectSettings.cs | 31 +++++++++++--------- RuntimeSettings/RuntimeSettings.cs | 33 --------------------- SiteFile/SiteFile.ConverterFunctions.cs | 12 ++++---- SiteFile/SiteFile.cs | 10 +++---- 5 files changed, 42 insertions(+), 83 deletions(-) delete mode 100644 RuntimeSettings/RuntimeSettings.cs diff --git a/Program.cs b/Program.cs index 447b581..b980fa4 100644 --- a/Program.cs +++ b/Program.cs @@ -149,21 +149,14 @@ class Program AnsiConsole.Console.Profile.Capabilities.Ansi = true; - // NOTE: Future refactors may merge ProjectSettings and RuntimeSettings - ProjectSettings projectSettings = GetProjectSettings(ProjectDirectory); - - DirectoryInfo inputDir = new(projectSettings.Source); - DirectoryInfo outputDir = new(projectSettings.Destination); - - RuntimeSettings settings = new(inputDir,outputDir); - settings.setBaseUrl(projectSettings.BaseUrl); + ProjectSettings settings = GetProjectSettings(ProjectDirectory); List siteFiles = new(); - Utils.GetFiles(inputDir).ForEach(x => siteFiles.Add(new SiteFile(x))); + Utils.GetFiles(settings.InputDirectory).ForEach(x => siteFiles.Add(new SiteFile(x))); Log.Information("SiteFiles: {@sf} {count}", siteFiles, siteFiles.Count); - Console.WriteLine($"Converting {siteFiles.Count} files from {inputDir.FullName} to {outputDir.FullName}"); + Console.WriteLine($"Converting {siteFiles.Count} files from {settings.InputDirectory.FullName} to {settings.OutputDirectory.FullName}"); Dictionary fileStatus = new(); @@ -224,40 +217,36 @@ class Program { Log.Information("Clean command was called, Beginning cleaning"); - // NOTE: Future refactors may merge ProjectSettings and RuntimeSettings - ProjectSettings projectSettings = GetProjectSettings(ProjectDirectory); + ProjectSettings settings = GetProjectSettings(ProjectDirectory); - DirectoryInfo inputDir = new(projectSettings.Source); - DirectoryInfo outputDir = new(projectSettings.Destination); - - if (!outputDir.Exists) + if (!settings.OutputDirectory.Exists) { - Log.Warning("Not deleting {dir} as it doesn't exist",outputDir.FullName); - AnsiConsole.MarkupLineInterpolated($"[bold][[[yellow]Warning[/]]][/] Not cleaning [blue]\"{outputDir}\"[/] as it does not exist."); + Log.Warning("Not deleting {dir} as it doesn't exist",settings.OutputDirectory.FullName); + AnsiConsole.MarkupLineInterpolated($"[bold][[[yellow]Warning[/]]][/] Not cleaning [blue]\"{settings.OutputDirectory.FullName}\"[/] as it does not exist."); return 0; // success because it doesn't exist. } try { - Log.Information("Cleaning {dir}",outputDir.FullName); - AnsiConsole.MarkupInterpolated($"Cleaning [blue]\"{outputDir.FullName}\"[/]"); - outputDir.Delete(recursive: true); - outputDir.Create(); + Log.Information("Cleaning {dir}",settings.OutputDirectory.FullName); + AnsiConsole.MarkupInterpolated($"Cleaning [blue]\"{settings.OutputDirectory.FullName}\"[/]"); + settings.OutputDirectory.Delete(recursive: true); + settings.OutputDirectory.Create(); AnsiConsole.MarkupLine(" [bold][[[green]OK[/]]][/]"); - AnsiConsole.MarkupLineInterpolated($"\t[grey]>>[/] [green]All files in {outputDir.FullName} purged successfully[/]"); + AnsiConsole.MarkupLineInterpolated($"\t[grey]>>[/] [green]All files in {settings.OutputDirectory.FullName} purged successfully[/]"); return 0; } catch (System.Security.SecurityException e) { AnsiConsole.MarkupLine(" [bold][[[red]Fail[/]]][/]"); AnsiConsole.MarkupLine("[orangered1]See log for more details about what went wrong.[/]"); - Log.Error(e, "Failed to delete directory {dir} due to permission error.", outputDir.FullName); + Log.Error(e, "Failed to delete directory {dir} due to permission error.", settings.OutputDirectory.FullName); return 1; } catch (Exception e) { AnsiConsole.MarkupLine(" [red][[[bold]Fail[/]]][/]"); AnsiConsole.MarkupLine("[orangered1]See log for more details about what went wrong.[/]"); - Log.Error(e, "Failed to delete/create directory {dir}", outputDir.FullName); + Log.Error(e, "Failed to delete/create directory {dir}", settings.OutputDirectory.FullName); return 1; } } diff --git a/ProjectSettings/ProjectSettings.cs b/ProjectSettings/ProjectSettings.cs index 5c2e412..9247dd2 100644 --- a/ProjectSettings/ProjectSettings.cs +++ b/ProjectSettings/ProjectSettings.cs @@ -2,32 +2,33 @@ using System.Text.Json.Serialization; // project settings is a user accessible config to set the Source and destination of a site // This may include more scope in the future such as holding the site base address etc.. -class ProjectSettings +public class ProjectSettings { - private string _Source; - private string _Destination; + // The Source and Destination need to be public for the json constructor to work properly. + public string Source {get; private set;} + public string Destination {get; private set;} private DirectoryInfo? _ProjectRoot; public string? BaseUrl {get; private set;} - public string Source {get { + public DirectoryInfo InputDirectory {get { if (_ProjectRoot is null) { - return _Source; + return new(Source); } - return Path.Combine(_ProjectRoot.FullName,_Source); + return new(Path.Combine(_ProjectRoot.FullName,Source)); }} - public string Destination {get { + public DirectoryInfo OutputDirectory {get { if (_ProjectRoot is null) { - return _Destination; + return new(Destination); } - return Path.Combine(_ProjectRoot.FullName,_Destination); + return new(Path.Combine(_ProjectRoot.FullName,Destination)); }} [JsonConstructor] public ProjectSettings(String source, String destination, string baseUrl) { - _Source = source; - _Destination = destination; + Source = source; + Destination = destination; BaseUrl = baseUrl; } @@ -38,7 +39,9 @@ class ProjectSettings _ProjectRoot = projectRoot; } - public void setBaseUrl(string baseUrl) { - BaseUrl = baseUrl; - } + /* + * public void setBaseUrl(string baseUrl) { + * BaseUrl = baseUrl; + * } + */ } diff --git a/RuntimeSettings/RuntimeSettings.cs b/RuntimeSettings/RuntimeSettings.cs deleted file mode 100644 index 8c7e627..0000000 --- a/RuntimeSettings/RuntimeSettings.cs +++ /dev/null @@ -1,33 +0,0 @@ -namespace csSiteGen; - - -/// -/// Class RuntimeSettings Contains all the settings that could be loaded from the commandline. -/// -public class RuntimeSettings { - public DirectoryInfo InputDirectory {get; private set;} - public DirectoryInfo OutputDirectory {get; private set;} - public string? BaseUrl {get; private set;} - - - public RuntimeSettings(string inputDirectory, string outputDirectory){ - InputDirectory = new DirectoryInfo(inputDirectory); - OutputDirectory = new DirectoryInfo(outputDirectory); - - /* NOTE: it is the responisbility of the UI code to check the values passed are good. - */ - } - - - public RuntimeSettings(DirectoryInfo inputDirectory, DirectoryInfo outputDirectory){ - InputDirectory = inputDirectory; - OutputDirectory = outputDirectory; - - /* NOTE: it is the responisbility of the UI code to check the values passed are good. - */ - } - - public void setBaseUrl(string? baseurl) { - BaseUrl = baseurl; - } -} diff --git a/SiteFile/SiteFile.ConverterFunctions.cs b/SiteFile/SiteFile.ConverterFunctions.cs index 0710b93..969019d 100644 --- a/SiteFile/SiteFile.ConverterFunctions.cs +++ b/SiteFile/SiteFile.ConverterFunctions.cs @@ -6,7 +6,7 @@ namespace csSiteGen; public static class Conversions{ - public delegate bool ConvertFunc(FileInfo file, RuntimeSettings settings); + public delegate bool ConvertFunc(FileInfo file, ProjectSettings settings); /// /// A Mapping of filetype to ConvertFunc. @@ -24,7 +24,7 @@ public static class Conversions{ /// /// TEST FUNCTION. /// - public static bool NoOp(FileInfo file, RuntimeSettings settings){ + public static bool NoOp(FileInfo file, ProjectSettings settings){ Log.Information("Performing NoOp Conversion"); string newName = GetNewName(file,settings,"NoOp"); Log.Debug("{FullName} -> {newName}",file.FullName,newName); @@ -35,7 +35,7 @@ public static class Conversions{ /// /// Copy the file verbatim (doing any baseurl replacements if needed) /// - public static bool RawCpy(FileInfo file, RuntimeSettings settings){ + public static bool RawCpy(FileInfo file, ProjectSettings settings){ FileInfo newPath = new FileInfo(GetNewName(file,settings,null)); Log.Information("RawCpy: Copying {file} to {dest}",file.FullName, newPath.FullName); @@ -65,7 +65,7 @@ public static class Conversions{ /// /// Execute pandoc on the file, automatically detecting the template to use. /// - public static bool Pandoc(FileInfo file, RuntimeSettings settings){ + public static bool Pandoc(FileInfo file, ProjectSettings settings){ // NOTE: Some of the code later where the tmpfile is created for baseurl replacement may be too safe. // the extension checks may be unnecessary, but this depends on if this function will be retooled to run pandoc for different conversions. // for now I have take the safer approach, but the leaner approach may be used in the future when the project is more mature @@ -208,13 +208,13 @@ public static class Conversions{ return true; } - private static string GetNewName(FileInfo file, RuntimeSettings settings, string? newExtension){ + private static string GetNewName(FileInfo file, ProjectSettings settings, string? newExtension){ return file.FullName .Replace(settings.InputDirectory.FullName, settings.OutputDirectory.FullName) .Replace(file.Extension,newExtension ?? file.Extension); } - private static string? BaseUrlReplace(FileInfo file, RuntimeSettings settings){ + private static string? BaseUrlReplace(FileInfo file, ProjectSettings settings){ Log.Information("Doing BaseUrlReplace for {f}", file.FullName); // Read the file using (StreamReader FileReader = file.OpenText()) diff --git a/SiteFile/SiteFile.cs b/SiteFile/SiteFile.cs index 933d945..e8020f4 100644 --- a/SiteFile/SiteFile.cs +++ b/SiteFile/SiteFile.cs @@ -30,7 +30,7 @@ public partial class SiteFile Log.Information("{file} extension is {ext}",fileInfo.FullName, fileInfo.Extension); // Using this Ensures that the ConverterFunction is Always set. - // ConverterFunctions ALWAYS accept just the FileInfo, and RuntimeSettings passed at convert time. + // ConverterFunctions ALWAYS accept just the FileInfo, and ProjectSettings passed at convert time. ConverterFunction = Conversions.Mappings.GetValueOrDefault(info.Extension, Conversions.RawCpy); } @@ -38,7 +38,7 @@ public partial class SiteFile /// Convert the file, placing it in the correct place in the output directory. /// If a filetype conversion is not needed, or specified, then the file is simply copied. /// - public bool Convert(RuntimeSettings settings) + public bool Convert(ProjectSettings settings) { if (!NeedsUpdating(settings)) { @@ -65,7 +65,7 @@ public partial class SiteFile return res; } - private bool NeedsUpdating(RuntimeSettings settings) + private bool NeedsUpdating(ProjectSettings settings) { if (Metadata is null) { @@ -87,7 +87,7 @@ public partial class SiteFile * But that ensures that if you remove the output directory the site will be * Fully recreated. */ - private void LoadMetadata(RuntimeSettings settings) + private void LoadMetadata(ProjectSettings settings) { string metaFile = $"{settings.OutputDirectory}/.files"; Log.Information("Loading Metadata from {file}",metaFile); @@ -113,7 +113,7 @@ public partial class SiteFile } } - private void SaveMetadata(RuntimeSettings settings) + private void SaveMetadata(ProjectSettings settings) { if (Metadata is null) {