diff --git a/OpenNest.Desktop.Tests/NestTemplateMigrationTests.cs b/OpenNest.Desktop.Tests/NestTemplateMigrationTests.cs new file mode 100644 index 00000000..3425d022 --- /dev/null +++ b/OpenNest.Desktop.Tests/NestTemplateMigrationTests.cs @@ -0,0 +1,100 @@ +using OpenNest.Data; +using OpenNest.Engine.BestFit; +using OpenNest.IO; + +namespace OpenNest.Desktop.Tests; + +public sealed class NestTemplateMigrationTests : IDisposable +{ + private readonly string directory = + Directory.CreateTempSubdirectory("opennest-desktop-template-").FullName; + + public void Dispose() => Directory.Delete(directory, recursive: true); + + private string DefaultsPath => Path.Combine(directory, "defaults.json"); + + private readonly BestFitCacheStore store = BestFitCacheStore.Shared; + + private string WriteTemplate() + { + var path = Path.Combine(directory, "template.nstdot"); + var nest = TestNests.OneSquare(); + nest.Units = Units.Millimeters; + Assert.True(new NestWriter(nest).Write(path)); + return path; + } + + [Theory] + [InlineData(null)] + [InlineData("")] + [InlineData(" ")] + public void NoTemplate_DoesNothing(string? templatePath) + { + var result = NestTemplateMigration.Run(templatePath!, DefaultsPath, store); + + Assert.Equal(new NestTemplateMigrationResult(false, null!), result); + Assert.False(File.Exists(DefaultsPath)); + } + + [Fact] + public void ATemplate_BecomesTheDefaultsFile_AndTheSettingIsCleared() + { + var settings = new Dictionary { ["NestTemplatePath"] = WriteTemplate() }; + + var result = NestTemplateMigration.Run(settings, DefaultsPath, store); + + Assert.True(result.ClearSetting); + Assert.Null(result.Warning); + Assert.Equal("", settings["NestTemplatePath"]); + Assert.Equal(Units.Millimeters, NestDefaults.Load(DefaultsPath).Units); + } + + [Fact] + public void RunsOnce_AClearedSettingNeverConvertsAgain() + { + var settings = new Dictionary { ["NestTemplatePath"] = WriteTemplate() }; + NestTemplateMigration.Run(settings, DefaultsPath, store); + File.Delete(DefaultsPath); + + var again = NestTemplateMigration.Run(settings, DefaultsPath, store); + + Assert.False(again.ClearSetting); + Assert.False(File.Exists(DefaultsPath)); + } + + [Fact] + public void ExistingDefaults_AreKept_AndTheSettingIsCleared() + { + File.WriteAllText(DefaultsPath, "{ \"kept\": true }"); + + var result = NestTemplateMigration.Run(WriteTemplate(), DefaultsPath, store); + + Assert.True(result.ClearSetting); + Assert.Equal("{ \"kept\": true }", File.ReadAllText(DefaultsPath)); + } + + [Fact] + public void AMissingTemplate_IsCleared() + { + var result = NestTemplateMigration.Run(Path.Combine(directory, "gone.nstdot"), DefaultsPath, store); + + Assert.Equal(new NestTemplateMigrationResult(true, null!), result); + Assert.False(File.Exists(DefaultsPath)); + } + + [Fact] + public void AnUnreadableTemplate_WarnsAndKeepsTheSetting() + { + var path = Path.Combine(directory, "broken.nstdot"); + File.WriteAllText(path, "not a nest"); + var settings = new Dictionary { ["NestTemplatePath"] = path }; + + var result = NestTemplateMigration.Run(settings, DefaultsPath, store); + + Assert.False(result.ClearSetting); + Assert.Contains(path, result.Warning); + Assert.Contains("Tools > Nest Defaults", result.Warning); + Assert.Equal(path, settings["NestTemplatePath"]); + Assert.False(File.Exists(DefaultsPath)); + } +} diff --git a/OpenNest.Desktop/NestTemplateMigration.cs b/OpenNest.Desktop/NestTemplateMigration.cs new file mode 100644 index 00000000..764981fe --- /dev/null +++ b/OpenNest.Desktop/NestTemplateMigration.cs @@ -0,0 +1,60 @@ +using System; +using System.Collections.Generic; +using System.IO; +using OpenNest.BestFit; +using OpenNest.Data; +using OpenNest.IO; + +namespace OpenNest.Desktop; + +/// What did. +/// Clear the NestTemplatePath setting: the template was converted or +/// there was nothing to convert, so it is never consulted again. +/// Why a conversion failed, to show the user; null when it did not. +public sealed record NestTemplateMigrationResult(bool ClearSetting, string Warning); + +/// +/// One-time upgrade of the legacy .nstdot nest template (the NestTemplatePath setting) into the +/// nest defaults file. A template is converted only when its file exists and no defaults file +/// does yet; a failed conversion keeps the setting so the user can still find the file. +/// +public static class NestTemplateMigration +{ + public const string SettingName = "NestTemplatePath"; + + public static NestTemplateMigrationResult Run(string templatePath, string defaultsPath, IBestFitStore bestFitStore) + { + if (string.IsNullOrWhiteSpace(templatePath)) + return new NestTemplateMigrationResult(false, null); + + if (File.Exists(templatePath) && !File.Exists(defaultsPath)) + { + try + { + var nest = new NestReader(templatePath) { BestFitStore = bestFitStore }.Read(); + NestDefaults.FromNest(nest).Save(defaultsPath); + } + catch (Exception ex) + { + return new NestTemplateMigrationResult(false, + $"The nest template could not be converted to the new defaults file:\n{templatePath}\n\n{ex.Message}\n\nIt will no longer be loaded automatically. Set defaults under Tools > Nest Defaults."); + } + } + + return new NestTemplateMigrationResult(true, null); + } + + /// + /// Runs the migration on the template named in and clears the + /// setting (to the empty text WinForms stores) when it is done with. + /// + public static NestTemplateMigrationResult Run(IDictionary settings, string defaultsPath, IBestFitStore bestFitStore) + { + ArgumentNullException.ThrowIfNull(settings); + settings.TryGetValue(SettingName, out var templatePath); + var result = Run(templatePath, defaultsPath, bestFitStore); + if (result.ClearSetting) + settings[SettingName] = ""; + return result; + } +} diff --git a/OpenNest/Forms/MainWindowSettings.cs b/OpenNest/Forms/MainWindowSettings.cs index 530a2d48..0bd878e5 100644 --- a/OpenNest/Forms/MainWindowSettings.cs +++ b/OpenNest/Forms/MainWindowSettings.cs @@ -1,9 +1,8 @@ using System; -using System.IO; using System.Windows.Forms; using OpenNest.Data; +using OpenNest.Desktop; using OpenNest.Engine.BestFit; -using OpenNest.IO; using OpenNest.Properties; namespace OpenNest.Forms @@ -80,33 +79,22 @@ namespace OpenNest.Forms /// public static void MigrateNestTemplate() { - var templatePath = Settings.Default.NestTemplatePath; - if (string.IsNullOrWhiteSpace(templatePath)) - return; + var result = NestTemplateMigration.Run( + Settings.Default.NestTemplatePath, NestDefaults.DefaultPath, BestFitCacheStore.Shared); // A failed conversion keeps the setting populated so the user // can still find their template file. - var converted = true; - if (File.Exists(templatePath) && !File.Exists(NestDefaults.DefaultPath)) + if (result.Warning != null) { - try - { - var nest = new NestReader(templatePath) { BestFitStore = BestFitCacheStore.Shared }.Read(); - NestDefaults.FromNest(nest).Save(NestDefaults.DefaultPath); - } - catch (Exception ex) - { - converted = false; - MessageBox.Show( - $"The nest template could not be converted to the new defaults file:\n{templatePath}\n\n{ex.Message}\n\nIt will no longer be loaded automatically. Set defaults under Tools > Nest Defaults.", - "Nest Template", - MessageBoxButtons.OK, - MessageBoxIcon.Warning - ); - } + MessageBox.Show( + result.Warning, + "Nest Template", + MessageBoxButtons.OK, + MessageBoxIcon.Warning + ); } - if (!converted) + if (!result.ClearSetting) return; Settings.Default.NestTemplatePath = "";