Skip to content

Commit

Permalink
Add exclude pattern for dotnet (#233)
Browse files Browse the repository at this point in the history
  • Loading branch information
attiasas authored Feb 25, 2024
1 parent bd9687a commit 096bf22
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 15 deletions.
2 changes: 1 addition & 1 deletion build/dotnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
28 changes: 23 additions & 5 deletions build/utils/dotnet/solution/solution.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -174,14 +177,22 @@ 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)
if err != nil {
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))
Expand All @@ -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) {
Expand Down
32 changes: 23 additions & 9 deletions build/utils/dotnet/solution/solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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()))
}

0 comments on commit 096bf22

Please sign in to comment.