-
-
Notifications
You must be signed in to change notification settings - Fork 80
Refactor code generator and fix issue with services with multiple target domains #819
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
FrankBakkerNl
merged 1 commit into
net-daemon:dev
from
FrankBakkerNl:Refactor_fix_multiTargetDomains
Dec 23, 2022
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 43 additions & 8 deletions
51
src/HassModel/NetDaemon.HassModel.CodeGenerator/CodeGeneration/Generator.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,33 +1,68 @@ | ||
| using Microsoft.CodeAnalysis.CSharp; | ||
| using System.Reflection; | ||
| using Microsoft.CodeAnalysis.CSharp; | ||
| using NetDaemon.HassModel.CodeGenerator.CodeGeneration; | ||
| using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; | ||
|
|
||
| namespace NetDaemon.HassModel.CodeGenerator; | ||
|
|
||
| internal static class Generator | ||
| { | ||
| public static IEnumerable<MemberDeclarationSyntax> GenerateTypes( | ||
| IReadOnlyCollection<EntityDomainMetadata> domains, | ||
| /// <summary> | ||
| /// Generates all Types from Entity and Services metadata | ||
| /// </summary> | ||
| public static MemberDeclarationSyntax[] GenerateTypes( | ||
| IReadOnlyCollection<EntityDomainMetadata> entityDomains, | ||
| IReadOnlyCollection<HassServiceDomain> services) | ||
| { | ||
| var orderedServiceDomains = services.OrderBy(x => x.Domain).ToArray(); | ||
|
|
||
| var helpers = HelpersGenerator.Generate(domains, orderedServiceDomains); | ||
| var entityClasses = EntitiesGenerator.Generate(domains); | ||
| var helpers = HelpersGenerator.Generate(entityDomains, orderedServiceDomains); | ||
| var entityClasses = EntitiesGenerator.Generate(entityDomains); | ||
| var serviceClasses = ServicesGenerator.Generate(orderedServiceDomains); | ||
| var extensionMethodClasses = ExtensionMethodsGenerator.Generate(orderedServiceDomains, domains); | ||
| var extensionMethodClasses = ExtensionMethodsGenerator.Generate(orderedServiceDomains, entityDomains); | ||
|
|
||
| return new[] {helpers, entityClasses, serviceClasses, extensionMethodClasses }.SelectMany(x => x).ToArray(); | ||
| return new[] { helpers, entityClasses, serviceClasses, extensionMethodClasses }.SelectMany(x => x).ToArray(); | ||
| } | ||
|
|
||
| public static CompilationUnitSyntax BuildCompilationUnit(string namespaceName, params MemberDeclarationSyntax[] generatedTypes) | ||
| { | ||
| return CompilationUnit() | ||
| .AddUsings(UsingNamespaces.Select(u => UsingDirective(ParseName(u))).ToArray()) | ||
| .WithLeadingTrivia(TriviaHelper.GetFileHeader() | ||
| .WithLeadingTrivia(GetFileHeader() | ||
| .Append(Trivia(NullableDirectiveTrivia(Token(SyntaxKind.EnableKeyword), true)))) | ||
| .AddMembers(FileScopedNamespaceDeclaration(ParseName(namespaceName))) | ||
| .AddMembers(generatedTypes) | ||
| .NormalizeWhitespace(); | ||
| } | ||
|
|
||
| private static readonly string GeneratorVersion = Assembly.GetAssembly(typeof(Generator))!.GetName().Version!.ToString(); | ||
|
|
||
| private static SyntaxTrivia[] GetFileHeader() | ||
| { | ||
| string headerText = @$" | ||
| //------------------------------------------------------------------------------ | ||
| // <auto-generated> | ||
| // Generated using NetDaemon CodeGenerator nd-codegen v{GeneratorVersion} | ||
| // At: {DateTime.Now:O} | ||
| // | ||
| // *** Make sure the version of the codegen tool and your nugets Joysoftware.NetDaemon.* have the same version.*** | ||
| // You can use following command to keep it up to date with the latest version: | ||
| // dotnet tool update JoySoftware.NetDaemon.HassModel.CodeGen | ||
| // | ||
| // To update this file with latest entities run this command in your project directory: | ||
| // dotnet tool run nd-codegen | ||
| // | ||
| // In the template projects we provided a convenience powershell script that will update | ||
| // the codegen and nugets to latest versions update_all_dependencies.ps1. | ||
| // | ||
| // For more information: https://netdaemon.xyz/docs/v3/hass_model/hass_model_codegen | ||
| // For more information about NetDaemon: https://netdaemon.xyz/ | ||
| // </auto-generated> | ||
| //------------------------------------------------------------------------------"; | ||
|
|
||
| var lines = headerText.Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); | ||
|
|
||
| SyntaxTrivia[] header = lines.SelectMany(l => new[] { Comment(l), LineFeed }).ToArray(); | ||
| return header; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could consider
HomeAssistantNameorNameInHomeAssistant. It is a bit longer but more clear. Not a biggie!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was the existing name, they are just re-ordered. Actually this whole class is a bit weird because it is just a slightly different view of the HassServiceField that is deserialized from the metadata. Maybe I will remove it in a future refactor