Skip to content

Commit 9b79ec0

Browse files
authored
Make the generated files more friendly to code cleanup functionality of Visual Studio (#753)
* Add #nullable enable and a header to generated files to exclude it from code clean up. More info: https://github.com/dotnet/roslyn/blob/70e158ba6c2c99bd3c3fc0754af0dbf82a6d353d/docs/features/nullable-reference-types.md#generated-code * Added note about update_all_dependencies.ps1 and fixed version. Co-authored-by: Ali Sanjabi <[email protected]>
1 parent 84ff98b commit 9b79ec0

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

src/HassModel/NetDaemon.HassModel.CodeGenerator/Generator.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
using System.Runtime.CompilerServices;
1+
using Microsoft.CodeAnalysis.CSharp;
2+
23
using NetDaemon.Client.HomeAssistant.Model;
4+
5+
using System.Runtime.CompilerServices;
6+
37
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
48

59
[assembly: InternalsVisibleTo("NetDaemon.Daemon.Tests")]
@@ -9,7 +13,7 @@ namespace NetDaemon.HassModel.CodeGenerator;
913
internal 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());
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
3+
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
4+
5+
namespace NetDaemon.HassModel.CodeGenerator.Helpers;
6+
internal static class TriviaHelper
7+
{
8+
internal static string GeneratorVersion = Assembly.GetAssembly(typeof(Generator))!.GetName().Version!.ToString();
9+
internal static SyntaxTrivia[] GetFileHeader()
10+
{
11+
string headerText = @$"
12+
//------------------------------------------------------------------------------
13+
// <auto-generated>
14+
// Generated using NetDaemon CodeGenerator nd-codegen v{GeneratorVersion}
15+
//
16+
// *** Make sure the version of the codegen tool and your nugets Joysoftware.NetDaemon.* have the same version.***
17+
// You can use following command to keep it up to date with the latest version:
18+
// dotnet tool update JoySoftware.NetDaemon.HassModel.CodeGen
19+
//
20+
// To update this file with latest entities run this command in your project directory:
21+
// dotnet tool run nd-codegen
22+
//
23+
// In the template projects we provided a convenience powershell script that will update
24+
// the codegen and nugets to latest versions update_all_dependencies.ps1.
25+
//
26+
// For more information: https://netdaemon.xyz/docs/v3/hass_model/hass_model_codegen
27+
// For more information about NetDaemon: https://netdaemon.xyz/
28+
// </auto-generated>
29+
//------------------------------------------------------------------------------";
30+
31+
var lines = headerText.Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
32+
33+
SyntaxTrivia[] header = lines.SelectMany(l => new[] { Comment(l), LineFeed }).ToArray();
34+
return header;
35+
}
36+
}

0 commit comments

Comments
 (0)