Ejemplo n.º 1
0
        public bool GenerateCode()
        {
            // Create compilationUnit
            var compilationUnit = SyntaxFactory.CompilationUnit();

            // Create a namespace
            var @namespace = SyntaxProjectFactory.CreateNamespace(
                ApiProjectOptions,
                NameConstants.Endpoints,
                FocusOnSegmentName);

            // Create class
            // TODO: var classDeclaration = SyntaxClassDeclarationFactory.CreateWithInterface(classTypeName, interfaceTypeName)
            var classDeclaration = SyntaxClassDeclarationFactory.Create(EndpointTypeName)
                                   .AddGeneratedCodeAttribute(ApiProjectOptions.ToolName, ApiProjectOptions.ToolVersion.ToString())
                                   .WithLeadingTrivia(SyntaxDocumentationFactory.CreateForResults(ApiOperation, FocusOnSegmentName));

            classDeclaration = classDeclaration.AddMembers(CreateFieldIHttpClientFactory());
            classDeclaration = classDeclaration.AddMembers(CreateFieldIHttpExchangeFactory());
            classDeclaration = classDeclaration.AddMembers(CreateConstructor());
            classDeclaration = classDeclaration.AddMembers(CreateMembers());

            // TODO: Imp. this.
            var usedApiOperations = new List <OpenApiOperation>();

            // Add using statement to compilationUnit
            var includeRestResults = classDeclaration
                                     .Select <IdentifierNameSyntax>()
                                     .Any(x => x.Identifier.ValueText.Contains(
                                              $"({Microsoft.OpenApi.Models.NameConstants.Pagination}<",
                                              StringComparison.Ordinal));

            compilationUnit = compilationUnit.AddUsingStatements(
                ProjectEndpointsFactory.CreateUsingList(
                    ApiProjectOptions,
                    FocusOnSegmentName,
                    usedApiOperations,
                    includeRestResults,
                    HasSharedResponseContract(),
                    forClient: true));

            // Add the class to the namespace.
            @namespace = @namespace.AddMembers(classDeclaration);

            // Add namespace to compilationUnit
            compilationUnit = compilationUnit.AddMembers(@namespace);

            // Set code property
            Code = compilationUnit;
            return(true);
        }
        public bool GenerateCode()
        {
            var controllerTypeName = FocusOnSegmentName.EnsureFirstCharacterToUpper() + "Controller";

            // Create compilationUnit
            var compilationUnit = SyntaxFactory.CompilationUnit();

            // Create a namespace
            var @namespace = SyntaxProjectFactory.CreateNamespace(
                ApiProjectOptions,
                NameConstants.Endpoints);

            // Create class
            var classDeclaration = SyntaxClassDeclarationFactory.Create(controllerTypeName);

            if (ApiProjectOptions.ApiOptions.Generator.UseAuthorization)
            {
                classDeclaration =
                    classDeclaration.AddAttributeLists(
                        SyntaxAttributeListFactory.Create(nameof(AuthorizeAttribute)));
            }

            classDeclaration =
                classDeclaration.AddAttributeLists(
                    SyntaxAttributeListFactory.Create(nameof(ApiControllerAttribute)),
                    SyntaxAttributeListFactory.CreateWithOneItemWithOneArgument(nameof(RouteAttribute), $"api/{ApiProjectOptions.ApiVersion}/{FocusOnSegmentName}"))
                .AddBaseListTypes(SyntaxFactory.SimpleBaseType(SyntaxFactory.ParseTypeName(nameof(ControllerBase))))
                .AddGeneratedCodeAttribute(ApiProjectOptions.ToolName, ApiProjectOptions.ToolVersion.ToString())
                .WithLeadingTrivia(SyntaxDocumentationFactory.CreateForEndpoints(FocusOnSegmentName));

            // Create Methods
            var usedApiOperations = new List <OpenApiOperation>();

            foreach (var(key, value) in ApiProjectOptions.Document.GetPathsByBasePathSegmentName(FocusOnSegmentName))
            {
                var hasRouteParameters = value.HasParameters();
                foreach (var apiOperation in value.Operations)
                {
                    var methodDeclaration = CreateMembersForEndpoints(apiOperation, key, FocusOnSegmentName, hasRouteParameters)
                                            .WithLeadingTrivia(SyntaxDocumentationFactory.CreateForEndpointMethods(apiOperation, FocusOnSegmentName));
                    classDeclaration = classDeclaration.AddMembers(methodDeclaration);

                    usedApiOperations.Add(apiOperation.Value);
                }
            }

            // Create private part for methods
            foreach (var(_, value) in ApiProjectOptions.Document.GetPathsByBasePathSegmentName(FocusOnSegmentName))
            {
                var hasRouteParameters = value.HasParameters();
                foreach (var apiOperation in value.Operations)
                {
                    var methodDeclaration = CreateMembersForEndpointsPrivateHelper(apiOperation, hasRouteParameters);
                    classDeclaration = classDeclaration.AddMembers(methodDeclaration);

                    usedApiOperations.Add(apiOperation.Value);
                }
            }

            // Add the class to the namespace.
            @namespace = @namespace.AddMembers(classDeclaration);

            // Add using statement to compilationUnit
            var includeRestResults = classDeclaration
                                     .Select <IdentifierNameSyntax>()
                                     .Any(x => x.Identifier.ValueText.Contains($"({Microsoft.OpenApi.Models.NameConstants.Pagination}<", StringComparison.Ordinal));

            compilationUnit = compilationUnit.AddUsingStatements(
                ProjectEndpointsFactory.CreateUsingList(
                    ApiProjectOptions,
                    FocusOnSegmentName,
                    usedApiOperations,
                    includeRestResults));

            // Add namespace to compilationUnit
            compilationUnit = compilationUnit.AddMembers(@namespace);

            // Set code property
            Code = compilationUnit;
            return(true);
        }