55using YamlDotNet . Serialization ;
66using YamlDotNet . Serialization . NamingConventions ;
77
8- namespace $safeprojectname $;
9-
10- public class ConfigurationService : IConfigurationService
8+ namespace $safeprojectname $
119{
12- private ConfigurationService ( ) { }
13-
14- private static readonly Lazy < IConfigurationService > Lazy = new ( ( ) => new ConfigurationService ( ) ) ;
15- public static IConfigurationService Service => Lazy . Value ;
16- public YamlContainer < T > Get < T > ( string inputFileName = "" ) where T : new ( )
10+ public class ConfigurationService : IConfigurationService
1711 {
18- var fileName = string . IsNullOrEmpty ( inputFileName ) ? $ "{ typeof ( T ) . Name } .yaml". GetSafePathRegardlessHowApplicationStarted ( ) : inputFileName . GetSafePathRegardlessHowApplicationStarted ( ) ;
19- var yamlContent = File . ReadAllText ( fileName ) ;
12+ private ConfigurationService ( ) { }
2013
21- var deserializer = new DeserializerBuilder ( )
22- . WithNamingConvention ( CamelCaseNamingConvention . Instance )
23- . Build ( ) ;
24- try
14+ private static readonly Lazy < IConfigurationService > Lazy = new ( ( ) => new ConfigurationService ( ) ) ;
15+ public static IConfigurationService Service => Lazy . Value ;
16+ public YamlContainer < T > Get < T > ( string inputFileName = "" ) where T : new ( )
2517 {
26- return deserializer . Deserialize < YamlContainer < T > > ( yamlContent ) ;
18+ var fileName = string . IsNullOrEmpty ( inputFileName ) ? $ "{ typeof ( T ) . Name } .yaml". GetSafePathRegardlessHowApplicationStarted ( ) : inputFileName . GetSafePathRegardlessHowApplicationStarted ( ) ;
19+ var yamlContent = File . ReadAllText ( fileName ) ;
20+
21+ var deserializer = new DeserializerBuilder ( )
22+ . WithNamingConvention ( CamelCaseNamingConvention . Instance )
23+ . Build ( ) ;
24+ try
25+ {
26+ return deserializer . Deserialize < YamlContainer < T > > ( yamlContent ) ;
27+ }
28+ catch ( Exception )
29+ {
30+ Console . WriteLine ( $ "Could not deserialize the configuration file, default configuration will be loaded instead\n A template configuration file named default_{ typeof ( T ) . Name } .yaml will be created in application root.") ;
31+ var defaultConfig = new T ( ) ;
32+ SaveChanges ( defaultConfig , $ "default_{ typeof ( T ) . Name } .yaml") ;
33+ return new YamlContainer < T > ( ) ;
34+ }
2735 }
28- catch ( Exception )
36+ public YamlContainer < T > GetByNodeName < T > ( string filePath , string nodeName ) where T : new ( )
2937 {
30- Console . WriteLine ( $ "Could not deserialize the configuration file, default configuration will be loaded instead\n A template configuration file named default_{ typeof ( T ) . Name } .yaml will be created in application root.") ;
31- var defaultConfig = new T ( ) ;
32- SaveChanges ( defaultConfig , $ "default_{ typeof ( T ) . Name } .yaml") ;
33- return new YamlContainer < T > ( ) ;
34- }
35- }
36- public YamlContainer < T > GetByNodeName < T > ( string filePath , string nodeName ) where T : new ( )
37- {
38- var stringBuilder = new StringBuilder ( ) ;
39- stringBuilder . AppendLine ( "version: 1.0\r \n configuration:" ) ;
40- var nodeFound = false ;
41- var nodeIndentation = 0 ;
38+ var stringBuilder = new StringBuilder ( ) ;
39+ stringBuilder . AppendLine ( "version: 1.0\r \n configuration:" ) ;
40+ var nodeFound = false ;
41+ var nodeIndentation = 0 ;
4242
43- using ( var reader = new StreamReader ( filePath ) )
44- {
45- while ( reader . ReadLine ( ) is { } line )
43+ using ( var reader = new StreamReader ( filePath ) )
4644 {
47- if ( line . Trim ( ) . StartsWith ( nodeName + ":" ) )
45+ while ( reader . ReadLine ( ) is { } line )
4846 {
49- nodeFound = true ;
50- nodeIndentation = line . TakeWhile ( Char . IsWhiteSpace ) . Count ( ) ;
47+ if ( line . Trim ( ) . StartsWith ( nodeName + ":" ) )
48+ {
49+ nodeFound = true ;
50+ nodeIndentation = line . TakeWhile ( Char . IsWhiteSpace ) . Count ( ) ;
51+ stringBuilder . AppendLine ( line ) ;
52+ continue ;
53+ }
54+ if ( ! nodeFound ) continue ;
55+ var currentIndentation = line . TakeWhile ( Char . IsWhiteSpace ) . Count ( ) ;
56+ if ( string . IsNullOrWhiteSpace ( line ) || currentIndentation <= nodeIndentation ) break ;
5157 stringBuilder . AppendLine ( line ) ;
52- continue ;
5358 }
54- if ( ! nodeFound ) continue ;
55- var currentIndentation = line . TakeWhile ( Char . IsWhiteSpace ) . Count ( ) ;
56- if ( string . IsNullOrWhiteSpace ( line ) || currentIndentation <= nodeIndentation ) break ;
57- stringBuilder . AppendLine ( line ) ;
5859 }
59- }
6060
61- var yamlContent = stringBuilder . ToString ( ) ;
62- var deserializer = new DeserializerBuilder ( )
63- . WithNamingConvention ( CamelCaseNamingConvention . Instance )
64- . Build ( ) ;
65- try
66- {
67- return deserializer . Deserialize < YamlContainer < T > > ( yamlContent ) ;
61+ var yamlContent = stringBuilder . ToString ( ) ;
62+ var deserializer = new DeserializerBuilder ( )
63+ . WithNamingConvention ( CamelCaseNamingConvention . Instance )
64+ . Build ( ) ;
65+ try
66+ {
67+ return deserializer . Deserialize < YamlContainer < T > > ( yamlContent ) ;
68+ }
69+ catch ( Exception )
70+ {
71+ Console . WriteLine ( $ "Could not deserialize the configuration file, default configuration will be loaded instead\n A template configuration file named default_{ typeof ( T ) . Name } .yaml will be created in application root.") ;
72+ return new YamlContainer < T > ( ) ;
73+ }
74+
6875 }
69- catch ( Exception )
76+ public string SaveChanges < T > ( T configuration , string inputFileName = "" ) where T : new ( )
7077 {
71- Console . WriteLine ( $ "Could not deserialize the configuration file, default configuration will be loaded instead\n A template configuration file named default_{ typeof ( T ) . Name } .yaml will be created in application root.") ;
72- return new YamlContainer < T > ( ) ;
73- }
74-
75- }
76- public string SaveChanges < T > ( T configuration , string inputFileName = "" ) where T : new ( )
77- {
78- if ( configuration is null ) return "" ;
79- var fileName = string . IsNullOrEmpty ( inputFileName ) ? $ "{ configuration . GetType ( ) . Name } .yaml". GetSafePathRegardlessHowApplicationStarted ( ) : inputFileName . GetSafePathRegardlessHowApplicationStarted ( ) ;
80-
81- var yamlContainer = new YamlContainer < T > { Configuration = configuration , Version = "1.0" } ;
82- var serializer = new SerializerBuilder ( )
83- . WithNamingConvention ( CamelCaseNamingConvention . Instance )
84- . Build ( ) ;
85- var yamlData = serializer . Serialize ( yamlContainer ) ;
86- File . WriteAllText ( fileName , yamlData ) ;
87- return fileName ;
88- }
89- public void Create < T > ( T configuration , string fullFileName ) where T : new ( )
90- {
91- if ( configuration is null ) return ;
92- var yamlContainer = new YamlContainer < T > { Configuration = configuration , Version = "1.0" } ;
93- var serializer = new SerializerBuilder ( )
94- . WithNamingConvention ( CamelCaseNamingConvention . Instance )
95- . Build ( ) ;
96- var yamlData = serializer . Serialize ( yamlContainer ) ;
97- File . WriteAllText ( fullFileName , yamlData ) ;
98- }
78+ if ( configuration is null ) return "" ;
79+ var fileName = string . IsNullOrEmpty ( inputFileName ) ? $ "{ configuration . GetType ( ) . Name } .yaml". GetSafePathRegardlessHowApplicationStarted ( ) : inputFileName . GetSafePathRegardlessHowApplicationStarted ( ) ;
9980
100- /// <summary>
101- /// Return a configuration file stored in the AppData/Roaming/PowerCommands directory, if the file does not exist it will be created.
102- /// </summary>
103- /// <typeparam name="T"></typeparam>
104- /// <param name="defaultIfMissing"></param>
105- /// <param name="inputFileName"></param>
106- /// <returns></returns>
107- public YamlContainer < T > GetAppDataConfiguration < T > ( string inputFileName = "" ) where T : new ( )
108- {
109- var directory = $ "{ Environment . GetFolderPath ( Environment . SpecialFolder . ApplicationData ) } \\ { nameof ( PowerCommands ) } ";
110- var fileName = Path . Combine ( directory , inputFileName ) ;
111- if ( ! File . Exists ( fileName ) ) return new YamlContainer < T > ( ) ;
112- var yamlContent = File . ReadAllText ( fileName ) ;
113- var deserializer = new DeserializerBuilder ( )
114- . WithNamingConvention ( CamelCaseNamingConvention . Instance )
115- . Build ( ) ;
116- return deserializer . Deserialize < YamlContainer < T > > ( yamlContent ) ;
117- }
118- private string CreateContent < T > ( T item ) where T : new ( )
119- {
120- if ( item is not null )
81+ var yamlContainer = new YamlContainer < T > { Configuration = configuration , Version = "1.0" } ;
82+ var serializer = new SerializerBuilder ( )
83+ . WithNamingConvention ( CamelCaseNamingConvention . Instance )
84+ . Build ( ) ;
85+ var yamlData = serializer . Serialize ( yamlContainer ) ;
86+ File . WriteAllText ( fileName , yamlData ) ;
87+ return fileName ;
88+ }
89+ public void Create < T > ( T configuration , string fullFileName ) where T : new ( )
12190 {
122- var yamlContainer = new YamlContainer < T > { Configuration = item , Version = "1.0" } ;
91+ if ( configuration is null ) return ;
92+ var yamlContainer = new YamlContainer < T > { Configuration = configuration , Version = "1.0" } ;
12393 var serializer = new SerializerBuilder ( )
12494 . WithNamingConvention ( CamelCaseNamingConvention . Instance )
12595 . Build ( ) ;
12696 var yamlData = serializer . Serialize ( yamlContainer ) ;
127- return yamlData ;
97+ File . WriteAllText ( fullFileName , yamlData ) ;
98+ }
99+
100+ /// <summary>
101+ /// Return a configuration file stored in the AppData/Roaming/PowerCommands directory, if the file does not exist it will be created.
102+ /// </summary>
103+ /// <typeparam name="T"></typeparam>
104+ /// <param name="defaultIfMissing"></param>
105+ /// <param name="inputFileName"></param>
106+ /// <returns></returns>
107+ public YamlContainer < T > GetAppDataConfiguration < T > ( string inputFileName = "" ) where T : new ( )
108+ {
109+ var directory = $ "{ Environment . GetFolderPath ( Environment . SpecialFolder . ApplicationData ) } \\ { nameof ( PowerCommands ) } ";
110+ var fileName = Path . Combine ( directory , inputFileName ) ;
111+ if ( ! File . Exists ( fileName ) ) return new YamlContainer < T > ( ) ;
112+ var yamlContent = File . ReadAllText ( fileName ) ;
113+ var deserializer = new DeserializerBuilder ( )
114+ . WithNamingConvention ( CamelCaseNamingConvention . Instance )
115+ . Build ( ) ;
116+ return deserializer . Deserialize < YamlContainer < T > > ( yamlContent ) ;
117+ }
118+ private string CreateContent < T > ( T item ) where T : new ( )
119+ {
120+ if ( item is not null )
121+ {
122+ var yamlContainer = new YamlContainer < T > { Configuration = item , Version = "1.0" } ;
123+ var serializer = new SerializerBuilder ( )
124+ . WithNamingConvention ( CamelCaseNamingConvention . Instance )
125+ . Build ( ) ;
126+ var yamlData = serializer . Serialize ( yamlContainer ) ;
127+ return yamlData ;
128+ }
129+ return "--- item is null and can not be serialized ---" ;
128130 }
129- return "--- item is null and can not be serialized ---" ;
130131 }
131132}
0 commit comments