/// <summary> /// 生成生成器 /// </summary> /// <param name="path"></param> /// <returns></returns> private static CodeGenInfo CreateGenerationer(string path, EntityInfo entityInfo) { FileInfo file = new FileInfo(path); string workspace = file.DirectoryName; Encoding encoding = CodeFileHelper.GetFileEncoding(path); string content = File.ReadAllText(path, encoding); string backCodePath = path + "c"; string backCode = ""; if (File.Exists(backCodePath)) { encoding = CodeFileHelper.GetFileEncoding(backCodePath); backCode = File.ReadAllText(backCodePath, encoding); } //if (backCode == null) //{ // backCode = ""; //} ModelCompiler compiler = new ModelCompiler(content, backCode, entityInfo); string className = "ModelCompilerClass" + _classCount; StringBuilder sbError = new StringBuilder(); StringBuilder lastCodeCache = new StringBuilder(); CodeGenInfo info = compiler.GetCompileType(className, lastCodeCache, sbError); if (sbError.Length > 0) { CompileException ex = new CompileException("模版编译错误:\n" + sbError); ex.Code = lastCodeCache.ToString(); throw ex; } _classCount++; return(info); }
/// <summary> /// 生成DB声明 /// </summary> public void GenerateBQLEntityDB() { //FileInfo info = new FileInfo(EntityFileName); string dicPath = GenerateBasePath + "\\BQLEntity"; if (!Directory.Exists(dicPath)) { Directory.CreateDirectory(dicPath); } string fileName = dicPath + "\\" + DBName + ".cs"; string model = Models.BQLDB; List <string> codes = new List <string>(); using (StringReader reader = new StringReader(model)) { string tmp = null; while ((tmp = reader.ReadLine()) != null) { tmp = tmp.Replace("<%=BQLEntityNamespace%>", BQLEntityNamespace); tmp = tmp.Replace("<%=DBName%>", DBName); codes.Add(tmp); } } CodeFileHelper.SaveFile(fileName, codes); EnvDTE.ProjectItem newit = DesignerInfo.CurrentProject.ProjectItems.AddFromFile(fileName); newit.Properties.Item("BuildAction").Value = (int)BuildAction.Code; }
/// <summary> /// 填充类信息 /// </summary> private void FillClassInfo() { ClrType ctype = _classType; _className = ctype.Name; _baseType = GetBaseClass(ctype, out _baseTypeName); _fileName = GetFileName(ctype, out _cp); EnvDTE.ProjectItem classItem = GetProjectItemByFileName(DesignerInfo, _fileName); //foreach (CodeElementPosition cp in ctype.SourceCodePositions) //{ // if (cp.FileName.IndexOf(".extend.cs") <0) // { // _fileName = cp.FileName; // _cp = cp; // break; // } //} _namespace = ctype.OwnerNamespace.Name; _summary = ctype.DocSummary; _tableName = EntityFieldBase.ToCamelName(_className); _lstSource = CodeFileHelper.ReadFile(FileName); _fileEncoding = CodeFileHelper.GetFileEncoding(_fileName); if (ctype.Generic) { InitGeneric(ctype, _dicGenericInfo); } }
/// <summary> /// 生成数据层 /// </summary> /// <param name="entity"></param> public void GenerateDataAccess() { //FileInfo info = new FileInfo(EntityFileName); string dicPath = GenerateBasePath + "\\DataAccess"; if (!Directory.Exists(dicPath)) { Directory.CreateDirectory(dicPath); } string dal = Models.DataAccess; foreach (ComboBoxItem itype in DataAccessTypes) { if (!this.BbConfig.IsAllDal && !this.BbConfig.DbType.Equals(itype.Value)) { continue; } string type = itype.Value.ToString(); string dalPath = dicPath + "\\" + type; if (!Directory.Exists(dalPath)) { Directory.CreateDirectory(dalPath); } string fileName = dalPath + "\\" + ClassName + "DataAccess.cs"; if (File.Exists(fileName)) { continue; } List <string> codes = new List <string>(); using (StringReader reader = new StringReader(dal)) { string tmp = null; while ((tmp = reader.ReadLine()) != null) { tmp = tmp.Replace("<%=EntityNamespace%>", EntityNamespace); tmp = tmp.Replace("<%=Summary%>", Table.Description); tmp = tmp.Replace("<%=DataAccessNamespace%>", DataAccessNamespace); tmp = tmp.Replace("<%=DataBaseType%>", type); tmp = tmp.Replace("<%=ClassName%>", ClassName); codes.Add(tmp); } } dmt.AppendDal(DataAccessNamespace + "." + type + "." + ClassName + "DataAccess", DataAccessNamespace + ".IDataAccess.I" + ClassName + "DataAccess"); dmt.AppendBo(BusinessNamespace + "." + ClassName + "Business", EntityNamespace + "." + ClassName); CodeFileHelper.SaveFile(fileName, codes); EnvDTE.ProjectItem newit = DesignerInfo.CurrentProject.ProjectItems.AddFromFile(fileName); newit.Properties.Item("BuildAction").Value = (int)BuildAction.Code; } dmt.SaveXML(); }
/// <summary> /// 生成代码 /// </summary> /// <param name="code">代码行</param> /// <param name="tiers">层数</param> public void GreanCode(XmlDocument doc) { string model = Buffalo.DBTools.Models.Entity; string baseType = BaseType; List <string> codes = new List <string>(); using (StringReader reader = new StringReader(model)) { string tmp = null; while ((tmp = reader.ReadLine()) != null) { tmp = tmp.Replace("<%=EntityNamespace%>", EntityNamespace); tmp = tmp.Replace("<%=Summary%>", _belongTable.Description); tmp = tmp.Replace("<%=EntityBaseType%>", baseType); tmp = tmp.Replace("<%=ClassName%>", ClassName); tmp = tmp.Replace("<%=EntityFields%>", BildFields()); tmp = tmp.Replace("<%=EntityRelations%>", BildRelations()); tmp = tmp.Replace("<%=EntityContext%>", BuildContext()); codes.Add(tmp); } } string eFile = GetEntityFileName(FileName); CodeFileHelper.SaveFile(eFile, codes); EnvDTE.ProjectItem newit = DesignerInfo.CurrentProject.ProjectItems.AddFromFile(eFile); newit.Properties.Item("BuildAction").Value = (int)BuildAction.Code; GenerateExtendCode(); BQLEntityGenerater bqlEntity = new BQLEntityGenerater(this, DesignerInfo); GenerateExtendCode(); if (_currentDBConfigInfo.Tier == 3) { Generate3Tier g3t = new Generate3Tier(this, DesignerInfo); g3t.GenerateBusiness(); if (!string.IsNullOrEmpty(this._belongTable.Name)) { g3t.GenerateIDataAccess(); g3t.GenerateDataAccess(); g3t.GenerateBQLDataAccess(); } } bqlEntity.GenerateBQLEntityDB(); bqlEntity.GenerateBQLEntity(); EntityMappingConfig.SaveXML(this); SetToDiagram(doc); }
/// <summary> /// 生成扩展类代码文件 /// </summary> private void GenerateExtendCode() { FileInfo fileInfo = new FileInfo(FileName); string fileName = fileInfo.DirectoryName + "\\" + fileInfo.Name.Replace(".cs", ".extend.cs"); if (File.Exists(fileName)) { return; } string model = Models.UserEntity; List <string> codes = new List <string>(); using (StringReader reader = new StringReader(model)) { string tmp = null; while ((tmp = reader.ReadLine()) != null) { tmp = tmp.Replace("<%=EntityNamespace%>", Namespace); tmp = tmp.Replace("<%=Summary%>", _summary); string classFullName = null; if (ClassType.Generic) { classFullName = ClassType.GenericTypeName; } else { classFullName = ClassName; } tmp = tmp.Replace("<%=ClassFullName%>", classFullName); codes.Add(tmp); } } CodeFileHelper.SaveFile(fileName, codes); EnvDTE.ProjectItem classItem = GetProjectItemByFileName(DesignerInfo, _cp.FileName); //DTEHelper.AddFileToProjectItem(item, NHBFilePath, 3); EnvDTE.ProjectItem newit = classItem.ProjectItems.AddFromFile(fileName); //EnvDTE.ProjectItem newit = _currentProject.ProjectItems.AddFromFile(fileName); newit.Properties.Item("BuildAction").Value = (int)BuildAction.Code; }
/// <summary> /// 生成BQL数据层 /// </summary> public void GenerateBQLDataAccess() { FileInfo info = new FileInfo(ClassDesignerFileName); string dicPath = info.DirectoryName + "\\DataAccess"; if (!Directory.Exists(dicPath)) { Directory.CreateDirectory(dicPath); } string type = "Bql"; string idalPath = dicPath + "\\" + type; if (!Directory.Exists(idalPath)) { Directory.CreateDirectory(idalPath); } string fileName = idalPath + "\\" + ClassName + "DataAccess.cs"; if (File.Exists(fileName)) { return; } string idal = Models.BQLDataAccess; List <string> codes = new List <string>(); using (StringReader reader = new StringReader(idal)) { string tmp = null; while ((tmp = reader.ReadLine()) != null) { tmp = tmp.Replace("<%=EntityNamespace%>", EntityNamespace); tmp = tmp.Replace("<%=Summary%>", Table.Description); tmp = tmp.Replace("<%=DataAccessNamespace%>", DataAccessNamespace); tmp = tmp.Replace("<%=DataBaseType%>", type); tmp = tmp.Replace("<%=ClassName%>", ClassName); tmp = tmp.Replace("<%=BQLEntityNamespace%>", BQLEntityNamespace); codes.Add(tmp); } } CodeFileHelper.SaveFile(fileName, codes); EnvDTE.ProjectItem newit = DesignerInfo.CurrentProject.ProjectItems.AddFromFile(fileName); newit.Properties.Item("BuildAction").Value = (int)BuildAction.Code; }
/// <summary> /// 保存结果文件 /// </summary> /// <param name="fileName">文件名</param> /// <param name="selectedProject">选中的项目</param> /// <param name="content">文件内容</param> /// <param name="baction">生成文件的类型</param> /// <param name="parentItem"></param> /// <param name="encoding"></param> /// <returns></returns> private ProjectItem SaveItem(string fileName, Project selectedProject, string content, BuildAction baction, ProjectItem parentItem, Encoding encoding) { CodeFileHelper.SaveFile(fileName, content, encoding); EnvDTE.ProjectItem newit = null; if (parentItem != null) { newit = parentItem.ProjectItems.AddFromFile(fileName); newit.Properties.Item("BuildAction").Value = (int)baction; } else { newit = selectedProject.ProjectItems.AddFromFile(fileName); newit.Properties.Item("BuildAction").Value = (int)baction; } return(newit); }
/// <summary> /// 生成扩展类代码文件 /// </summary> private void GenerateExtendCode() { string eFileName = GetEntityFileName(FileName); FileInfo fileInfo = new FileInfo(eFileName); string fileName = fileInfo.DirectoryName + "\\" + fileInfo.Name.Replace(".cs", ".extend.cs"); if (File.Exists(fileName)) { return; } string model = Models.UserEntity; List <string> codes = new List <string>(); using (StringReader reader = new StringReader(model)) { string tmp = null; while ((tmp = reader.ReadLine()) != null) { tmp = tmp.Replace("<%=EntityNamespace%>", EntityNamespace); tmp = tmp.Replace("<%=Summary%>", this._belongTable.Description); string classFullName = ClassName; tmp = tmp.Replace("<%=ClassFullName%>", classFullName); codes.Add(tmp); } } CodeFileHelper.SaveFile(fileName, codes); EnvDTE.ProjectItem classItem = EntityConfig.GetProjectItemByFileName(DesignerInfo, eFileName); EnvDTE.ProjectItem newit = classItem.ProjectItems.AddFromFile(fileName); newit.Properties.Item("BuildAction").Value = (int)BuildAction.Code; //EnvDTE.ProjectItem newit = _currentProject.ProjectItems.AddFromFile(fileName); //newit.Properties.Item("BuildAction").Value = 1; }
/// <summary> /// 生成BQL实体 /// </summary> public void GenerateBQLEntity() { TagManager tag = new TagManager(); //FileInfo info = new FileInfo(EntityFileName); string dicPath = GenerateBasePath + "\\BQLEntity"; if (!Directory.Exists(dicPath)) { Directory.CreateDirectory(dicPath); } string fileName = dicPath + "\\" + ClassName + ".cs"; string idal = Models.BQLEntity; List <string> codes = new List <string>(); string baseType = null; if (EntityConfig.IsSystemTypeName(EntityBaseTypeName)) { baseType = "BQLEntityTableHandle"; } else { baseType = BaseNamespace + ".BQLEntity." + FormatClassName(EntityBaseTypeShortName); } using (StringReader reader = new StringReader(idal)) { string tmp = null; while ((tmp = reader.ReadLine()) != null) { if (tmp.StartsWith("<%#IF TableName%>")) { tag.AddTag("TableName"); } else if (tmp.StartsWith("<%#ENDIF%>")) { tag.PopTag(); } else { //if (tag.CurrentTag == "TableName" && string.IsNullOrEmpty(Table.TableName)) //{ // continue; //} tmp = tmp.Replace("<%=EntityNamespace%>", EntityNamespace); tmp = tmp.Replace("<%=BQLClassName%>", FormatClassName(ClassName)); tmp = tmp.Replace("<%=BQLEntityNamespace%>", BQLEntityNamespace); tmp = tmp.Replace("<%=Summary%>", Table.Description); tmp = tmp.Replace("<%=DBName%>", DBName); string args = GetBastGenericArgs(); string realbasetype = baseType; if (!string.IsNullOrEmpty(args)) { realbasetype += "<" + args + ">"; } tmp = tmp.Replace("<%=BQLEntityBaseType%>", realbasetype); tmp = tmp.Replace("<%=DataAccessNamespace%>", DataAccessNamespace); string className = ClassName; tmp = tmp.Replace("<%=ClassName%>", className); if (GenericInfo != null && GenericInfo.Count != 0) //有泛型 { StringBuilder generic = new StringBuilder(200); StringBuilder where = new StringBuilder(200); GetGeneric(generic, where); tmp = tmp.Replace("<%=Generic%>", generic.ToString()); tmp = tmp.Replace("<%=GenericWhere%>", where.ToString()); tmp = tmp.Replace("<%=HasGeneric%>", "<>"); } else { tmp = tmp.Replace("<%=Generic%>", ""); tmp = tmp.Replace("<%=GenericWhere%>", ""); tmp = tmp.Replace("<%=HasGeneric%>", ""); } string entityClassName = ClassName; tmp = tmp.Replace("<%=EntityClassName%>", entityClassName); tmp = tmp.Replace("<%=PropertyDetail%>", GenProperty()); tmp = tmp.Replace("<%=RelationDetail%>", GenRelation()); tmp = tmp.Replace("<%=PropertyInit%>", GenInit()); codes.Add(tmp); } } } CodeFileHelper.SaveFile(fileName, codes); EnvDTE.ProjectItem newit = DesignerInfo.CurrentProject.ProjectItems.AddFromFile(fileName); newit.Properties.Item("BuildAction").Value = (int)BuildAction.Code; }
/// <summary> /// 生成代码 /// </summary> public void GenerateCode() { //InitDBConfig(); List <string> lstTarget = new List <string>(_lstSource.Count); bool isUsing = true; Dictionary <string, bool> dicUsing = new Dictionary <string, bool>(); Dictionary <int, CodeElementPosition> dicNeedVirtual = NeedVirtual(); for (int i = 0; i < _lstSource.Count; i++) { string str = _lstSource[i]; if (i == _cp.StartLine - 1) { if (str.IndexOf("class") > 0) { if (str.IndexOf(" partial ") < 0) { str = str.Replace("class", "partial class"); } } lstTarget.Add(str); } else if (i == _cp.EndLine - 1) { string space = CutSpace(str) + " "; foreach (EntityParamField param in _eParamFields) { if (param.IsGenerate) { param.AddSource(lstTarget, space); } } foreach (EntityRelationItem relation in _eRelation) { if (relation.IsGenerate) { relation.AddSource(lstTarget, space); } } if (_dbParams != null) { foreach (EntityParam param in _dbParams) { StringBuilder sb = new StringBuilder(); DBEntityInfo.AppendFieldInfo(param, sb); lstTarget.Add(sb.ToString()); } } if (_dbRelations != null) { foreach (TableRelationAttribute er in _dbRelations) { StringBuilder sb = new StringBuilder(); DBEntityInfo.FillRelationsInfo(er, sb); lstTarget.Add(sb.ToString()); } } AddContext(lstTarget); lstTarget.Add(str); } else if (isUsing && str.IndexOf("namespace " + Namespace) >= 0) { AddSqlCommonUsing(dicUsing, lstTarget); lstTarget.Add(str); isUsing = false; } else { if (isUsing) { if (str.IndexOf("using ") >= 0) { dicUsing[str.Trim()] = true; } } if (dicNeedVirtual.ContainsKey(i + 1)) { str = VirtualProperty(str); } lstTarget.Add(str); } } CodeFileHelper.SaveFile(FileName, lstTarget); GenerateExtenCode(); }
/// <summary> /// 生成业务层 /// </summary> /// <param name="entity"></param> public void GenerateBusiness() { FileInfo info = new FileInfo(ClassDesignerFileName); string dicPath = info.DirectoryName + "\\Business"; if (!Directory.Exists(dicPath)) { Directory.CreateDirectory(dicPath); } string fileName = dicPath + "\\" + ClassName + "Business.cs"; if (File.Exists(fileName)) { return; } string model = Models.Business; string baseClass = null; string businessClassName = ClassName + "Business"; if (EntityConfig.IsSystemTypeName(EntityBaseTypeName)) { baseClass = "BusinessModelBase"; } else { baseClass = BaseNamespace + ".Business." + EntityBaseTypeShortName + "BusinessBase"; } List <string> codes = new List <string>(); TagManager tag = new TagManager(); using (StringReader reader = new StringReader(model)) { string tmp = null; while ((tmp = reader.ReadLine()) != null) { if (tmp.StartsWith("<%#IF TableName%>")) { tag.AddTag("TableName"); } else if (tmp.StartsWith("<%#ENDIF%>")) { tag.PopTag(); } else { if (tag.CurrentTag == "TableName" && string.IsNullOrEmpty(Table.TableName)) { continue; } tmp = tmp.Replace("<%=EntityNamespace%>", EntityNamespace); tmp = tmp.Replace("<%=Summary%>", Table.Description); tmp = tmp.Replace("<%=BusinessClassName%>", businessClassName); tmp = tmp.Replace("<%=ClassName%>", ClassName); tmp = tmp.Replace("<%=BusinessNamespace%>", BusinessNamespace); tmp = tmp.Replace("<%=BaseBusinessClass%>", baseClass); codes.Add(tmp); } } } CodeFileHelper.SaveFile(fileName, codes); EnvDTE.ProjectItem newit = DesignerInfo.CurrentProject.ProjectItems.AddFromFile(fileName); newit.Properties.Item("BuildAction").Value = (int)BuildAction.Code; }