• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

jamsoft / JamSoft.Helpers / 15162929158

21 May 2025 01:03PM UTC coverage: 96.696%. Remained the same
15162929158

push

github

jamsoft
Turned off signing for GH build

292 of 312 branches covered (93.59%)

Branch coverage included in aggregate %.

820 of 838 relevant lines covered (97.85%)

27595.08 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.12
/JamSoft.Helpers/Configuration/SettingsBase.cs
1
using System;
2
using System.Globalization;
3
using System.IO;
4
using System.Text.Json;
5

6
namespace JamSoft.Helpers.Configuration
7
{
8
    /// <summary>
9
    /// Provides loading and saving of JSON settings and configuration data at runtime
10
    /// </summary>
11
    /// <typeparam name="T"></typeparam>
12
    public abstract class SettingsBase<T> where T : SettingsBase<T>, new()
13
    {
14
        // ReSharper disable once StaticMemberInGenericType
15
        private static string? _filePath;
16

17
        // ReSharper disable once StaticMemberInGenericType
18
        private static string _basePath = null!;
2✔
19

20
        /// <summary>
21
        /// The class instance containing the values
22
        /// </summary>
23
        public static T Instance { get; private set; } = new();
62✔
24

25
        private static string GetLocalFilePath(string fileName)
26
        {
20✔
27
            return Path.Combine(_basePath, fileName);
20✔
28
        }
20✔
29

30
        /// <summary>
31
        /// Loads the settings file for the class type found at the provided base path. If no file exists, the defaults are loaded
32
        /// </summary>
33
        /// <param name="basePath">The base settings directory</param>
34
        /// <param name="filename">The settings filename (including extension) (optional: the type name will be used if this is null, empty or white-space. Creates a file with .json extension)</param>
35
        /// <exception cref="ArgumentNullException"></exception>
36
        public static void Load(string basePath, string? filename = null)
37
        {
22✔
38
            if (string.IsNullOrWhiteSpace(basePath)) throw new ArgumentNullException(nameof(basePath));
24✔
39
            
40
            _basePath = basePath;
20✔
41
            _filePath = GetLocalFilePath((string.IsNullOrWhiteSpace(filename) ? $"{typeof(T).Name.ToLower(CultureInfo.CurrentCulture)}.json" : filename)!);
20✔
42
            Instance = (File.Exists(_filePath) ? JsonSerializer.Deserialize<T>(File.ReadAllText(_filePath)) : new T())!;
20✔
43
        }
20✔
44

45
        /// <summary>
46
        /// Returns all properties to their defaults and optionally saves to disk
47
        /// </summary>
48
        /// <param name="saveToDisk">A flag to optionally save to disk (default: True)</param>
49
        /// <exception cref="InvalidOperationException" />
50
        public static void ResetToDefaults(bool saveToDisk = true)
51
        {
2✔
52
            Instance = new T();
2✔
53

54
            if (saveToDisk)
2✔
55
            {
1✔
56
                Save();
1✔
57
            }
1✔
58
        }
2✔
59

60
        /// <summary>
61
        /// Save the current setting to the file they were originally loaded from
62
        /// </summary>
63
        /// <exception cref="InvalidOperationException" />
64
        public static void Save()
65
        {
10✔
66
            if (string.IsNullOrWhiteSpace(_filePath))
10!
67
                throw new InvalidOperationException("No file specified, ensure you have previously called the Load method");
×
68
            
69
            string json = JsonSerializer.Serialize(Instance);
10✔
70
            var settingsPath = Path.GetDirectoryName(_filePath);
10✔
71
            if (settingsPath != null)
10✔
72
            {
10✔
73
                Directory.CreateDirectory(settingsPath);
10✔
74
                File.WriteAllText(_filePath, json);
10✔
75
            }
10✔
76
        }
10✔
77
    }
78
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc