本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.3KB

  1. package packages
  2. import (
  3. "fmt"
  4. "os"
  5. "sort"
  6. )
  7. // Visit visits all the packages in the import graph whose roots are
  8. // pkgs, calling the optional pre function the first time each package
  9. // is encountered (preorder), and the optional post function after a
  10. // package's dependencies have been visited (postorder).
  11. // The boolean result of pre(pkg) determines whether
  12. // the imports of package pkg are visited.
  13. func Visit(pkgs []*Package, pre func(*Package) bool, post func(*Package)) {
  14. seen := make(map[*Package]bool)
  15. var visit func(*Package)
  16. visit = func(pkg *Package) {
  17. if !seen[pkg] {
  18. seen[pkg] = true
  19. if pre == nil || pre(pkg) {
  20. paths := make([]string, 0, len(pkg.Imports))
  21. for path := range pkg.Imports {
  22. paths = append(paths, path)
  23. }
  24. sort.Strings(paths) // Imports is a map, this makes visit stable
  25. for _, path := range paths {
  26. visit(pkg.Imports[path])
  27. }
  28. }
  29. if post != nil {
  30. post(pkg)
  31. }
  32. }
  33. }
  34. for _, pkg := range pkgs {
  35. visit(pkg)
  36. }
  37. }
  38. // PrintErrors prints to os.Stderr the accumulated errors of all
  39. // packages in the import graph rooted at pkgs, dependencies first.
  40. // PrintErrors returns the number of errors printed.
  41. func PrintErrors(pkgs []*Package) int {
  42. var n int
  43. Visit(pkgs, nil, func(pkg *Package) {
  44. for _, err := range pkg.Errors {
  45. fmt.Fprintln(os.Stderr, err)
  46. n++
  47. }
  48. })
  49. return n
  50. }
上海开阖软件有限公司 沪ICP备12045867号-1