functional_core.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package watch
  2. import (
  3. "os"
  4. "path/filepath"
  5. "strings"
  6. "github.com/smartystreets/goconvey/web/server/messaging"
  7. )
  8. ///////////////////////////////////////////////////////////////////////////////
  9. func Categorize(items chan *FileSystemItem, root string, watchSuffixes []string) (folders, profiles, goFiles []*FileSystemItem) {
  10. for item := range items {
  11. if item.IsFolder && !isHidden(item.Name) && !foundInHiddenDirectory(item, root) {
  12. folders = append(folders, item)
  13. } else if strings.HasSuffix(item.Name, ".goconvey") && len(item.Name) > len(".goconvey") {
  14. profiles = append(profiles, item)
  15. } else {
  16. for _, suffix := range watchSuffixes {
  17. if strings.HasSuffix(item.Name, suffix) && !isHidden(item.Name) && !foundInHiddenDirectory(item, root) {
  18. goFiles = append(goFiles, item)
  19. }
  20. }
  21. }
  22. }
  23. return folders, profiles, goFiles
  24. }
  25. func foundInHiddenDirectory(item *FileSystemItem, root string) bool {
  26. path := item.Path
  27. if len(path) > len(root) {
  28. path = path[len(root):]
  29. }
  30. for _, folder := range strings.Split(filepath.Dir(path), slash) {
  31. if isHidden(folder) {
  32. return true
  33. }
  34. }
  35. return false
  36. }
  37. func isHidden(path string) bool {
  38. return strings.HasPrefix(path, ".") || strings.HasPrefix(path, "_") || strings.HasPrefix(path, "flymake_")
  39. }
  40. ///////////////////////////////////////////////////////////////////////////////
  41. func ParseProfile(profile string) (isDisabled bool, tags, arguments []string) {
  42. lines := strings.Split(profile, "\n")
  43. for _, line := range lines {
  44. line = strings.TrimSpace(line)
  45. if len(arguments) == 0 && strings.ToLower(line) == "ignore" {
  46. return true, nil, nil
  47. } else if strings.HasPrefix(line, "-tags=") {
  48. tags = append(tags, strings.Split(strings.SplitN(line, "=", 2)[1], ",")...)
  49. continue
  50. } else if len(line) == 0 {
  51. continue
  52. } else if strings.HasPrefix(line, "#") {
  53. continue
  54. } else if strings.HasPrefix(line, "//") {
  55. continue
  56. } else if line == "-cover" || strings.HasPrefix(line, "-coverprofile") {
  57. continue
  58. } else if line == "-v" {
  59. continue // Verbose mode is always enabled so there is no need to record it here.
  60. }
  61. arguments = append(arguments, line)
  62. }
  63. return false, tags, arguments
  64. }
  65. ///////////////////////////////////////////////////////////////////////////////
  66. func CreateFolders(items []*FileSystemItem) messaging.Folders {
  67. folders := map[string]*messaging.Folder{}
  68. for _, item := range items {
  69. folders[item.Path] = &messaging.Folder{Path: item.Path, Root: item.Root}
  70. }
  71. return folders
  72. }
  73. ///////////////////////////////////////////////////////////////////////////////
  74. func LimitDepth(folders messaging.Folders, depth int) {
  75. if depth < 0 {
  76. return
  77. }
  78. for path, folder := range folders {
  79. if strings.Count(path[len(folder.Root):], slash) > depth {
  80. delete(folders, path)
  81. }
  82. }
  83. }
  84. ///////////////////////////////////////////////////////////////////////////////
  85. func AttachProfiles(folders messaging.Folders, items []*FileSystemItem) {
  86. for _, profile := range items {
  87. if folder, exists := folders[filepath.Dir(profile.Path)]; exists {
  88. folder.Disabled, folder.BuildTags, folder.TestArguments = profile.ProfileDisabled, profile.ProfileTags, profile.ProfileArguments
  89. }
  90. }
  91. }
  92. ///////////////////////////////////////////////////////////////////////////////
  93. func MarkIgnored(folders messaging.Folders, ignored map[string]struct{}) {
  94. if len(ignored) == 0 {
  95. return
  96. }
  97. for _, folder := range folders {
  98. for ignored := range ignored {
  99. if !folder.Ignored && strings.HasSuffix(folder.Path, ignored) {
  100. folder.Ignored = true
  101. }
  102. }
  103. }
  104. }
  105. ///////////////////////////////////////////////////////////////////////////////
  106. func ActiveFolders(folders messaging.Folders) messaging.Folders {
  107. var active messaging.Folders = map[string]*messaging.Folder{}
  108. for path, folder := range folders {
  109. if folder.Ignored || folder.Disabled {
  110. continue
  111. }
  112. active[path] = folder
  113. }
  114. return active
  115. }
  116. ///////////////////////////////////////////////////////////////////////////////
  117. func Sum(folders messaging.Folders, items []*FileSystemItem) int64 {
  118. var sum int64
  119. for _, item := range items {
  120. if _, exists := folders[filepath.Dir(item.Path)]; exists {
  121. sum += item.Size + item.Modified
  122. }
  123. }
  124. return sum
  125. }
  126. ///////////////////////////////////////////////////////////////////////////////
  127. const slash = string(os.PathSeparator)
  128. ///////////////////////////////////////////////////////////////////////////////