#29 Deduplicate CollectProjectDeps
Opened 8 months ago by krouma. Modified 8 months ago
krouma/golist code-dedup  into  master

file modified
+26 -31
@@ -348,47 +348,42 @@ 

  func (p *PackageInfoCollector) BuildPackageTree(includeMain, withTests, testsOnly bool) ([]string, error) {

  	// TODO(jchaloup): strip all main package unless explicitely requested

  	var entryPoints []string

- 	if testsOnly {

+ 	if testsOnly || withTests {

  		for p, pkgInfo := range p.packageInfos {

  			if len(pkgInfo.TestGoFiles) > 0 || len(pkgInfo.XTestGoFiles) > 0 {

  				entryPoints = append(entryPoints, p)

  			}

  		}

- 	} else {

- 		if withTests {

- 			for p, pkgInfo := range p.packageInfos {

- 				if len(pkgInfo.TestGoFiles) > 0 || len(pkgInfo.XTestGoFiles) > 0 {

- 					entryPoints = append(entryPoints, p)

- 				}

- 			}

+ 		if testsOnly {

+ 			return entryPoints, nil

  		}

+ 	}

  

- 		for pkgName, pkgInfo := range p.packageInfos {

- 			// check package name of each file

- 			var nonMainFiles []string

- 			files := pkgInfo.GoFiles

- 			files = append(files, pkgInfo.CgoFiles...)

- 			for _, file := range files {

- 				f, err := parser.ParseFile(token.NewFileSet(), path.Join(pkgInfo.Dir, file), nil, 0)

- 				if err != nil {

- 					return nil, err

- 				}

- 				if f.Name.Name == "main" && file != "doc.go" {

- 					importsList := make([]string, 0)

- 					for _, i := range f.Imports {

- 						importsList = append(importsList, i.Path.Value[1:len(i.Path.Value)-1])

- 					}

- 					p.mainFiles[path.Join(pkgInfo.ImportPath, file)] = importsList

- 				}

- 

- 				if !includeMain && f.Name.Name == "main" {

- 					continue

+ 	for pkgName, pkgInfo := range p.packageInfos {

+ 		// check package name of each file

+ 		var nonMainFiles []string

+ 		files := pkgInfo.GoFiles

+ 		files = append(files, pkgInfo.CgoFiles...)

+ 		for _, file := range files {

+ 			f, err := parser.ParseFile(token.NewFileSet(), path.Join(pkgInfo.Dir, file), nil, 0)

+ 			if err != nil {

+ 				return nil, err

+ 			}

+ 			if f.Name.Name == "main" && file != "doc.go" {

+ 				importsList := make([]string, 0)

+ 				for _, i := range f.Imports {

+ 					importsList = append(importsList, i.Path.Value[1:len(i.Path.Value)-1])

  				}

- 				nonMainFiles = append(nonMainFiles, file)

+ 				p.mainFiles[path.Join(pkgInfo.ImportPath, file)] = importsList

  			}

- 			if len(nonMainFiles) > 0 {

- 				entryPoints = append(entryPoints, pkgName)

+ 

+ 			if !includeMain && f.Name.Name == "main" {

+ 				continue

  			}

+ 			nonMainFiles = append(nonMainFiles, file)

+ 		}

+ 		if len(nonMainFiles) > 0 {

+ 			entryPoints = append(entryPoints, pkgName)

  		}

  	}

  

There is duplicate code when handling only test files and all files

Metadata