|
| 1 | +using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; |
| 2 | +using static Microsoft.CodeAnalysis.CSharp.SyntaxKind; |
| 3 | + |
| 4 | +namespace NetDaemon.HassModel.CodeGenerator.CodeGeneration; |
| 5 | + |
| 6 | +internal static class HelpersGenerator |
| 7 | +{ |
| 8 | + public static IEnumerable<MemberDeclarationSyntax> Generate(IReadOnlyCollection<EntityDomainMetadata> domains, IEnumerable<HassServiceDomain> orderedServiceDomains) |
| 9 | + { |
| 10 | + var extensionClass = GenerateServiceCollectionExtension(domains, orderedServiceDomains); |
| 11 | + return new[] { extensionClass }; |
| 12 | + } |
| 13 | + |
| 14 | + /// <summary> |
| 15 | + /// Generates the ServiceCollectionExtensions class |
| 16 | + /// </summary> |
| 17 | + /// public static class GeneratedExtensions |
| 18 | + /// { |
| 19 | + /// ... |
| 20 | + /// } |
| 21 | + private static ClassDeclarationSyntax GenerateServiceCollectionExtension(IReadOnlyCollection<EntityDomainMetadata> domains, IEnumerable<HassServiceDomain> orderedServiceDomains) |
| 22 | + { |
| 23 | + return |
| 24 | + ClassDeclaration("GeneratedExtensions").WithModifiers(TokenList(Token(PublicKeyword), Token(StaticKeyword))) |
| 25 | + .WithMembers(new SyntaxList<MemberDeclarationSyntax>(new[] |
| 26 | + { |
| 27 | + BuildAddHomeAssistantGenerated(domains, orderedServiceDomains) |
| 28 | + } |
| 29 | + )); |
| 30 | + } |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// Generates the AddHomeAssistantGenerated method |
| 34 | + /// </summary> |
| 35 | + // |
| 36 | + // public static IServiceCollection AddGeneratedCode(this IServiceCollection serviceCollection) |
| 37 | + // { |
| 38 | + // serviceCollection.AddTransient<Entities>(); |
| 39 | + // serviceCollection.AddTransient<AutomationEntities>(); |
| 40 | + // serviceCollection.AddTransient<BinarySensorEntities>(); |
| 41 | + // serviceCollection.AddTransient<Services>(); |
| 42 | + // serviceCollection.AddTransient<AlarmControlPanelServices>(); |
| 43 | + // return serviceCollection; |
| 44 | + // } |
| 45 | + private static MethodDeclarationSyntax BuildAddHomeAssistantGenerated(IEnumerable<EntityDomainMetadata> domains, IEnumerable<HassServiceDomain> orderedServiceDomains) |
| 46 | + { |
| 47 | + |
| 48 | + var injectableTypes = GetInjectableTypes(domains, orderedServiceDomains); |
| 49 | + |
| 50 | + var statements = injectableTypes.Select(name => |
| 51 | + ExpressionStatement(InvocationExpression( |
| 52 | + MemberAccessExpression(SimpleMemberAccessExpression, IdentifierName("serviceCollection"), |
| 53 | + GenericName(Identifier("AddTransient")) |
| 54 | + .WithTypeArgumentList(TypeArgumentList(SingletonSeparatedList<TypeSyntax>(IdentifierName(name)))))))); |
| 55 | + |
| 56 | + return MethodDeclaration(IdentifierName("IServiceCollection"), Identifier("AddHomeAssistantGenerated")) |
| 57 | + .WithModifiers(TokenList(Token(PublicKeyword), Token(StaticKeyword))) |
| 58 | + .WithParameterList(ParameterList(SingletonSeparatedList(Parameter(Identifier("serviceCollection")) |
| 59 | + .WithModifiers(TokenList(Token(ThisKeyword))).WithType(IdentifierName("IServiceCollection"))))) |
| 60 | + .WithBody(Block( |
| 61 | + statements |
| 62 | + .Append<StatementSyntax>(ReturnStatement(IdentifierName("serviceCollection"))))) |
| 63 | + .WithSummaryComment("Registers all injectable generated types in the serviceCollection"); |
| 64 | + } |
| 65 | + |
| 66 | + private static IEnumerable<string> GetInjectableTypes(IEnumerable<EntityDomainMetadata> domains, IEnumerable<HassServiceDomain> orderedServiceDomains) => |
| 67 | + domains.Select(d => d.EntitiesForDomainClassName) |
| 68 | + .Prepend(EntitiesClassName) |
| 69 | + .Append(ServicesClassName) |
| 70 | + .Union(orderedServiceDomains.Select(d => GetServicesTypeName(d.Domain))); |
| 71 | +} |
0 commit comments