SettingsScope.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Reflection;
  6. using System.Text.Json;
  7. using System.Text.Json.Serialization;
  8. #nullable enable
  9. namespace Terminal.Gui.Configuration {
  10. public static partial class ConfigurationManager {
  11. /// <summary>
  12. /// The root object of Terminal.Gui configuration settings / JSON schema. Contains only properties
  13. /// attributed with <see cref="SettingsScope"/>.
  14. /// </summary>
  15. /// <example><code>
  16. /// {
  17. /// "$schema" : "https://gui-cs.github.io/Terminal.Gui/schemas/tui-config-schema.json",
  18. /// "Application.UseSystemConsole" : true,
  19. /// "Theme" : "Default",
  20. /// "Themes": {
  21. /// },
  22. /// },
  23. /// </code></example>
  24. /// <remarks>
  25. /// </remarks>
  26. [JsonConverter (typeof (ScopeJsonConverter<SettingsScope>))]
  27. public class SettingsScope : Scope<SettingsScope> {
  28. /// <summary>
  29. /// Points to our JSON schema.
  30. /// </summary>
  31. [JsonInclude, JsonPropertyName ("$schema")]
  32. public string Schema { get; set; } = "https://gui-cs.github.io/Terminal.Gui/schemas/tui-config-schema.json";
  33. public List<string> Sources = new List<string> ();
  34. /// <summary>
  35. /// Updates the <see cref="SettingsScope"/> with the settings in a JSON string.
  36. /// </summary>
  37. /// <param name="stream">Json document to update the settings with.</param>
  38. /// <param name="source">The source (filename/resource name) the Json document was read from.</param>
  39. public SettingsScope? Update (Stream stream, string source)
  40. {
  41. // Update the existing settings with the new settings.
  42. try {
  43. Update (JsonSerializer.Deserialize<SettingsScope> (stream, serializerOptions)!);
  44. OnUpdated ();
  45. Debug.WriteLine ($"ConfigurationManager: Read configuration from \"{source}\"");
  46. Sources.Add (source);
  47. return this;
  48. } catch (JsonException e) {
  49. if (ThrowOnJsonErrors ?? false) {
  50. throw;
  51. } else {
  52. AddJsonError ($"Error deserializing {source}: {e.Message}");
  53. }
  54. }
  55. return this;
  56. }
  57. /// <summary>
  58. /// Updates the <see cref="SettingsScope"/> with the settings in a JSON file.
  59. /// </summary>
  60. /// <param name="filePath"></param>
  61. public SettingsScope? Update (string filePath)
  62. {
  63. var realPath = filePath.Replace("~", Environment.GetFolderPath (Environment.SpecialFolder.UserProfile));
  64. if (!File.Exists (realPath)) {
  65. Debug.WriteLine ($"ConfigurationManager: Configuration file \"{realPath}\" does not exist.");
  66. Sources.Add (filePath);
  67. return this;
  68. }
  69. var stream = File.OpenRead (realPath);
  70. return Update (stream, filePath);
  71. }
  72. /// <summary>
  73. /// Updates the <see cref="SettingsScope"/> with the settings from a Json resource.
  74. /// </summary>
  75. /// <param name="assembly"></param>
  76. /// <param name="resourceName"></param>
  77. public SettingsScope? UpdateFromResource (Assembly assembly, string resourceName)
  78. {
  79. if (resourceName == null || string.IsNullOrEmpty (resourceName)) {
  80. Debug.WriteLine ($"ConfigurationManager: Resource \"{resourceName}\" does not exist in \"{assembly.GetName ().Name}\".");
  81. return this;
  82. }
  83. using Stream? stream = assembly.GetManifestResourceStream (resourceName)!;
  84. if (stream == null) {
  85. Debug.WriteLine ($"ConfigurationManager: Failed to read resource \"{resourceName}\" from \"{assembly.GetName ().Name}\".");
  86. return this;
  87. }
  88. return Update (stream, $"resource://[{assembly.GetName().Name}]/{resourceName}");
  89. }
  90. /// <summary>
  91. /// Updates the <see cref="SettingsScope"/> with the settings in a JSON string.
  92. /// </summary>
  93. /// <param name="json">Json document to update the settings with.</param>
  94. /// <param name="source">The source (filename/resource name) the Json document was read from.</param>
  95. public SettingsScope? Update (string json, string source)
  96. {
  97. var stream = new MemoryStream ();
  98. var writer = new StreamWriter (stream);
  99. writer.Write (json);
  100. writer.Flush ();
  101. stream.Position = 0;
  102. return Update (stream, source);
  103. }
  104. }
  105. }
  106. }