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 static void AddGeneratedFiles(IVsProject project, string file)
 {
     if (project.IsCppProject())
     {
         AddCppGeneratedFiles(project, file);
     }
     else
     {
         AddCSharpGeneratedFiles(project, file);
         //
         // Workaround for .NET Core project system adding duplicate
         // items when the added item is part of a glob
         project.RemoveGeneratedItemDuplicates();
     }
 }
 public void InitializeProject(IVsProject project)
 {
     if (project.IsMSBuildIceBuilderInstalled())
     {
         if (project.IsCppProject())
         {
             VCUtil.SetupSliceFilter(project.GetDTEProject());
         }
         else
         {
             if (project is IVsAggregatableProject)
             {
                 project.AddProjectFlavorIfNotExists(IceBuilderNewFlavor);
             }
         }
     }
 }
 //
 // Check if IceBuilder 4.x is enabled
 //
 public static IceBuilderProjectType IsIceBuilderEnabled(IVsProject project)
 {
     if (project != null)
     {
         IceBuilderProjectType type = project.IsCppProject() ? IceBuilderProjectType.CppProjectType :
                                      project.IsCSharpProject() ? IceBuilderProjectType.CsharpProjectType : IceBuilderProjectType.None;
         if (type != IceBuilderProjectType.None)
         {
             return(project.WithProject((MSProject msproject) =>
             {
                 if (MSBuildUtils.IsIceBuilderEnabled(msproject))
                 {
                     return type;
                 }
                 return IceBuilderProjectType.None;
             }));
         }
     }
     return(IceBuilderProjectType.None);
 }
        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
        }
        public static void SetupGenerated(IVsProject project)
        {
            ThreadHelper.ThrowIfNotOnUIThread();
            var projectDir = project.GetProjectBaseDirectory();

            //
            // Remove all CompileClCompile and ClInclude items that have an associted SliceCompileSource
            // item metadata that doesn't much any of the project SliceCompile items
            //
            if (project.IsCppProject())
            {
                project.DeleteItems(project.WithProject((MSProject msproject) =>
                {
                    var sliceCompile = msproject.AllEvaluatedItems.Where(
                        item => item.ItemType.Equals("SliceCompile")).Select(
                        item => item.EvaluatedInclude);

                    return(msproject.AllEvaluatedItems.Where(
                               item =>
                    {
                        if (item.ItemType.Equals("ClCompile") || item.ItemType.Equals("ClInclude"))
                        {
                            if (item.HasMetadata("SliceCompileSource"))
                            {
                                var value = item.GetMetadataValue("SliceCompileSource");
                                return !sliceCompile.Contains(value);
                            }
                        }
                        return false;
                    }).Select(item => Path.Combine(projectDir, item.EvaluatedInclude)).ToList());
                }));
            }
            else // C# project
            {
                project.DeleteItems(project.WithProject((MSProject msproject) =>
                {
                    var sliceCompile = msproject.AllEvaluatedItems.Where(
                        item => item.ItemType.Equals("SliceCompile")).Select(
                        item => item.EvaluatedInclude);

                    return(msproject.AllEvaluatedItems.Where(
                               item =>
                    {
                        if (item.ItemType.Equals("Compile"))
                        {
                            if (item.HasMetadata("SliceCompileSource"))
                            {
                                var value = item.GetMetadataValue("SliceCompileSource");
                                return !sliceCompile.Contains(value);
                            }
                        }
                        return false;
                    }).Select(item => Path.Combine(projectDir, item.EvaluatedInclude)).ToList());
                }));
            }

            // Now add any missing generated items
            var sliceItems = project.GetIceBuilderItems();

            foreach (var item in sliceItems)
            {
                AddGeneratedFiles(project, item);
            }
        }