Ejemplo n.º 1
0
        //private ConstructorDeclarationSyntax GenerateConstructor(string className)
        //{
        //    ConstructorInitializerSyntax constructorInitializerSyntax = SyntaxFactory.ConstructorInitializer(
        //        SyntaxKind.BaseConstructorInitializer,
        //        SyntaxFactory.Token(SyntaxKind.ColonToken),
        //        SyntaxFactory.Token(SyntaxKind.BaseKeyword),
        //        SyntaxFactory.ArgumentList());

        //    ConstructorDeclarationSyntax constructor =
        //        SyntaxFactory.ConstructorDeclaration(SyntaxFactory.Identifier(className))
        //            .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
        //            .AddBodyStatements()
        //            .WithParameterList(SyntaxFactory.ParseParameterList("(HttpRepositorySettings settings, IHttpClientFactory httpClientFactory, ISerializer<string> serializer)"))
        //            .WithInitializer(constructorInitializerSyntax);

        //    return constructor;
        //}

        private MethodDeclarationSyntax GenerateMethod(OperationContext operationContext)
        {
            string methodReturnType = GetMethodReturnType(operationContext);
            string relativeUrl      = GenerateRelativeUrl(operationContext);

            StatementSyntax syntax = GetMethodSyntax(operationContext, relativeUrl);

            var methodDeclaration = SyntaxFactory.MethodDeclaration(SyntaxFactory.ParseTypeName(methodReturnType), $"{operationContext.Name.ToCamelCase()}Async")
                                    .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
                                    .AddModifiers(SyntaxFactory.Token(SyntaxKind.VirtualKeyword))
                                    .WithBody(SyntaxFactory.Block(syntax));

            if (operationContext.BodyParameter != null)
            {
                string bodyParameterType = operationContext.BodyParameter?.Type?.ToCamelCase();

                methodDeclaration = methodDeclaration.AddParameterListParameters(
                    SyntaxFactory.Parameter(
                        SyntaxFactory.Identifier(ParameterContext.DefaultParameterName))
                    .WithType(SyntaxFactory.ParseTypeName(bodyParameterType)));
            }

            if (operationContext.PathParameters.Any())
            {
                foreach (ParameterContext parameter in operationContext.PathParameters)
                {
                    string typeName = typeNameResolver.ResolveType(parameter);
                    methodDeclaration = methodDeclaration.AddParameterListParameters(
                        SyntaxFactory.Parameter(SyntaxFactory.Identifier(parameter.Name)).WithType(SyntaxFactory.ParseTypeName(typeName)));
                }
            }

            if (operationContext.QueryParameters.Any())
            {
                foreach (ParameterContext parameter in operationContext.QueryParameters)
                {
                    string typeName = typeNameResolver.ResolveType(parameter);
                    methodDeclaration = methodDeclaration.AddParameterListParameters(
                        SyntaxFactory.Parameter(SyntaxFactory.Identifier(parameter.Name)).WithType(SyntaxFactory.ParseTypeName(typeName)));
                }
            }

            return(methodDeclaration);
        }
Ejemplo n.º 2
0
        private void GenerateModelClass(KeyValuePair <string, DataModelContext> contextDataModel)
        {
            try
            {
                string className    = typeNameResolver.FixPossibleTypeNameIssues(contextDataModel.Key);
                string fileName     = $"{className}.cs";
                string fileLocation = Path.Combine(configuration.ModelFolderLocation, fileName);

                CompilationUnitSyntax compilationUnit = SyntaxFactory.CompilationUnit();
                compilationUnit = compilationUnit
                                  .AddUsings(SyntaxFactory.UsingDirective(SyntaxFactory.ParseName("System")))
                                  .AddUsings(SyntaxFactory.UsingDirective(SyntaxFactory.ParseName("System.Collections.Generic")))
                                  .AddUsings(SyntaxFactory.UsingDirective(SyntaxFactory.ParseName("Newtonsoft.Json")));

                var @namespace = SyntaxFactory.NamespaceDeclaration(SyntaxFactory.ParseName($"{configuration.ProjectName}.{configuration.ModelFolderName}")).NormalizeWhitespace();

                var classDeclaration = SyntaxFactory.ClassDeclaration(className)
                                       .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword));

                foreach (KeyValuePair <string, ParameterContext> valuePropety in contextDataModel.Value.Propeties)
                {
                    string typeName = typeNameResolver.ResolveType(valuePropety.Value);

                    if (valuePropety.Value.IsCollectionParameter)
                    {
                        typeName = $"List<{typeName}>";
                    }

                    ExpressionSyntax attributeSyntax = SyntaxFactory.ParseExpression($"\"{valuePropety.Value.Name}\"");

                    AttributeSyntax attribute = SyntaxFactory.Attribute(SyntaxFactory.IdentifierName("JsonProperty"))
                                                .WithArgumentList(SyntaxFactory.AttributeArgumentList()
                                                                  .WithOpenParenToken(SyntaxFactory.Token(SyntaxKind.OpenParenToken))
                                                                  .WithCloseParenToken(SyntaxFactory.Token(SyntaxKind.CloseParenToken))
                                                                  .AddArguments(SyntaxFactory.AttributeArgument(attributeSyntax)));

                    PropertyDeclarationSyntax property = SyntaxFactory.PropertyDeclaration(
                        SyntaxFactory.ParseTypeName(typeName), SyntaxFactory.Identifier(valuePropety.Value.Name.ToCamelCase()))
                                                         .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
                                                         .AddAttributeLists(
                        SyntaxFactory.AttributeList()
                        .WithOpenBracketToken(SyntaxFactory.Token(SyntaxKind.OpenBracketToken))
                        .WithCloseBracketToken(SyntaxFactory.Token(SyntaxKind.CloseBracketToken))
                        .AddAttributes(attribute))
                                                         .AddAccessorListAccessors(
                        SyntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration).WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken)),
                        SyntaxFactory.AccessorDeclaration(SyntaxKind.SetAccessorDeclaration).WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken)));

                    classDeclaration = classDeclaration.AddMembers(property);
                }

                @namespace      = @namespace.AddMembers(classDeclaration);
                compilationUnit = compilationUnit.AddMembers(@namespace);

                string fileContent = compilationUnit.NormalizeWhitespace().ToFullString();

                File.WriteAllText(fileLocation, fileContent);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }