Esempio n. 1
0
        public void SaveToCSharp(QlikApiConfig config, List <IEngineObject> engineObjects, string savePath)
        {
            try
            {
                var enumList    = new List <string>();
                var fileContent = new StringBuilder();
                fileContent.Append($"namespace {config.NamespaceName}");
                fileContent.AppendLine();
                fileContent.AppendLine("{");
                fileContent.AppendLine(QlikApiUtils.Indented("#region Usings", 1));
                fileContent.AppendLine(QlikApiUtils.Indented("using System;", 1));
                fileContent.AppendLine(QlikApiUtils.Indented("using System.ComponentModel;", 1));
                fileContent.AppendLine(QlikApiUtils.Indented("using System.Collections.Generic;", 1));
                fileContent.AppendLine(QlikApiUtils.Indented("using Newtonsoft.Json;", 1));
                fileContent.AppendLine(QlikApiUtils.Indented("using Newtonsoft.Json.Linq;", 1));
                fileContent.AppendLine(QlikApiUtils.Indented("using System.Threading.Tasks;", 1));
                fileContent.AppendLine(QlikApiUtils.Indented("using System.Threading;", 1));
                fileContent.AppendLine(QlikApiUtils.Indented("using enigma;", 1));
                fileContent.AppendLine(QlikApiUtils.Indented("#endregion", 1));
                fileContent.AppendLine();
                var lineCounter = 0;
                var enumObjects = engineObjects.Where(d => d.EngType == EngineType.ENUM).ToList();
                if (enumObjects.Count > 0)
                {
                    fileContent.AppendLine(QlikApiUtils.Indented("#region Enums", 1));
                    logger.Debug($"Write Enums {enumObjects.Count}");
                    foreach (EngineEnum enumValue in enumObjects)
                    {
                        lineCounter++;
                        var enumResult = GetFormatedEnumBlock(enumValue);
                        fileContent.AppendLine(enumResult);
                        if (lineCounter < enumObjects.Count)
                        {
                            fileContent.AppendLine();
                        }
                    }
                    fileContent.AppendLine(QlikApiUtils.Indented("#endregion", 1));
                    fileContent.AppendLine();
                }
                var interfaceObjects = engineObjects.Where(d => d.EngType == EngineType.INTERFACE).ToList();
                if (interfaceObjects.Count > 0)
                {
                    logger.Debug($"Write Interfaces {interfaceObjects.Count}");
                    fileContent.AppendLine(QlikApiUtils.Indented("#region Interfaces", 1));
                    lineCounter = 0;
                    foreach (EngineInterface interfaceObject in interfaceObjects)
                    {
                        lineCounter++;

                        if (interfaceObject.Name == "IObjectInterface")
                        {
                            continue;
                        }
                        // TODO fix that simple workaround to remove the ObjectInterface

                        //Special for ObjectInterface => Add IObjectInterface
                        var implInterface = String.Empty;
                        if (Config.BaseObjectInterfaceName != interfaceObject.Name)
                        {
                            implInterface = $" : {Config.BaseObjectInterfaceName}";
                        }

                        fileContent.AppendLine(QlikApiUtils.Indented($"public interface {interfaceObject.Name}{implInterface}", 1));
                        fileContent.AppendLine(QlikApiUtils.Indented("{", 1));
                        foreach (var property in interfaceObject.Properties)
                        {
                            fileContent.AppendLine(QlikApiUtils.Indented($"{QlikApiUtils.GetDotNetType(property.Type)} {property.Name} {{ get; set; }}", 2));
                        }
                        foreach (var methodObject in interfaceObject.Methods)
                        {
                            fileContent.AppendLine(GetFormatedMethod(methodObject));
                        }

                        if (Config.BaseObjectInterfaceName == interfaceObject.Name)
                        {
                            fileContent.AppendLine(QlikApiUtils.Indented("event EventHandler Changed;", 2));
                            fileContent.AppendLine(QlikApiUtils.Indented("event EventHandler Closed;", 2));
                            fileContent.AppendLine(QlikApiUtils.Indented("void OnChanged();", 2));
                        }

                        fileContent.AppendLine(QlikApiUtils.Indented("}", 1));
                        if (lineCounter < interfaceObjects.Count)
                        {
                            fileContent.AppendLine();
                        }
                    }
                    fileContent.AppendLine(QlikApiUtils.Indented("#endregion", 2));
                    fileContent.AppendLine();
                }
                var classObjects = engineObjects.Where(d => d.EngType == EngineType.CLASS).ToList();
                if (classObjects.Count > 0)
                {
                    fileContent.AppendLine(QlikApiUtils.Indented("#region Classes", 1));
                    logger.Debug($"Write Classes {classObjects.Count}");
                    lineCounter = 0;
                    var descBuilder = new DescritpionBuilder(Config.UseDescription);
                    foreach (EngineClass classObject in classObjects)
                    {
                        lineCounter++;
                        descBuilder.Summary = classObject.Description;
                        descBuilder.SeeAlso = classObject.SeeAlso;
                        var desc = descBuilder.Generate(1);
                        if (!String.IsNullOrEmpty(desc))
                        {
                            fileContent.AppendLine(desc);
                        }
                        fileContent.AppendLine(QlikApiUtils.Indented($"public class {classObject.Name}<###implements###>", 1));
                        fileContent.AppendLine(QlikApiUtils.Indented("{", 1));
                        if (classObject.Properties.Count > 0)
                        {
                            fileContent.AppendLine(QlikApiUtils.Indented("#region Properties", 2));
                            var propertyCount = 0;
                            foreach (var property in classObject.Properties)
                            {
                                propertyCount++;
                                if (!String.IsNullOrEmpty(property.Description))
                                {
                                    var builder = new DescritpionBuilder(Config.UseDescription)
                                    {
                                        Summary = property.Description,
                                    };
                                    var description = builder.Generate(2);
                                    if (!String.IsNullOrEmpty(description))
                                    {
                                        fileContent.AppendLine(description);
                                    }
                                }

                                var dValue = String.Empty;
                                if (property.Default != null)
                                {
                                    dValue = property.Default.ToLowerInvariant();
                                    if (property.IsEnumType)
                                    {
                                        dValue = GetEnumDefault(property.Type, property.Default);
                                    }
                                    fileContent.AppendLine(QlikApiUtils.Indented($"[DefaultValue({dValue})]", 2));
                                }

                                var implements = String.Empty;
                                var refType    = property.GetRefType();
                                if (classObject.Type == "array")
                                {
                                    implements = GetImplemention(property);
                                    fileContent.Replace("<###implements###>", implements);
                                }
                                else if (property.Type == "array")
                                {
                                    fileContent.AppendLine(QlikApiUtils.Indented($"public List<{QlikApiUtils.GetDotNetType(refType)}> {property.Name} {{ get; set; }}", 2));
                                }
                                else
                                {
                                    var resultType = QlikApiUtils.GetDotNetType(property.Type);
                                    if (!String.IsNullOrEmpty(refType))
                                    {
                                        resultType = refType;
                                    }
                                    var propertyText = QlikApiUtils.Indented($"public {resultType} {property.Name} {{ get; set; }}", 2);
                                    if (property.Default != null)
                                    {
                                        propertyText += $" = {dValue};";
                                    }
                                    fileContent.AppendLine(propertyText);
                                }

                                if (propertyCount < classObject.Properties.Count)
                                {
                                    fileContent.AppendLine();
                                }
                            }
                            fileContent.AppendLine(QlikApiUtils.Indented("#endregion", 2));
                        }
                        fileContent.Replace("<###implements###>", "");
                        fileContent.AppendLine(QlikApiUtils.Indented("}", 1));
                        if (lineCounter < classObjects.Count)
                        {
                            fileContent.AppendLine();
                        }
                    }
                    fileContent.AppendLine(QlikApiUtils.Indented("#endregion", 1));
                }
                fileContent.AppendLine("}");
                var content = fileContent.ToString().Trim();
                File.WriteAllText(savePath, content);
            }
            catch (Exception ex)
            {
                logger.Error(ex, $"The .NET file could not create.");
            }
        }
Esempio n. 2
0
        private string GetFormatedMethod(EngineMethod method)
        {
            var response    = method.Responses.FirstOrDefault() ?? null;
            var descBuilder = new DescritpionBuilder(Config.UseDescription)
            {
                Summary = method.Description,
                SeeAlso = method.SeeAlso,
                Param   = method.Parameters,
            };

            if (response != null)
            {
                descBuilder.Return = response.Description;
            }
            var description = descBuilder.Generate(2);
            var returnType  = "Task";

            if (response != null)
            {
                returnType = $"Task<{QlikApiUtils.GetDotNetType(response.GetRealType())}>";
                var serviceType = response.GetServiceType();
                if (serviceType != null)
                {
                    returnType = $"Task<{serviceType}>";
                }

                if (method?.Responses?.Count > 1 || !Config.UseQlikResponseLogic)
                {
                    logger.Debug($"The method {method?.Name} has {method?.Responses?.Count} responses.");
                    var resultClass = method.GetMultipleClass();
                    EngineObjects.Add(resultClass);
                    returnType = $"Task<{resultClass.Name}>";
                }
            }
            if (method.UseGeneric)
            {
                returnType = "Task<T>";
            }
            var methodBuilder = new StringBuilder();

            if (!String.IsNullOrEmpty(description))
            {
                methodBuilder.AppendLine(description);
            }
            var asyncValue = String.Empty;

            switch (Config.AsyncMode)
            {
            case AsyncMode.NONE:
                asyncValue = String.Empty;
                break;

            case AsyncMode.SHOW:
                asyncValue = "Async";
                break;

            default:
                asyncValue = String.Empty;
                break;
            }
            var cancellationToken = String.Empty;
            var parameter         = new StringBuilder();

            if (method.Parameters.Count > 0)
            {
                //Sort parameters by required
                var parameters = method.Parameters.OrderBy(p => p.Required == false);
                foreach (var para in parameters)
                {
                    var defaultValue = String.Empty;
                    if (!para.Required)
                    {
                        defaultValue = $" = {QlikApiUtils.GetDefaultValue(para.Type, para.Default)}";
                    }
                    parameter.Append($"{QlikApiUtils.GetDotNetType(para.Type)} {para.Name}{defaultValue}, ");
                }
            }
            var parameterValue = parameter.ToString().TrimEnd().TrimEnd(',');

            if (Config.GenerateCancelationToken)
            {
                if (String.IsNullOrEmpty(parameterValue.Trim()))
                {
                    cancellationToken = "CancellationToken? token = null";
                }
                else
                {
                    cancellationToken = ", CancellationToken? token = null";
                }
            }
            if (method.Deprecated)
            {
                methodBuilder.AppendLine(QlikApiUtils.Indented("[ObsoleteAttribute]", 2));
            }
            var tvalue = String.Empty;

            if (method.UseGeneric)
            {
                tvalue = "<T>";
            }
            methodBuilder.AppendLine(QlikApiUtils.Indented($"{returnType} {method.Name}{asyncValue}{tvalue}({parameterValue}{cancellationToken});", 2));
            return(methodBuilder.ToString());
        }