file_system_utils.rb 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. require 'rubygems'
  2. require 'rake'
  3. require 'set'
  4. require 'fileutils'
  5. require 'file_path_utils.rb'
  6. class FileSystemUtils
  7. constructor :file_wrapper
  8. # build up path list from input of one or more strings or arrays of (+/-) paths & globs
  9. def collect_paths(*paths)
  10. raw = [] # all paths and globs
  11. plus = Set.new # all paths to expand and add
  12. minus = Set.new # all paths to remove from plus set
  13. # assemble all globs and simple paths, reforming our glob notation to ruby globs
  14. paths.each do |paths_container|
  15. case (paths_container)
  16. when String then raw << (FilePathUtils::reform_glob(paths_container))
  17. when Array then paths_container.each {|path| raw << (FilePathUtils::reform_glob(path))}
  18. else raise "Don't know how to handle #{paths_container.class}"
  19. end
  20. end
  21. # iterate through each path and glob
  22. raw.each do |path|
  23. dirs = [] # container for only (expanded) paths
  24. # if a glob, expand it and slurp up all non-file paths
  25. if path.include?('*')
  26. # grab base directory only if globs are snug up to final path separator
  27. if (path =~ /\/\*+$/)
  28. dirs << FilePathUtils.extract_path(path)
  29. end
  30. # grab expanded sub-directory globs
  31. expanded = @file_wrapper.directory_listing( FilePathUtils.extract_path_no_aggregation_operators(path) )
  32. expanded.each do |entry|
  33. dirs << entry if @file_wrapper.directory?(entry)
  34. end
  35. # else just grab simple path
  36. # note: we could just run this through glob expansion but such an
  37. # approach doesn't handle a path not yet on disk)
  38. else
  39. dirs << FilePathUtils.extract_path_no_aggregation_operators(path)
  40. end
  41. # add dirs to the appropriate set based on path aggregation modifier if present
  42. FilePathUtils.add_path?(path) ? plus.merge(dirs) : minus.merge(dirs)
  43. end
  44. return (plus - minus).to_a.uniq
  45. end
  46. # given a file list, add to it or remove from it
  47. def revise_file_list(list, revisions)
  48. revisions.each do |revision|
  49. # include or exclude file or glob to file list
  50. file = FilePathUtils.extract_path_no_aggregation_operators( revision )
  51. FilePathUtils.add_path?(revision) ? list.include(file) : list.exclude(file)
  52. end
  53. end
  54. end