/// <summary> /// Called when [execute]. /// </summary> public override void Execute() { string tempfile = Path.GetTempFileName(); try { //string foldert = System.IO.Path.GetDirectoryName(Project.FullName); string fileFullName = string.Empty; //DirectoryInfo dInfo = new DirectoryInfo(foldert); ProjectItem folderItem = VSIPHelper.FindItemByName(Project.ProjectItems, Subfolder, true); if (folderItem == null) { try { project.ProjectItems.AddFolder(Subfolder); folderItem = VSIPHelper.FindItemByName(Project.ProjectItems, Subfolder, true); } catch { } } using (StreamWriter sw = new StreamWriter(tempfile, false, new UTF8Encoding(true, true))) { sw.WriteLine(content); } // Check it the targetFileName already exists and delete it so it can be added. ProjectItem targetItem = VSIPHelper.FindItemByName(Project.ProjectItems, targetFileName, true); if (targetItem != null) { targetItem.Delete(); } if (!String.IsNullOrEmpty(itemName)) { ProjectItem item = VSIPHelper.FindItemByName(Project.ProjectItems, itemName, true); if (item != null) { projectItem = item.ProjectItems.AddFromTemplate(tempfile, targetFileName); } } else { //projectItem = project.ProjectItems.AddFromTemplate(tempfile, targetFileName); projectItem = folderItem.ProjectItems.AddFromTemplate(tempfile, targetFileName); } if (open && projectItem != null) { Window wnd = projectItem.Open(EnvDTE.Constants.vsViewKindPrimary); wnd.Visible = true; wnd.Activate(); } } finally { File.Delete(tempfile); } }
/// <summary> /// Opens the specified filename from the specified project name. /// </summary> public EditorWindow OpenItem(string project, params string[] path) { foreach (EnvDTE.Project proj in VSTestContext.DTE.Solution.Projects) { if (proj.Name == project) { var items = proj.ProjectItems; EnvDTE.ProjectItem item = null; foreach (var itemName in path) { item = items.Item(itemName); items = item.ProjectItems; } Assert.IsNotNull(item); var window = item.Open(); window.Activate(); return(App.GetDocument(item.Document.FullName)); } } throw new InvalidOperationException( String.Format( "Failed to find {0} item in project {1}", String.Join("\\", path), project ) ); }
/// <summary> /// The method that creates a new item from the intput string. /// </summary> public override void Execute() { DTE vs = (DTE)GetService(typeof(DTE)); string tempfile = Path.GetTempFileName(); try { using (StreamWriter sw = new StreamWriter(tempfile, false, new UTF8Encoding(true, true))) { sw.WriteLine(content); } // Check it the targetFileName already exists and delete it so it can be added. ProjectItem targetItem = DteHelperEx.FindItemByName(Project.ProjectItems, targetFileName, true); if (targetItem != null) { targetItem.Delete(); } if (!String.IsNullOrEmpty(itemName)) { ProjectItem item = DteHelperEx.FindItemByName(Project.ProjectItems, itemName, true); if (item != null) { projectItem = item.ProjectItems.AddFromTemplate(tempfile, targetFileName); } } else { projectItem = project.ProjectItems.AddFromTemplate(tempfile, targetFileName); } if (open && projectItem != null) { Window wnd = projectItem.Open(Constants.vsViewKindPrimary); wnd.Visible = true; wnd.Activate(); } } finally { File.Delete(tempfile); } }
/// <summary> /// The method that creates a new item from the intput string. /// </summary> public override void Execute() { DTE vs = GetService <DTE>(true); string tempfile = Path.GetTempFileName(); using (StreamWriter sw = new StreamWriter(tempfile, false)) { sw.WriteLine(content); } projectItem = project.ProjectItems.AddFromTemplate(tempfile, targetFileName); if (open) { Window wnd = projectItem.Open(Constants.vsViewKindPrimary); wnd.Visible = true; wnd.Activate(); } File.Delete(tempfile); }
private static EditorWindow OpenItem(VisualStudioApp app, string startItem, Project project, out Window window) { EnvDTE.ProjectItem item = null; if (startItem.IndexOf('\\') != -1) { var items = project.ProjectItems; foreach (var itemName in startItem.Split('\\')) { Console.WriteLine(itemName); item = items.Item(itemName); items = item.ProjectItems; } } else { item = project.ProjectItems.Item(startItem); } Assert.IsNotNull(item); window = item.Open(); window.Activate(); return(app.GetDocument(item.Document.FullName)); }
/// <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); }
/// <summary> /// Replaces GUID placeholders in a project item. /// </summary> /// <param name="projectItem">The project item.</param> private void ReplaceGuidPlaceholders(EnvDTE.ProjectItem projectItem) { // Attempt to open the document if not already opened. var wasOpen = projectItem.IsOpen[Constants.vsViewKindTextView] || projectItem.IsOpen[Constants.vsViewKindCode]; if (!wasOpen) { try { projectItem.Open(Constants.vsViewKindTextView); } catch (Exception) { // File cannot be opened (e.g.: deleted from disk, non-text based type) } } try { if (projectItem.Document != null) { ReplaceGuidPlaceholders(projectItem.Document); // Close the document if it was opened for cleanup. if (!wasOpen) { projectItem.Document.Close(vsSaveChanges.vsSaveChangesYes); } } } catch (Exception) { // File cannot be opened (e.g.: deleted from disk, non-text based type) } }
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"); }); }
/// <summary> /// The method that creates a new item from the intput string. /// </summary> public override void Execute() { if (!AddItem) { return; } DTE vs = GetService <DTE>(); IDictionaryService dictionaryService = GetService <IDictionaryService>(); string targetFileNameProcessed = ArgumentsHelper.ReplaceToken(targetFileName, ReplaceToken, dictionaryService); if (!string.IsNullOrEmpty(itemName)) { itemName = ArgumentsHelper.ReplaceToken(itemName, ReplaceToken, dictionaryService); } string tempfile = Path.GetTempFileName(); try { IUIService uiService = (IUIService)GetService(typeof(IUIService)); ProjectItem parentProjectItem = null; if (!string.IsNullOrEmpty(itemName)) { parentProjectItem = DteHelper.FindItemByName(Project.ProjectItems, itemName, true); if (parentProjectItem == null) { try { parentProjectItem = Project.ProjectItems.AddFolder(itemName, Resources.NewFolderKind); }catch { uiService.ShowMessage(string.Format(Resources.ErrCreatingNewFolder, ItemName)); parentProjectItem = null; } } } else if (Item != null) { parentProjectItem = Item; } if (parentProjectItem != null) { projectItem = DteHelper.FindItemByName(parentProjectItem.ProjectItems, targetFileNameProcessed, false); } else { projectItem = DteHelper.FindItemByName(Project.ProjectItems, targetFileNameProcessed, false); } if (PromptForOverwriting && projectItem != null) { DialogResult result = uiService.ShowMessage(string.Format(Resources.FileAlreadyExists, targetFileNameProcessed), Resources.FileAlreadyExistsCaption, MessageBoxButtons.OKCancel); if (result == DialogResult.Cancel) { CancelByTheUser = true; return; } } if (projectItem != null) { projectItem.Delete(); } using (StreamWriter sw = new StreamWriter(tempfile, false, Encoding.UTF8)) { sw.WriteLine(content); } if (parentProjectItem != null) { projectItem = parentProjectItem.ProjectItems.AddFromTemplate(tempfile, targetFileNameProcessed); } else { projectItem = project.ProjectItems.AddFromTemplate(tempfile, targetFileNameProcessed); } if (open && projectItem != null) { Window wnd = projectItem.Open(Constants.vsViewKindPrimary); wnd.Visible = true; wnd.Activate(); } } finally { File.Delete(tempfile); } }