Ejemplo n.º 1
0
        protected virtual async Task GenerateServerSideCode(CodeModelCs codeModel)
        {
            foreach (string methodGrp in codeModel.MethodGroupNames)
            {
                using (NewContext)
                {
                    codeModel.Name = methodGrp;
                    // Service server
                    var serviceControllerTemplate = new AutoRest.CSharp.vanilla.Templates.Rest.Server.WcfServiceInterfaceTemplate {
                        Model = codeModel
                    };
                    await Write(serviceControllerTemplate, $"{GeneratedSourcesBaseFolder}{codeModel.Name}{ImplementationFileExtension}");
                }
            }
            // Models
            foreach (CompositeTypeCs model in codeModel.ModelTypes.Union(codeModel.HeaderTypes))
            {
                if (true == model.Extensions.Get <bool>(SwaggerExtensions.ExternalExtension))
                {
                    continue;
                }

                var modelTemplate = new ModelTemplate {
                    Model = model
                };
                await Write(modelTemplate, $"{GeneratedSourcesBaseFolder}{FolderModels}/{model.Name}{ImplementationFileExtension}");
            }
        }
Ejemplo n.º 2
0
        protected virtual async Task GenerateClientSideCode(CodeModelCs codeModel)
        {
            await GenerateServiceClient <ServiceClientTemplate>(codeModel);
            await GenerateOperations <MethodGroupTemplate>(codeModel.Operations);
            await GenerateModels(codeModel.ModelTypes.Union(codeModel.HeaderTypes));
            await GenerateEnums(codeModel.EnumTypes);
            await GenerateExceptions(codeModel.ErrorTypes);

            if (codeModel.ShouldGenerateXmlSerialization)
            {
                await GenerateXmlSerialization();
            }
        }
Ejemplo n.º 3
0
        protected override async Task GenerateClientSideCode(CodeModelCs codeModel)
        {
            await GenerateServiceClient <ServiceClientTemplate>(codeModel);

            // await GenerateOperations<VanillaTemplates.MethodGroupTemplate>(codeModel.Operations);
            await GenerateModels(codeModel.ModelTypes.Union(codeModel.HeaderTypes));
            await GenerateEnums(codeModel.EnumTypes);
            await GenerateExceptions(codeModel.ErrorTypes);

            if (codeModel.ShouldGenerateXmlSerialization)
            {
                await GenerateXmlSerialization();
            }

            await Write(new ExtendedServiceClientTemplate { Model = codeModel }, $"{GeneratedSourcesBaseFolder}LoadBalanced{codeModel.Name}{ImplementationFileExtension}");
            await Write(new ConfigTemplate { Model = codeModel }, $"{GeneratedSourcesBaseFolder}LoadBalancingConfigBase{ImplementationFileExtension}");
            await Write(new ConfigInterfaceTemplate { Model = codeModel }, $"{GeneratedSourcesBaseFolder}ILoadBalancingConfig{ImplementationFileExtension}");
        }
Ejemplo n.º 4
0
        private async Task GenerateRestCode(CodeModelCs codeModel)
        {
            if (Settings.Instance.CodeGenerationMode.IsNullOrEmpty() || Settings.Instance.CodeGenerationMode.EqualsIgnoreCase("rest-client"))
            {
                Logger.Instance.Log(Category.Debug, "Defaulting to generate client side Code");
                await GenerateClientSideCode(codeModel);
            }
            else if (Settings.Instance.CodeGenerationMode.EqualsIgnoreCase("rest"))
            {
                Logger.Instance.Log(Category.Debug, "Generating client side Code");
                await GenerateClientSideCode(codeModel);

                Logger.Instance.Log(Category.Debug, "Generating server side Code");
                await GenerateServerSideCode(codeModel);
            }
            else if (Settings.Instance.CodeGenerationMode.EqualsIgnoreCase("rest-server"))
            {
                Logger.Instance.Log(Category.Debug, "Generating server side Code");
                await GenerateServerSideCode(codeModel);
            }
        }
Ejemplo n.º 5
0
 protected virtual async Task GenerateServerSideCode(CodeModelCs codeModel)
 {
     foreach (string methodGrp in codeModel.MethodGroupNames)
     {
         using (NewContext)
         {
             codeModel.Name = methodGrp;
             // Service server
             var serviceControllerTemplate = new AutoRest.CSharp.Templates.Rest.Server.ServiceControllerTemplate {
                 Model = codeModel
             };
             await Write(serviceControllerTemplate, $"{codeModel.Name}{ImplementationFileExtension}");
         }
     }
     // Models
     foreach (CompositeTypeCs model in codeModel.ModelTypes.Union(codeModel.HeaderTypes))
     {
         var modelTemplate = new ModelTemplate {
             Model = model
         };
         await Write(modelTemplate, Path.Combine(Settings.Instance.ModelsName, $"{model.Name}{ImplementationFileExtension}"));
     }
 }
Ejemplo n.º 6
0
 protected virtual async Task GenerateServiceClient <T>(CodeModelCs codeModel) where T : Template <CodeModelCs>, new()
 {
     await Write(new T { Model = codeModel }, $"{GeneratedSourcesBaseFolder}{codeModel.Name}{ImplementationFileExtension}");
     await Write(new ServiceClientInterfaceTemplate { Model = codeModel }, $"{GeneratedSourcesBaseFolder}I{codeModel.Name}{ImplementationFileExtension}");
 }
Ejemplo n.º 7
0
        private async Task GenerateClientSideCode(CodeModelCs codeModel)
        {
            CompositeTypeCs.DefaultPropertyTypeSelectionStrategy = new WrappedPropertyTypeSelectionStrategy();

            var usings       = new List <string>();
            var methodGroups = codeModel.Operations.Cast <MethodGroupCs>();
            var methods      = codeModel.Methods.Where(m => m.Group.IsNullOrEmpty()).Cast <MethodCs>().ToList();

            var project = new ProjectModel
            {
                RootNameSpace = codeModel.Namespace
            };

            var metricsTemplate = new MetricsTemplate {
                Model = methods
            };
            var metricsFilePath = "Metrics.cs";

            await Write(metricsTemplate, metricsFilePath);

            var brokenRuleTemplate = new BrokenRuleTemplate();
            var brokenRuleFilePath = "BrokenRule.cs";

            await Write(brokenRuleTemplate, brokenRuleFilePath);

            var responseTemplate = new ResponseTemplate();
            var responseFilePath = "Response.cs";

            await Write(responseTemplate, responseFilePath);

            usings.AddRange(new[]
            {
                "System", "System.Collections.Generic", "System.Linq", "System.Threading",
                "System.Threading.Tasks", "Microsoft.Rest", "System.IO",
                "Microsoft.Rest.Serialization", "Agoda.RoundRobin", "Newtonsoft.Json",
                $"{codeModel.Namespace}.Models", "Agoda.RoundRobin.Constants", "System.ComponentModel",
                "AutoRest.CSharp.LoadBalanced.Json"
            });

            usings = usings.Where(u => !string.IsNullOrWhiteSpace(u)).Distinct().ToList();

            codeModel.Usings = usings;

            var clients = methods.GroupBy(m => m.Tags.First()).ToArray();

            var libPath = Path.Combine(Settings.Instance.OutputDirectory, "lib");

            await new LibFolderCreator(libPath).ExecuteAsync();

            foreach (var client in clients)
            {
                var clientName              = $"{client.Key.ToPascalCase()}Client";
                var clientMethods           = client.ToArray();
                var clientClassFileName     = $"{clientName}{ImplementationFileExtension}";
                var clientInterfaceFileName = $"I{clientClassFileName}";

                var model = new Tuple <CodeModelCs, string, MethodCs[]>(codeModel, clientName, clientMethods);

                // Service client interface
                var serviceClientInterfaceTemplate = new ServiceClientInterfaceTemplate {
                    Model = model
                };
                await Write(serviceClientInterfaceTemplate, clientInterfaceFileName);


                // Service client
                var serviceClientTemplate = new ServiceClientTemplate {
                    Model = model
                };
                await Write(serviceClientTemplate, clientClassFileName);
            }

            var apiBaseTemplate = new ApiBaseTemplate {
                Model = codeModel
            };
            var apiBaseCsPath = "ApiBase.cs";

            await Write(apiBaseTemplate, apiBaseCsPath);

            var badRequestExceptionTemplate = new BadRequestExceptionTemplate();
            var badRequestPath = "BadRequestException.cs";

            await Write(badRequestExceptionTemplate, badRequestPath);

            // operations
            foreach (var methodGroup in methodGroups)
            {
                if (methodGroup.Name.IsNullOrEmpty())
                {
                    continue;
                }

                // Operation
                var operationsTemplate = new MethodGroupTemplate {
                    Model = methodGroup
                };
                var operationsFilePath = $"{operationsTemplate.Model.TypeName}{ImplementationFileExtension}";

                await Write(operationsTemplate, operationsFilePath);

                // Operation interface
                var operationsInterfaceTemplate = new MethodGroupInterfaceTemplate {
                    Model = methodGroup
                };
                var operationsInterfacePath =
                    $"I{operationsInterfaceTemplate.Model.TypeName}{ImplementationFileExtension}";

                await Write(operationsInterfaceTemplate, operationsInterfacePath);
            }

            // Models
            var models = codeModel.ModelTypes.Union(codeModel.HeaderTypes).Cast <CompositeTypeCs>();

            foreach (var model in models)
            {
                if (model.Extensions.ContainsKey(SwaggerExtensions.ExternalExtension) &&
                    (bool)model.Extensions[SwaggerExtensions.ExternalExtension])
                {
                    continue;
                }

                Template <CompositeTypeCs> modelTemplate = null;

                if (model.PropertyTypeSelectionStrategy.IsCollection(model))
                {
                    modelTemplate = new CollectionModelTemplate {
                        Model = model
                    };
                }
                else
                {
                    modelTemplate = new ModelTemplate {
                        Model = model
                    };
                }

                var modelPath = Path.Combine(Settings.Instance.ModelsName, $"{model.Name}{ImplementationFileExtension}");

                await Write(modelTemplate, modelPath);
            }

            // Enums
            foreach (EnumTypeCs enumType in codeModel.EnumTypes)
            {
                var enumTemplate = new EnumTemplate {
                    Model = enumType
                };
                var enumFilePath = Path.Combine(Settings.Instance.ModelsName, $"{enumTemplate.Model.Name}{ImplementationFileExtension}");

                await Write(enumTemplate, enumFilePath);
            }

            // Exceptions
            foreach (CompositeTypeCs exceptionType in codeModel.ErrorTypes)
            {
                var exceptionTemplate = new ExceptionTemplate {
                    Model = exceptionType,
                };
                var exceptionFilePath =
                    Path.Combine(Settings.Instance.ModelsName, $"{exceptionTemplate.Model.ExceptionTypeDefinitionName}{ImplementationFileExtension}");

                await Write(exceptionTemplate, exceptionFilePath);
            }

            // CB models
            var couchbaseModels = codeModel.ModelTypes.Union(codeModel.HeaderTypes).Cast <CompositeTypeCs>();

            foreach (var model in couchbaseModels)
            {
                model.isCouchbaseModel = true;
                if (model.Extensions.ContainsKey(SwaggerExtensions.ExternalExtension) &&
                    (bool)model.Extensions[SwaggerExtensions.ExternalExtension])
                {
                    continue;
                }

                Template <CompositeTypeCs> modelTemplate = null;

                if (model.PropertyTypeSelectionStrategy.IsCollection(model))
                {
                    modelTemplate = new CollectionModelTemplate {
                        Model = model
                    };
                }
                else
                {
                    modelTemplate = new ModelTemplate {
                        Model = model
                    };
                }
                var modelPath = Path.Combine(Settings.Instance.ModelsName, $"Couchbase/{model.Name}{ImplementationFileExtension}");
                project.FilePaths.Add(modelPath);

                await Write(modelTemplate, modelPath);
            }
        }
Ejemplo n.º 8
0
 private async Task GenerateRestCode(CodeModelCs codeModel)
 {
     Logger.Instance.Log(Category.Info, "Defaulting to generate client side Code");
     await GenerateClientSideCode(codeModel);
 }
Ejemplo n.º 9
0
        private async Task GenerateClientSideCode(CodeModelCs codeModel)
        {
            CompositeTypeCs.DefaultPropertyTypeSelectionStrategy = new PropertyTypeSelectionStrategy();

            var usings  = new List <string>(codeModel.Usings);
            var methods = codeModel.Methods.Where(m => m.Group.IsNullOrEmpty()).Cast <MethodCs>().ToList();

            var project = new ProjectModel
            {
                RootNameSpace = codeModel.Namespace
            };

            var metricsTemplate = new MetricsTemplate {
                Model = methods
            };
            var metricsFilePath = "Metrics.cs";

            project.FilePaths.Add(metricsFilePath);
            await Write(metricsTemplate, metricsFilePath);

            var responseTemplate = new ResponseTemplate();
            var responseFilePath = "Response.cs";

            project.FilePaths.Add(responseFilePath);
            await Write(responseTemplate, responseFilePath);

            usings.Add("System");
            usings.Add("System.Collections.Generic");
            usings.Add("System.Linq");
            usings.Add("System.Threading");
            usings.Add("System.Threading.Tasks");
            usings.Add("Microsoft.Rest");
            usings.Add("System.IO");
            usings.Add("Microsoft.Rest.Serialization");
            usings.Add("System.Net.Http");
            usings.Add("Agoda.Frameworks.Http");
            usings.Add("Agoda.Frameworks.Http.AutoRestExt");
            usings.Add("Newtonsoft.Json");
            usings.Add($"{codeModel.Namespace}.Models");

            codeModel.Usings = usings.Where(u => !string.IsNullOrWhiteSpace(u)).Distinct();

            // Service client
            var serviceClientTemplate = new ServiceClientTemplate {
                Model = codeModel
            };

            var clientPath = $"{codeModel.Name}{ImplementationFileExtension}";

            project.FilePaths.Add(clientPath);

            await Write(serviceClientTemplate, clientPath);

            // Service client interface
            var serviceClientInterfaceTemplate = new ServiceClientInterfaceTemplate {
                Model = codeModel
            };

            var interfacePath = $"I{codeModel.Name}{ImplementationFileExtension}";

            project.FilePaths.Add(interfacePath);

            await Write(serviceClientInterfaceTemplate, interfacePath);

            var apiBaseTemplate = new ApiBaseTemplate {
                Model = codeModel
            };
            var apiBaseCsPath = "ApiBase.cs";

            project.FilePaths.Add(apiBaseCsPath);
            await Write(apiBaseTemplate, apiBaseCsPath);

            // operations
            foreach (var methodGroup1 in codeModel.Operations)
            {
                var methodGroup = (MethodGroupCs)methodGroup1;

                if (!methodGroup.Name.IsNullOrEmpty())
                {
                    // Operation
                    var operationsTemplate = new MethodGroupTemplate {
                        Model = methodGroup
                    };
                    var operationsFilePath = $"{operationsTemplate.Model.TypeName}{ImplementationFileExtension}";
                    project.FilePaths.Add(operationsFilePath);

                    await Write(operationsTemplate, operationsFilePath);

                    // Operation interface
                    var operationsInterfaceTemplate = new MethodGroupInterfaceTemplate {
                        Model = methodGroup
                    };
                    var operationsInterfacePath =
                        $"I{operationsInterfaceTemplate.Model.TypeName}{ImplementationFileExtension}";
                    project.FilePaths.Add(operationsInterfacePath);

                    await Write(operationsInterfaceTemplate, operationsInterfacePath);
                }
            }

            // Models
            foreach (CompositeTypeCs model in codeModel.ModelTypes.Union(codeModel.HeaderTypes))
            {
                if (model.Extensions.ContainsKey(SwaggerExtensions.ExternalExtension) &&
                    (bool)model.Extensions[SwaggerExtensions.ExternalExtension])
                {
                    continue;
                }

                var modelTemplate = new ModelTemplate {
                    Model = model
                };
                var modelPath = Path.Combine(Settings.Instance.ModelsName, $"{model.Name}{ImplementationFileExtension}");
                project.FilePaths.Add(modelPath);

                await Write(modelTemplate, modelPath);
            }

            // Enums
            foreach (EnumTypeCs enumType in codeModel.EnumTypes)
            {
                var enumTemplate = new EnumTemplate {
                    Model = enumType
                };
                var enumFilePath = Path.Combine(Settings.Instance.ModelsName, $"{enumTemplate.Model.Name}{ImplementationFileExtension}");
                project.FilePaths.Add(enumFilePath);

                await Write(enumTemplate, enumFilePath);
            }

            // Exceptions
            foreach (CompositeTypeCs exceptionType in codeModel.ErrorTypes)
            {
                var exceptionTemplate = new ExceptionTemplate {
                    Model = exceptionType,
                };
                var exceptionFilePath =
                    Path.Combine(Settings.Instance.ModelsName, $"{exceptionTemplate.Model.ExceptionTypeDefinitionName}{ImplementationFileExtension}");
                project.FilePaths.Add(exceptionFilePath);

                await Write(exceptionTemplate, exceptionFilePath);
            }

            // Xml Serialization
            if (codeModel.ShouldGenerateXmlSerialization)
            {
                var xmlSerializationTemplate = new XmlSerializationTemplate();
                var xmlSerializationPath     = Path.Combine(Settings.Instance.ModelsName,
                                                            $"{XmlSerialization.XmlDeserializationClass}{ImplementationFileExtension}");
                project.FilePaths.Add(xmlSerializationPath);

                await Write(xmlSerializationTemplate, xmlSerializationPath);
            }
        }
Ejemplo n.º 10
0
        protected virtual async Task GenerateClientSideCode(CodeModelCs codeModel)
        {
            // Service client
            var serviceClientTemplate = new ServiceClientTemplate {
                Model = codeModel
            };

            await Write(serviceClientTemplate, $"{codeModel.Name}{ImplementationFileExtension}");

            // Service client interface
            var serviceClientInterfaceTemplate = new ServiceClientInterfaceTemplate {
                Model = codeModel
            };

            await Write(serviceClientInterfaceTemplate, $"I{codeModel.Name}{ImplementationFileExtension}");

            // operations
            foreach (MethodGroupCs methodGroup in codeModel.Operations)
            {
                if (!methodGroup.Name.IsNullOrEmpty())
                {
                    // Operation
                    var operationsTemplate = new MethodGroupTemplate {
                        Model = methodGroup
                    };
                    await Write(operationsTemplate, $"{operationsTemplate.Model.TypeName}{ImplementationFileExtension}");

                    // Operation interface
                    var operationsInterfaceTemplate = new MethodGroupInterfaceTemplate {
                        Model = methodGroup
                    };
                    await Write(operationsInterfaceTemplate, $"I{operationsInterfaceTemplate.Model.TypeName}{ImplementationFileExtension}");
                }

                var operationExtensionsTemplate = new ExtensionsTemplate {
                    Model = methodGroup
                };
                await Write(operationExtensionsTemplate, $"{methodGroup.ExtensionTypeName}Extensions{ImplementationFileExtension}");
            }

            // Models
            foreach (CompositeTypeCs model in codeModel.ModelTypes.Union(codeModel.HeaderTypes))
            {
                if (model.Extensions.ContainsKey(SwaggerExtensions.ExternalExtension) &&
                    (bool)model.Extensions[SwaggerExtensions.ExternalExtension])
                {
                    continue;
                }

                var modelTemplate = new ModelTemplate {
                    Model = model
                };
                await Write(modelTemplate, Path.Combine(Settings.Instance.ModelsName, $"{model.Name}{ImplementationFileExtension}"));
            }

            // Enums
            foreach (EnumTypeCs enumType in codeModel.EnumTypes)
            {
                var enumTemplate = new EnumTemplate {
                    Model = enumType
                };
                await Write(enumTemplate, Path.Combine(Settings.Instance.ModelsName, $"{enumTemplate.Model.Name}{ImplementationFileExtension}"));
            }

            // Exceptions
            foreach (CompositeTypeCs exceptionType in codeModel.ErrorTypes)
            {
                var exceptionTemplate = new ExceptionTemplate {
                    Model = exceptionType,
                };
                await Write(exceptionTemplate, Path.Combine(Settings.Instance.ModelsName, $"{exceptionTemplate.Model.ExceptionTypeDefinitionName}{ImplementationFileExtension}"));
            }

            // Xml Serialization
            if (codeModel.ShouldGenerateXmlSerialization)
            {
                var xmlSerializationTemplate = new XmlSerializationTemplate();
                await Write(xmlSerializationTemplate, Path.Combine(Settings.Instance.ModelsName, $"{XmlSerialization.XmlDeserializationClass}{ImplementationFileExtension}"));
            }
        }