Class: FilePathCollectionUtils

Inherits:
Object
  • Object
show all
Defined in:
lib/ceedling/file_path_collection_utils.rb

Instance Method Summary collapse

Instance Method Details

#collect_paths(paths) ⇒ Object

Build up a directory path list from one or more strings or arrays of (+:/-:) simple paths & globs



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ceedling/file_path_collection_utils.rb', line 25

def collect_paths(paths)
  plus  = Set.new # All real, expanded directory paths to add
  minus = Set.new # All real, expanded paths to exclude
  
  # Iterate each path possibly decorated with aggregation modifiers and/or containing glob characters
  paths.each do |path|
    dirs = [] # Working list for evaluated directory paths
  
    # Get path stripped of any +:/-: aggregation modifier
    _path = FilePathUtils.no_aggregation_decorators( path )

    # If it's a glob, modify it for Ceedling's recursive subdirectory convention
    _reformed = FilePathUtils::reform_subdirectory_glob( _path )

    # Expand paths using Ruby's Dir.glob()
    #  - A simple path will yield that path
    #  - A path glob will expand to one or more paths
    # Note: `sort()` becuase of Github Issue #860
    @file_wrapper.directory_listing( _reformed ).sort.each do |entry|
      # For each result, add it to the working list *only* if it's a directory
      # Previous validation has already made warnings about filepaths in the list
      dirs << entry if @file_wrapper.directory?(entry)
    end
    
    # For recursive directory glob at end of a path, collect parent directories too.
    # Ceedling's recursive glob convention includes parent directories (unlike Ruby's glob).
    if path.end_with?('/**') or path.end_with?('/*')
      parents = []
      
      dirs.each {|dir| parents << File.join(dir, '..')}

      # Handle edge case of subdirectory glob but no subdirectories and therefore no parents
      # (Containing parent directory still exists)
      parents << FilePathUtils.no_decorators( _path ) if dirs.empty?

      dirs += parents
    end

    # Based on aggregation modifiers, add entries to plus and minus sets.
    # Use full, absolute paths to ensure logical paths are compared properly.
    # './<path>' is logically equivalent to '<path>' but is not equivalent as strings.
    # Because plus and minus are sets, each insertion eliminates any duplicates
    # (such as the parent directories for each directory as added above).
    dirs.each do |dir|
      abs_path = File.expand_path( dir )
      if FilePathUtils.add_path?( path )
        plus << abs_path
      else
        minus << abs_path
      end
    end
  end

  # Use Set subtraction operator to remove any excluded paths
  paths = (plus - minus).to_a
  paths.map! {|path| shortest_path_from_working(path) }

  return paths
end

#revise_filelist(list, revisions) ⇒ Object

Given a file list, add to it or remove from it considering (+:/-:) aggregation operators. Rake’s FileList does not robustly handle relative filepaths and patterns. So, we rebuild the FileList ourselves and return it. TODO: Replace FileList with our own, better version.



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/ceedling/file_path_collection_utils.rb', line 90

def revise_filelist(list, revisions)
  plus  = Set.new # All real, expanded directory paths to add
  minus = Set.new # All real, expanded paths to exclude
  
  # Build base plus set for revised path
  list.each do |path|
    # Start with expanding all list entries to absolute paths
    plus << File.expand_path( path )
  end

  revisions.each do |revision|
    # Include or exclude revisions in file list
    path = FilePathUtils.no_aggregation_decorators( revision )
    
    # Working list of revisions
    filepaths = []

    # Expand path by pattern as needed and add only filepaths to working list
    @file_wrapper.directory_listing( path ).each do |entry|
      filepaths << File.expand_path( entry ) if !@file_wrapper.directory?( entry )
    end

    # Handle +: / -: revisions
    if FilePathUtils.add_path?( revision )
      plus.merge( filepaths )
    else
      minus.merge( filepaths )
    end
  end

  # Use Set subtraction operator to remove any excluded paths
  paths = (plus - minus).to_a
  paths.map! {|path| shortest_path_from_working(path) }

  return FileList.new( paths )
end

#setupObject



19
20
21
22
# File 'lib/ceedling/file_path_collection_utils.rb', line 19

def setup()
  # TODO: Update Dir.pwd() to use a project root once it has been figured out
  @working_dir_path = Pathname.new( Dir.pwd() )
end

#shortest_path_from_working(path) ⇒ Object



127
128
129
130
131
132
133
134
135
# File 'lib/ceedling/file_path_collection_utils.rb', line 127

def shortest_path_from_working(path)
  begin
    # Reform path from full absolute to nice, neat relative path instead
    (Pathname.new( path ).relative_path_from( @working_dir_path )).to_s
  rescue StandardError
    # If we can't form a relative path between these paths, use the absolute
    path 
  end
end