1
1
// Licensed to the .NET Foundation under one or more agreements.
2
2
// The .NET Foundation licenses this file to you under the MIT license.
3
3
4
- using System . Text . Json ;
4
+ using Microsoft . Extensions . Configuration . Json ;
5
5
6
6
namespace Microsoft . Extensions . Configuration . DotnetCli . Providers ;
7
7
8
8
/// <summary>
9
9
/// Configuration provider that reads global.json files and maps keys to the canonical format.
10
10
/// </summary>
11
- public class GlobalJsonConfigurationProvider : ConfigurationProvider
11
+ public class GlobalJsonConfigurationProvider : JsonConfigurationProvider
12
12
{
13
- private readonly string ? _path ;
14
-
15
13
private static readonly Dictionary < string , string > GlobalJsonKeyMappings = new ( )
16
14
{
17
15
[ "sdk:version" ] = "sdk:version" ,
@@ -21,54 +19,26 @@ public class GlobalJsonConfigurationProvider : ConfigurationProvider
21
19
// Add more mappings as the global.json schema evolves
22
20
} ;
23
21
24
- public GlobalJsonConfigurationProvider ( string workingDirectory )
22
+ public GlobalJsonConfigurationProvider ( GlobalJsonConfigurationSource source ) : base ( source )
25
23
{
26
- _path = FindGlobalJson ( workingDirectory ) ;
27
24
}
28
25
29
26
public override void Load ( )
30
27
{
31
- Data . Clear ( ) ;
32
-
33
- if ( _path == null || ! File . Exists ( _path ) )
34
- return ;
28
+ base . Load ( ) ;
29
+ // Transform keys according to our mapping
30
+ var transformedData = new Dictionary < string , string ? > ( Data . Count , StringComparer . OrdinalIgnoreCase ) ;
35
31
36
- try
37
- {
38
- var json = File . ReadAllText ( _path ) ;
39
- var document = JsonDocument . Parse ( json ) ;
40
-
41
- LoadGlobalJsonData ( document . RootElement , "" ) ;
42
- }
43
- catch ( Exception ex )
32
+ foreach ( var kvp in Data )
44
33
{
45
- throw new InvalidOperationException ( $ "Error parsing global.json at { _path } ", ex ) ;
34
+ string key = kvp . Key ;
35
+ string ? value = kvp . Value ;
36
+ var mappedKey = MapGlobalJsonKey ( key ) ;
37
+ transformedData [ mappedKey ] = value ;
46
38
}
47
- }
48
39
49
- private void LoadGlobalJsonData ( JsonElement element , string prefix )
50
- {
51
- foreach ( var property in element . EnumerateObject ( ) )
52
- {
53
- var rawKey = string . IsNullOrEmpty ( prefix )
54
- ? property . Name
55
- : $ "{ prefix } :{ property . Name } ";
56
-
57
- switch ( property . Value . ValueKind )
58
- {
59
- case JsonValueKind . Object :
60
- LoadGlobalJsonData ( property . Value , rawKey ) ;
61
- break ;
62
- case JsonValueKind . String :
63
- case JsonValueKind . Number :
64
- case JsonValueKind . True :
65
- case JsonValueKind . False :
66
- // Map to canonical key format
67
- var canonicalKey = MapGlobalJsonKey ( rawKey ) ;
68
- Data [ canonicalKey ] = GetValueAsString ( property . Value ) ;
69
- break ;
70
- }
71
- }
40
+ // Replace the data with transformed keys
41
+ Data = transformedData ;
72
42
}
73
43
74
44
private string MapGlobalJsonKey ( string rawKey )
@@ -84,29 +54,4 @@ private string MapGlobalJsonKey(string rawKey)
84
54
// Default: convert to lowercase and normalize separators
85
55
return rawKey . ToLowerInvariant ( ) . Replace ( "-" , ":" ) ;
86
56
}
87
-
88
- private string GetValueAsString ( JsonElement element )
89
- {
90
- return element . ValueKind switch
91
- {
92
- JsonValueKind . String => element . GetString ( ) ! ,
93
- JsonValueKind . Number => element . GetRawText ( ) ,
94
- JsonValueKind . True => "true" ,
95
- JsonValueKind . False => "false" ,
96
- _ => element . GetRawText ( )
97
- } ;
98
- }
99
-
100
- private string ? FindGlobalJson ( string startDirectory )
101
- {
102
- var current = new DirectoryInfo ( startDirectory ) ;
103
- while ( current != null )
104
- {
105
- var globalJsonPath = Path . Combine ( current . FullName , "global.json" ) ;
106
- if ( File . Exists ( globalJsonPath ) )
107
- return globalJsonPath ;
108
- current = current . Parent ;
109
- }
110
- return null ;
111
- }
112
57
}
0 commit comments