@@ -27,7 +27,7 @@ namespace Terminal.Gui;
27
27
public class SettingsScope : Scope < SettingsScope >
28
28
{
29
29
/// <summary>The list of paths to the configuration files.</summary>
30
- public List < string > Sources = new ( ) ;
30
+ public Dictionary < ConfigLocations , string > Sources { get ; } = new ( ) ;
31
31
32
32
/// <summary>Points to our JSON schema.</summary>
33
33
[ JsonInclude ]
@@ -37,19 +37,20 @@ public class SettingsScope : Scope<SettingsScope>
37
37
/// <summary>Updates the <see cref="SettingsScope"/> with the settings in a JSON string.</summary>
38
38
/// <param name="stream">Json document to update the settings with.</param>
39
39
/// <param name="source">The source (filename/resource name) the Json document was read from.</param>
40
+ /// <param name="location">Location</param>
40
41
[ RequiresUnreferencedCode ( "AOT" ) ]
41
42
[ RequiresDynamicCode ( "AOT" ) ]
42
- public SettingsScope ? Update ( Stream stream , string source )
43
+ public SettingsScope ? Update ( Stream stream , string source , ConfigLocations location )
43
44
{
44
45
// Update the existing settings with the new settings.
45
46
try
46
47
{
47
48
Update ( ( SettingsScope ) JsonSerializer . Deserialize ( stream , typeof ( SettingsScope ) , _serializerOptions ) ! ) ;
48
49
OnUpdated ( ) ;
49
50
Debug . WriteLine ( $ "ConfigurationManager: Read configuration from \" { source } \" ") ;
50
- if ( ! Sources . Contains ( source ) )
51
+ if ( ! Sources . ContainsValue ( source ) )
51
52
{
52
- Sources . Add ( source ) ;
53
+ Sources . Add ( location , source ) ;
53
54
}
54
55
55
56
return this ;
@@ -68,19 +69,20 @@ public class SettingsScope : Scope<SettingsScope>
68
69
}
69
70
70
71
/// <summary>Updates the <see cref="SettingsScope"/> with the settings in a JSON file.</summary>
71
- /// <param name="filePath"></param>
72
+ /// <param name="filePath">Path to the file.</param>
73
+ /// <param name="location">The location</param>
72
74
[ RequiresUnreferencedCode ( "AOT" ) ]
73
75
[ RequiresDynamicCode ( "AOT" ) ]
74
- public SettingsScope ? Update ( string filePath )
76
+ public SettingsScope ? Update ( string filePath , ConfigLocations location )
75
77
{
76
78
string realPath = filePath . Replace ( "~" , Environment . GetFolderPath ( Environment . SpecialFolder . UserProfile ) ) ;
77
79
78
80
if ( ! File . Exists ( realPath ) )
79
81
{
80
82
Debug . WriteLine ( $ "ConfigurationManager: Configuration file \" { realPath } \" does not exist.") ;
81
- if ( ! Sources . Contains ( filePath ) )
83
+ if ( ! Sources . ContainsValue ( filePath ) )
82
84
{
83
- Sources . Add ( filePath ) ;
85
+ Sources . Add ( location , filePath ) ;
84
86
}
85
87
86
88
return this ;
@@ -95,15 +97,15 @@ public class SettingsScope : Scope<SettingsScope>
95
97
try
96
98
{
97
99
FileStream ? stream = File . OpenRead ( realPath ) ;
98
- SettingsScope ? s = Update ( stream , filePath ) ;
100
+ SettingsScope ? s = Update ( stream , filePath , location ) ;
99
101
stream . Close ( ) ;
100
102
stream . Dispose ( ) ;
101
103
102
104
return s ;
103
105
}
104
106
catch ( IOException ioe )
105
107
{
106
- Debug . WriteLine ( $ "Couldn't open { filePath } . Retrying...: { ioe } ") ;
108
+ Debug . WriteLine ( $ "Couldn't open { filePath } . Retrying...: { ioe } ") ;
107
109
Task . Delay ( 100 ) ;
108
110
retryCount ++ ;
109
111
}
@@ -115,32 +117,33 @@ public class SettingsScope : Scope<SettingsScope>
115
117
/// <summary>Updates the <see cref="SettingsScope"/> with the settings in a JSON string.</summary>
116
118
/// <param name="json">Json document to update the settings with.</param>
117
119
/// <param name="source">The source (filename/resource name) the Json document was read from.</param>
120
+ /// <param name="location">The location.</param>
118
121
[ RequiresUnreferencedCode ( "AOT" ) ]
119
122
[ RequiresDynamicCode ( "AOT" ) ]
120
- public SettingsScope ? Update ( string ? json , string source )
123
+ public SettingsScope ? Update ( string ? json , string source , ConfigLocations location )
121
124
{
122
- //if (string.IsNullOrEmpty (json))
123
- //{
124
- // Debug.WriteLine ($"ConfigurationManager: Configuration file \"{source}\" is empty.");
125
- // return this;
126
- //}
125
+ if ( string . IsNullOrEmpty ( json ) )
126
+ {
127
+ return null ;
128
+ }
127
129
var stream = new MemoryStream ( ) ;
128
130
var writer = new StreamWriter ( stream ) ;
129
131
writer . Write ( json ) ;
130
132
writer . Flush ( ) ;
131
133
stream . Position = 0 ;
132
134
133
- return Update ( stream , source ) ;
135
+ return Update ( stream , source , location ) ;
134
136
}
135
137
136
138
/// <summary>Updates the <see cref="SettingsScope"/> with the settings from a Json resource.</summary>
137
139
/// <param name="assembly"></param>
138
140
/// <param name="resourceName"></param>
141
+ /// <param name="location"></param>
139
142
[ RequiresUnreferencedCode ( "AOT" ) ]
140
143
[ RequiresDynamicCode ( "AOT" ) ]
141
- public SettingsScope ? UpdateFromResource ( Assembly assembly , string resourceName )
144
+ public SettingsScope ? UpdateFromResource ( Assembly assembly , string resourceName , ConfigLocations location )
142
145
{
143
- if ( resourceName is null || string . IsNullOrEmpty ( resourceName ) )
146
+ if ( string . IsNullOrEmpty ( resourceName ) )
144
147
{
145
148
Debug . WriteLine (
146
149
$ "ConfigurationManager: Resource \" { resourceName } \" does not exist in \" { assembly . GetName ( ) . Name } \" ."
@@ -149,20 +152,13 @@ public class SettingsScope : Scope<SettingsScope>
149
152
return this ;
150
153
}
151
154
152
- // BUG: Not trim-compatible
153
- // Not a bug, per se, but it's easily fixable by just loading the file.
154
- // Defaults can just be field initializers for involved types.
155
- using Stream ? stream = assembly . GetManifestResourceStream ( resourceName ) ! ;
155
+ using Stream ? stream = assembly . GetManifestResourceStream ( resourceName ) ;
156
156
157
157
if ( stream is null )
158
158
{
159
- Debug . WriteLine (
160
- $ "ConfigurationManager: Failed to read resource \" { resourceName } \" from \" { assembly . GetName ( ) . Name } \" ."
161
- ) ;
162
-
163
- return this ;
159
+ return null ;
164
160
}
165
161
166
- return Update ( stream , $ "resource://[{ assembly . GetName ( ) . Name } ]/{ resourceName } ") ;
162
+ return Update ( stream , $ "resource://[{ assembly . GetName ( ) . Name } ]/{ resourceName } ", location ) ;
167
163
}
168
164
}
0 commit comments