SettingsScope.cs 4.3 KB

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