public static bool TryAddItem(IVsProject project, string path)
        {
            var item = project.GetProjectItem(path);

            if (item == null)
            {
                project.EnsureIsCheckout();
                if (!Directory.Exists(Path.GetDirectoryName(path)))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(path));
                }

                if (!File.Exists(path))
                {
                    File.Create(path).Dispose();
                }
                //
                // Check again in case the item gets auto included once the file
                // has been created
                //
                item = project.GetProjectItem(path);
                if (item == null)
                {
                    project.AddFromFile(path);
                }
                return(true);
            }
            return(false);
        }
Exemplo n.º 2
0
 public int OnBeforeDocumentWindowShow(uint docCookie, int fFirstShow, IVsWindowFrame pFrame)
 {
     try
     {
         if (fFirstShow != 0)
         {
             IVsProject project = null;
             uint       itemref = 0;
             string     path    = null;
             GetDocumentInfo(docCookie, ref project, ref itemref, ref path);
             if (project != null && !string.IsNullOrEmpty(path))
             {
                 if (project.IsIceBuilderGeneratedItem(path))
                 {
                     ProjectItem item = project.GetProjectItem(itemref);
                     if (item != null)
                     {
                         item.Document.ReadOnly = true;
                     }
                 }
             }
         }
     }
     catch (Exception)
     {
         // Could happend with some document types
     }
     return(0);
 }
        public static EnvDTE.ProjectItem GetProjectItem(this IVsProject project, string path)
        {
            int  found;
            uint item;
            var  priority = new VSDOCUMENTPRIORITY[1];

            ErrorHandler.ThrowOnFailure(project.IsDocumentInProject(path, out found, priority, out item));
            if (found == 0 || (priority[0] != VSDOCUMENTPRIORITY.DP_Standard && priority[0] != VSDOCUMENTPRIORITY.DP_Intrinsic))
            {
                return(null);
            }
            return(project.GetProjectItem(item));
        }
Exemplo n.º 4
0
 public int OnAfterAddFilesEx(int projectsLength, int filesLength, IVsProject[] projects, int[] indices,
                              string[] names, VSADDFILEFLAGS[] rgFlags)
 {
     ThreadHelper.JoinableTaskFactory.Run(async() =>
     {
         await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
         try
         {
             for (int i = 0; i < projectsLength; ++i)
             {
                 IVsProject project = projects[i];
                 if (project.IsMSBuildIceBuilderInstalled())
                 {
                     int j = indices[i];
                     int k = i < (projectsLength - 1) ? indices[i + 1] : filesLength;
                     for (; j < k; ++j)
                     {
                         string path = names[i];
                         if (ProjectUtil.IsSliceFileName(path))
                         {
                             //
                             // Ensure the .ice file item has SliceCompile ItemType
                             //
                             var projectItem = project.GetProjectItem(path);
                             if (projectItem != null)
                             {
                                 project.EnsureIsCheckout();
                                 var property = projectItem.Properties.Item("ItemType");
                                 if (property != null && !property.Value.Equals("SliceCompile"))
                                 {
                                     property.Value = "SliceCompile";
                                 }
                             }
                             ProjectUtil.AddGeneratedFiles(project, path);
                             break;
                         }
                     }
                 }
             }
         }
         catch (OperationCanceledException)
         {
             // Ignore, this could happen if the project is reloaded
         }
         catch (Exception ex)
         {
             Package.UnexpectedExceptionWarning(ex);
         }
     });
     return(0);
 }
Exemplo n.º 5
0
 public int OnAfterAddFilesEx(int projectsLength, int filesLength, IVsProject[] projects, int[] indices,
                              string[] names, VSADDFILEFLAGS[] rgFlags)
 {
     try
     {
         for (int i = 0; i < projectsLength; ++i)
         {
             IVsProject project = projects[i];
             if (project.IsMSBuildIceBuilderInstalled())
             {
                 int j = indices[i];
                 int k = i < (projectsLength - 1) ? indices[i + 1] : filesLength;
                 for (; j < k; ++j)
                 {
                     string path = names[i];
                     if (ProjectUtil.IsSliceFileName(path))
                     {
                         //
                         // Ensure the .ice file item has SliceCompile ItemType
                         //
                         var projectItem = project.GetProjectItem(path);
                         if (projectItem != null)
                         {
                             project.EnsureIsCheckout();
                             var property = projectItem.Properties.Item("ItemType");
                             if (property != null && !property.Value.Equals("SliceCompile"))
                             {
                                 property.Value = "SliceCompile";
                             }
                         }
                         ProjectUtil.AddGeneratedFiles(project, path);
                         break;
                     }
                 }
             }
         }
     }
     catch (Exception ex)
     {
         Package.UnexpectedExceptionWarning(ex);
     }
     return(0);
 }
        public static void DeleteItems(this IVsProject project, List <string> paths)
        {
            project.EnsureIsCheckout();
            foreach (string path in paths)
            {
                EnvDTE.ProjectItem item = project.GetProjectItem(path);
                if (item != null)
                {
                    if (File.Exists(path))
                    {
                        item.Delete();
                    }
                    else
                    {
                        item.Remove();
                    }
                }
            }

            var sliceCompileDependencies = paths.Select(
                p =>
            {
                return(Path.Combine(Path.GetDirectoryName(p),
                                    string.Format("SliceCompile.{0}.d", Path.GetFileNameWithoutExtension(p))));
            }).Distinct().ToList();

            foreach (var path in sliceCompileDependencies)
            {
                if (File.Exists(path))
                {
                    try
                    {
                        File.Delete(path);
                    }
                    catch (IOException)
                    {
                    }
                }
            }
        }
Exemplo n.º 7
0
 public int OnBeforeDocumentWindowShow(uint docCookie, int fFirstShow, IVsWindowFrame pFrame)
 {
     ThreadHelper.JoinableTaskFactory.Run(async() =>
     {
         await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
         try
         {
             if (fFirstShow != 0)
             {
                 IVsProject project = null;
                 uint itemref       = 0;
                 string path        = null;
                 GetDocumentInfo(docCookie, ref project, ref itemref, ref path);
                 if (project != null && !string.IsNullOrEmpty(path) &&
                     (path.EndsWith(".cs", StringComparison.CurrentCultureIgnoreCase) ||
                      path.EndsWith(".cpp", StringComparison.CurrentCultureIgnoreCase) ||
                      path.EndsWith(".h", StringComparison.CurrentCultureIgnoreCase)))
                 {
                     if (project.IsIceBuilderGeneratedItem(path))
                     {
                         ProjectItem item = project.GetProjectItem(itemref);
                         if (item != null)
                         {
                             item.Document.ReadOnly = true;
                         }
                     }
                 }
             }
         }
         catch (Exception)
         {
             // Could happend with some document types
         }
     });
     return(0);
 }