SettingsScope.cs 4.5 KB

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