示例#1
0
        /// <summary>Generates the file.</summary>
        /// <param name="document">The document.</param>
        /// <param name="type">The type.</param>
        /// <returns>The code.</returns>
        protected string GenerateFile(SwaggerDocument document, ClientGeneratorOutputType type)
        {
            var clientCode    = string.Empty;
            var operations    = GetOperations(document);
            var clientClasses = new List <string>();

            if (BaseSettings.OperationNameGenerator.SupportsMultipleClients)
            {
                foreach (var controllerOperations in operations.GroupBy(o => o.ControllerName))
                {
                    var controllerName      = controllerOperations.Key;
                    var controllerClassName = BaseSettings.GenerateControllerName(controllerOperations.Key);
                    clientCode += GenerateClientClass(controllerName, controllerClassName, controllerOperations.ToList(), type) + "\n\n";
                    clientClasses.Add(controllerClassName);
                }
            }
            else
            {
                var controllerName      = string.Empty;
                var controllerClassName = BaseSettings.GenerateControllerName(controllerName);
                clientCode = GenerateClientClass(controllerName, controllerClassName, operations, type);
                clientClasses.Add(controllerClassName);
            }

            return(GenerateFile(clientCode, clientClasses, type)
                   .Replace("\r", string.Empty)
                   .Replace("\n\n\n\n", "\n\n")
                   .Replace("\n\n\n", "\n\n"));
        }
        /// <summary>Generates the client types.</summary>
        /// <returns>The code artifact collection.</returns>
        protected virtual IEnumerable <CodeArtifact> GenerateAllClientTypes()
        {
            var operations  = GetOperations(_document);
            var clientTypes = new List <CodeArtifact>();

            if (BaseSettings.OperationNameGenerator.SupportsMultipleClients)
            {
                var controllerOperationsGroups = operations.GroupBy(o => o.ControllerName).ToList();
                foreach (var controllerOperations in controllerOperationsGroups)
                {
                    var controllerName      = controllerOperations.Key;
                    var controllerClassName = BaseSettings.GenerateControllerName(controllerOperations.Key);
                    var clientType          = GenerateClientTypes(controllerName, controllerClassName, controllerOperations.ToList());
                    clientTypes.AddRange(clientType);
                }
            }
            else
            {
                var controllerName      = string.Empty;
                var controllerClassName = BaseSettings.GenerateControllerName(controllerName);
                var clientType          = GenerateClientTypes(controllerName, controllerClassName, operations);
                clientTypes.AddRange(clientType);
            }

            return(clientTypes);
        }
示例#3
0
        /// <summary>
        /// Identifies the clients.
        /// </summary>
        public void IdentifyClients()
        {
            Console.WriteLine($"Parsing swagger spec for '{Enum.GetName(typeof(Language), Language)}' code generation.");
            var operations = GetOperations();

            Clients = new List <BaseClientInfo>();

            Console.WriteLine($"--- Code generation mode : '{nameof(BaseSettings.OperationNameGenerator.SupportsMultipleClients)}:{(BaseSettings.OperationNameGenerator.SupportsMultipleClients? "TRUE":"FALSE")}' ---");

            if (BaseSettings.OperationNameGenerator.SupportsMultipleClients)
            {
                foreach (var controllerOperations in operations.Cast <CSharpOperationModel>().GroupBy(o => o.ControllerName))
                {
                    Console.WriteLine($"Involking code generation for '{controllerOperations.Key}'.");
                    Clients.Add(new CSharpClientInfo(clientClassName: controllerOperations.Key,
                                                     clientControllerName: BaseSettings.GenerateControllerName(controllerOperations.Key),
                                                     operations: controllerOperations.ToList())
                                );
                }
            }
            else
            {
                Console.WriteLine($"Involking code generation.");
                Clients.Add(new CSharpClientInfo(clientClassName: string.Empty,
                                                 clientControllerName: BaseSettings.GenerateControllerName(string.Empty),
                                                 operations: operations.Cast <CSharpOperationModel>().ToList()));
            }

            //return GenerateFile(clientCode, clientClasses, type)
            //    .Replace("\r", string.Empty)
            //    .Replace("\n\n\n\n", "\n\n")
            //    .Replace("\n\n\n", "\n\n");
        }