Update UNSAFE_MigrationHelper with new appsettings.json getter

This commit is contained in:
Mbucari 2023-02-13 09:03:03 -07:00
parent 500b287721
commit f0ca349539
3 changed files with 35 additions and 20 deletions

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using Dinah.Core; using Dinah.Core;
using LibationFileManager;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
@ -25,9 +26,6 @@ namespace AppScaffolding
: value; : value;
#region appsettings.json #region appsettings.json
private static string APPSETTINGS_JSON { get; } = Path.Combine(Path.GetDirectoryName(Environment.ProcessPath), "appsettings.json");
public static bool APPSETTINGS_Json_Exists => File.Exists(APPSETTINGS_JSON);
public static bool APPSETTINGS_TryGet(string key, out string value) public static bool APPSETTINGS_TryGet(string key, out string value)
{ {
@ -61,11 +59,7 @@ namespace AppScaffolding
/// <param name="save">True: save if contents changed. False: no not attempt save</param> /// <param name="save">True: save if contents changed. False: no not attempt save</param>
private static void process_APPSETTINGS_Json(Action<JObject> action, bool save = true) private static void process_APPSETTINGS_Json(Action<JObject> action, bool save = true)
{ {
// only insert if not exists var startingContents = File.ReadAllText(Configuration.AppsettingsJsonFile);
if (!APPSETTINGS_Json_Exists)
return;
var startingContents = File.ReadAllText(APPSETTINGS_JSON);
JObject jObj; JObject jObj;
try try
@ -88,7 +82,7 @@ namespace AppScaffolding
if (startingContents.EqualsInsensitive(endingContents_indented) || startingContents.EqualsInsensitive(endingContents_compact)) if (startingContents.EqualsInsensitive(endingContents_indented) || startingContents.EqualsInsensitive(endingContents_compact))
return; return;
File.WriteAllText(APPSETTINGS_JSON, endingContents_indented); File.WriteAllText(Configuration.AppsettingsJsonFile, endingContents_indented);
System.Threading.Thread.Sleep(100); System.Threading.Thread.Sleep(100);
} }
#endregion #endregion

View File

@ -10,7 +10,7 @@ namespace LibationFileManager
{ {
public partial class Configuration public partial class Configuration
{ {
private static string APPSETTINGS_JSON { get; } = getAppsettingsFile(); public static string AppsettingsJsonFile { get; } = getOrCreateAppsettingsFile();
private const string LIBATION_FILES_KEY = "LibationFiles"; private const string LIBATION_FILES_KEY = "LibationFiles";
@ -45,7 +45,30 @@ namespace LibationFileManager
private static string libationFilesPathCache { get; set; } private static string libationFilesPathCache { get; set; }
private static string getAppsettingsFile() /// <summary>
/// Try to find appsettings.json in the following locations:
/// <list type="number">
/// <item>
/// <description>[App Directory]</description>
/// </item>
/// <item>
/// <description>%LocalAppData%\Libation</description>
/// </item>
/// <item>
/// <description>%AppData%\Libation</description>
/// </item>
/// <item>
/// <description>%Temp%\Libation</description>
/// </item>
/// </list>
///
/// If not found, try to create it in each of the same locations in-order until successful.
///
/// <para>This method must complete successfully for Libation to continue.</para>
/// </summary>
/// <returns>appsettings.json file path</returns>
/// <exception cref="ApplicationException">appsettings.json could not be found or created.</exception>
private static string getOrCreateAppsettingsFile()
{ {
const string appsettings_filename = "appsettings.json"; const string appsettings_filename = "appsettings.json";
@ -99,7 +122,7 @@ namespace LibationFileManager
{ {
// do not check whether directory exists. special/meta directory (eg: AppDir) is valid // do not check whether directory exists. special/meta directory (eg: AppDir) is valid
// verify from live file. no try/catch. want failures to be visible // verify from live file. no try/catch. want failures to be visible
var jObjFinal = JObject.Parse(File.ReadAllText(APPSETTINGS_JSON)); var jObjFinal = JObject.Parse(File.ReadAllText(AppsettingsJsonFile));
var valueFinal = jObjFinal[LIBATION_FILES_KEY].Value<string>(); var valueFinal = jObjFinal[LIBATION_FILES_KEY].Value<string>();
return valueFinal; return valueFinal;
} }
@ -108,7 +131,7 @@ namespace LibationFileManager
{ {
libationFilesPathCache = null; libationFilesPathCache = null;
var startingContents = File.ReadAllText(APPSETTINGS_JSON); var startingContents = File.ReadAllText(AppsettingsJsonFile);
var jObj = JObject.Parse(startingContents); var jObj = JObject.Parse(startingContents);
jObj[LIBATION_FILES_KEY] = directory; jObj[LIBATION_FILES_KEY] = directory;
@ -120,13 +143,13 @@ namespace LibationFileManager
try try
{ {
// now it's set in the file again but no settings have moved yet // now it's set in the file again but no settings have moved yet
File.WriteAllText(APPSETTINGS_JSON, endingContents); File.WriteAllText(AppsettingsJsonFile, endingContents);
tryLog(() => Log.Logger.Information("Libation files changed {@DebugInfo}", new { APPSETTINGS_JSON, LIBATION_FILES_KEY, directory })); tryLog(() => Log.Logger.Information("Libation files changed {@DebugInfo}", new { AppsettingsJsonFile, LIBATION_FILES_KEY, directory }));
} }
catch (IOException ex) catch (IOException ex)
{ {
tryLog(() => Log.Logger.Error(ex, "Failed to change Libation files location {@DebugInfo}", new { APPSETTINGS_JSON, LIBATION_FILES_KEY, directory })); tryLog(() => Log.Logger.Error(ex, "Failed to change Libation files location {@DebugInfo}", new { AppsettingsJsonFile, LIBATION_FILES_KEY, directory }));
} }
static void tryLog(Action logAction) static void tryLog(Action logAction)

View File

@ -9,9 +9,7 @@ namespace LibationFileManager
{ {
public partial class Configuration : PropertyChangeFilter public partial class Configuration : PropertyChangeFilter
{ {
public bool LibationSettingsAreValid public bool LibationSettingsAreValid => SettingsFileIsValid(SettingsFilePath);
=> File.Exists(APPSETTINGS_JSON)
&& SettingsFileIsValid(SettingsFilePath);
public static bool SettingsFileIsValid(string settingsFile) public static bool SettingsFileIsValid(string settingsFile)
{ {