/// <summary> /// 编辑CustomDtoMapper.cs,添加映射 /// </summary> /// <param name="applicationProject"></param> /// <param name="className"></param> /// <param name="classCnName"></param> private void SetMapper(Project applicationProject, string className, string classCnName) { ThreadHelper.ThrowIfNotOnUIThread(); ProjectItem customDtoMapperProjectItem = _dte.Solution.FindProjectItem(applicationProject.FileName.Substring(0, applicationProject.FileName.LastIndexOf("\\")) + "\\CustomDtoMapper.cs"); if (customDtoMapperProjectItem != null) { CodeClass codeClass = GetClass(customDtoMapperProjectItem.FileCodeModel.CodeElements); var codeChilds = codeClass.Members; foreach (CodeElement codeChild in codeChilds) { if (codeChild.Kind == vsCMElement.vsCMElementFunction && codeChild.Name == "CreateMappings") { var insertCode = codeChild.GetEndPoint(vsCMPart.vsCMPartBody).CreateEditPoint(); insertCode.Insert(" // " + classCnName + "\r\n"); insertCode.Insert(" configuration.CreateMap<" + className + ", " + className + "EditDto>();\r\n"); insertCode.Insert(" configuration.CreateMap<" + className + ", " + className + "ListDto>();\r\n"); insertCode.Insert(" configuration.CreateMap<" + className + "EditDto, " + className + ">();\r\n"); insertCode.Insert(" configuration.CreateMap<" + className + "ListDto, " + className + ">();\r\n"); insertCode.Insert("\r\n"); } } customDtoMapperProjectItem.Save(); } }
/// <summary> /// 添加DbSet到DbContext /// </summary> /// <param name="topProject"></param> /// <param name="className"></param> private void SetDbSetToDbContext(Project entityFrameworkProject, string namespaceStr, string className) { ThreadHelper.ThrowIfNotOnUIThread(); ProjectItem customDbContextProviderProjectItem = _dte.Solution.FindProjectItem(entityFrameworkProject.FileName.Substring(0, entityFrameworkProject.FileName.LastIndexOf("\\")) + "\\EntityFrameworkCore\\AbpFrameDbContext.Custom.cs"); if (customDbContextProviderProjectItem != null) { CodeClass codeClass = GetClass(customDbContextProviderProjectItem.FileCodeModel.CodeElements); var codeChilds = codeClass.Collection; foreach (CodeElement codeChild in codeChilds) { var insertCode = codeChild.GetEndPoint(vsCMPart.vsCMPartBody).CreateEditPoint(); insertCode.Insert(" public DbSet<" + namespaceStr + "." + className + "> " + className + "s { get; set; }\r\n"); } customDbContextProviderProjectItem.Save(); } }
/// <summary> /// 添加权限 /// </summary> /// <param name="topProject"></param> /// <param name="className"></param> private void SetPermission(Project topProject, string className) { ThreadHelper.ThrowIfNotOnUIThread(); ProjectItem AppAuthorizationProviderProjectItem = _dte.Solution.FindProjectItem(topProject.FileName.Substring(0, topProject.FileName.LastIndexOf("\\")) + "\\Authorization\\AppAuthorizationProvider.cs"); if (AppAuthorizationProviderProjectItem != null) { CodeClass codeClass = GetClass(AppAuthorizationProviderProjectItem.FileCodeModel.CodeElements); var codeChilds = codeClass.Members; foreach (CodeElement codeChild in codeChilds) { if (codeChild.Kind == vsCMElement.vsCMElementFunction && codeChild.Name == "SetPermissions") { var insertCode = codeChild.GetEndPoint(vsCMPart.vsCMPartBody).CreateEditPoint(); insertCode.Insert(" Set" + className + "Permissions(pages);\r\n"); insertCode.Insert("\r\n"); } } AppAuthorizationProviderProjectItem.Save(); } }
/// <summary> /// Execute Visual Studio commands against the project item. /// </summary> /// <param name="item">The current project item.</param> /// <param name="command">The vs command as string.</param> /// <returns>An error message if the command fails.</returns> public static string ExecuteVsCommand(EnvDTE.DTE dte, EnvDTE.ProjectItem item, params string[] command) { if (item == null) { throw new ArgumentNullException("item"); } string error = String.Empty; try { EnvDTE.Window window = item.Open(); window.Activate(); foreach (var cmd in command) { if (String.IsNullOrWhiteSpace(cmd) == true) { continue; } EnvDTE80.DTE2 dte2 = dte as EnvDTE80.DTE2; dte2.ExecuteCommand(cmd, String.Empty); } item.Save(); window.Visible = false; // window.Close(); // Ends VS, but not the tab :( } catch (Exception ex) { error = String.Format("Error processing file {0} {1}", item.Name, ex.Message); } return(error); }
public void TestAutomationOnProjectItem() { UIThreadInvoker.Invoke((ThreadInvoker) delegate() { //Get the global service provider and the dte IServiceProvider sp = VsIdeTestHostContext.ServiceProvider; DTE dte = (DTE)sp.GetService(typeof(DTE)); string destination = Path.Combine(TestContext.TestDir, TestContext.TestName); Utilities.CreateMyNestedProject(sp, dte, TestContext.TestName, destination, true); OAProject automation = Utilities.FindExtObject(sp, Utilities.NestedProjectGuid, TestContext.TestName) as OAProject; Assert.IsNotNull(automation, "Failed to create a project using automation"); ProjectNode project = automation.Project; // Get the AssemblyInfo.cs, try to open it and then ask using automation that it is opened. EnvDTE.ProjectItem item = automation.ProjectItems.Item("AssemblyInfo.cs"); Assert.IsNotNull(item, "Could not retrieve AssemblyInfo.cs"); EnvDTE.Window window = item.Open(VSConstants.LOGVIEWID_Primary.ToString()); Assert.IsNotNull(window, "Could not open the AssemblyInfo.cs"); window.Activate(); bool isOpen = item.get_IsOpen(VSConstants.LOGVIEWID_Primary.ToString()); Assert.IsTrue(isOpen, "The AssemblyInfo.cs file should have been opened"); // Now save it item.Save(""); Assert.IsTrue(item.Saved, "The renamed AssemblyInfo.cs has not been saved"); // Get the Document EnvDTE.Document document = item.Document; Assert.IsNotNull(document, "Could not retrieve the document object"); Assert.IsTrue(document.Name == "AssemblyInfo.cs", "The document for the file item is incorrect. It's name should be AssemblyInfo.cs"); // Try the properties on a nested item EnvDTE.ProjectItem nestedProject = automation.ProjectItems.Item("ANestedProject"); EnvDTE.ProjectItem nestedProjectItem = nestedProject.ProjectItems.Item("Program.cs"); EnvDTE.Properties nesteditemsProps = nestedProjectItem.Properties; EnvDTE.Property nestedItemProperty = nesteditemsProps.Item("BuildAction"); Assert.IsNotNull(nestedItemProperty, "Could not retrieve the BuildAction property from the nested project item"); nestedItemProperty.Value = BuildAction.Content; Assert.AreEqual((BuildAction)nestedItemProperty.Value, BuildAction.Content); // Now try the properties on the top project item EnvDTE.Properties props = item.Properties; Assert.IsNotNull(props, "Could not retrieve the BuildAction property from the nested project item"); EnvDTE.Property itemProperty = props.Item("BuildAction"); Assert.IsNotNull(itemProperty, "Could not retrieve the BuildAction property from the nested project item"); Assert.IsFalse(itemProperty is OANullProperty, "Could not retrieve the BuildAction property from the nested project item"); itemProperty.Value = BuildAction.Content; Assert.AreEqual(itemProperty.Value, BuildAction.Content); // Now save as Assert.IsTrue(item.SaveAs("AssemblyInfo1.cs"), "The file AssemblyInfo.cs could not be reanmed to AssemblyInfo1.cs"); Assert.IsTrue(item.Name == "AssemblyInfo1.cs", "File item has been renamed to AssemblyInfo1.cs but the Name property has not"); // Now try the Program.cs. That should not be opened EnvDTE.ProjectItem item1 = automation.ProjectItems.Item("Program.cs"); Assert.IsNotNull(item1, "Could not retrieve AssemblyInfo.cs"); isOpen = item1.get_IsOpen(VSConstants.LOGVIEWID_Primary.ToString()); Assert.IsFalse(isOpen, "The Program.cs should not have been opened"); // Now get the Reference folder as a project item and expand it. EnvDTE.ProjectItem references = automation.ProjectItems.Item("References"); references.ExpandView(); // Check that actually it was expanded. IVsUIHierarchyWindow uiHierarchy = VsShellUtilities.GetUIHierarchyWindow(project.Site, HierarchyNode.SolutionExplorer); System.Reflection.MethodInfo mi = typeof(ProjectNode).GetMethod("FindChild", BindingFlags.NonPublic | BindingFlags.Instance); ReferenceContainerNode containerNode = (ReferenceContainerNode)mi.Invoke(project, new object[] { "References" }); __VSHIERARCHYITEMSTATE state; uint stateAsInt; uiHierarchy.GetItemState(project, (uint)containerNode.ID, (uint)__VSHIERARCHYITEMSTATE.HIS_Expanded, out stateAsInt); state = (__VSHIERARCHYITEMSTATE)stateAsInt; Assert.IsTrue(state == __VSHIERARCHYITEMSTATE.HIS_Expanded, "The References folder has not been expanded"); }); }