refactor(desktop): share the one-time nest template migration

NestTemplateMigration converts the legacy NestTemplatePath template into
the nest defaults file when the template exists and no defaults file does,
and says whether to clear the setting (converted or nothing to convert)
or the warning to show when the conversion failed, which keeps the
setting. A settings-map overload clears it to the empty text WinForms
stores, so it runs once. WinForms' MainWindowSettings now calls it and
still shows the same warning.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
ajandClaude Opus 5.5 committed 2026-11-11 21:13:53 -05:00
1 parent 725e90dada
commit cb5e2a5854
3 files changed
+171 -23

No files matched your search

@@ -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<string, string> { ["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<string, string> { ["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<string, string> { ["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));
}
}
+60
View File
@@ -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;
/// <summary>What <see cref="NestTemplateMigration"/> did.</summary>
/// <param name="ClearSetting">Clear the NestTemplatePath setting: the template was converted or
/// there was nothing to convert, so it is never consulted again.</param>
/// <param name="Warning">Why a conversion failed, to show the user; null when it did not.</param>
public sealed record NestTemplateMigrationResult(bool ClearSetting, string Warning);
/// <summary>
/// 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.
/// </summary>
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);
}
/// <summary>
/// Runs the migration on the template named in <paramref name="settings"/> and clears the
/// setting (to the empty text WinForms stores) when it is done with.
/// </summary>
public static NestTemplateMigrationResult Run(IDictionary<string, string> 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;
}
}
+11 -23
View File
@@ -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
/// </summary>
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 = "";