public void RemoveGeneratedItemCustomMetadata(IVsProject project, List <string> paths) { #if VS2017 || VS2019 var projectDir = project.GetProjectBaseDirectory(); project.UpdateProject((MSProject msproject) => { var items = msproject.Xml.Items.Where(item => { if (item.ItemType.Equals("Compile") || item.ItemType.Equals("ClCompile") || item.ItemType.Equals("ClInclude")) { return(paths.Contains(Path.Combine(projectDir, item.Update))); } else { return(false); } }); foreach (var item in items) { item.Parent.RemoveChild(item); } }); #endif }
public void AddFromFile(IVsProject project, string file) { #if VS2017 || VS2019 if (project.IsCppProject() || GetUnconfiguredProject(project) == null) { project.GetDTEProject().ProjectItems.AddFromFile(file); } else { project.UpdateProject((MSProject msproject) => { if (Path.GetExtension(file).Equals(".cs", StringComparison.CurrentCultureIgnoreCase)) { var path = FileUtil.RelativePath(Path.GetDirectoryName(msproject.FullPath), file); var items = msproject.GetItemsByEvaluatedInclude(path); if (items.Count == 0) { msproject.AddItem("Compile", path); } } }); } #else project.GetDTEProject().ProjectItems.AddFromFile(file); #endif }
public void SetGeneratedItemCustomMetadata(IVsProject project, string slice, string generated, List <string> excludedConfigurations = null) { project.UpdateProject((MSProject msproject) => { var item = msproject.AllEvaluatedItems.FirstOrDefault(i => generated.Equals(i.EvaluatedInclude)); if (item != null) { var element = item.Xml; if (excludedConfigurations != null) { foreach (var conf in excludedConfigurations) { var metadata = element.AddMetadata("ExcludedFromBuild", "true"); metadata.Condition = string.Format("'$(Configuration)|$(Platform)'=='{0}'", conf); } } // // Only set SliceCompileSource if the item doesn't originate // from a glob expression // if (item.EvaluatedInclude.Equals(item.UnevaluatedInclude)) { item.SetMetadataValue("SliceCompileSource", slice); } #if VS2017 || VS2019 // // With Visual Studio 2017 if the item originate from a glob we // update the item medata using the Update attribute // else { var updateItem = msproject.Xml.Items.FirstOrDefault(i => generated.Equals(i.Update)); if (updateItem == null) { updateItem = msproject.Xml.CreateItemElement(item.ItemType); var group = msproject.Xml.ItemGroups.FirstOrDefault(); if (group == null) { group = msproject.Xml.AddItemGroup(); } updateItem.Update = generated; group.AppendChild(updateItem); } var metadata = updateItem.Metadata.FirstOrDefault(m => m.Name.Equals("SliceCompileSource")); if (metadata != null) { metadata.Value = slice; } else { updateItem.AddMetadata("SliceCompileSource", slice); } } #endif } }); }
public void RemoveGeneratedItemDuplicates(IVsProject project) { #if VS2017 || VS2019 // // With .NET Core project system when default compile items is enabled we // can end up with duplicate generated items, as the call to AddItem doesn't // detect that the new create file is already part of a glob and adds a // second item with the given include. // if (!project.IsCppProject() && GetUnconfiguredProject(project) != null) { project.UpdateProject((MSProject msproject) => { var all = msproject.Xml.Items.Where(item => item.ItemType.Equals("Compile")); foreach (var item in all) { // // If there is a glob item that already match the evaluated include path we // can remove the non glob item as it is a duplicate // var globItem = msproject.AllEvaluatedItems.FirstOrDefault(i => { return(i.HasMetadata("SliceCompileSource") && !i.EvaluatedInclude.Equals(i.UnevaluatedInclude) && i.EvaluatedInclude.Equals(item.Include)); }); if (globItem != null) { item.Parent.RemoveChild(item); } } }); } #endif }