1- using System . Runtime . CompilerServices ;
1+ using Microsoft . CodeAnalysis . CSharp ;
2+
23using NetDaemon . Client . HomeAssistant . Model ;
4+
5+ using System . Runtime . CompilerServices ;
6+
37using static Microsoft . CodeAnalysis . CSharp . SyntaxFactory ;
48
59[ assembly: InternalsVisibleTo ( "NetDaemon.Daemon.Tests" ) ]
@@ -9,7 +13,7 @@ namespace NetDaemon.HassModel.CodeGenerator;
913internal static class Generator
1014{
1115 public static string GenerateCode ( CodeGenerationSettings codeGenerationSettings , IReadOnlyCollection < HassState > entities , IReadOnlyCollection < HassServiceDomain > services )
12- {
16+ {
1317 var code = CreateCompilationUnitSyntax ( codeGenerationSettings , entities , services ) ;
1418 return code . ToFullString ( ) ;
1519 }
@@ -35,9 +39,13 @@ internal static IEnumerable<CompilationUnitSyntax> CreateCompilationUnitSyntaxPe
3539 {
3640 units . Add ( CompilationUnit ( )
3741 . AddUsings ( UsingDirective ( ParseName ( "System" ) ) )
42+ . WithLeadingTrivia ( TriviaHelper . GetFileHeader ( ) )
3843 . AddUsings ( UsingDirective ( ParseName ( "System.Collections.Generic" ) ) )
3944 . AddUsings ( UsingNamespaces . OrderBy ( s => s ) . Select ( u => UsingDirective ( ParseName ( u ) ) ) . ToArray ( ) )
40- . AddMembers ( NamespaceDeclaration ( ParseName ( codeGenerationSettings . Namespace ) ) . NormalizeWhitespace ( ) . AddMembers ( x ) )
45+ . AddMembers ( NamespaceDeclaration ( ParseName ( codeGenerationSettings . Namespace ) )
46+ . AppendTrivia ( Trivia ( NullableDirectiveTrivia ( Token ( SyntaxKind . EnableKeyword ) , true ) ) )
47+ . NormalizeWhitespace ( )
48+ . AddMembers ( x ) )
4149 . NormalizeWhitespace ( Tab . ToString ( ) , eol : "\n " ) ) ;
4250 } ) ;
4351
@@ -51,11 +59,13 @@ internal static CompilationUnitSyntax CreateCompilationUnitSyntax(CodeGeneration
5159
5260 var code = CompilationUnit ( )
5361 . AddUsings ( UsingDirective ( ParseName ( "System" ) ) )
62+ . WithLeadingTrivia ( TriviaHelper . GetFileHeader ( ) )
5463 . AddUsings ( UsingDirective ( ParseName ( "System.Collections.Generic" ) ) )
5564 . AddUsings ( UsingNamespaces . OrderBy ( s => s ) . Select ( u => UsingDirective ( ParseName ( u ) ) ) . ToArray ( ) ) ;
5665
5766 var namespaceDeclaration = NamespaceDeclaration ( ParseName ( codeGenerationSettings . Namespace ) ) . NormalizeWhitespace ( ) ;
5867
68+ namespaceDeclaration = namespaceDeclaration . AppendTrivia ( Trivia ( NullableDirectiveTrivia ( Token ( SyntaxKind . EnableKeyword ) , true ) ) ) ;
5969 namespaceDeclaration = namespaceDeclaration . AddMembers ( EntitiesGenerator . Generate ( codeGenerationSettings , orderedEntities ) . ToArray ( ) ) ;
6070 namespaceDeclaration = namespaceDeclaration . AddMembers ( ServicesGenerator . Generate ( orderedServiceDomains ) . ToArray ( ) ) ;
6171 namespaceDeclaration = namespaceDeclaration . AddMembers ( ExtensionMethodsGenerator . Generate ( orderedServiceDomains , entities ) . ToArray ( ) ) ;
0 commit comments