public bool Equals(VSProjectItem project) { return(project.Hash == this.Hash); }
internal void AddChild(VSProjectItem item) { this.childItems.Add(item); }
public VSProjectItem(VSProject project, object internalProjectItem, VSProjectItem parentItem = null, bool isHidden = false) { FileInfo file; if (internalProjectItem is string) { var path = (string)internalProjectItem; if (path == "References") { RelativePath = path; this.itemType = "References"; } else { RelativePath = path; FilePath = Path.Combine(Path.GetDirectoryName(project.FileName), path); if (File.Exists(FilePath)) { this.itemType = "File"; } else { this.itemType = "Folder"; } } this.project = project; this.HasMetadata = false; this.Include = null; this.Hash = this.RelativePath.GetHash(); } else { this.itemType = s_ProjectItemElement_ItemType.GetValue(internalProjectItem, null) as string; this.Include = s_ProjectItemElement_Include.GetValue(internalProjectItem, null) as string; this.HasMetadata = (bool)s_ProjectItemElement_HasMetadata.GetValue(internalProjectItem, null); this.project = project; this.internalProjectItem = internalProjectItem; file = new FileInfo(project.FileName); metadata = s_ProjectItemElement_Metadata.GetValue(internalProjectItem, null) as ICollection <ProjectMetadataElement>; if (this.itemType != "ProjectConfiguration") { if (this.itemType == "WebReferenceUrl" || this.itemType == "Reference") { var hintPath = metadata.SingleOrDefault(m => m.Name == "HintPath"); if (hintPath != null) { RelativePath = hintPath.Value; } FilePath = this.Include.RemoveSurroundingSlashes(); } else { if (this.itemType != "SchemaFiles" && this.itemType != "WebReferenceUrl") { try { var include = this.Include.RemoveSurroundingSlashes(); var pattern = string.Format(@"(?<variable>\$\((?<value>{0})\))", StringExtensions.REGEX_IDENTIFIER_MIDSTRING); var hasErrors = true; var errorCount = 0; while (hasErrors) { hasErrors = false; if (include.RegexIsMatch(pattern)) { var regex = new Regex(pattern); var match = regex.Match(include); var variable = match.Groups["variable"].Value; var value = match.Groups["value"].Value; if (this.ParentProject.Properties.Any(p => p.Name == value)) { var property = this.ParentProject.Properties.Single(p => p.Name == value); var propertyValue = property.Value; propertyValue = propertyValue.Replace("$(MSBuildThisFileDirectory)", Path.GetDirectoryName(this.ParentProject.FileName) + @"\"); include = include.RegexReplace(pattern, propertyValue); } else { include = include.Replace("$(MSBuildThisFileDirectory)", Path.GetDirectoryName(this.ParentProject.FileName) + @"\"); } } errorCount++; if (errorCount > 10) { DebugUtils.Break(); } } RelativePath = include; FilePath = Path.GetFullPath(Path.Combine(file.DirectoryName, RelativePath)); } catch (Exception ex) { FilePath = ex.Message; } } } } } this.IsHidden = isHidden; this.InstanceId = Guid.NewGuid(); try { if (this.itemType != "SchemaFiles" && this.itemType != "WebReferenceUrl") { UpdateInfo(); } } catch { } childItems = new List <IVSProjectItem>(); if (parentItem != null) { this.ParentItem = parentItem; parentItem.AddChild(this); } }
public void RemoveItem(VSProjectItem item) { ProjectElementContainer parent = ((ProjectElement)item.InternalProjectItem).Parent; parent.RemoveChild((ProjectElement)item.InternalProjectItem); }
private void AddToItems(string fileNameSource, ProjectItemElement element, bool isUnsaved = true) { var relativePath = fileNameSource.RemoveStart(Path.GetDirectoryName(this.FileName) + @"\"); var item = new VSProjectItem(this, element); var existingItem = items.SingleOrDefault(i => i.FilePath == item.FilePath); if (existingItem != null) { if (existingItem.IsHidden) { items.Remove(existingItem); } else { DebugUtils.Break(); } } items.Add(item); if (item.ProjectPath == null) { DebugUtils.Break(); } else { if (item.ProjectPath != null && !item.HasDependentUpon) { VSProjectItem parentItem = null; var parts = item.ProjectPath.Split('\\', StringSplitOptions.RemoveEmptyEntries); string path = null; foreach (var part in parts) { if (path == null) { path = part; } else { path += @"\" + part; } if (part == item.Name) { if (parentItem != null) { if (item.ParentItem != (IVSProjectItem)parentItem) { if (parentItem.ChildItems.Any(i => i.FilePath == item.FilePath)) { existingItem = (VSProjectItem)parentItem.ChildItems.Single(i => i.FilePath == item.FilePath); if (existingItem.IsHidden) { parentItem.Remove(existingItem); } } item.ParentItem = parentItem; parentItem.AddChild(item); } } } else { if (items.Any(i => i.RelativePath == path)) { parentItem = items.Single(i => i.RelativePath == path); if (parentItem.IsHidden) { parentItem.IsHidden = false; } } else { var newItem = new VSProjectItem(this, path, parentItem); items.Add(newItem); newItem.IsUnsaved = isUnsaved; parentItem = newItem; } } } } else { var dependencyItem = item; var dependentUponItem = items.Single(i => i.RelativePath == dependencyItem.RelativePath.RemoveEnd(dependencyItem.Name) + dependencyItem.DependentUponMetadata.Value); dependencyItem.IsUnsaved = isUnsaved; dependencyItem.DependentUpon = dependentUponItem; dependentUponItem.AddChild(dependencyItem); if (dependencyItem.ParentItem != null && dependencyItem.ParentItem.ChildItems.Any(i => i.FilePath == dependencyItem.FilePath)) { ((VSProjectItem)dependencyItem.ParentItem).Remove(dependencyItem); } dependencyItem.ParentItem = dependentUponItem; } } }
private IEnumerable <IVSProjectItem> GetItems() { ICollection propertyCollection; List <VSProjectItem> references; int count; var dependencyItemsToResolve = new List <VSProjectItem>(); List <VSProjectItem> unsavedItems; if (this.InPotentiallyPartialIterator()) { var fullItems = this.GetItems(); foreach (var item in fullItems.ToList()) { yield return(item); } yield break; } itemCollection = (ICollection)s_ProjectRootElement_Items.GetValue(rootElement, null); count = itemCollection.Count; unsavedItems = items.Where(i => i.IsUnsaved).ToList(); items.Clear(); foreach (var unsavedItem in unsavedItems) { items.Add(unsavedItem); if (unsavedItem.DependentUponMetadata == null) { yield return(unsavedItem); } } foreach (var item in itemCollection) { var projectItem = new VSProjectItem(this, item); if (!unsavedItems.Any(i => i.FilePath == projectItem.FilePath)) { items.Add(projectItem); if (projectItem.DependentUponMetadata == null) { yield return(projectItem); } } } references = items.Where(i => i.ItemType.IsOneOf("Reference", "ProjectReference")).ToList(); if (references.Count > 0) { var referenceFolder = new VSProjectItem(this, "References"); items.Add(referenceFolder); yield return(referenceFolder); foreach (var reference in references) { reference.ParentItem = referenceFolder; referenceFolder.AddChild(reference); } } foreach (var item in items.Where(i => i.ItemType.NotOneOf("Reference", "ProjectReference")).ToList()) { if (item.ProjectPath != null && !item.HasDependentUpon) { VSProjectItem parentItem = null; var parts = item.ProjectPath.Split('\\', StringSplitOptions.RemoveEmptyEntries); string path = null; foreach (var part in parts) { if (path == null) { path = part; } else { path += @"\" + part; } if (part == item.Name) { if (parentItem != null) { item.ParentItem = parentItem; parentItem.AddChild(item); } } else { if (items.Any(i => i.RelativePath == path)) { parentItem = items.Single(i => i.RelativePath == path); } else { var newItem = new VSProjectItem(this, path, parentItem); items.Add(newItem); yield return(newItem); parentItem = newItem; } } } } else { if (item.HasDependentUpon) { dependencyItemsToResolve.Add(item); } } } if (includeHiddenItems) { var directoryName = Path.GetDirectoryName(this.FileName); var directory = new DirectoryInfo(directoryName); Action <DirectoryInfo> recurseItems = null; Action <string> addParts = null; var hiddenItems = new List <VSProjectItem>(); addParts = (p) => { var parts = p.Split('\\', StringSplitOptions.RemoveEmptyEntries); string path = null; VSProjectItem parentItem = null; foreach (var part in parts) { if (path == null) { path = part; } else { path += @"\" + part; } if (items.Any(i => i.RelativePath == path)) { VSProjectItem item; if (unsavedItems.Any(i => i.RelativePath == path)) { item = unsavedItems.Single(i => i.RelativePath == path); } else { item = items.Single(i => i.RelativePath == path); if (parentItem != null) { if (item.ParentItem != (IVSProjectItem)parentItem) { item.ParentItem = parentItem; parentItem.AddChild(item); } } } parentItem = item; } else { if (unsavedItems.Any(i => i.RelativePath == path)) { var item = unsavedItems.Single(i => i.RelativePath == path); parentItem = item; } else { var newItem = new VSProjectItem(this, path, parentItem, true); hiddenItems.Add(newItem); items.Add(newItem); parentItem = newItem; } } } }; recurseItems = (d) => { foreach (var subDirectory in d.GetDirectories()) { var relativePath = subDirectory.FullName.RemoveStart(directory.FullName + "\\"); addParts(relativePath); recurseItems(subDirectory); } foreach (var file in d.GetFiles()) { var relativePath = file.FullName.RemoveStart(directory.FullName + "\\"); addParts(relativePath); } }; recurseItems(directory); foreach (var hiddenItem in hiddenItems) { yield return(hiddenItem); } } propertyCollection = (ICollection)s_ProjectRootElement_Properties.GetValue(rootElement, null); properties.Clear(); foreach (var item in propertyCollection) { properties.Add(new VSProjectProperty(this, (ProjectPropertyElement)item)); } foreach (var dependencyItem in dependencyItemsToResolve) { try { var dependentUponItem = items.Single(i => i.RelativePath == dependencyItem.RelativePath.RemoveEnd(dependencyItem.Name) + dependencyItem.DependentUponMetadata.Value); if (!dependentUponItem.ChildItems.Any(i => i.FilePath == dependencyItem.FilePath)) { dependencyItem.DependentUpon = dependencyItem; if (dependencyItem.ParentItem != null && dependencyItem.ParentItem.ChildItems.Any(i => i.FilePath == dependencyItem.FilePath)) { ((VSProjectItem)dependencyItem.ParentItem).Remove(dependencyItem); } dependentUponItem.AddChild(dependencyItem); dependencyItem.ParentItem = dependentUponItem; if (items.Any(i => i.FilePath == dependencyItem.FilePath)) { var existingItem = items.Single(i => i.FilePath == dependencyItem.FilePath); existingItem.IsSubItem = true; } } } catch { } } loadOnDemand = false; }