/// <summary> /// Uses <see cref="DteHelper.FindItemByName"/> to search for the item specified by the "Name" attributte /// </summary> /// <param name="currentValue"></param> /// <param name="newValue"></param> /// <returns></returns> /// <seealso cref="ValueProvider.OnBeginRecipe"/> /// <seealso cref="DteHelper.FindItemByName"/> public override bool OnBeginRecipe(object currentValue, out object newValue) { if (currentValue == null) { IDictionaryService dictservice = (IDictionaryService) ServiceHelper.GetService(this, typeof(IDictionaryService)); string itemFullName = ArgumentsHelper.ReplaceToken(itemFullNameExpression, dictservice); Project project = dictservice.GetValue(projectArgumentName) as Project; if (project != null) { try { newValue = DteHelper.FindItemByName(project.ProjectItems, itemFullName, true); } catch { //With Web projects without any subfolder this method throws an exception newValue = DteHelper.FindItemByName(project.ProjectItems, itemFullName, false); } if (newValue != null) { return(true); } } } newValue = currentValue; return(false); }
/// <summary> /// Delete ProjectItems content filtering by the input kind guid /// </summary> public override void Execute() { if (projectItems != null) { foreach (ProjectItem projectItem in projectItems) { if (string.IsNullOrEmpty(kind) || projectItem.Kind.Equals(kind, StringComparison.InvariantCultureIgnoreCase)) { DTE vs = (DTE)GetService(typeof(DTE)); if (string.Compare(projectItem.Name, "properties", StringComparison.InvariantCultureIgnoreCase) == 0) { DeleteItem(projectItem, vs); } } } } else if (this.projectItem != null) { DTE vs = (DTE)GetService(typeof(DTE)); DeleteItem(projectItem, vs); } else if (project != null && !string.IsNullOrEmpty(itemName)) { DTE vs = (DTE)GetService(typeof(DTE)); this.projectItem = DteHelper.FindItemByName(project.ProjectItems, itemName, true); if (this.projectItem != null) { DeleteItem(projectItem, vs); } } }
/// <summary> /// Finds the project item in project. /// </summary> /// <param name="project">The project.</param> /// <param name="name">The name.</param> /// <param name="recursive">if set to <c>true</c> [recursive].</param> /// <returns>ProjectItem instance of set name or null if not found</returns> public static ProjectItem FindProjectItemInProject(Project project, string name, bool recursive) { ProjectItem projectItem = null; if (project.Kind != EnvDTE.Constants.vsProjectKindSolutionItems) { if (project.ProjectItems != null && project.ProjectItems.Count > 0) { projectItem = DteHelper.FindItemByName(project.ProjectItems, name, recursive); } } else { // if solution folder, one of its ProjectItems might be a real project foreach (ProjectItem item in project.ProjectItems) { var realProject = item.Object as Project; if (realProject != null) { projectItem = FindProjectItemInProject(realProject, name, recursive); if (projectItem != null) { break; } } } } return(projectItem); }
/// <summary> /// Searchs the fol configuration files and nest them. ///From Radim: http://stackoverflow.com/questions/19427333/how-to-find-a-projectitem-by-the-file-name /// </summary> /// <param name="projectItems">The project items.</param> private static void SearchFolConfigFilesAndNestThem(ProjectItems projectItems) { foreach (ProjectItem projectItem in projectItems) { string transFileName; if (projectItem.TryGetPropertyValue(ProjectItemExt.FullPathProperty, out transFileName)) { transFileName = Path.GetFileName(transFileName); string configFileName; if (TransformationProvider.CheckTransformationFileAndGetSourceFileFromIt(transFileName, Options.Options.TransfomationFileNameRegexp, Options.Options.SourceFileRegexpMatchIndex, out configFileName)) { var configItem = DteHelper.FindItemByName(projectItems, configFileName, true); var itemToBeNested = DteHelper.FindItemByName(projectItems, transFileName, true); if (configItem == null || itemToBeNested == null) { continue; } // ReSharper disable once UnusedVariable var pitn = configItem.ProjectItems.AddFromFile(itemToBeNested.GetFullPath()); string itemType; if (itemToBeNested.TryGetPropertyValue(ProjectItemExt.ItemTypeProperty, out itemType)) { pitn.TrySetPropertyValue(ProjectItemExt.ItemTypeProperty, itemType); } } } } }
/// <summary> /// The method that creates a new item from the intput string. /// </summary> public override void Execute() { if (!string.IsNullOrEmpty(itemName)) { ProjectItem item = DteHelper.FindItemByName(targetProject.ProjectItems, itemName, true); if (item != null) { AddItem(item, item.ProjectItems); } } else if (projectItem != null) { AddItem(projectItem, projectItem.ProjectItems); } else if (targetProject != null) { AddItem(targetProject, targetProject.ProjectItems); } if (open && outputProjectItem != null) { Window wnd = outputProjectItem.Open(Constants.vsViewKindPrimary); wnd.Visible = true; wnd.Activate(); } }
private ProjectItems WhereToAdd() { ProjectItems whereToAdd; if ((destFolder != String.Empty) && (destFolder != null)) { //only works for a single foldername ProjectItem _folder = DteHelper.FindItemByName(this.Project.ProjectItems, destFolder, true); if (_folder != null) { whereToAdd = _folder.ProjectItems; } else { ProjectItems pitems = this.Project.ProjectItems; string projectpath = Helpers.GetFullPathOfProjectItem(Project); //folder doesnt exist //create the folder char[] sep = { '\\' }; string[] folders = destFolder.Split(sep); for (int i = 0; i < folders.Length; i++) { projectpath += folders[i] + "\\"; //does the folder already exist? _folder = DteHelper.FindItemByName(pitems, folders[i], true); if (_folder != null) { //folder exists pitems = _folder.ProjectItems; } else { //create folder try { _folder = pitems.AddFolder(folders[i], EnvDTE.Constants.vsProjectItemKindPhysicalFolder); pitems = _folder.ProjectItems; } catch (Exception) { _folder = pitems.AddFromDirectory(projectpath); pitems = _folder.ProjectItems; } } } whereToAdd = _folder.ProjectItems; } } else { whereToAdd = this.Project.ProjectItems; } return(whereToAdd); }
/// <summary> /// Executes the action. /// </summary> public override void Execute() { // Input validation if (string.IsNullOrEmpty(folderName) || currentProject == null) { Item = null; return; } DTE service = base.GetService <DTE>(true); this.Item = DteHelper.FindItemByName(currentProject.ProjectItems, folderName, true); }
public override void Execute() { if (_createFolder) { _createdItem = DteHelper.FindItemByName(_targetCollection, _folderName, false); if (_createdItem == null) { _createdItem = _targetCollection.AddFolder(_folderName, "{6BB5F8EF-4483-11D3-8BCF-00C04F8EC28C}"); } _folderCollection = _createdItem.ProjectItems; } else { _folderCollection = _targetCollection; } }
/// <summary> /// Adds the item. /// </summary> /// <param name="project">The project.</param> /// <param name="projectItems">The project items.</param> private void AddItem(Project project, ProjectItems projectItems) { if (!overwriteItemIfExists) { outputProjectItem = DteHelper.FindItemByName(projectItems, Path.GetFileName(sourceFilePath), false); if (outputProjectItem == null) { outputProjectItem = projectItems.AddFromFileCopy(sourceFilePath); } } else { File.Copy(sourceFilePath, Path.Combine(projectItem.Properties.Item("FullPath").Value.ToString(), Path.GetFileName(sourceFilePath)), true); } }
/// <summary> /// Finds the name of the item by. /// </summary> /// <param name="collection">The collection.</param> /// <param name="name">The name.</param> /// <param name="recursive">if set to <c>true</c> [recursive].</param> /// <returns></returns> public static ProjectItem FindItemByName(ProjectItems collection, string name, bool recursive) { foreach (ProjectItem item1 in collection) { if (string.Compare(item1.Name, name, StringComparison.InvariantCultureIgnoreCase) == 0) { return(item1); } if (recursive) { ProjectItem item2 = DteHelper.FindItemByName(item1.ProjectItems, name, recursive); if (item2 != null) { return(item2); } } } return(null); }
public override void Execute() { DTE vs = base.GetService <DTE>(true); string tempFilename = Path.GetTempFileName(); using (StreamWriter sw = new StreamWriter(tempFilename, false)) { sw.WriteLine(_content); } ProjectItem existingItem = DteHelper.FindItemByName(_targetCollection, _targetFileName, false); if (existingItem != null) { if (overwrite) { OverwriteFile(vs, existingItem.get_FileNames(1), _content); existingItem.Delete(); } } _createdItem = DteHelper.FindItemByName(_targetCollection, _targetFileName, false); if (_createdItem == null) { _createdItem = _targetCollection.AddFromTemplate(tempFilename, _targetFileName); } _createdProjectItems = _createdItem.ProjectItems; if (_open) { Window window = _createdItem.Open("{00000000-0000-0000-0000-000000000000}"); window.Visible = true; window.Activate(); } File.Delete(tempFilename); }
/// <summary> /// The method that creates a new item from the input string. /// </summary> public override void Execute() { if (ownerItem == null && ownerProject == null) { throw new ArgumentNullException(Properties.Resources.OwnerProjectOrItemRequired); } if (ownerItem != null) { ownerProject = ownerItem.ContainingProject; // If we got a parent item, build the virtual path to it // for the real full path we will work againt. string itemPath = String.Empty; DteHelper.BuildPathFromCollection(ownerProject.ProjectItems, ownerItem, ref itemPath); path = System.IO.Path.Combine(itemPath, path); } DTE vs = GetService <DTE>(); ProjectItem containingFolder; string targetPath; using (EnsureProjectFolderAction ensureFolder = new EnsureProjectFolderAction()) { this.Site.Container.Add(ensureFolder); ensureFolder.Path = path; ensureFolder.OwnerProject = ownerProject; ensureFolder.Execute(); containingFolder = ensureFolder.TargetFolder; } if (containingFolder.Object is Project) { containingFolder = new ProjectProjectItemAdapter((Project)containingFolder.Object); } using (EvaluateExpressionAction evaluateExpression = new EvaluateExpressionAction()) { this.Site.Container.Add(evaluateExpression); evaluateExpression.Expression = path; evaluateExpression.Execute(); targetPath = evaluateExpression.ReturnValue.ToString(); } string targetFileName = System.IO.Path.GetFileName(targetPath); // This may be different than the containingFolder variable, // as the EnsureProjectFolder only ensures folders, not // hierarchy of dependent items. So we need to look for // the actual parent which may be a project item, not a folder. ProjectItem parent = DteHelper.FindItemByPath( vs.Solution, System.IO.Path.Combine( DteHelper.BuildPath(ownerProject), /* This call just strips the last segment in the path. "Directory" * can still be a file name, as in the case of dependent items */ System.IO.Path.GetDirectoryName(targetPath) ) ); if (parent == null) { parent = containingFolder; } Debug.Assert(parent != null, "Didn't find parent with path: " + targetPath); addedItem = DteHelper.FindItemByName(parent.ProjectItems, targetFileName, false); if (addedItem != null) { if (overwrite) { OverwriteFile(vs, addedItem.get_FileNames(1), content); addedItem.Delete(); } } addedItem = DteHelper.FindItemByName(parent.ProjectItems, targetFileName, false); if (addedItem == null) { // At the FS level, dependent files are always inside the folder we determined // above using the EnsureProjectFolderAction. string folderPath = System.IO.Path.Combine( System.IO.Path.GetDirectoryName(ownerProject.FileName), DteHelper.BuildPath(containingFolder)); string fullPath = System.IO.Path.Combine(folderPath, targetFileName); if (File.Exists(fullPath)) { OverwriteFile(vs, fullPath, content); addedItem = parent.ProjectItems.AddFromFile(fullPath); } else { string tempfile = System.IO.Path.GetTempFileName(); try { File.WriteAllText(tempfile, content); addedItem = parent.ProjectItems.AddFromTemplate(tempfile, targetFileName); } finally { File.Delete(tempfile); } } } if (open) { Window wnd = addedItem.Open(Constants.vsViewKindPrimary); wnd.Visible = true; wnd.Activate(); } }
/// <summary> /// <see cref="IAction.Execute"/> /// </summary> public override void Execute() { this.ProjectItem = DteHelper.FindItemByName(this.Project.ProjectItems, this.ItemName, true); }
/// <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); } }