SettingsScope.cs 5.0 KB

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