Moved QuickFilters migration to AppScaffolding.
This commit is contained in:
parent
f92b2b65b2
commit
4801f37e7c
@ -2,7 +2,7 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net8.0</TargetFramework>
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
<Version>11.4.1.1</Version>
|
<Version>11.5</Version>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Octokit" Version="11.0.1" />
|
<PackageReference Include="Octokit" Version="11.0.1" />
|
||||||
|
|||||||
@ -1,16 +1,16 @@
|
|||||||
using System;
|
using ApplicationServices;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.IO;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Reflection;
|
|
||||||
using ApplicationServices;
|
|
||||||
using AudibleUtilities;
|
using AudibleUtilities;
|
||||||
using Dinah.Core;
|
|
||||||
using Dinah.Core.IO;
|
using Dinah.Core.IO;
|
||||||
using Dinah.Core.Logging;
|
using Dinah.Core.Logging;
|
||||||
using LibationFileManager;
|
using LibationFileManager;
|
||||||
|
using Newtonsoft.Json;
|
||||||
using Newtonsoft.Json.Linq;
|
using Newtonsoft.Json.Linq;
|
||||||
using Serilog;
|
using Serilog;
|
||||||
|
using System;
|
||||||
|
using System.Diagnostics;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Reflection;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
|
|
||||||
namespace AppScaffolding
|
namespace AppScaffolding
|
||||||
@ -87,6 +87,7 @@ namespace AppScaffolding
|
|||||||
//
|
//
|
||||||
|
|
||||||
Migrations.migrate_to_v6_6_9(config);
|
Migrations.migrate_to_v6_6_9(config);
|
||||||
|
Migrations.migrate_to_v11_5_0(config);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Initialize logging. Wire-up events. Run after migration</summary>
|
/// <summary>Initialize logging. Wire-up events. Run after migration</summary>
|
||||||
@ -404,5 +405,53 @@ namespace AppScaffolding
|
|||||||
UNSAFE_MigrationHelper.Settings_AddUniqueToArray("Serilog.Enrich", "WithExceptionDetails");
|
UNSAFE_MigrationHelper.Settings_AddUniqueToArray("Serilog.Enrich", "WithExceptionDetails");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void migrate_to_v11_5_0(Configuration config)
|
||||||
|
{
|
||||||
|
var writeToPath = "Serilog.WriteTo";
|
||||||
|
|
||||||
|
//QuickFilters.FilterState? inMemoryState;
|
||||||
|
|
||||||
|
// Read file, but convert old format to new (with Name field) as necessary.
|
||||||
|
if (!File.Exists(QuickFilters.JsonFile))
|
||||||
|
{
|
||||||
|
QuickFilters.InMemoryState = new();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (JsonConvert.DeserializeObject<QuickFilters.FilterState>(File.ReadAllText(QuickFilters.JsonFile))
|
||||||
|
is QuickFilters.FilterState inMemState)
|
||||||
|
{
|
||||||
|
QuickFilters.InMemoryState = inMemState;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
// Eat
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (JsonConvert.DeserializeObject<QuickFilters.OldFilterState>(File.ReadAllText(QuickFilters.JsonFile))
|
||||||
|
is QuickFilters.OldFilterState inMemState)
|
||||||
|
{
|
||||||
|
// Copy old structure to new.
|
||||||
|
QuickFilters.InMemoryState = new();
|
||||||
|
QuickFilters.InMemoryState.UseDefault = inMemState.UseDefault;
|
||||||
|
foreach (var oldFilter in inMemState.Filters)
|
||||||
|
QuickFilters.InMemoryState.Filters.Add(new QuickFilters.NamedFilter(oldFilter, null));
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Debug.Assert(false, "Should not get here, QuickFilters.json deserialization issue");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
// Eat
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,7 +2,6 @@
|
|||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
|
||||||
@ -11,56 +10,6 @@ namespace LibationFileManager
|
|||||||
{
|
{
|
||||||
public static class QuickFilters
|
public static class QuickFilters
|
||||||
{
|
{
|
||||||
static QuickFilters()
|
|
||||||
{
|
|
||||||
// Read file, but convert old format to new (with Name field) as necessary.
|
|
||||||
if (!File.Exists(JsonFile))
|
|
||||||
{
|
|
||||||
inMemoryState = new();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
if (JsonConvert.DeserializeObject<FilterState>(File.ReadAllText(JsonFile))
|
|
||||||
is FilterState inMemState)
|
|
||||||
{
|
|
||||||
inMemoryState = inMemState;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
Serilog.Log.Logger.Information("QuickFilters.json needs upgrade");
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
if (JsonConvert.DeserializeObject<OldFilterState>(File.ReadAllText(JsonFile))
|
|
||||||
is OldFilterState inMemState)
|
|
||||||
{
|
|
||||||
Serilog.Log.Logger.Error("Old format detected, upgrading QuickFilters.json");
|
|
||||||
|
|
||||||
// Copy old structure to new.
|
|
||||||
inMemoryState = new();
|
|
||||||
inMemoryState.UseDefault = inMemState.UseDefault;
|
|
||||||
foreach (var oldFilter in inMemState.Filters)
|
|
||||||
inMemoryState.Filters.Add(new NamedFilter(oldFilter, null));
|
|
||||||
|
|
||||||
Serilog.Log.Logger.Error($"QuickFilters.json upgraded, {inMemState.Filters?.Count ?? 0} filter(s) converted");
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
Debug.Assert(false, "Should not get here, QuickFilters.json deserialization issue");
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
Serilog.Log.Logger.Error(ex, "QuickFilters.json could not be upgraded, recreating");
|
|
||||||
}
|
|
||||||
|
|
||||||
inMemoryState = new FilterState();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static event EventHandler? Updated;
|
public static event EventHandler? Updated;
|
||||||
|
|
||||||
public static event EventHandler? UseDefaultChanged;
|
public static event EventHandler? UseDefaultChanged;
|
||||||
@ -81,11 +30,11 @@ namespace LibationFileManager
|
|||||||
|
|
||||||
|
|
||||||
// load json into memory. if file doesn't exist, nothing to do. save() will create if needed
|
// load json into memory. if file doesn't exist, nothing to do. save() will create if needed
|
||||||
static FilterState inMemoryState { get; }
|
public static FilterState InMemoryState { get; set; }
|
||||||
|
|
||||||
public static bool UseDefault
|
public static bool UseDefault
|
||||||
{
|
{
|
||||||
get => inMemoryState.UseDefault;
|
get => InMemoryState.UseDefault;
|
||||||
set
|
set
|
||||||
{
|
{
|
||||||
if (UseDefault == value)
|
if (UseDefault == value)
|
||||||
@ -93,7 +42,7 @@ namespace LibationFileManager
|
|||||||
|
|
||||||
lock (locker)
|
lock (locker)
|
||||||
{
|
{
|
||||||
inMemoryState.UseDefault = value;
|
InMemoryState.UseDefault = value;
|
||||||
save(false);
|
save(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -109,7 +58,7 @@ namespace LibationFileManager
|
|||||||
public string Name { get; set; } = Name;
|
public string Name { get; set; } = Name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static IEnumerable<NamedFilter> Filters => inMemoryState.Filters.AsReadOnly();
|
public static IEnumerable<NamedFilter> Filters => InMemoryState.Filters.AsReadOnly();
|
||||||
|
|
||||||
public static void Add(NamedFilter namedFilter)
|
public static void Add(NamedFilter namedFilter)
|
||||||
{
|
{
|
||||||
@ -121,10 +70,10 @@ namespace LibationFileManager
|
|||||||
lock (locker)
|
lock (locker)
|
||||||
{
|
{
|
||||||
// check for duplicates
|
// check for duplicates
|
||||||
if (inMemoryState.Filters.Select(x => x.Filter).ContainsInsensative(namedFilter.Filter))
|
if (InMemoryState.Filters.Select(x => x.Filter).ContainsInsensative(namedFilter.Filter))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
inMemoryState.Filters.Add(namedFilter);
|
InMemoryState.Filters.Add(namedFilter);
|
||||||
save();
|
save();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -133,7 +82,7 @@ namespace LibationFileManager
|
|||||||
{
|
{
|
||||||
lock (locker)
|
lock (locker)
|
||||||
{
|
{
|
||||||
inMemoryState.Filters.Remove(filter);
|
InMemoryState.Filters.Remove(filter);
|
||||||
save();
|
save();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -142,11 +91,11 @@ namespace LibationFileManager
|
|||||||
{
|
{
|
||||||
lock (locker)
|
lock (locker)
|
||||||
{
|
{
|
||||||
var index = inMemoryState.Filters.IndexOf(oldFilter);
|
var index = InMemoryState.Filters.IndexOf(oldFilter);
|
||||||
if (index < 0)
|
if (index < 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
inMemoryState.Filters = inMemoryState.Filters.Select(f => f == oldFilter ? newFilter : f).ToList();
|
InMemoryState.Filters = InMemoryState.Filters.Select(f => f == oldFilter ? newFilter : f).ToList();
|
||||||
|
|
||||||
save();
|
save();
|
||||||
}
|
}
|
||||||
@ -161,7 +110,7 @@ namespace LibationFileManager
|
|||||||
filter.Filter = filter.Filter.Trim();
|
filter.Filter = filter.Filter.Trim();
|
||||||
lock (locker)
|
lock (locker)
|
||||||
{
|
{
|
||||||
inMemoryState.Filters = new List<NamedFilter>(filters);
|
InMemoryState.Filters = new List<NamedFilter>(filters);
|
||||||
save();
|
save();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -172,7 +121,7 @@ namespace LibationFileManager
|
|||||||
private static void save(bool invokeUpdatedEvent = true)
|
private static void save(bool invokeUpdatedEvent = true)
|
||||||
{
|
{
|
||||||
// create json if not exists
|
// create json if not exists
|
||||||
void resave() => File.WriteAllText(JsonFile, JsonConvert.SerializeObject(inMemoryState, Formatting.Indented));
|
void resave() => File.WriteAllText(JsonFile, JsonConvert.SerializeObject(InMemoryState, Formatting.Indented));
|
||||||
try { resave(); }
|
try { resave(); }
|
||||||
catch (IOException)
|
catch (IOException)
|
||||||
{
|
{
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user