コード例 #1
0
        private static IEnumerable <Project> GetSolutionProjects()
        {
            IEnumerable <Project> projects = null;

            RetryUtil.RetryOnException(() => { projects = dte.Solution.Projects.Cast <Project>(); });

            return(projects);
        }
コード例 #2
0
 public static void RemoveFileFromProject(string fileName)
 {
     RetryUtil.RetryOnException(() =>
     {
         var projectItem = dte.Solution.FindProjectItem(fileName);
         projectItem?.Remove();
     });
 }
コード例 #3
0
 public static void ReplaceLineInProjectItem(ProjectItem item, string line, string replaceWith)
 {
     RetryUtil.RetryOnException(() =>
     {
         item.Document.ReplaceText(line, replaceWith);
         item.Save();
     });
 }
コード例 #4
0
        public static string GetProjectDirectory(string projectName)
        {
            string directory = null;

            RetryUtil.RetryOnException(() =>
            {
                var projectFilePath = GetSolutionProjects().First(p => p.Name.Contains(projectName))?.FullName;
                directory           = Path.GetDirectoryName(projectFilePath);
            });

            return(directory);
        }
コード例 #5
0
        public static DTE GetCurrentDte(string solutionName)
        {
            DTE dte = null;

            RetryUtil.RetryOnException(() =>
            {
                dte = GetDtes().First(x => x.Solution?.FileName.Contains(solutionName) == true);
                dte.MainWindow.Activate();
                dte.MainWindow.SetFocus();
            });

            return(dte);
        }
コード例 #6
0
        public static ProjectItem AddFileToProject(string projectName, string fileName)
        {
            ProjectItem projectItem = null;

            RetryUtil.RetryOnException(() =>
            {
                var project = GetSolutionProjects().First(n => n.Name == projectName);
                var path    = Path.GetFullPath(fileName);
                projectItem = project.ProjectItems.AddFromFile(path);
            });

            return(projectItem);
        }
コード例 #7
0
        public static void SaveFileAutomaticallyRunCustomTool(ProjectItem item)
        {
            RetryUtil.RetryOnException(() =>
            {
                if (CanOpenFileInCodeWindow && item.Document == null)
                {
                    var filePath = GetProjectItemFullPath(item);
                    dte.ItemOperations.OpenFile(filePath);
                }

                item.Open();
                item.Save();
            });
        }
コード例 #8
0
        public static string GetCustomToolByFileName(string name, string projectName)
        {
            string customTool = null;

            RetryUtil.RetryOnException(() =>
            {
                var item = dte.Solution.FindProjectItem(name);
                if (item != null)
                {
                    customTool = item.Properties.Item("CustomTool").Value?.ToString();
                }
            });

            return(customTool);
        }
コード例 #9
0
        public static void RecoverTemplateIfCorrupted(string path, string content)
        {
            RetryUtil.RetryOnException(() =>
            {
                // EnvDTE in .NET 5 can lost content of the template
                var template = File.ReadAllText(path);

                if (string.IsNullOrWhiteSpace(template))
                {
                    dte.ItemOperations.OpenFile(path);
                    dte.ActiveDocument.Close();
                    File.WriteAllText(path, content);
                    dte.ItemOperations.OpenFile(path);
                }
            });
        }
コード例 #10
0
        public static void CleanupFiles(string[] projectNames, string[] extensions)
        {
            RetryUtil.RetryOnException(() =>
            {
                var cleanupItems = new List <ProjectItem>();

                foreach (var projectName in projectNames)
                {
                    var project = GetSolutionProjects().First(p => p.Name == projectName);

                    var items = GetAllProjectItemsRecursive(project.ProjectItems);

                    foreach (var extension in extensions)
                    {
                        cleanupItems.AddRange(items.Where(n => n.Name.EndsWith(extension)));
                    }
                }

                foreach (var item in cleanupItems)
                {
                    var fullPath = GetProjectItemFullPath(item);

                    item.Remove();

                    if (File.Exists(fullPath))
                    {
                        File.Delete(fullPath);
                    }
                }

                foreach (var projectName in projectNames)
                {
                    var projectPath = GetProjectDirectory(projectName);
                    foreach (var extension in extensions)
                    {
                        foreach (var file in Directory.EnumerateFiles(
                                     projectPath,
                                     $"*{extension}",
                                     SearchOption.AllDirectories))
                        {
                            File.Delete(file);
                        }
                    }
                }
            });
        }