/// <summary> /// Save the config to file /// </summary> public static async Task SaveToFileAsync( Path configFile, LocalUserConfig config) { // Ensure the parent folder exists var folder = configFile.GetParent(); if (!LifetimeManager.Get <IFileSystem>().Exists(folder)) { Log.Info($"Creating directory {folder}"); LifetimeManager.Get <IFileSystem>().CreateDirectory2(folder); } // Open the file to write to var file = LifetimeManager.Get <IFileSystem>().OpenWrite(configFile, true); // Serialize the contents of the recipe var documentSyntax = config.MirrorSyntax; if (documentSyntax == null) { throw new ArgumentException("The provided config does not have a mirrored syntax tree.", nameof(config)); } // Write the recipe to the file stream await ValueTableTomlUtilities.SerializeAsync(documentSyntax, file.GetOutStream()); }
/// <summary> /// Attempt to load from file /// </summary> public static async Task <(bool IsSuccess, Recipe Result)> TryLoadRecipeFromFileAsync(Path recipeFile) { // Verify the requested file exists Log.Diag("Load Recipe: " + recipeFile.ToString()); if (!System.IO.File.Exists(recipeFile.ToString())) { Log.Info("Recipe file does not exist."); return(false, new Recipe()); } // Open the file to read from using (var fileStream = System.IO.File.OpenRead(recipeFile.ToString())) using (var reader = new System.IO.StreamReader(fileStream)) { // Read the contents of the recipe file try { var result = ValueTableTomlUtilities.Deserialize( recipeFile, await reader.ReadToEndAsync()); return(true, new Recipe(result)); } catch (Exception ex) { Log.Error($"Deserialize Threw: {ex.Message}"); Log.Info("Failed to parse Recipe."); return(false, new Recipe()); } } }
/// <summary> /// Attempt to load from file /// </summary> public static async Task <(bool IsSuccess, LocalUserConfig Result)> TryLoadLocalUserConfigFromFileAsync( Path localUserConfigFile) { // Verify the requested file exists Log.Diag("Load Local User Config: " + localUserConfigFile.ToString()); if (!LifetimeManager.Get <IFileSystem>().Exists(localUserConfigFile)) { Log.Warning("Local User Config file does not exist."); return(false, new LocalUserConfig()); } // Open the file to read from var file = LifetimeManager.Get <IFileSystem>().OpenRead(localUserConfigFile); // Open the file to read from using (var reader = new System.IO.StreamReader(file.GetInStream())) { // Read the contents of the local user config file try { var result = ValueTableTomlUtilities.Deserialize( localUserConfigFile, await reader.ReadToEndAsync()); return(true, new LocalUserConfig(result)); } catch (Exception ex) { Log.Error("Deserialize Threw: " + ex.Message); Log.Info("Failed to parse local user config."); return(false, new LocalUserConfig()); } } }
/// <summary> /// Save the recipe to file /// </summary> public static async Task SaveToFileAsync( Path recipeFile, Recipe recipe) { // Open the file to write to var file = LifetimeManager.Get <IFileSystem>().OpenWrite(recipeFile, true); // Serialize the contents of the recipe var documentSyntax = recipe.MirrorSyntax; if (documentSyntax == null) { throw new ArgumentException("The provided recipe does not have a mirrored syntax tree.", nameof(recipe)); } // Write the recipe to the file stream await ValueTableTomlUtilities.SerializeAsync(documentSyntax, file.GetOutStream()); }