private void BuildEventsOnOnBuildBegin(vsBuildScope scope, vsBuildAction action)
        {
            Debug.WriteLine("BuildEventsOnOnBuildBegin {0} {1}", scope, action);

            _projects          = _dte.AllProjects();
            _solutionDirectory = Path.GetDirectoryName(_dte.Solution.FullName);

            // unhook event handlers to reduce risk of memory leaks
            foreach (MissingErrorTask task in _errorListProvider.Tasks)
            {
                task.Navigate -= SelectParentProjectInSolution;
                task.Navigate -= NewErrorOnNavigate;
            }

            _errorListProvider.Tasks.Clear();

            _gitignores.Clear();
            AddGitIgnoreFromDirectory(_solutionDirectory);

            _filters = new List <Regex>();

            if (!string.IsNullOrEmpty(Options.IgnorePhysicalFiles))
            {
                _filters.AddRange(Options.IgnorePhysicalFiles.Split(new[] { "\r\n" },
                                                                    StringSplitOptions.RemoveEmptyEntries).Select(p => FindFilesPatternToRegex.Convert(p.Trim())));
            }
        }
Beispiel #2
0
        private void FindMissingFiles()
        {
            // unhook event handlers to reduce risk of memory leaks
            foreach (MissingErrorTask task in _errorListProvider.Tasks)
            {
                task.Navigate -= SelectParentProjectInSolution;
                task.Navigate -= NewErrorOnNavigate;
            }

            _errorListProvider.Tasks.Clear();

            var projects = _dte.AllProjects();

            var solutionDirectory = Path.GetDirectoryName(_dte.Solution.FullName);

            var        gitIgnoreFile = Path.Combine(solutionDirectory, ".gitignore");
            IgnoreList gitignores    = null;

            if (Options.UseGitIgnore && File.Exists(gitIgnoreFile))
            {
                gitignores = new IgnoreList(gitIgnoreFile);
            }

            var filters = new List <Regex>();

            if (!string.IsNullOrEmpty(Options.IgnorePhysicalFiles))
            {
                filters.AddRange(Options.IgnorePhysicalFiles.Split(new[] { "\r\n" },
                                                                   StringSplitOptions.RemoveEmptyEntries).Select(p => FindFilesPatternToRegex.Convert(p.Trim())));
            }

            foreach (Project proj in projects)
            {
                Debug.WriteLine(proj.Name);

                IDictionary <string, string> dict = new Dictionary <string, string>();
                dict.Add("Configuration", proj.ConfigurationManager.ActiveConfiguration.ConfigurationName);
                using (var projectCollection = new ProjectCollection(dict))
                {
                    var buildProject = projectCollection.LoadProject(proj.FullName);

                    var physicalFiles          = new HashSet <string>(StringComparer.OrdinalIgnoreCase);
                    var logicalFiles           = new HashSet <string>(StringComparer.OrdinalIgnoreCase);
                    var physicalFileProjectMap = new Dictionary <string, string>();

                    NavigateProjectItems(proj.ProjectItems, buildProject, physicalFiles, logicalFiles,
                                         new HashSet <string>(), physicalFileProjectMap);

                    if (Options.NotIncludedFiles)
                    {
                        var errorCategory = Options.MessageLevel;

                        physicalFiles.ExceptWith(logicalFiles);

                        foreach (var file in physicalFiles)
                        {
                            Debug.WriteLine($"Physical file: {file}");

                            if (filters.Any(f => f.IsMatch(file)) || (gitignores != null && gitignores.IsIgnored(file, false)))
                            {
                                Debug.WriteLine("\tIgnored by filter");
                                continue;
                            }

                            IVsHierarchy hierarchyItem;
                            string       physicalFileProject = physicalFileProjectMap[file];
                            _solution.GetProjectOfUniqueName(physicalFileProject, out hierarchyItem);

                            var newError = new MissingErrorTask()
                            {
                                ErrorCategory = errorCategory,
                                Category      = TaskCategory.BuildCompile,
                                Text          = "File on disk is not included in project",
                                Code          = Constants.FileOnDiskNotInProject,
                                Document      = file,
                                HierarchyItem = hierarchyItem,
                                ProjectPath   = physicalFileProject,
                            };

                            newError.Navigate += SelectParentProjectInSolution;
                            Debug.WriteLine("\t\t** Missing");

                            _errorListProvider.Tasks.Add(newError);
                        }
                    }
                }
            }

            if (_errorListProvider.Tasks.Count > 0)
            {
                _errorListProvider.Show();

                if (Options.FailBuildOnError && Options.Timing == RunWhen.BeforeBuild)
                {
                    _dte.ExecuteCommand("Build.Cancel");
                }
            }
        }