SettingsScope.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. 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. /// <summary>
  34. /// The list of paths to the configuration files.
  35. /// </summary>
  36. public List<string> Sources = new List<string> ();
  37. /// <summary>
  38. /// Updates the <see cref="SettingsScope"/> with the settings in a JSON string.
  39. /// </summary>
  40. /// <param name="stream">Json document to update the settings with.</param>
  41. /// <param name="source">The source (filename/resource name) the Json document was read from.</param>
  42. public SettingsScope? Update (Stream stream, string source)
  43. {
  44. // Update the existing settings with the new settings.
  45. try {
  46. Update (JsonSerializer.Deserialize<SettingsScope> (stream, serializerOptions)!);
  47. OnUpdated ();
  48. Debug.WriteLine ($"ConfigurationManager: Read configuration from \"{source}\"");
  49. Sources.Add (source);
  50. return this;
  51. } catch (JsonException e) {
  52. if (ThrowOnJsonErrors ?? false) {
  53. throw;
  54. } else {
  55. AddJsonError ($"Error deserializing {source}: {e.Message}");
  56. }
  57. }
  58. return this;
  59. }
  60. /// <summary>
  61. /// Updates the <see cref="SettingsScope"/> with the settings in a JSON file.
  62. /// </summary>
  63. /// <param name="filePath"></param>
  64. public SettingsScope? Update (string filePath)
  65. {
  66. var realPath = filePath.Replace("~", Environment.GetFolderPath (Environment.SpecialFolder.UserProfile));
  67. if (!File.Exists (realPath)) {
  68. Debug.WriteLine ($"ConfigurationManager: Configuration file \"{realPath}\" does not exist.");
  69. Sources.Add (filePath);
  70. return this;
  71. }
  72. var stream = File.OpenRead (realPath);
  73. return Update (stream, filePath);
  74. }
  75. /// <summary>
  76. /// Updates the <see cref="SettingsScope"/> with the settings from a Json resource.
  77. /// </summary>
  78. /// <param name="assembly"></param>
  79. /// <param name="resourceName"></param>
  80. public SettingsScope? UpdateFromResource (Assembly assembly, string resourceName)
  81. {
  82. if (resourceName == null || string.IsNullOrEmpty (resourceName)) {
  83. Debug.WriteLine ($"ConfigurationManager: Resource \"{resourceName}\" does not exist in \"{assembly.GetName ().Name}\".");
  84. return this;
  85. }
  86. using Stream? stream = assembly.GetManifestResourceStream (resourceName)!;
  87. if (stream == null) {
  88. Debug.WriteLine ($"ConfigurationManager: Failed to read resource \"{resourceName}\" from \"{assembly.GetName ().Name}\".");
  89. return this;
  90. }
  91. return Update (stream, $"resource://[{assembly.GetName().Name}]/{resourceName}");
  92. }
  93. /// <summary>
  94. /// Updates the <see cref="SettingsScope"/> with the settings in a JSON string.
  95. /// </summary>
  96. /// <param name="json">Json document to update the settings with.</param>
  97. /// <param name="source">The source (filename/resource name) the Json document was read from.</param>
  98. public SettingsScope? Update (string json, string source)
  99. {
  100. var stream = new MemoryStream ();
  101. var writer = new StreamWriter (stream);
  102. writer.Write (json);
  103. writer.Flush ();
  104. stream.Position = 0;
  105. return Update (stream, source);
  106. }
  107. }
  108. }
  109. }