/// <summary> /// Adds an item to the project. /// </summary> /// <param name="path">The full path of the item to add.</param> /// <param name="op">The <paramref name="VSADDITEMOPERATION"/> to use when adding the item.</param> /// <returns>A ProjectItem object. </returns> protected virtual ProjectItem AddItem(string path, VSADDITEMOPERATION op) { CheckProjectIsValid(); return(ThreadHelper.JoinableTaskFactory.Run(async delegate { await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); ProjectNode proj = this.Project.Project; ProjectItem itemAdded = null; using (AutomationScope scope = new AutomationScope(this.Project.Project.Site)) { VSADDRESULT[] result = new VSADDRESULT[1]; ErrorHandler.ThrowOnFailure(proj.AddItem(this.NodeWithItems.ID, op, path, 0, new string[1] { path }, IntPtr.Zero, result)); string fileName = System.IO.Path.GetFileName(path); string fileDirectory = proj.GetBaseDirectoryForAddingFiles(this.NodeWithItems); string filePathInProject = System.IO.Path.Combine(fileDirectory, fileName); itemAdded = this.EvaluateAddResult(result[0], filePathInProject); } return itemAdded; })); }
public static void AddSolutionItem(IVsSolution solution, string fileName) { uint itemId = DteHelper2.FindItemByName( solution as IVsHierarchy, "Solution Items"); IntPtr ptr = IntPtr.Zero; Guid solutionFolderGuid = new Guid("2150E333-8FDC-42a3-9474-1A3956D46DE8"); Guid iidProject = typeof(IVsHierarchy).GUID; int res = solution.CreateProject( ref solutionFolderGuid, null, null, "Solution Items", 0, ref iidProject, out ptr); if (ptr != IntPtr.Zero) { IVsHierarchy hierarchy = (IVsHierarchy)Marshal.GetObjectForIUnknown(ptr); Guid projGuid; hierarchy.GetGuidProperty( VSConstants.VSITEMID_ROOT, (int)__VSHPROPID.VSHPROPID_ProjectIDGuid, out projGuid); ProjectNode node = new ProjectNode(solution, projGuid); node.AddItem(fileName); } }
/// <summary> /// Adds an item to the project. /// </summary> /// <param name="path">The full path of the item to add.</param> /// <param name="op">The <paramref name="VSADDITEMOPERATION"/> to use when adding the item.</param> /// <returns>A ProjectItem object. </returns> protected virtual EnvDTE.ProjectItem AddItem(string path, VSADDITEMOPERATION op) { if (this.Project == null || this.Project.Project == null || this.Project.Project.Site == null || this.Project.Project.IsClosed) { throw new InvalidOperationException(); } return(UIThread.DoOnUIThread(delegate() { ProjectNode proj = this.Project.Project; EnvDTE.ProjectItem itemAdded = null; using (AutomationScope scope = new AutomationScope(this.Project.Project.Site)) { VSADDRESULT[] result = new VSADDRESULT[1]; ErrorHandler.ThrowOnFailure(proj.AddItem(this.NodeWithItems.ID, op, path, 0, new string[1] { path }, IntPtr.Zero, result)); string fileName = System.IO.Path.GetFileName(path); string fileDirectory = proj.GetBaseDirectoryForAddingFiles(this.NodeWithItems); string filePathInProject = System.IO.Path.Combine(fileDirectory, fileName); itemAdded = this.EvaluateAddResult(result[0], filePathInProject); } return itemAdded; })); }
public void AddItemWithInvalidNameThrows() { ProjectNode projectNode = new ProjectNode(_vsSolution, _project.Guid); string itemName = "Invalid<Name>"; projectNode.AddItem(itemName); }
private bool AddFile(ProjectNode projectNode, string file, string content) { // add the file to the project HierarchyNode node = projectNode.AddItem(file); if (node == null) { return(false); } using (node) { // Hide the file already added IVsWindowFrame frame = projectNode.OpenItem(node); frame.Hide(); using (DocData docData = new DocData(serviceProvider, node.Path)) { docData.CheckoutFile(serviceProvider); using (DocDataTextWriter writer = new DocDataTextWriter(docData)) { writer.Write(content); } } frame.CloseFrame((uint)__FRAMECLOSE.FRAMECLOSE_SaveIfDirty); return(true); } }
/// <summary> /// Adds an item to the project. /// </summary> /// <param name="path">The full path of the item to add.</param> /// <param name="op">The <paramref name="VSADDITEMOPERATION"/> to use when adding the item.</param> /// <returns>A ProjectItem object. </returns> protected virtual EnvDTE.ProjectItem AddItem(string path, VSADDITEMOPERATION op) { CheckProjectIsValid(); return(Project.ProjectNode.Site.GetUIThread().Invoke <EnvDTE.ProjectItem>(() => { ProjectNode proj = this.Project.ProjectNode; EnvDTE.ProjectItem itemAdded = null; using (AutomationScope scope = new AutomationScope(this.Project.ProjectNode.Site)) { VSADDRESULT[] result = new VSADDRESULT[1]; ErrorHandler.ThrowOnFailure(proj.AddItem(this.NodeWithItems.ID, op, path, 0, new string[1] { path }, IntPtr.Zero, result)); string realPath = null; if (op != VSADDITEMOPERATION.VSADDITEMOP_LINKTOFILE) { string fileName = Path.GetFileName(path); string fileDirectory = proj.GetBaseDirectoryForAddingFiles(this.NodeWithItems); realPath = Path.Combine(fileDirectory, fileName); } else { realPath = path; } itemAdded = this.EvaluateAddResult(result[0], realPath); } return itemAdded; })); }
public void AddItemWithEmptyNameThrows() { ProjectNode projectNode = new ProjectNode(_vsSolution, _project.Guid); string itemName = ".cs"; string fullItemName = new FileInfo(itemName).FullName; projectNode.AddItem(itemName); }
public void TestAddItem() { ProjectNode projectNode = new ProjectNode(vsSolution, project.GUID); string itemName = "item1.cs"; projectNode.AddItem(itemName); string fullItemName = new FileInfo(itemName).FullName; Assert.IsTrue(project.Children.Contains(fullItemName)); }
public void TestOpenItem() { ProjectNode projectNode = new ProjectNode(_vsSolution, _project.Guid); string itemName = "item1.cs"; HierarchyNode item = projectNode.AddItem(itemName); IVsWindowFrame wnd = projectNode.OpenItem(item); Assert.IsNotNull(wnd); }
/// <summary> /// Creates a new project item from an existing item template file and adds it to the project. /// </summary> /// <param name="fileName">The full path and file name of the template project file.</param> /// <param name="name">The file name to use for the new project item.</param> /// <returns>A ProjectItem object. </returns> public override EnvDTE.ProjectItem AddFromTemplate(string fileName, string name) { if (this.Project == null || this.Project.Project == null || this.Project.Project.Site == null || this.Project.Project.IsClosed) { throw new InvalidOperationException(); } return(UIThread.DoOnUIThread(delegate() { ProjectNode proj = this.Project.Project; EnvDTE.ProjectItem itemAdded = null; using (AutomationScope scope = new AutomationScope(this.Project.Project.Site)) { string fixedFileName = fileName; if (!File.Exists(fileName)) { string tempFileName = GetTemplateNoZip(fileName); if (File.Exists(tempFileName)) { fixedFileName = tempFileName; } } // Determine the operation based on the extension of the filename. // We should run the wizard only if the extension is vstemplate // otherwise it's a clone operation VSADDITEMOPERATION op; if (Utilities.IsTemplateFile(fixedFileName)) { op = VSADDITEMOPERATION.VSADDITEMOP_RUNWIZARD; } else { op = VSADDITEMOPERATION.VSADDITEMOP_CLONEFILE; } VSADDRESULT[] result = new VSADDRESULT[1]; // It is not a very good idea to throw since the AddItem might return Cancel or Abort. // The problem is that up in the call stack the wizard code does not check whether it has received a ProjectItem or not and will crash. // The other problem is that we cannot get add wizard dialog back if a cancel or abort was returned because we throw and that code will never be executed. Typical catch 22. ErrorHandler.ThrowOnFailure(proj.AddItem(this.NodeWithItems.ID, op, name, 0, new string[1] { fixedFileName }, IntPtr.Zero, result)); string fileDirectory = proj.GetBaseDirectoryForAddingFiles(this.NodeWithItems); string templateFilePath = System.IO.Path.Combine(fileDirectory, name); itemAdded = this.EvaluateAddResult(result[0], templateFilePath); } return itemAdded; })); }
public void TestAddItem() { ProjectNode projectNode = new ProjectNode(_vsSolution, _project.Guid); string itemName = "item1.cs"; projectNode.AddItem(itemName); string fullItemName = new FileInfo(itemName).FullName; Assert.IsTrue(_project.Children.Contains(fullItemName)); }
public void TestGetMSBuildItem() { ProjectNode projectNode = new ProjectNode(vsSolution, project.GUID); string itemName = "item1.cs"; Assert.IsNotNull(projectNode.AddItem(itemName)); Assert.IsNotNull(projectNode.GetBuildItem(itemName)); string itemName2 = ".\\item2.cs"; Assert.IsNotNull(projectNode.AddItem(itemName2)); Assert.IsNotNull(projectNode.GetBuildItem(itemName2)); string itemName3 = ".\\FolderForItem3\\item3.cs"; Assert.IsNotNull(projectNode.AddItem(itemName3)); Assert.IsNotNull(projectNode.GetBuildItem(itemName3)); string itemName4 = "FolderForItem4\\item4.cs"; Assert.IsNotNull(projectNode.AddItem(itemName4)); Assert.IsNotNull(projectNode.GetBuildItem(itemName4)); }
public void RemoveItem() { MockVSHierarchy hierarchy = new MockVSHierarchy(); MockVsSolution solution = new MockVsSolution(hierarchy); MockVSHierarchy project = new MockVSHierarchy("Project3.project"); hierarchy.AddProject(project); ProjectNode projectNode = new ProjectNode(solution, project.GUID); string itemName = "item1"; HierarchyNode node = projectNode.AddItem(itemName); Assert.IsNotNull(projectNode.FindByName(itemName)); node.Remove(); Assert.IsNull(projectNode.FindByName(itemName)); }
EnvDTE.ProjectItem AddItem(string path, VSADDITEMOPERATION op) { ProjectNode proj = this.project.project; VSADDRESULT[] result = new VSADDRESULT[1]; NativeMethods.ThrowOnFailure(proj.AddItem(node.ID, op, path, 0, new string[1] { path }, IntPtr.Zero, null)); if (result[0] == VSADDRESULT.ADDRESULT_Success) { HierarchyNode child = node.LastChild; EnvDTE.ProjectItem item = new OAProjectItem(this.project, child); this.items.Add(item); return(item); } return(null); }
private HierarchyNode AddFile(string path, string location) { using (HierarchyNode folder = new HierarchyNode(VSSolution).FindOrCreateSolutionFolder(location)) if (folder != null) { using (ProjectNode folderProject = new ProjectNode(VSSolution, folder.ProjectGuid)) { // Add xsd HierarchyNode item = folderProject.AddItem(path); Debug.Assert(item != null, "Upps the PMT item could not be added"); IVsWindowFrame wnd = folderProject.OpenItem(item); Debug.Assert(wnd != null, "Upps the PMT.xsd item could not be opened"); wnd.CloseFrame((uint)__FRAMECLOSE.FRAMECLOSE_SaveIfDirty); return(item); } } return(null); }
/// <summary> /// Creates a new project item from an existing item template file and adds it to the project. /// </summary> /// <param name="fileName">The full path and file name of the template project file.</param> /// <param name="name">The file name to use for the new project item.</param> /// <returns>A ProjectItem object. </returns> public override EnvDTE.ProjectItem AddFromTemplate(string fileName, string name) { if (this.Project == null || this.Project.Project == null || this.Project.Project.Site == null || this.Project.Project.IsClosed) { throw new InvalidOperationException(); } ProjectNode proj = this.Project.Project; IVsExtensibility3 extensibility = this.Project.Project.Site.GetService(typeof(IVsExtensibility)) as IVsExtensibility3; if (extensibility == null) { throw new InvalidOperationException(); } EnvDTE.ProjectItem itemAdded = null; extensibility.EnterAutomationFunction(); try { VSADDITEMOPERATION op = VSADDITEMOPERATION.VSADDITEMOP_CLONEFILE; VSADDRESULT[] result = new VSADDRESULT[1]; // It is not a very good idea to throw since the AddItem might return Cancel or Abort. // The problem is that up in the call stack the wizard code does not check whether it has received a ProjectItem or not and will crash. // The other problem is that we cannot get add wizard dialog back if a cancel or abort was returned because we throw and that code will never be executed. Typical catch 22. ErrorHandler.ThrowOnFailure(proj.AddItem(this.NodeWithItems.ID, op, name, 0, new string[1] { fileName }, IntPtr.Zero, result)); string fileDirectory = proj.GetBaseDirectoryForAddingFiles(this.NodeWithItems); string templateFilePath = System.IO.Path.Combine(fileDirectory, name); itemAdded = this.EvaluateAddResult(result[0], templateFilePath); } finally { extensibility.ExitAutomationFunction(); } return(itemAdded); }
/// <summary> /// Adds an item to the project. /// </summary> /// <param name="path">The full path of the item to add.</param> /// <param name="op">The VSADDITEMOPERATION to use when adding the item.</param> /// <returns>A ProjectItem object. </returns> public virtual EnvDTE.ProjectItem AddItem(string path, VSADDITEMOPERATION op) { return(UIThread.DoOnUIThread(delegate() { if (this.Project == null || this.Project.Project == null || this.Project.Project.Site == null || this.Project.Project.IsClosed) { throw new InvalidOperationException(); } ProjectNode proj = this.Project.Project; IVsExtensibility3 extensibility = this.Project.Project.Site.GetService(typeof(IVsExtensibility)) as IVsExtensibility3; if (extensibility == null) { throw new InvalidOperationException(); } EnvDTE.ProjectItem itemAdded = null; extensibility.EnterAutomationFunction(); try { VSADDRESULT[] result = new VSADDRESULT[1]; ErrorHandler.ThrowOnFailure(proj.AddItem(this.NodeWithItems.ID, op, path, 0, new string[1] { path }, IntPtr.Zero, result)); string fileName = System.IO.Path.GetFileName(path); string fileDirectory = proj.GetBaseDirectoryForAddingFiles(this.NodeWithItems); string filePathInProject = System.IO.Path.Combine(fileDirectory, fileName); itemAdded = this.EvaluateAddResult(result[0], filePathInProject); } finally { extensibility.ExitAutomationFunction(); } return itemAdded; })); }
/// <summary> /// Adds an item to the project. /// </summary> /// <param name="path">The full path of the item to add.</param> /// <param name="op">The <paramref name="VSADDITEMOPERATION"/> to use when adding the item.</param> /// <returns>A ProjectItem object. </returns> protected virtual EnvDTE.ProjectItem AddItem(string path, VSADDITEMOPERATION op) { CheckProjectIsValid(); return(UIThread.Instance.RunSync <EnvDTE.ProjectItem>(() => { string ext = Path.GetExtension(path); foreach (var extension in this.Project.ProjectNode.CodeFileExtensions) { // http://pytools.codeplex.com/workitem/617 // We are currently in create project from existing code mode. The wizard walks all of the top-level // files and adds them. It then lets us handle any subdirectories by calling AddFromDirectory. // But we want to filter the files for both top-level and subdirectories. Therefore we derive from // PageManager and track when we're running the wizard and adding files for the wizard. If we are // currently adding them ignore anything other than a .py/.pyw files - returnning null is fine // here, the wizard doesn't care about the result. if (String.Compare(ext, extension, StringComparison.OrdinalIgnoreCase) == 0) { ProjectNode proj = this.Project.ProjectNode; EnvDTE.ProjectItem itemAdded = null; using (AutomationScope scope = new AutomationScope(this.Project.ProjectNode.Site)) { VSADDRESULT[] result = new VSADDRESULT[1]; ErrorHandler.ThrowOnFailure(proj.AddItem(this.NodeWithItems.ID, op, path, 0, new string[1] { path }, IntPtr.Zero, result)); string fileName = System.IO.Path.GetFileName(path); string fileDirectory = proj.GetBaseDirectoryForAddingFiles(this.NodeWithItems); string filePathInProject = System.IO.Path.Combine(fileDirectory, fileName); itemAdded = this.EvaluateAddResult(result[0], filePathInProject); } return itemAdded; } } return null; })); }
/// <summary> /// Creates a new project item from an existing item template file and adds it to the project. /// </summary> /// <param name="fileName">The full path and file name of the template project file.</param> /// <param name="name">The file name to use for the new project item.</param> /// <returns>A ProjectItem object. </returns> public override ProjectItem AddFromTemplate(string fileName, string name) { CheckProjectIsValid(); ProjectNode proj = Project.Project; ProjectItem itemAdded = null; using (var scope = new AutomationScope(Project.Project.Site)) { // Determine the operation based on the extension of the filename. // We should run the wizard only if the extension is vstemplate // otherwise it's a clone operation VSADDITEMOPERATION op; if (Utilities.IsTemplateFile(fileName)) { op = VSADDITEMOPERATION.VSADDITEMOP_RUNWIZARD; } else { op = VSADDITEMOPERATION.VSADDITEMOP_CLONEFILE; } var result = new VSADDRESULT[1]; // It is not a very good idea to throw since the AddItem might return Cancel or Abort. // The problem is that up in the call stack the wizard code does not check whether it has received a ProjectItem or not and will crash. // The other problem is that we cannot get add wizard dialog back if a cancel or abort was returned because we throw and that code will never be executed. Typical catch 22. ErrorHandler.ThrowOnFailure(proj.AddItem(NodeWithItems.ID, op, name, 0, new string[1] { fileName }, IntPtr.Zero, result)); string fileDirectory = proj.GetBaseDirectoryForAddingFiles(NodeWithItems); string templateFilePath = Path.Combine(fileDirectory, name); itemAdded = EvaluateAddResult(result[0], templateFilePath); } return(itemAdded); }
/// <summary> /// Adds an item to the project. /// </summary> /// <param name="path">The full path of the item to add.</param> /// <param name="op">The <paramref name="VSADDITEMOPERATION"/> to use when adding the item.</param> /// <returns>A ProjectItem object. </returns> protected virtual EnvDTE.ProjectItem AddItem(string path, VSADDITEMOPERATION op) { CheckProjectIsValid(); ProjectNode proj = this.Project.Project; EnvDTE.ProjectItem itemAdded = null; using (AutomationScope scope = new AutomationScope(this.Project.Project.Site)) { VSADDRESULT[] result = new VSADDRESULT[1]; ErrorHandler.ThrowOnFailure(proj.AddItem(this.NodeWithItems.ID, op, path, 0, new string[1] { path }, IntPtr.Zero, result)); string fileName = System.IO.Path.GetFileName(path); string fileDirectory = proj.GetBaseDirectoryForAddingFiles(this.NodeWithItems); string filePathInProject = System.IO.Path.Combine(fileDirectory, fileName); itemAdded = this.EvaluateAddResult(result[0], filePathInProject); } return(itemAdded); }
internal static FileNode AddFile(ProjectNode project, string fileNameWithoutExtension, string extension) { string newModelFilePath = TestUtils.GetNewFileName(project.ProjectFolder, fileNameWithoutExtension, extension); VSADDRESULT[] result = new VSADDRESULT[1]; project.AddItem(VSConstants.VSITEMID_ROOT, VSADDITEMOPERATION.VSADDITEMOP_OPENFILE, String.Empty, 1, new string[] { newModelFilePath }, IntPtr.Zero, result); string fileName = Path.GetFileName(newModelFilePath); for (HierarchyNode n = project.FirstChild; n != null; n = n.NextSibling) { if (n is FileNode) { string nodeFileName = Path.GetFileName(n.GetMKDocument()); if (nodeFileName == fileName) { return(n as FileNode); } } } return(null); }
public void AddItemWithNullNameThrows() { ProjectNode projectNode = new ProjectNode(vsSolution, project.GUID); projectNode.AddItem(null); }
public void AddItemWithEmptyNameThrows() { ProjectNode projectNode = new ProjectNode(vsSolution, project.GUID); string itemName = ".cs"; string fullItemName = new FileInfo(itemName).FullName; projectNode.AddItem(itemName); }
public void AddItemWithInvalidNameThrows() { ProjectNode projectNode = new ProjectNode(vsSolution, project.GUID); string itemName = "Invalid<Name>"; projectNode.AddItem(itemName); }
public void TestOpenItem() { ProjectNode projectNode = new ProjectNode(vsSolution, project.GUID); string itemName = "item1.cs"; HierarchyNode item = projectNode.AddItem(itemName); IVsWindowFrame wnd = projectNode.OpenItem(item); Assert.IsNotNull(wnd); }
public void AddItemWithNullNameThrows() { ProjectNode projectNode = new ProjectNode(_vsSolution, _project.Guid); projectNode.AddItem(null); }