From f0ca349539abb4b304740b365d69202690b8e412 Mon Sep 17 00:00:00 2001 From: Mbucari Date: Mon, 13 Feb 2023 09:03:03 -0700 Subject: [PATCH] Update UNSAFE_MigrationHelper with new appsettings.json getter --- .../AppScaffolding/UNSAFE_MigrationHelper.cs | 12 ++---- .../Configuration.LibationFiles.cs | 39 +++++++++++++++---- Source/LibationFileManager/Configuration.cs | 4 +- 3 files changed, 35 insertions(+), 20 deletions(-) diff --git a/Source/AppScaffolding/UNSAFE_MigrationHelper.cs b/Source/AppScaffolding/UNSAFE_MigrationHelper.cs index 4a63c1ff..63327778 100644 --- a/Source/AppScaffolding/UNSAFE_MigrationHelper.cs +++ b/Source/AppScaffolding/UNSAFE_MigrationHelper.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; using Dinah.Core; +using LibationFileManager; using Newtonsoft.Json; using Newtonsoft.Json.Linq; @@ -25,9 +26,6 @@ namespace AppScaffolding : value; #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) { @@ -61,11 +59,7 @@ namespace AppScaffolding /// True: save if contents changed. False: no not attempt save private static void process_APPSETTINGS_Json(Action action, bool save = true) { - // only insert if not exists - if (!APPSETTINGS_Json_Exists) - return; - - var startingContents = File.ReadAllText(APPSETTINGS_JSON); + var startingContents = File.ReadAllText(Configuration.AppsettingsJsonFile); JObject jObj; try @@ -88,7 +82,7 @@ namespace AppScaffolding if (startingContents.EqualsInsensitive(endingContents_indented) || startingContents.EqualsInsensitive(endingContents_compact)) return; - File.WriteAllText(APPSETTINGS_JSON, endingContents_indented); + File.WriteAllText(Configuration.AppsettingsJsonFile, endingContents_indented); System.Threading.Thread.Sleep(100); } #endregion diff --git a/Source/LibationFileManager/Configuration.LibationFiles.cs b/Source/LibationFileManager/Configuration.LibationFiles.cs index a30d209a..5a1443c2 100644 --- a/Source/LibationFileManager/Configuration.LibationFiles.cs +++ b/Source/LibationFileManager/Configuration.LibationFiles.cs @@ -10,7 +10,7 @@ namespace LibationFileManager { public partial class Configuration { - private static string APPSETTINGS_JSON { get; } = getAppsettingsFile(); + public static string AppsettingsJsonFile { get; } = getOrCreateAppsettingsFile(); private const string LIBATION_FILES_KEY = "LibationFiles"; @@ -19,7 +19,7 @@ namespace LibationFileManager { get { - if (libationFilesPathCache is not null) + if (libationFilesPathCache is not null) return libationFilesPathCache; // FIRST: must write here before SettingsFilePath in next step reads cache @@ -45,7 +45,30 @@ namespace LibationFileManager private static string libationFilesPathCache { get; set; } - private static string getAppsettingsFile() + /// + /// Try to find appsettings.json in the following locations: + /// + /// + /// [App Directory] + /// + /// + /// %LocalAppData%\Libation + /// + /// + /// %AppData%\Libation + /// + /// + /// %Temp%\Libation + /// + /// + /// + /// If not found, try to create it in each of the same locations in-order until successful. + /// + /// This method must complete successfully for Libation to continue. + /// + /// appsettings.json file path + /// appsettings.json could not be found or created. + private static string getOrCreateAppsettingsFile() { 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 // 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(); return valueFinal; } @@ -108,7 +131,7 @@ namespace LibationFileManager { libationFilesPathCache = null; - var startingContents = File.ReadAllText(APPSETTINGS_JSON); + var startingContents = File.ReadAllText(AppsettingsJsonFile); var jObj = JObject.Parse(startingContents); jObj[LIBATION_FILES_KEY] = directory; @@ -120,13 +143,13 @@ namespace LibationFileManager try { // 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) { - 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) diff --git a/Source/LibationFileManager/Configuration.cs b/Source/LibationFileManager/Configuration.cs index 4747331c..3c71abe9 100644 --- a/Source/LibationFileManager/Configuration.cs +++ b/Source/LibationFileManager/Configuration.cs @@ -9,9 +9,7 @@ namespace LibationFileManager { public partial class Configuration : PropertyChangeFilter { - public bool LibationSettingsAreValid - => File.Exists(APPSETTINGS_JSON) - && SettingsFileIsValid(SettingsFilePath); + public bool LibationSettingsAreValid => SettingsFileIsValid(SettingsFilePath); public static bool SettingsFileIsValid(string settingsFile) {