コード例 #1
0
        public void ImportedFilePathIsFound()
        {
            var sample                = ProjectSampleGenerator.GetProjectWithImportSample("SampleProject.csproj");
            var projectFilePath       = sample.ProjectFile.GetFullPath(Repository.BasePath);
            var importedPropsFilePath = Path.Combine(Repository.BasePath, sample.ImportedPropsFile.Name);

            Repository
            .WriteFile(sample.ProjectFile)
            .WriteFile(sample.ImportedPropsFile);

            var projectFile = new SlnFileWithPath(projectFilePath, new SlnFile(FileType.Project, ProjectId.CreateNewId()));
            var imports     = ProjectImportsFinder.FindProjectImports(new[] { projectFile });

            imports.Values.Should().BeEquivalentTo(new ImportedFile(importedPropsFilePath, new[] { projectFile }.ToImmutableList()));
        }
コード例 #2
0
        protected override async Task <Dictionary <string, SlnFile> > ProcessImpl(
            Task <Dictionary <string, SlnFile> > previousTask)
        {
            var fileDictObj = await previousTask;

            var fileDict = (Dictionary <string, SlnFile>)fileDictObj;

            var repoResult = GitRunner.FindRepository(_workingDirectory);

            if (!repoResult.foundRepo)
            {
                Logger.LogError("Unable to find Git repository located in {0}. Shutting down.", _workingDirectory);
                return(new Dictionary <string, SlnFile>());
            }

            // validate the target branch
            if (!DiffHelper.HasBranch(repoResult.repo, _targetGitBranch))
            {
                Logger.LogError("Current git repository doesn't have any branch named [{0}]. Shutting down.", _targetGitBranch);
                return(new Dictionary <string, SlnFile>());
            }

            var repo          = repoResult.repo;
            var affectedFiles = DiffHelper.ChangedFiles(repo, _targetGitBranch).ToList();

            var projectFiles   = fileDict.Where(x => x.Value.FileType == FileType.Project).ToList();
            var projectFolders = projectFiles.ToLookup(x => Path.GetDirectoryName(x.Key), v => Tuple.Create(v.Key, v.Value));
            var projectImports = ProjectImportsFinder.FindProjectImports(projectFiles.Select(pair => new SlnFileWithPath(pair.Key, pair.Value)));

            // filter out any files that aren't affected by the diff
            var newDict = new Dictionary <string, SlnFile>();

            foreach (var file in affectedFiles)
            {
                Logger.LogDebug("Affected file: {0}", file);
                // this file is in the solution
                if (fileDict.ContainsKey(file))
                {
                    newDict[file] = fileDict[file];
                }
                else
                {
                    // special case - not all of the affected files were in the solution.
                    // Check to see if these affected files are in the same folder as any of the projects
                    var directoryName = Path.GetDirectoryName(file);

                    if (TryFindSubFolder(projectFolders.Select(c => c.Key), directoryName, out var projectFolder))
                    {
                        var affectedProjects = projectFolders[projectFolder];
                        foreach (var affectedProject in affectedProjects)
                        {
                            var project     = affectedProject.Item2;
                            var projectPath = affectedProject.Item1;
                            Logger.LogInformation("Adding project {0} to the set of affected files because non-code file {1}, " +
                                                  "found inside same directory [{2}], was modified.", projectPath, file, directoryName);
                            newDict[projectPath] = project;
                        }
                    }
                }

                // special case - if affected file was imported to some projects, need to mark importing project as affected
                if (projectImports.ContainsKey(file))
                {
                    // Mark all dependant as affected
                    foreach (var dependentProject in projectImports[file].DependantProjects)
                    {
                        newDict[dependentProject.Path] = dependentProject.File;
                    }
                }
            }

            return(newDict);
        }