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