-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
SystemVariables.cs
executable file
·148 lines (118 loc) · 4.39 KB
/
SystemVariables.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
namespace Money_CLI;
#pragma warning disable CS8602
using System;
using System.Text.Json;
using System.Text.Json.Serialization;
using Money_CLI.Controllers;
using Serilog;
public static class SystemVariables
{
/// <summary>
/// The path to the executable's directory.
/// </summary>
private static string _AppDomain => AppDomain.CurrentDomain.BaseDirectory;
/// <summary>
/// The full path to the System Variables file.
/// </summary>
private static string SystemVariablesFileName => @$"{_AppDomain}SystemVariables.json";
/// <summary>
/// The string containing System Variables in JSON format.
/// </summary>
private static string SystemVariablesString => File.ReadAllText(SystemVariablesFileName);
/// <summary>
/// The System Variables as Dictionary object.
/// </summary>
private static Dictionary<string, string>? SystemVariablesJSON => JsonSerializer.Deserialize<Dictionary<string, string>>(SystemVariablesString);
/// <summary>
/// Handles the path for the folder used to export data.
/// </summary>
public static string ExportFolder {
get {
try {
return SystemVariablesJSON["EXPORT_FOLDER"];
} catch (Exception) {
return string.Empty;
}
}
set {
try {
Dictionary<string, string>? _data = SystemVariablesJSON;
_data["EXPORT_FOLDER"] = GenericController.EnsureDirectory(value);
string json = JsonSerializer.Serialize(_data);
File.WriteAllText(SystemVariablesFileName, json);
Log.Information("Export folder has successfully been set.");
} catch (Exception) {
Log.Error("Could not set export folder.");
}
}
}
/// <summary>
/// Handles the path for the folder used to host the database.
/// </summary>
public static string DatabaseFolder {
get {
try {
return SystemVariablesJSON["DATABASE_FOLDER"];
} catch (Exception) {
return string.Empty;
}
}
set {
try {
Dictionary<string, string>? _data = SystemVariablesJSON;
string oldValue = _data["DATABASE_FOLDER"];
_data["DATABASE_FOLDER"] = GenericController.EnsureDirectory(value);
string json = JsonSerializer.Serialize(_data);
File.WriteAllText(SystemVariablesFileName, json);
Log.Information("Database folder has successfully been set.");
} catch (Exception) {
Log.Error("Could not set database folder.");
}
}
}
/// <summary>
/// Handles the currency that is returned on export.
/// </summary>
public static string Currency {
get {
try {
return SystemVariablesJSON["CURRENCY"];
} catch (Exception) {
return string.Empty;
}
}
set {
try {
Dictionary<string, string>? _data = SystemVariablesJSON;
string oldValue = _data["CURRENCY"];
_data["CURRENCY"] = value;
string json = JsonSerializer.Serialize(_data);
File.WriteAllText(SystemVariablesFileName, json);
Log.Information("Currency has successfully been set.");
} catch (Exception) {
Log.Error("Could not set currency.");
}
}
}
/// <summary>
/// Ensures that the file for the system variables exists.
/// </summary>
public static bool EnsureCreated() {
if (File.Exists(SystemVariablesFileName))
return true;
try {
File.Create(SystemVariablesFileName).Close();
Dictionary<string, string> _data = new Dictionary<string, string>();
_data.Add("EXPORT_FOLDER", _AppDomain);
_data.Add("DATABASE_FOLDER", _AppDomain);
_data.Add("CURRENCY", "en-US");
string json = JsonSerializer.Serialize(_data);
File.WriteAllText(SystemVariablesFileName, json);
Log.Information("System variables file has successfully been created.");
return true;
} catch (Exception) {
Log.Error("Could not create system variables file.");
return false;
}
}
}