public static string GenerateString()
        {
            // Templates
            var classGenerationTemplates = ReadTemplates.Read();

            return(classGenerationTemplates.CachedEntityObject);
        }
Пример #2
0
        public static string GenerateString(
            ClassStatement classStatement
            )
        {
            // Templates
            var classGenerationTemplates = ReadTemplates.Read();

            return(classGenerationTemplates.ClassShim.Replace(
                       "[[CLASS_NAME]]",
                       classStatement.Name
                       ));
        }
        public static void SetReadTemplates()
        {
            var templatesPath = "EventHorizon.Blazor.Interop.Generator.Templates";

            // Class Template
            var classTemplate = ReadAllText(
                $"{templatesPath}.ClassTemplate.txt"
                );
            // Class Shim Template
            var classShimTemplate = ReadAllText(
                $"{templatesPath}.ClassShimTemplate.txt"
                );
            // Cached Entity Object Template
            var cachedEntityObjectTemplate = ReadAllText(
                $"{templatesPath}.CachedEntityObjectTemplate.txt"
                );
            // Accessor/Property Template
            var accessorTemplate = ReadAllText($"{templatesPath}.AccessorTemplate.txt"
                                               );
            // Accessor/Property With Setter Template
            var accessorWithSetterTemplate = ReadAllText(
                $"{templatesPath}.AccessorWithSetterTemplate.txt"
                );
            // Constructor Template
            var constructorTemplate = ReadAllText(
                $"{templatesPath}.ConstructorTemplate.txt"
                );
            // Constructor To Base Template
            var constructorToBaseTemplate = ReadAllText(
                $"{templatesPath}.ConstructorToBaseTemplate.txt"
                );
            // Constructor With Arguments Template
            var constructorWithArgumentsTemplate = ReadAllText(
                $"{templatesPath}.ConstructorWithArgumentsTemplate.txt"
                );
            // Method Template
            var methodTemplate = ReadAllText(
                $"{templatesPath}.MethodTemplate.txt"
                );
            var methodActionTemplate = ReadAllText(
                $"{templatesPath}.MethodActionTemplate.txt"
                );
            var methodStaticActionTemplate = ReadAllText(
                $"{templatesPath}.MethodStaticActionTemplate.txt"
                );
            var returnTypePrimitiveTemplate = ReadAllText(
                $"{templatesPath}.ReturnTypePrimitiveTemplate.txt"
                );
            var returnTypeClassTemplate = ReadAllText(
                $"{templatesPath}.ReturnTypeClassTemplate.txt")
            ;
            var returnTypeVoidTemplate = ReadAllText(
                $"{templatesPath}.ReturnTypeVoidTemplate.txt"
                );

            // Interop Templates
            var interopGetTemplate = ReadAllText(
                $"{templatesPath}.InteropGetTemplate.txt"
                );
            var interopSetTemplate = ReadAllText(
                $"{templatesPath}.InteropSetTemplate.txt"
                );
            var interopFuncTemplate = ReadAllText(
                $"{templatesPath}.InteropFuncTemplate.txt"
                );
            var interopFuncArrayTemplate = ReadAllText(
                $"{templatesPath}.InteropFuncArrayTemplate.txt"
                );
            var interopFuncClassTemplate = ReadAllText(
                $"{templatesPath}.InteropFuncClassTemplate.txt"
                );
            var interopFuncArrayClassTemplate = ReadAllText(
                $"{templatesPath}.InteropFuncArrayClassTemplate.txt"
                );
            var interopGetArrayClassTemplate = ReadAllText(
                $"{templatesPath}.InteropGetArrayClassTemplate.txt"
                );
            var interopGetArrayTemplate = ReadAllText(
                $"{templatesPath}.InteropGetArrayTemplate.txt"
                );
            var interopGetClassTemplate = ReadAllText(
                $"{templatesPath}.InteropGetClassTemplate.txt"
                );

            var generatedTemplates = ReadTemplates.Read();

            generatedTemplates.Class              = classTemplate;
            generatedTemplates.ClassShim          = classShimTemplate;
            generatedTemplates.CachedEntityObject = cachedEntityObjectTemplate;
            generatedTemplates.Accessor           = accessorTemplate;
            generatedTemplates.AccessorWithSetter = accessorWithSetterTemplate;
            generatedTemplates.Constructor        = constructorTemplate;
            generatedTemplates.ConstructorToBase  = constructorToBaseTemplate;
            generatedTemplates.ConstructorWithArgumentsTemplate = constructorWithArgumentsTemplate;
            generatedTemplates.Method = methodTemplate;
            generatedTemplates.MethodActionTemplate        = methodActionTemplate;
            generatedTemplates.MethodStaticActionTemplate  = methodStaticActionTemplate;
            generatedTemplates.ReturnTypePrimitiveTemplate = returnTypePrimitiveTemplate;
            generatedTemplates.ReturnTypeClass             = returnTypeClassTemplate;
            generatedTemplates.ReturnTypeVoidTemplate      = returnTypeVoidTemplate;

            generatedTemplates.InteropFunc           = interopFuncTemplate;
            generatedTemplates.InteropFuncClass      = interopFuncClassTemplate;
            generatedTemplates.InteropFuncArray      = interopFuncArrayTemplate;
            generatedTemplates.InteropFuncArrayClass = interopFuncArrayClassTemplate;
            generatedTemplates.InteropGetArrayClass  = interopGetArrayClassTemplate;
            generatedTemplates.InteropGetArray       = interopGetArrayTemplate;
            generatedTemplates.InteropGetClass       = interopGetClassTemplate;
            generatedTemplates.InteropGet            = interopGetTemplate;
            generatedTemplates.InteropSet            = interopSetTemplate;
        }
        public static string Generate(
            ClassStatement classStatement,
            TextFormatter textFormatter
            )
        {
            var classTokenMap = new Dictionary <string, string>
            {
                { "[[INTERFACE_SECTION]]", string.Empty },
                { "[[EXTENDED_CLASSES_SECTION]]", string.Empty },
                { "[[WHERE_CONSTRAINT]]", string.Empty },
                { "[[CLASS_GENERICS]]", string.Empty },
                { "[[STATIC_ACCESSORS]]", string.Empty },
                { "[[STATIC_PROPERTIES]]", string.Empty },
                { "[[STATIC_METHODS]]", string.Empty },
                { "[[ACCESSORS]]", string.Empty },
                { "[[PROPERTIES]]", string.Empty },
                { "[[CONSTRUCTOR]]", string.Empty },
                { "[[METHODS]]", string.Empty },
                { "[[ASSEMBLY]]", classStatement.ProjectAssembly },
                { "[[CLASS_NAME]]", string.Empty },
                { "[[NAMESPACE]]", string.Empty },
                { "[[BASE_CONSTRUCTOR]]", string.Empty },
                { "[[INTERFACE_POSTFIX]]", string.Empty },
            };

            // Group Parts of the Class
            var staticAccessors    = classStatement.AccessorStatements.Where(a => a.IsStatic);
            var staticProperties   = classStatement.PublicPropertyStatements.Where(a => a.IsStatic);
            var staticMethods      = classStatement.PublicMethodStatements.Where(a => a.IsStatic).Distinct();
            var accessors          = classStatement.AccessorStatements.Where(a => !a.IsStatic);
            var properties         = classStatement.PublicPropertyStatements.Where(a => !a.IsStatic);
            var constructorDetails = classStatement.ConstructorStatement;
            var methods            = classStatement.PublicMethodStatements.Where(a => !a.IsStatic).Distinct();

            // Templates
            var classGenerationTemplates = ReadTemplates.Read();

            var classTemplate = classGenerationTemplates.Class;

            // Generate Tokens
            var namespaceReplaced = classStatement.Namespace;

            if (string.IsNullOrWhiteSpace(namespaceReplaced))
            {
                namespaceReplaced = string.Empty;
                classTemplate     = classGenerationTemplates.ClassWithNoNamespace;
            }
            classTokenMap["[[NAMESPACE]]"]  = namespaceReplaced;
            classTokenMap["[[CLASS_NAME]]"] = classStatement.Name;

            classTokenMap["[[INTERFACE_SECTION]]"] = InterfaceSectionWriter.Write(
                classStatement,
                classGenerationTemplates
                );
            classTokenMap["[[INTERFACE_POSTFIX]]"] = classStatement.IsInterface ? Constants.INTERFACE_POSTFIX : string.Empty;
            classTokenMap["[[CLASS_GENERICS]]"]    = BuildClassGenerics(
                classStatement
                );
            classTokenMap["[[JSON_CONVERTER_CLASS_GENERICS]]"] = BuildJsonConvertClassGenerics(
                classStatement
                );
            classTokenMap["[[EXTENDED_CLASSES_SECTION]]"] = BuildExtendedClassesSection(
                classStatement
                );
            classTokenMap["[[WHERE_CONSTRAINT]]"] = classStatement.GenericTypes.Any()
                ? string.Join(
                "",
                classStatement.GenericTypes.Select(
                    genericType => $" where {genericType.Name} : CachedEntity, new()"
                    )
                )
                : string.Empty;
            classTokenMap["[[STATIC_ACCESSORS]]"] = AccessorsSectionWriter.Write(
                classStatement,
                staticAccessors,
                classGenerationTemplates
                );
            classTokenMap["[[STATIC_PROPERTIES]]"] = PropertiesSectionWriter.Write(
                classStatement,
                staticProperties,
                classGenerationTemplates
                );
            classTokenMap["[[STATIC_METHODS]]"] = MethodsSectionWriter.Write(
                classStatement,
                staticMethods,
                classGenerationTemplates
                );
            classTokenMap["[[ACCESSORS]]"] = AccessorsSectionWriter.Write(
                classStatement,
                accessors,
                classGenerationTemplates
                );
            classTokenMap["[[PROPERTIES]]"] = PropertiesSectionWriter.Write(
                classStatement,
                properties,
                classGenerationTemplates
                );
            classTokenMap["[[CONSTRUCTOR]]"] = ConstructorSectionWriter.Write(
                classStatement,
                constructorDetails,
                classGenerationTemplates
                );
            classTokenMap["[[BASE_CONSTRUCTOR]]"] = BaseConstructorSectionWriter.Write(
                classStatement,
                constructorDetails,
                classGenerationTemplates
                );
            classTokenMap["[[METHODS]]"] = MethodsSectionWriter.Write(
                classStatement,
                methods,
                classGenerationTemplates
                );

            var classStringBuilder = new StringBuilder(
                classTemplate
                );

            classStringBuilder = classTokenMap.Aggregate(
                classStringBuilder,
                (acc, token) => acc.Replace(
                    token.Key,
                    token.Value
                    )
                );

            return(textFormatter.Format(
                       classStringBuilder.ToString()
                       ));
        }