11// Licensed to the .NET Foundation under one or more agreements.
22// The .NET Foundation licenses this file to you under the MIT license.
33
4- using System . Text . Json ;
4+ using Microsoft . Extensions . Configuration . Json ;
55
66namespace Microsoft . Extensions . Configuration . DotnetCli . Providers ;
77
88/// <summary>
99/// Configuration provider that reads global.json files and maps keys to the canonical format.
1010/// </summary>
11- public class GlobalJsonConfigurationProvider : ConfigurationProvider
11+ public class GlobalJsonConfigurationProvider : JsonConfigurationProvider
1212{
13- private readonly string ? _path ;
14-
1513 private static readonly Dictionary < string , string > GlobalJsonKeyMappings = new ( )
1614 {
1715 [ "sdk:version" ] = "sdk:version" ,
@@ -21,54 +19,26 @@ public class GlobalJsonConfigurationProvider : ConfigurationProvider
2119 // Add more mappings as the global.json schema evolves
2220 } ;
2321
24- public GlobalJsonConfigurationProvider ( string workingDirectory )
22+ public GlobalJsonConfigurationProvider ( GlobalJsonConfigurationSource source ) : base ( source )
2523 {
26- _path = FindGlobalJson ( workingDirectory ) ;
2724 }
2825
2926 public override void Load ( )
3027 {
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 ) ;
3531
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 )
4433 {
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 ;
4638 }
47- }
4839
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 ;
7242 }
7343
7444 private string MapGlobalJsonKey ( string rawKey )
@@ -84,29 +54,4 @@ private string MapGlobalJsonKey(string rawKey)
8454 // Default: convert to lowercase and normalize separators
8555 return rawKey . ToLowerInvariant ( ) . Replace ( "-" , ":" ) ;
8656 }
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- }
11257}
0 commit comments