public MainWindow(DTE2 _dte, Setting setting) { InitializeComponent(); _setting = setting; projectHelper = new ProjectHelper(_dte); DtoFileModel dto = projectHelper.GetDtoModel(); foreach (var item in dto.ClassPropertys) { if ("Id".Equals(item.Name) || (dto.Name + "Id").Equals(item.Name)) { ClassKeyType.Text = item.PropertyType; continue; } DataList.Add(new DtoPropertyInfo { PropertyName = item.Name, PropertyType = item.PropertyType, IsEdit = true, IsList = true, Local = item.Name }); } PropertyGrid.ItemsSource = DataList; }
private void CreateDtoFiles(Document document, string name) { var parentItem = document.ProjectItem.Collection.Parent as ProjectItem; var dtoFolder = parentItem.ProjectItems.Cast <ProjectItem>().FirstOrDefault(item => item.Name == "Dto"); if (dtoFolder == null) { dtoFolder = parentItem.ProjectItems.AddFolder("Dto"); } string nameSpace = GetNamespace(document.ProjectItem); foreach (var str in new[] { "Input", "Output" }) { var model = new DtoFileModel() { Namespace = nameSpace, Name = name, InputOrOutput = str }; string content = Engine.Razor.RunCompile("DtoTemplate", typeof(DtoFileModel), model); string fileName = $"{name}{str}.cs"; try { CreateAndAddFile(dtoFolder, fileName, content); } catch (Exception e) { Utils.MessageBox(e.Message, MessageBoxButton.OK, MessageBoxImage.Exclamation); } } }
private void Query_Click(object sender, RoutedEventArgs e) { DtoFileModel dto = projectHelper.GetDtoModel(); projectHelper.CreateFile(new CreateFileInput() { AbsoluteNamespace = dto.Namespace.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries).Last(), Namespace = dto.Namespace, ClassName = dto.Name, KeyType = ClassKeyType.Text, LocalName = ClassLocalName.Text, Prefix = NamespacePrefix.Text, DirectoryName = dto.DirName, PropertyInfos = DataList, Setting = _setting, ValidationType = _setting.ValidationType, Controller = _setting.Controller, ApplicationService = _setting.ApplicationService, DomainService = _setting.DomainService, AuthorizationService = _setting.AuthorizationService, ExcelImportAndExport = _setting.ExcelImportAndExport, PictureUpload = _setting.PictureUpload, IsStandardProject = _setting.IsStandardProject }); MessageBoxResult result = MessageBox.Show("代码生成成功", "提示", MessageBoxButton.OK, MessageBoxImage.Information, MessageBoxResult.OK); if (result == MessageBoxResult.OK) { //获取父窗体并关闭 System.Windows.Window parentWindow = System.Windows.Window.GetWindow(this); parentWindow.Close(); return; } }
/// <summary> /// 创建Dto类 /// </summary> /// <param name="model"></param> /// <param name="name"></param> /// <param name="dtoFolder"></param> private void CreateDtoFile(DtoFileModel model, string name, ProjectItem dtoFolder) { ThreadHelper.ThrowIfNotOnUIThread(); string content_Edit = Engine.Razor.RunCompile("EditDtoTemplate", typeof(DtoFileModel), model); string fileName_Edit = $"{name}EditDto.cs"; AddFileToProjectItem(dtoFolder, content_Edit, fileName_Edit); string content_List = Engine.Razor.RunCompile("ListDtoTemplate", typeof(DtoFileModel), model); string fileName_List = $"{name}ListDto.cs"; AddFileToProjectItem(dtoFolder, content_List, fileName_List); string content_CreateAndUpdate = Engine.Razor.RunCompile("CreateOrUpdateInputDtoTemplate", typeof(DtoFileModel), model); string fileName_CreateAndUpdate = $"CreateOrUpdate{name}Input.cs"; AddFileToProjectItem(dtoFolder, content_CreateAndUpdate, fileName_CreateAndUpdate); string content_GetForUpdate = Engine.Razor.RunCompile("GetForEditOutputDtoTemplate", typeof(DtoFileModel), model); string fileName_GetForUpdate = $"Get{name}ForEditOutput.cs"; AddFileToProjectItem(dtoFolder, content_GetForUpdate, fileName_GetForUpdate); string content_GetsInput = Engine.Razor.RunCompile("GetsInputTemplate", typeof(DtoFileModel), model); string fileName_GetsInput = $"Get{name}sInput.cs"; AddFileToProjectItem(dtoFolder, content_GetsInput, fileName_GetsInput); }
/// <summary> /// 获取DtoModel /// </summary> /// <param name="applicationStr"></param> /// <param name="name"></param> /// <param name="dirName"></param> /// <param name="codeClass"></param> /// <returns></returns> public DtoFileModel GetDtoModel() { var model = new DtoFileModel() { Namespace = ApplicationRootNamespace, Name = CodeClass.Name, DirName = ClassAbsolutePathInProject.Replace("\\", ".") }; List <ClassProperty> classProperties = new List <ClassProperty>(); if (CodeClass.Bases.Count > 0) { GetBaseProperty(CodeClass, ref classProperties); } var codeMembers = CodeClass.Members; AddClassProperty(codeMembers, ref classProperties); model.ClassPropertys = classProperties; return(model); }
/// <summary> /// 创建前端文件 /// </summary> /// <param name="model"></param> /// <param name="name"></param> /// <param name="frontPath"></param> private static void CreateFrontFile(DtoFileModel model, string name, string frontPath) { string content_List_Html = Engine.Razor.RunCompile("Front_List_HtmlTemplate", typeof(DtoFileModel), model); string fileName_List_Html = $"{name.ToLower()}.component.html"; AddFileToDirectory(frontPath, content_List_Html, fileName_List_Html); string content_List_Ts = Engine.Razor.RunCompile("Front_List_TsTemplate", typeof(DtoFileModel), model); string fileName_List_Ts = $"{name.ToLower()}.component.ts"; AddFileToDirectory(frontPath, content_List_Ts, fileName_List_Ts); string content_Edit_Html = Engine.Razor.RunCompile("Front_Edit_HtmlTemplate", typeof(DtoFileModel), model); string fileName_Edit_Html = $"create-or-edit-{name.ToLower()}-modal.component.html"; AddFileToDirectory(frontPath, content_Edit_Html, fileName_Edit_Html); string content_Edit_Ts = Engine.Razor.RunCompile("Front_Edit_TsTemplate", typeof(DtoFileModel), model); string fileName_Edit_Ts = $"create-or-edit-{name.ToLower()}-modal.component.ts"; AddFileToDirectory(frontPath, content_Edit_Ts, fileName_Edit_Ts); }
private void btnBuild_Click(object sender, EventArgs e) { var selectItem = (ProjectItemFullPath)comboBox1.SelectedValue; var projectItem = selectItem.ProjectItem; string namespaceStr = selectItem.Namespace; var dtoFileModel = new DtoFileModel { Description = txtRemark.Text, Name = txtName.Text, EntityName = lblSelectEntity.Text, UsingName = lblSelectEntity.Tag.ToString(), DirName = "", Namespace = namespaceStr, ClassProperties = new List <ClassProperty>() }; foreach (TextValue item in listSelectEntity.SelectedItems) { dtoFileModel.ClassProperties.Add(new ClassProperty { CnName = item.Text, Name = item.Value.Name, PropertyType = item.Value.PropertyType }); } string content = Engine.Razor.RunCompile("DtoTemplate", typeof(DtoFileModel), dtoFileModel); content = content.Replace("< /summary>", "</summary>"); string fileName = dtoFileModel.Name + ".cs"; SolutionUnit.AddFileToProjectItem(projectItem, content, fileName); MessageBox.Show("生成成功"); this.Close(); }
/// <summary> /// 获取DtoModel /// </summary> /// <param name="applicationStr"></param> /// <param name="name"></param> /// <param name="dirName"></param> /// <param name="codeClass"></param> /// <returns></returns> private static DtoFileModel GetDtoModel(string applicationStr, string name, string cnName, string description, string dirName, CodeClass codeClass) { ThreadHelper.ThrowIfNotOnUIThread(); var model = new DtoFileModel() { Namespace = applicationStr, Name = name, CnName = cnName, Description = description, DirName = dirName.Replace("\\", ".") }; List <ClassProperty> classProperties = new List <ClassProperty>(); List <ClassAttribute> classAttributes = null; var codeMembers = codeClass.Members; foreach (CodeElement codeMember in codeMembers) { if (codeMember.Kind == vsCMElement.vsCMElementProperty) { ClassProperty classProperty = new ClassProperty(); CodeProperty property = codeMember as CodeProperty; classProperty.Name = property.Name; //获取属性类型 var propertyType = property.Type; switch (propertyType.TypeKind) { case vsCMTypeRef.vsCMTypeRefString: classProperty.PropertyType = "string"; break; case vsCMTypeRef.vsCMTypeRefInt: classProperty.PropertyType = "int"; break; case vsCMTypeRef.vsCMTypeRefBool: classProperty.PropertyType = "bool"; break; case vsCMTypeRef.vsCMTypeRefDecimal: classProperty.PropertyType = "decimal"; break; case vsCMTypeRef.vsCMTypeRefDouble: classProperty.PropertyType = "double"; break; case vsCMTypeRef.vsCMTypeRefFloat: classProperty.PropertyType = "float"; break; } string propertyCnName = "";//属性中文名称 classAttributes = new List <ClassAttribute>(); //获取属性特性 foreach (CodeAttribute codeAttribute in property.Attributes) { ClassAttribute classAttribute = new ClassAttribute(); if (codeAttribute.Name == "Required") { classAttribute.NameValue = "[Required]"; classAttribute.Name = "Required"; classAttribute.Value = "true"; } else { classAttribute.NameValue = "[" + codeAttribute.Name + "(" + codeAttribute.Value + ")]"; classAttribute.Name = codeAttribute.Name; classAttribute.Value = codeAttribute.Value; if (codeAttribute.Name == "Display") { propertyCnName = codeAttribute.Value.Replace("Name = ", "").Replace("\"", ""); } } classAttributes.Add(classAttribute); } classProperty.CnName = string.IsNullOrEmpty(propertyCnName) ? property.Name : propertyCnName; classProperty.ClassAttributes = classAttributes; classProperties.Add(classProperty); } } model.ClassPropertys = classProperties; return(model); }
/// <summary> /// This function is the callback used to execute the command when the menu item is clicked. /// See the constructor to see how the menu item is associated with this function using /// OleMenuCommandService service and MenuCommand class. /// </summary> /// <param name="sender">Event sender.</param> /// <param name="e">Event args.</param> private static void Execute(object sender, EventArgs e) { ThreadHelper.ThrowIfNotOnUIThread(); string message = ""; if (_dte.SelectedItems.Count > 0) { SelectedItem selectedItem = _dte.SelectedItems.Item(1); ProjectItem selectProjectItem = selectedItem.ProjectItem; if (selectProjectItem != null) { //前端工程源码目录 string frontBaseUrl = ""; #region 获取出基础信息 //获取当前点击的类所在的项目 Project topProject = selectProjectItem.ContainingProject; //当前类在当前项目中的目录结构 string dirPath = GetSelectFileDirPath(topProject, selectProjectItem); //当前类命名空间 string namespaceStr = selectProjectItem.FileCodeModel.CodeElements.OfType <CodeNamespace>().First().FullName; //当前项目根命名空间 string applicationStr = ""; if (!string.IsNullOrEmpty(namespaceStr)) { applicationStr = namespaceStr.Substring(0, namespaceStr.IndexOf(".")); } //当前类 CodeClass codeClass = GetClass(selectProjectItem.FileCodeModel.CodeElements); //当前项目类名 string className = codeClass.Name; //当前类中文名 [Display(Name = "供应商")] string classCnName = ""; //当前类说明 [Description("品牌信息")] string classDescription = ""; //获取类的中文名称和说明 foreach (CodeAttribute classAttribute in codeClass.Attributes) { switch (classAttribute.Name) { case "Display": if (!string.IsNullOrEmpty(classAttribute.Value)) { string displayStr = classAttribute.Value.Trim(); foreach (var displayValueStr in displayStr.Split(',')) { if (!string.IsNullOrEmpty(displayValueStr)) { if (displayValueStr.Split('=')[0].Trim() == "Name") { classCnName = displayValueStr.Split('=')[1].Trim().Replace("\"", ""); } } } } break; case "Description": classDescription = classAttribute.Value; break; } } //获取当前解决方案里面的项目列表 List <ProjectItem> solutionProjectItems = GetSolutionProjects(_dte.Solution); #endregion #region 流程简介 //1.同级目录添加 Authorization 文件夹 //2.往新增的 Authorization 文件夹中添加 xxxPermissions.cs 文件 //3.往新增的 Authorization 文件夹中添加 xxxAuthorizationProvider.cs 文件 //4.往当前项目根目录下文件夹 Authorization 里面的AppAuthorizationProvider.cs类中的SetPermissions方法最后加入 SetxxxPermissions(pages); //5.往xxxxx.Application项目中增加当前所选文件所在的文件夹 //6.往第五步新增的文件夹中增加Dto目录 //7.往第六步新增的Dto中增加CreateOrUpdatexxxInput.cs xxxEditDto.cs xxxListDto.cs GetxxxForEditOutput.cs GetxxxsInput.cs这五个文件 //8.编辑CustomDtoMapper.cs,添加映射 //9.往第五步新增的文件夹中增加 xxxAppService.cs和IxxxAppService.cs 类 //10.编辑DbContext //11.新增前端文件 #endregion #region 流程实现 ////1.同级目录添加 Authorization 文件夹 //var authorizationFolder = selectProjectItem.ProjectItems.AddFolder("Authorization");//向同级目录插入文件夹 ////2.往新增的 Authorization 文件夹中添加 xxxPermissions.cs 文件 //CreatePermissionFile(applicationStr, className, authorizationFolder); ////3.往新增的 Authorization 文件夹中添加 xxxAuthorizationProvider.cs 文件 //CreateAppAuthorizationProviderFile(applicationStr, className, classCnName, authorizationFolder); ////4.往当前项目根目录下文件夹 Authorization 里面的 AppAuthorizationProvider.cs类中的 SetPermissions 方法最后加入 SetxxxPermissions(pages); //SetPermission(topProject, className); ////5.往xxxxx.Application项目中增加当前所选文件所在的文件夹 //ProjectItem applicationProjectItem = solutionProjectItems.Find(t => t.Name == applicationStr + ".Application"); //var applicationNewFolder = applicationProjectItem.SubProject.ProjectItems.Item(dirPath); //if (applicationNewFolder == null) //{ // applicationNewFolder = applicationProjectItem.SubProject.ProjectItems.AddFolder(dirPath); //} ////6.往第五步新增的文件夹中增加Dto目录 //var applicationDtoFolder = applicationNewFolder.ProjectItems.Item("Dto"); //if (applicationDtoFolder == null) //{ // applicationDtoFolder = applicationNewFolder.ProjectItems.AddFolder("Dto"); //} ////7.往第六步新增的Dto中增加CreateOrUpdatexxxInput.cs xxxEditDto.cs xxxListDto.cs GetxxxForEditOutput.cs GetxxxsInput.cs这五个文件 DtoFileModel dtoModel = GetDtoModel(applicationStr, className, classCnName, classDescription, dirPath, codeClass); //CreateDtoFile(dtoModel, className, applicationDtoFolder); ////8.编辑CustomDtoMapper.cs,添加映射 //SetMapper(applicationProjectItem.SubProject, className, classCnName); ////9.往第五步新增的文件夹中增加 xxxAppService.cs和IxxxAppService.cs 类 //CreateServiceFile(applicationStr, className, classCnName, applicationNewFolder, dirPath, codeClass); ////10.编辑DbContext //ProjectItem entityFrameworkProjectItem = solutionProjectItems.Find(t => t.Name == applicationStr + ".EntityFrameworkCore"); //SetDbSetToDbContext(entityFrameworkProjectItem.SubProject, namespaceStr, className); //11.生成前端 frontBaseUrl = topProject.FullName.Substring(0, topProject.FullName.IndexOf("src") - 1); frontBaseUrl = frontBaseUrl.Substring(0, frontBaseUrl.LastIndexOf("\\")) + "\\angular\\src\\"; //11.1 往app\\admin文件夹下面加xxx文件夹 string componetBasePath = frontBaseUrl + "app\\admin\\" + dirPath.ToLower(); if (!Directory.Exists(componetBasePath)) { Directory.CreateDirectory(componetBasePath); } //11.2 往新增的文件夹加xxx.component.html xxx.component.ts create-or-edit-xxx-modal.component.html create-or-edit-xxx-modal.component.ts这4个文件 CreateFrontFile(dtoModel, className, componetBasePath); //11.3 修改app\\admin\\admin.module.ts文件, import新增的组件 注入组件 EditModule(frontBaseUrl, className, dirPath); //11.4 修改app\\admin\\admin-routing.module.ts文件 添加路由 EditRouter(frontBaseUrl, className, dirPath); //11.5 修改 app\\shared\\layout\\nav\\app-navigation.service.ts文件 添加菜单 AddMenu(frontBaseUrl, classCnName, className); //11.6 修改 shared\\service-proxies\\service-proxy.module.ts文件 提供服务 AddProxy(frontBaseUrl, className); //如果需要新增一级目录的话,需要修改app\\\app-routing.module.ts文件 message = "生成成功!"; #endregion } } string title = "abp代码生成器"; // Show a message box to prove we were here VsShellUtilities.ShowMessageBox( asyncPackage, message, title, OLEMSGICON.OLEMSGICON_INFO, OLEMSGBUTTON.OLEMSGBUTTON_OK, OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST); }
public void InitData() { ThreadHelper.ThrowIfNotOnUIThread(); if (_dte.SelectedItems.Count > 0) { SelectedItem selectedItem = _dte.SelectedItems.Item(1); ProjectItem selectProjectItem = selectedItem.ProjectItem; if (selectProjectItem != null) { #region 获取出基础信息 //获取当前点击的类所在的项目 Project topProject = selectProjectItem.ContainingProject; //当前类在当前项目中的目录结构 //string dirPath = GetSelectFileDirPath(topProject, selectProjectItem); //当前类命名空间 string namespaceStr = selectProjectItem.FileCodeModel.CodeElements.OfType <CodeNamespace>().First().FullName; //当前项目根命名空间 string applicationStr = ""; if (!string.IsNullOrEmpty(namespaceStr)) { applicationStr = namespaceStr.Substring(0, namespaceStr.IndexOf(".")); } //当前类 CodeClass codeClass = SolutionUnit.GetClass(selectProjectItem.FileCodeModel.CodeElements); //当前项目类名 string className = codeClass.Name; //当前类中文名 [Display(Name = "供应商")] string classCnName = ""; //当前类说明 [Description("品牌信息")] string classDescription = ""; //获取类的中文名称和说明 foreach (CodeAttribute classAttribute in codeClass.Attributes) { switch (classAttribute.Name) { case "Display": if (!string.IsNullOrEmpty(classAttribute.Value)) { string displayStr = classAttribute.Value.Trim(); foreach (var displayValueStr in displayStr.Split(',')) { if (!string.IsNullOrEmpty(displayValueStr)) { if (displayValueStr.Split('=')[0].Trim() == "Name") { classCnName = displayValueStr.Split('=')[1].Trim().Replace("\"", ""); } } } } break; case "Description": classDescription = classAttribute.Value; break; } } //获取当前解决方案里面的项目列表 List <ProjectItem> solutionProjectItems = SolutionUnit.GetSolutionProjects(_dte.Solution); #endregion DtoFileModel dtoModel = GetDtoModel("", "", classCnName, namespaceStr, classDescription, "", codeClass); lblSelectEntity.Text = className; lblSelectEntity.Tag = namespaceStr; var chkListDataSource = dtoModel.ClassProperties.Select(s => new TextValue { Text = $"{s.Name}-{s.CnName}", Value = s }).ToList(); listSelectEntity.DataSource = chkListDataSource; listSelectEntity.DisplayMember = "Text"; listSelectEntity.ValueMember = "Value"; var applicationProject = solutionProjectItems.Find(w => w.Name.EndsWith(".Application")); var coreProject = solutionProjectItems.Find(w => w.Name.EndsWith(".Core")); var comBoxDataSource = new List <TextProjectItem>(); comBoxDataSource.Add(new TextProjectItem { Text = applicationProject.Name, Value = new ProjectItemFullPath { ProjectItem = applicationProject, Namespace = applicationProject.Name } }); foreach (EnvDTE.ProjectItem item in applicationProject.SubProject.ProjectItems) { if (item.Kind == EnvDTE.Constants.vsProjectItemKindPhysicalFolder) { comBoxDataSource.Add(new TextProjectItem { Text = $" {item.Name}", Value = new ProjectItemFullPath { ProjectItem = item, Namespace = $"{applicationProject.Name}.{item.Name}" } }); } if (item.ProjectItems.Count > 0) { foreach (EnvDTE.ProjectItem childItem in item.ProjectItems) { if (childItem.Kind == EnvDTE.Constants.vsProjectItemKindPhysicalFolder) { comBoxDataSource.Add(new TextProjectItem { Text = $" {childItem.Name}", Value = new ProjectItemFullPath { ProjectItem = childItem, Namespace = $"{applicationProject.Name}.{item.Name}.{childItem.Name}" } }); } } } } comBoxDataSource.Add(new TextProjectItem { Text = coreProject.Name, Value = new ProjectItemFullPath { ProjectItem = coreProject, Namespace = coreProject.Name } }); foreach (EnvDTE.ProjectItem item in coreProject.SubProject.ProjectItems) { if (item.Kind == EnvDTE.Constants.vsProjectItemKindPhysicalFolder) { comBoxDataSource.Add(new TextProjectItem { Text = $" {item.Name}", Value = new ProjectItemFullPath { ProjectItem = item, Namespace = $"{coreProject.Name}.{item.Name}" } }); } if (item.ProjectItems.Count > 0) { foreach (EnvDTE.ProjectItem childItem in item.ProjectItems) { if (childItem.Kind == EnvDTE.Constants.vsProjectItemKindPhysicalFolder) { comBoxDataSource.Add(new TextProjectItem { Text = $" {childItem.Name}", Value = new ProjectItemFullPath { ProjectItem = childItem, Namespace = $"{coreProject.Name}.{item.Name}.{childItem.Name}" } }); } } } } comboBox1.DataSource = comBoxDataSource; comboBox1.DisplayMember = "Text"; comboBox1.ValueMember = "Value"; } } }
/// <summary> /// 获取DtoModel /// </summary> /// <param name="applicationStr"></param> /// <param name="name"></param> /// <param name="dirName"></param> /// <param name="codeClass"></param> /// <returns></returns> private DtoFileModel GetDtoModel(string applicationStr, string name, string entityName, string usingName, string description, string dirName, CodeClass codeClass) { ThreadHelper.ThrowIfNotOnUIThread(); var model = new DtoFileModel() { Namespace = applicationStr, Name = name, UsingName = usingName, Description = description, EntityName = entityName, DirName = dirName.Replace("\\", ".") }; List <ClassProperty> classProperties = new List <ClassProperty>(); var codeMembers = codeClass.Members; foreach (CodeElement codeMember in codeMembers) { if (codeMember.Kind == vsCMElement.vsCMElementProperty) { ClassProperty classProperty = new ClassProperty(); CodeProperty property = codeMember as CodeProperty; classProperty.Name = property.Name; //获取属性类型 var propertyType = property.Type; switch (propertyType.TypeKind) { case vsCMTypeRef.vsCMTypeRefString: classProperty.PropertyType = "string"; break; case vsCMTypeRef.vsCMTypeRefInt: classProperty.PropertyType = "int"; break; case vsCMTypeRef.vsCMTypeRefBool: classProperty.PropertyType = "bool"; break; case vsCMTypeRef.vsCMTypeRefDecimal: classProperty.PropertyType = "decimal"; break; case vsCMTypeRef.vsCMTypeRefDouble: classProperty.PropertyType = "double"; break; case vsCMTypeRef.vsCMTypeRefFloat: classProperty.PropertyType = "float"; break; case vsCMTypeRef.vsCMTypeRefLong: classProperty.PropertyType = "long"; break; default: classProperty.PropertyType = propertyType.AsString; break; } string propertyCnName = "";//属性中文名称 foreach (CodeAttribute classAttribute in property.Attributes) { switch (classAttribute.Name) { case "Description": propertyCnName = classAttribute.Value.Replace("\"", ""); break; } } classProperty.CnName = string.IsNullOrEmpty(propertyCnName) ? property.Name : propertyCnName; classProperties.Add(classProperty); } } model.ClassProperties = classProperties; return(model); }