From 096bf22ca54c3effc7af04898bb23ad3c8a7bb9a Mon Sep 17 00:00:00 2001 From: Assaf Attias <49212512+attiasas@users.noreply.github.com> Date: Sun, 25 Feb 2024 13:39:43 +0200 Subject: [PATCH] Add exclude pattern for dotnet (#233) --- build/dotnet.go | 2 +- build/utils/dotnet/solution/solution.go | 28 ++++++++++++++--- build/utils/dotnet/solution/solution_test.go | 32 ++++++++++++++------ 3 files changed, 47 insertions(+), 15 deletions(-) diff --git a/build/dotnet.go b/build/dotnet.go index b753d0ac..ce06cb49 100644 --- a/build/dotnet.go +++ b/build/dotnet.go @@ -84,7 +84,7 @@ func (dm *DotnetModule) CalcDependencies() error { if err != nil { return err } - sol, err := solution.Load(dm.solutionPath, slnFile, dm.containingBuild.logger) + sol, err := solution.Load(dm.solutionPath, slnFile, "", dm.containingBuild.logger) if err != nil { return err } diff --git a/build/utils/dotnet/solution/solution.go b/build/utils/dotnet/solution/solution.go index 73876384..69c62e65 100644 --- a/build/utils/dotnet/solution/solution.go +++ b/build/utils/dotnet/solution/solution.go @@ -25,10 +25,10 @@ type Solution interface { var projectRegExp *regexp.Regexp -func Load(path, slnFile string, log utils.Log) (Solution, error) { +func Load(path, slnFile, excludePattern string, log utils.Log) (Solution, error) { solution := &solution{path: path, slnFile: slnFile} // Reads all projects from '.sln' files. - slnProjects, err := solution.getProjectsListFromSlns(log) + slnProjects, err := solution.getProjectsListFromSlns(excludePattern, log) if err != nil { return solution, err } @@ -148,13 +148,16 @@ func (solution *solution) DependenciesSourcesAndProjectsPathExist() bool { return len(solution.dependenciesSources) > 0 && len(solution.projects) > 0 } -func (solution *solution) getProjectsListFromSlns(log utils.Log) ([]project.Project, error) { +func (solution *solution) getProjectsListFromSlns(excludePattern string, log utils.Log) ([]project.Project, error) { slnProjects, err := solution.getProjectsFromSlns() if err != nil { return nil, err } if slnProjects != nil { - return solution.parseProjectsFromSolutionFile(slnProjects, log) + if len(excludePattern) > 0 { + log.Debug(fmt.Sprintf("Testing to exclude projects by pattern: %s", excludePattern)) + } + return solution.parseProjectsFromSolutionFile(slnProjects, excludePattern, log) } return nil, nil } @@ -174,7 +177,7 @@ func (solution *solution) loadProjects(slnProjects []project.Project, log utils. return nil } -func (solution *solution) parseProjectsFromSolutionFile(slnProjects []string, log utils.Log) ([]project.Project, error) { +func (solution *solution) parseProjectsFromSolutionFile(slnProjects []string, excludePattern string, log utils.Log) ([]project.Project, error) { var projects []project.Project for _, projectLine := range slnProjects { projectName, projFilePath, err := parseProjectLine(projectLine, solution.path) @@ -182,6 +185,14 @@ func (solution *solution) parseProjectsFromSolutionFile(slnProjects []string, lo log.Error(err) continue } + // Exclude projects by pattern. + if exclude, err := isProjectExcluded(projFilePath, excludePattern); err != nil { + log.Error(err) + continue + } else if exclude { + log.Debug(fmt.Sprintf("Skipping a project \"%s\", since the path '%s' is excluded", projectName, projFilePath)) + continue + } // Looking for .*proj files. if !strings.HasSuffix(filepath.Ext(projFilePath), "proj") { log.Debug(fmt.Sprintf("Skipping a project \"%s\", since it doesn't have a '.*proj' file path.", projectName)) @@ -192,6 +203,13 @@ func (solution *solution) parseProjectsFromSolutionFile(slnProjects []string, lo return projects, nil } +func isProjectExcluded(projFilePath, excludePattern string) (exclude bool, err error) { + if len(excludePattern) == 0 { + return + } + return regexp.MatchString(excludePattern, projFilePath) +} + func (solution *solution) loadSingleProjectFromDir(log utils.Log) error { // List files with .*proj extension. projFiles, err := utils.ListFilesByFilterFunc(solution.path, func(filePath string) (bool, error) { diff --git a/build/utils/dotnet/solution/solution_test.go b/build/utils/dotnet/solution/solution_test.go index 1878f040..9234c940 100644 --- a/build/utils/dotnet/solution/solution_test.go +++ b/build/utils/dotnet/solution/solution_test.go @@ -17,7 +17,7 @@ import ( var logger = utils.NewDefaultLogger(utils.INFO) func TestEmptySolution(t *testing.T) { - solution, err := Load(".", "", logger) + solution, err := Load(".", "", "", logger) if err != nil { t.Error(err) } @@ -142,6 +142,7 @@ func replaceCarriageSign(results []string) { } func TestLoad(t *testing.T) { + // Prepare log := utils.NewDefaultLogger(utils.INFO) wd, err := os.Getwd() if err != nil { @@ -156,13 +157,26 @@ func TestLoad(t *testing.T) { nugetCmd := exec.Command("nuget", "restore", filepath.Join(wd, "tmp", "nugetproj", "solutions", "nugetproj.sln")) assert.NoError(t, nugetCmd.Run()) - // 'nugetproj' contains 2 'packages.config' files for 2 projects - - // 1. located in the project's root directory. - // 2. located in solutions directory. - solution := solution{path: filepath.Join(wd, "testdata", "nugetproj", "solutions"), slnFile: "nugetproj.sln"} - solutions, err := Load(solution.path, solution.slnFile, log) - if err != nil { - t.Error(err) + testCases := []struct { + name string + excludePattern string + expectedProjectCount int + }{ + {"noExcludePattern", "", 2}, + {"excludePattern", "proj1", 1}, + } + + for _, testCase := range testCases { + t.Run(testCase.name, func(t *testing.T) { + // 'nugetproj' contains 2 'packages.config' files for 2 projects - + // 1. located in the project's root directory. + // 2. located in solutions directory. + solution := solution{path: filepath.Join(wd, "testdata", "nugetproj", "solutions"), slnFile: "nugetproj.sln"} + solutions, err := Load(solution.path, solution.slnFile, testCase.excludePattern, log) + if err != nil { + t.Error(err) + } + assert.Equal(t, testCase.expectedProjectCount, len(solutions.GetProjects())) + }) } - assert.Equal(t, 2, len(solutions.GetProjects())) }