Beispiel #1
0
 internal static void GenerateConstructor(this ViewModelBuilder vmBuilder, ViewModelToGenerate viewModelToGenerate)
 {
     if (viewModelToGenerate.GenerateConstructor)
     {
         Generate(vmBuilder, viewModelToGenerate.ViewModelClassSymbol.Name,
                  viewModelToGenerate.InjectionsToGenerate,
                  viewModelToGenerate.CommandsToGenerate?.Any() == true,
                  viewModelToGenerate.IsEventSubscriber);
     }
 }
        /// <summary>
        /// Called for every syntax node in the compilation, we can inspect the nodes and save any information useful for generation
        /// </summary>
        public void OnVisitSyntaxNode(GeneratorSyntaxContext context)
        {
            if (context.Node is ClassDeclarationSyntax
            {
                AttributeLists : { Count : > 0 }
            } classDeclarationSyntax)
            {
                var viewModelClassSymbol   = context.SemanticModel.GetDeclaredSymbol(classDeclarationSyntax);
                var viewModelAttributeData = viewModelClassSymbol?.GetAttributes().SingleOrDefault(x => x.AttributeClass?.ToDisplayString() == "MvvmGen.ViewModelAttribute");

                if (viewModelClassSymbol is not null && viewModelAttributeData is not null)
                {
                    var(commandsToGenerate,
                        commandsToInvalidateByPropertyName,
                        propertiesToGenerate,
                        propertyInvalidationsByGeneratedPropertyName) = ViewModelMemberInspector.Inspect(viewModelClassSymbol);

                    var viewModelToGenerate = new ViewModelToGenerate(viewModelClassSymbol)
                    {
                        InjectionsToGenerate               = ViewModelInjectAttributeInspector.Inspect(viewModelClassSymbol),
                        GenerateConstructor                = ViewModelAttributeInspector.Inspect(viewModelAttributeData),
                        ViewModelFactoryToGenerate         = ViewModelGenerateFactoryAttributeInspector.Inspect(viewModelClassSymbol),
                        CommandsToGenerate                 = commandsToGenerate,
                        PropertiesToGenerate               = propertiesToGenerate,
                        CommandsToInvalidateByPropertyName = commandsToInvalidateByPropertyName
                    };

                    viewModelToGenerate.WrappedModelType = ModelMemberInspector.Inspect(viewModelAttributeData, viewModelToGenerate.PropertiesToGenerate);

                    SetPropertiesToInvalidatePropertyOnPropertiesToGenerate(viewModelToGenerate.PropertiesToGenerate, propertyInvalidationsByGeneratedPropertyName);

                    viewModelToGenerate.IsEventSubscriber = viewModelClassSymbol.Interfaces.Any(x => x.ToDisplayString().StartsWith("MvvmGen.Events.IEventSubscriber"));

                    ViewModelsToGenerate.Add(viewModelToGenerate);
                }
            }
        }
Beispiel #3
0
        internal static void GenerateFactoryClass(this ViewModelBuilder vmBuilder, ViewModelToGenerate viewModelToGenerate)
        {
            if (viewModelToGenerate.ViewModelFactoryToGenerate is null)
            {
                return;
            }

            var factoryToGenerate  = viewModelToGenerate.ViewModelFactoryToGenerate;
            var viewModelClassName = viewModelToGenerate.ViewModelClassSymbol.Name;

            var injectionsToGenerate = viewModelToGenerate.InjectionsToGenerate ?? Enumerable.Empty <InjectionToGenerate>();

            var accessModifier = viewModelToGenerate.ViewModelClassSymbol.DeclaredAccessibility switch
            {
                Accessibility.Public => "public",
                Accessibility.Internal => "internal",
                _ => ""
            };

            vmBuilder.AppendLine();
            vmBuilder.AppendLine($"{accessModifier} interface {factoryToGenerate.InterfaceName} : IViewModelFactory<{viewModelClassName}> {{ }}");
            vmBuilder.AppendLine();
            vmBuilder.AppendLine($"{accessModifier} class {factoryToGenerate.ClassName} : {factoryToGenerate.InterfaceName}");
            vmBuilder.AppendLine("{");
            vmBuilder.IncreaseIndent();

            vmBuilder.Append($"public {factoryToGenerate.ClassName}(");
            var first = true;

            foreach (var injectionToGenerate in injectionsToGenerate)
            {
                if (!first)
                {
                    vmBuilder.Append(", ");
                }
                first = false;
                vmBuilder.Append($"{injectionToGenerate.Type} {injectionToGenerate.PropertyName.ToCamelCase()}");
            }

            vmBuilder.AppendLine(")");
            vmBuilder.AppendLine("{");
            vmBuilder.IncreaseIndent();

            foreach (var injectionToGenerate in injectionsToGenerate)
            {
                vmBuilder.AppendLine($"this.{injectionToGenerate.PropertyName} = {injectionToGenerate.PropertyName.ToCamelCase()};");
            }

            vmBuilder.DecreaseIndent();
            vmBuilder.AppendLine("}");

            InjectionPropertyGenerator.GenerateInjectionProperties(vmBuilder, injectionsToGenerate);

            vmBuilder.AppendLine();
            vmBuilder.Append($"public {viewModelClassName} Create() => new {viewModelClassName}(");
            first = true;
            foreach (var injectionToGenerate in injectionsToGenerate)
            {
                if (!first)
                {
                    vmBuilder.Append(", ");
                }
                first = false;
                vmBuilder.Append(injectionToGenerate.PropertyName);
            }

            vmBuilder.AppendLine(");");
            vmBuilder.DecreaseIndent();
            vmBuilder.AppendLine("}");
        }
    }