private void CreateTypeFile(DLLTypeInfo dllTypeInfoModels, List <DLLTypeInfo> DLLTypeInfoCSSPModelsList)
        {
            StringBuilder sb = new StringBuilder();
            List <string> fileNameUsedList = new List <string>();
            List <string> removeTypeList   = new List <string>()
            {
                "Int32", "Int16", "Int64", "Single", "Float", "Double", "String", "DateTime", "Boolean"
            };
            bool          IsExtra     = false;;
            string        LastLetter  = "";
            string        BaseClass   = "";
            string        ParentClass = "";
            List <string> PropToSkip  = new List <string>();

            if (dllTypeInfoModels.Name == "AppTaskParameter")
            {
                int seilfj = 34;
            }

            sb.AppendLine(@"/*");
            sb.AppendLine(@" * Auto generated from the CSSPWebToolsAngCodeWriter.proj by clicking on the [AngularInterfacesGenerate.cs] button");
            sb.AppendLine(@" *");
            sb.AppendLine(@" * Do not edit this file.");
            sb.AppendLine(@" *");
            sb.AppendLine(@" */");
            sb.AppendLine(@"");

            if (dllTypeInfoModels.Name == "CSSPError")
            {
                sb.AppendLine($"import {{ ValidationResult }} from './validationresult.interface';");
            }
            else if (dllTypeInfoModels.Name == "LastUpdate")
            {
                sb.AppendLine($"import {{ CSSPError }} from './cssperror.interface';");
            }
            else
            {
                if (!dllTypeInfoModels.HasNotMappedAttribute)
                {
                    sb.AppendLine($"import {{ LastUpdate }} from './lastupdate.interface';");
                }
                else
                {
                    if (dllTypeInfoModels.Name.Length > 5)
                    {
                        IsExtra = dllTypeInfoModels.Name.Substring(dllTypeInfoModels.Name.Length - 6).StartsWith("Extra");

                        if (!IsExtra)
                        {
                            sb.AppendLine($"import {{ CSSPError }} from './cssperror.interface';");
                        }
                    }
                    else
                    {
                        sb.AppendLine($"import {{ CSSPError }} from './cssperror.interface';");
                    }
                }
            }

            if (dllTypeInfoModels.Name.Length > 6)
            {
                IsExtra = dllTypeInfoModels.Name.Substring(dllTypeInfoModels.Name.Length - 6).StartsWith("Extra");
                if (IsExtra)
                {
                    LastLetter = dllTypeInfoModels.Name.Substring(dllTypeInfoModels.Name.Length - 1);
                    BaseClass  = dllTypeInfoModels.Name.Substring(0, dllTypeInfoModels.Name.Length - 6);
                    switch (LastLetter)
                    {
                    case "A":
                    {
                        ParentClass = BaseClass;
                        fileNameUsedList.Add(ParentClass.ToLower());
                        sb.AppendLine($"import {{ { ParentClass } }} from './{ ParentClass.ToLower() }.interface';");
                    }
                    break;

                    case "B":
                    {
                        ParentClass = BaseClass + "ExtraA";
                        fileNameUsedList.Add(ParentClass.ToLower());
                        sb.AppendLine($"import {{ { ParentClass } }} from './{ ParentClass.ToLower() }.interface';");
                    }
                    break;

                    case "C":
                    {
                        ParentClass = BaseClass + "ExtraB";
                        fileNameUsedList.Add(ParentClass.ToLower());
                        sb.AppendLine($"import {{ { ParentClass } }} from './{ ParentClass.ToLower() }.interface';");
                    }
                    break;

                    case "D":
                    {
                        ParentClass = BaseClass + "ExtraC";
                        fileNameUsedList.Add(ParentClass.ToLower());
                        sb.AppendLine($"import {{ { ParentClass } }} from './{ ParentClass.ToLower() }.interface';");
                    }
                    break;

                    case "E":
                    {
                        ParentClass = BaseClass + "ExtraD";
                        fileNameUsedList.Add(ParentClass.ToLower());
                        sb.AppendLine($"import {{ { ParentClass } }} from './{ ParentClass.ToLower() }.interface';");
                    }
                    break;

                    case "F":
                    {
                        ParentClass = BaseClass + "ExtraE";
                        fileNameUsedList.Add(ParentClass.ToLower());
                        sb.AppendLine($"import {{ { ParentClass } }} from './{ ParentClass.ToLower() }.interface';");
                    }
                    break;

                    default:
                        break;
                    }

                    foreach (DLLTypeInfo dllTypeInfoModels2 in DLLTypeInfoCSSPModelsList)
                    {
                        if (dllTypeInfoModels2.Name == ParentClass)
                        {
                            foreach (DLLPropertyInfo dllPropertyInfo in dllTypeInfoModels2.PropertyInfoList.OrderBy(c => c.CSSPProp.PropName))
                            {
                                PropToSkip.Add(dllPropertyInfo.CSSPProp.PropName);
                            }
                        }
                    }
                }
                sb.AppendLine(@"");
            }

            foreach (DLLPropertyInfo dllPropertyInfo in dllTypeInfoModels.PropertyInfoList.OrderBy(c => c.CSSPProp.PropName))
            {
                string fileName = dllPropertyInfo.CSSPProp.PropType.ToLower();
                if (dllPropertyInfo.CSSPProp.HasCSSPEnumTypeAttribute)
                {
                    fileName = fileName.Replace("enum", ".enum");
                    if (!fileNameUsedList.Contains(fileName))
                    {
                        fileNameUsedList.Add(fileName);
                        if (PropToSkip.Contains(dllPropertyInfo.CSSPProp.PropName))
                        {
                            continue;
                        }
                        sb.AppendLine($"import {{ { dllPropertyInfo.CSSPProp.PropType } }} from '../../enums/generated/{ fileName }';");
                    }
                }

                if (!fileNameUsedList.Contains(fileName))
                {
                    if (!(dllPropertyInfo.CSSPProp.PropName == "ValidationResults" || dllPropertyInfo.CSSPProp.PropName == "HasErrors"))
                    {
                        if (!removeTypeList.Contains(dllPropertyInfo.CSSPProp.PropType))
                        {
                            fileNameUsedList.Add(fileName);
                            if (PropToSkip.Contains(dllPropertyInfo.CSSPProp.PropName))
                            {
                                continue;
                            }
                            if (dllPropertyInfo.CSSPProp.IsList)
                            {
                                sb.AppendLine($"import {{ { dllPropertyInfo.CSSPProp.PropType } }} from './{ fileName }.interface';");
                            }
                            else
                            {
                                sb.AppendLine($"import {{ { dllPropertyInfo.CSSPProp.PropType } }} from './{ fileName }.interface';");
                            }
                        }
                    }
                }
            }
            sb.AppendLine(@"");

            string MappedText = "";

            if (dllTypeInfoModels.HasNotMappedAttribute)
            {
                if (IsExtra)
                {
                    MappedText = $" extends { ParentClass }";
                }
                else
                {
                    MappedText = " extends CSSPError";
                }
            }
            else
            {
                if (dllTypeInfoModels.Type.Name == "LastUpdate")
                {
                    MappedText = " extends CSSPError";
                }
                else if (dllTypeInfoModels.Type.Name == "CSSPError")
                {
                    MappedText = "";
                }
                else
                {
                    if (IsExtra)
                    {
                        MappedText = $" extends { ParentClass }";
                    }
                    else
                    {
                        MappedText = " extends LastUpdate";
                    }
                }
            }
            sb.AppendLine($@"export interface { dllTypeInfoModels.Name }{ MappedText } {{");

            foreach (DLLPropertyInfo dllPropertyInfo in dllTypeInfoModels.PropertyInfoList.OrderBy(c => c.CSSPProp.PropName))
            {
                if (PropToSkip.Contains(dllPropertyInfo.CSSPProp.PropName))
                {
                    continue;
                }

                if (dllTypeInfoModels.Name != "CSSPError")
                {
                    if (dllPropertyInfo.CSSPProp.PropName == "HasErrors" || dllPropertyInfo.CSSPProp.PropName == "ValidationResults")
                    {
                        continue;
                    }
                }

                string IsNull = "";
                if (dllPropertyInfo.CSSPProp.IsNullable)
                {
                    IsNull = "?";
                }

                string typeText = "";
                switch (dllPropertyInfo.CSSPProp.PropType)
                {
                case "Int32":
                case "Int16":
                case "Int64":
                case "Single":
                case "Float":
                case "Double":
                {
                    typeText = "number";
                }
                break;

                case "String":
                {
                    typeText = "string";
                    IsNull   = "";
                }
                break;

                case "DateTime":
                {
                    typeText = "Date";
                }
                break;

                case "Boolean":
                {
                    typeText = "boolean";
                }
                break;

                default:
                {
                    if (dllPropertyInfo.CSSPProp.PropName == "ValidationResults")
                    {
                        typeText = "ValidationResult";
                    }
                    else
                    {
                        typeText = dllPropertyInfo.CSSPProp.PropType;
                    }
                }
                break;
                }
                if (dllPropertyInfo.CSSPProp.IsList)
                {
                    sb.AppendLine($@"    { dllPropertyInfo.CSSPProp.PropName }{ IsNull }: { typeText }[];");
                }
                else
                {
                    sb.AppendLine($@"    { dllPropertyInfo.CSSPProp.PropName }{ IsNull }: { typeText };");
                }
            }

            sb.AppendLine(@"}");

            FileInfo fiOutputGen = new FileInfo($@"C:\CSSPCode\CSSPWebToolsAng\src\app\interfaces\generated\{ dllTypeInfoModels.Name.ToLower() }.interface.ts");

            using (StreamWriter sw2 = fiOutputGen.CreateText())
            {
                sw2.Write(sb.ToString());
                StatusPermanentEvent(new StatusEventArgs($"Created [{ dllTypeInfoModels.Name.ToLower() }.interface.ts]"));
            }
        }
Ejemplo n.º 2
0
        public bool FillDLLTypeInfoList(FileInfo fiDLL, List <DLLTypeInfo> DLLTypeInfoList)
        {
            var         importAssembly = Assembly.LoadFile(fiDLL.FullName);
            List <Type> typeList       = importAssembly.GetTypes().ToList();

            int count = 0;

            foreach (Type type in typeList)
            {
                if (type.Name.StartsWith("<") || type.FullName.StartsWith("CSSPWebAPI.Models"))
                {
                    continue;
                }

                DLLTypeInfo dllTypeInfo = new DLLTypeInfo();
                dllTypeInfo.Type   = type;
                dllTypeInfo.Name   = type.Name;
                dllTypeInfo.IsEnum = false;
                if (type.GetTypeInfo().BaseType == typeof(System.Enum))
                {
                    dllTypeInfo.IsEnum = true;
                }

                dllTypeInfo.HasNotMappedAttribute = type.CustomAttributes.Where(c => c.AttributeType.Name == "NotMappedAttribute").Any();

                foreach (FieldInfo fieldInfo in type.GetFields())
                {
                    if (!fieldInfo.FieldType.Name.EndsWith("Enum"))
                    {
                        continue;
                    }

                    DLLFieldInfo dllFieldInfo = new DLLFieldInfo();
                    dllFieldInfo.FieldInfo = fieldInfo;
                    dllFieldInfo.Name      = fieldInfo.Name;

                    dllTypeInfo.FieldInfoList.Add(dllFieldInfo);
                }

                foreach (PropertyInfo propertyInfo in type.GetProperties())
                {
                    bool CanReadPropertyType = false;
                    try
                    {
                        var PropertyType = propertyInfo.PropertyType;
                        CanReadPropertyType = true;
                    }
                    catch (Exception)
                    {
                        // not used
                    }
                    if (!CanReadPropertyType)
                    {
                        continue;
                    }
                    if (propertyInfo.PropertyType.Name.StartsWith("<"))
                    {
                        continue;
                    }

                    count += 1;
                    if (count % 200 == 0)
                    {
                        StatusTempEvent(new StatusEventArgs($"Reading type file [{ fiDLL.Name }] [{ type.Name }] [{ propertyInfo.Name }]"));
                    }

                    DLLPropertyInfo dllPropertyInfo = new DLLPropertyInfo();
                    dllPropertyInfo.PropertyInfo = propertyInfo;

                    CSSPProp csspProp = new CSSPProp();
                    if (!FillCSSPProp(propertyInfo, csspProp, type))
                    {
                        CSSPErrorEvent(new CSSPErrorEventArgs("CSSPError while filling CSSPProp"));
                        return(false);
                    }
                    dllPropertyInfo.CSSPProp = csspProp;

                    dllTypeInfo.PropertyInfoList.Add(dllPropertyInfo);
                }

                foreach (MethodInfo methodInfo in type.GetMethods())
                {
                    if (methodInfo.Name.StartsWith("<"))
                    {
                        continue;
                    }

                    count += 1;
                    if (count % 200 == 0)
                    {
                        StatusTempEvent(new StatusEventArgs($"Reading type file [{ fiDLL.Name }] [{ type.Name }] [{ methodInfo.Name }]"));
                    }

                    if (methodInfo.DeclaringType.FullName.StartsWith("CSSP"))
                    {
                        DLLMethodInfo dllMethodInfo = new DLLMethodInfo();
                        dllMethodInfo.MethodInfo    = methodInfo;
                        dllMethodInfo.Name          = methodInfo.Name;
                        dllMethodInfo.DeclaringType = methodInfo.DeclaringType;

                        string DeclaringTypeName = methodInfo.DeclaringType.FullName;
                        if (DeclaringTypeName.StartsWith("System.Nullable"))
                        {
                            DeclaringTypeName = DeclaringTypeName.Substring(DeclaringTypeName.IndexOf("[[") + 2);
                            DeclaringTypeName = DeclaringTypeName.Substring(DeclaringTypeName.IndexOf(".") + 1);
                            DeclaringTypeName = DeclaringTypeName.Substring(0, DeclaringTypeName.IndexOf(","));
                        }

                        dllMethodInfo.DeclaringTypeName = DeclaringTypeName;

                        try
                        {
                            foreach (ParameterInfo parameterInfo in methodInfo.GetParameters())
                            {
                                DLLParameterInfo dllParameterInfo = new DLLParameterInfo();
                                dllParameterInfo.ParameterInfo = parameterInfo;
                                dllParameterInfo.Name          = parameterInfo.Name;

                                dllMethodInfo.ParameterInfoList.Add(dllParameterInfo);
                            }
                        }
                        catch (Exception)
                        {
                            // nothing
                        }

                        dllTypeInfo.MethodInfoList.Add(dllMethodInfo);
                    }
                }

                DLLTypeInfoList.Add(dllTypeInfo);
            }


            return(false);
        }