Esempio n. 1
0
        public void GenerateOne(string nspace, string table)
        {
            var columns = _context.UserTableColumns.Where(x => x.TABLE_NAME == table);

            if (!columns.Any())
            {
                return;
            }
            var columnComments = _context.UserColumnComments.Where(x => x.TABLE_NAME == table).ToList();
            var tableComment   = _context.UserTableComments.FirstOrDefault(x => x.TABLE_NAME == table)?.COMMENTS;

            StringBuilder sb = new StringBuilder();

            foreach (var column in columns)
            {
                var comment = columnComments.FirstOrDefault(x => x.COLUMN_NAME == column.COLUMN_NAME)?.COMMENTS;

                sb.AppendLine(FieldTemplate.Replace("{{comment}}", comment)
                              .Replace("{{name}}", column.COLUMN_NAME)
                              .Replace("{{type}}", GetFieldTypeString(column)))
                .AppendLine();
            }

            var text = ClassTemplate.Replace("{{namespace}}", nspace)
                       .Replace("{{comment}}", tableComment)
                       .Replace("{{classname}}", table)
                       .Replace("{{fields}}", sb.ToString().TrimEnd());

            File.WriteAllText(Path.Combine(SavePath, table + ".cs"), text);
        }
Esempio n. 2
0
 public string Generate()
 {
     Init();
     return(ClassTemplate.Replace("__getvariabletype_content", GenerateGetVariableType())
            .Replace("__method_content", GenerateGetMethod())
            .Replace("__getvalue_content", GenerateGetValue())
            .Replace("__setvalue_content", GenerateSetValue())
            .Replace("__constructor_content", GenerateConstructor())
            .Replace("__methods_content", GenerateMethod())
            .Replace("__class", m_ScorpioClassName)
            .Replace("__fullname", m_FullName));
 }
Esempio n. 3
0
 public string Generate()
 {
     Init();
     return(ClassTemplate.Replace("__extensions_using", GetExtensionsUsing())
            .Replace("__reflect_content", GenerateReflectList())
            .Replace("__getvariabletype_content", GenerateGetVariableType())
            .Replace("__method_content", GenerateGetMethod())
            .Replace("__getvalue_content", GenerateGetValue())
            .Replace("__setvalue_content", GenerateSetValue())
            .Replace("__constructor_content", GenerateConstructor())
            .Replace("__methods_content", GenerateMethod())
            .Replace("__class", ScorpioClassName)
            .Replace("__fullname", FullName));
 }
        /// <summary>
        /// 生成代码文本
        /// </summary>
        /// <param name="enumInfo">枚举信息</param>
        /// <param name="namespacePfx">命名空间前辍</param>
        /// <returns>枚举名</returns>
        public string BuilderCodeText(EnumInfo enumInfo, string namespacePfx)
        {
            string name = $"{enumInfo.Code.FristUpper()}Enum";

            if (generatorNames.Contains(name))
            {
                return(name);
            }

            StringBuilder itemCode = new StringBuilder();

            for (int i = 0; i < enumInfo.Items.Length; i++)
            {
                EnumItem enumItem = enumInfo.Items[i];
                itemCode.Append(ItemTemplate
                                .Replace("|Description|", enumItem.Desc)
                                .Replace("|CODE|", enumItem.Code)
                                .Replace("|VALUE|", enumItem.Value.ToString()));

                if (i == enumInfo.Items.Length - 1)
                {
                    continue;
                }

                itemCode.AppendLine();
                itemCode.AppendLine();
            }

            string desc     = string.IsNullOrWhiteSpace(enumInfo.Desc) ? name : enumInfo.Desc;
            string codeText = ClassTemplate
                              .Replace("|NamespacePfx|", namespacePfx)
                              .Replace("|Description|", desc)
                              .Replace("|Name|", name)
                              .Replace("|Item|", itemCode.ToString());

            FOLDER_ROOT.CreateNotExistsDirectory();

            $"{FOLDER_ROOT}{name}.cs".WriteFileContent(codeText);

            generatorNames.Add(name);

            return(name);
        }
Esempio n. 5
0
        public static string Generate(Type nbRefType)
        {
            var dicTypeRefs = NbRefFieldValue.GetAllNbRefFieldStrings(nbRefType);

            StringBuilder sb = new StringBuilder();

            foreach (var dicTypeRef in dicTypeRefs)
            {
                sb.AppendLine();
                var filedTemplate = FiledTemplate
                                    .Replace(Holder_NbRefType, nbRefType.Name)
                                    .Replace(Holder_NbRefFieldName, dicTypeRef.FieldName)
                                    .Replace(Holder_NbRefFieldValue, dicTypeRef.FieldValue)
                                    .Replace(Holder_NbRefFieldDesc, dicTypeRef.Description);
                sb.Append(filedTemplate);
                sb.AppendLine();
            }

            var classTemplate = ClassTemplate
                                .Replace(Holder_NbRefType, nbRefType.Name)
                                .Replace(Holder_Fileds, sb.ToString());

            return(classTemplate);
        }
        /// <summary>
        /// 生成代码文本集合
        /// </summary>
        /// <param name="table">表名</param>
        /// <param name="namespacePfx">命名空间前辍</param>
        /// <param name="type">类型</param>
        /// <param name="fileNames">文件名集合</param>
        /// <returns>代码文本集合</returns>
        protected override string[] BuilderCodeTexts(TableInfo table, string namespacePfx, string type, out string[] fileNames)
        {
            string name        = $"{table.Name.FristUpper()}Info";
            string parentClass = "UserInfo".Equals(name) ? "BasicUserInfo" : "PersonTimeInfo";

            EnumGenerator enumGenerator = new EnumGenerator();

            fileNames = new string[] { $"{name}.cs" };
            StringBuilder propCode = new StringBuilder();

            ITypeMapperService typeMapper = SimpleFactory.Create(type);

            if (!table.Columns.IsNullOrCount0())
            {
                for (int i = 0; i < table.Columns.Count; i++)
                {
                    ColumnInfo c        = table.Columns[i];
                    string     propName = c.Name.FristUpper();
                    if (IGNORE_PROP_NAMES.Contains(propName))
                    {
                        continue;
                    }

                    if ("UserInfo".Equals(name))
                    {
                        if (USER_IGNORE_PROP_NAMES.Contains(propName))
                        {
                            continue;
                        }
                    }

                    string      propType = typeMapper.GetPropertyType(c);
                    CommentInfo comment  = null;
                    if (!string.IsNullOrWhiteSpace(c.Description))
                    {
                        try
                        {
                            comment = JsonUtil.Deserialize <CommentInfo>(c.Description);
                            if (comment != null && comment.Enum != null)
                            {
                                propType = enumGenerator.BuilderCodeText(comment.Enum, namespacePfx);
                            }
                        }
                        catch { }
                    }

                    string        commentDesc = null;
                    StringBuilder attrCode    = new StringBuilder();
                    if (!c.IsNull)
                    {
                        attrCode.Append(GetAttrCode("Required"));
                        attrCode.AppendLine();
                    }
                    if (c.Length != null && "string".Equals(propType))
                    {
                        attrCode.Append(GetAttrCode(string.Format("MaxLength({0})", c.Length)));
                        attrCode.AppendLine();
                    }
                    if (comment != null)
                    {
                        if (string.IsNullOrWhiteSpace(comment.Desc))
                        {
                            commentDesc = c.Description;
                        }
                        else
                        {
                            commentDesc = comment.Desc;
                        }

                        if (!string.IsNullOrWhiteSpace(comment.Name))
                        {
                            attrCode.Append(GetAttrCode(string.Format("DisplayName(\"{0}\")", comment.Name)));
                            attrCode.AppendLine();
                        }

                        if (comment.MinLength != null && "string".Equals(propType))
                        {
                            attrCode.Append(GetAttrCode(string.Format("MinLength({0})", comment.MinLength)));
                            attrCode.AppendLine();
                        }

                        if (comment.Range != null && comment.Range.Length == 2)
                        {
                            attrCode.Append(GetAttrCode(string.Format("Range({0}, {1})", comment.Range[0], comment.Range[1])));
                            attrCode.AppendLine();
                        }
                    }
                    else
                    {
                        commentDesc = c.Description;
                    }

                    if (string.IsNullOrWhiteSpace(commentDesc))
                    {
                        commentDesc = propName;
                    }

                    propCode.Append(PropertyTemplate
                                    .Replace("|Description|", commentDesc)
                                    .Replace("|JsonName|", c.Name.FristLower())
                                    .Replace("|Attribute|", attrCode.ToString())
                                    .Replace("|Type|", propType)
                                    .Replace("|Name|", propName)
                                    .Replace("|Order|", (i + 1).ToString()));

                    if (i == table.Columns.Count - 1)
                    {
                        continue;
                    }

                    propCode.AppendLine();
                    propCode.AppendLine();
                }
            }

            var desc = string.IsNullOrWhiteSpace(table.Description) ? name : table.Description;

            return(new string[]
            {
                ClassTemplate
                .Replace("|NamespacePfx|", namespacePfx)
                .Replace("|Description|", desc)
                .Replace("|Name|", name)
                .Replace("|Inherit|", parentClass)
                .Replace("|Property|", propCode.ToString())
            });
        }