public static ProjectItem AddFileToProject(
            string projectName,
            string fileName,
            string customTool = "TextTemplatingFileGenerator")
        {
            ProjectItem projectItem = null;

            RetryUtil.RetryOnException(() =>
            {
                var project = GetSolutionProjects().First(n => n.Name == projectName);

                var path = Path.GetFullPath(fileName);

                projectItem = project.ProjectItems.AddFromFile(path);

                var property = projectItem.Properties.Item("CustomTool");

                if (property == null)
                {
                    throw new ArgumentException("The property CustomTool was not found.");
                }

                property.Value = customTool;
            });

            return(projectItem);
        }
        private static IEnumerable <Project> GetSolutionProjects()
        {
            IEnumerable <Project> projects = null;

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

            return(projects);
        }
 public static void RemoveFileFromProject(string fileName)
 {
     RetryUtil.RetryOnException(() =>
     {
         var projectItem = Dte.Solution.FindProjectItem(fileName);
         projectItem?.Remove();
     });
 }
 public static void ReplaceLineInProjectItem(ProjectItem item, string line, string replaceWith)
 {
     RetryUtil.RetryOnException(() =>
     {
         item.Document.ReplaceText(line, replaceWith);
         item.Save();
     });
 }
        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);
        }
Exemplo n.º 6
0
        public static DTE GetCurrentDte()
        {
            DTE dte = null;

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

            return(dte);
        }
        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();
            });
        }
        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;
                }
            });

            return(customTool);
        }
        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);
                        }
                    }
                }
            });
        }