| Class | PathList |
| In: |
lib/path_list.rb
|
| Parent: | Object |
A PathList is essentially an array with a few helper methods defined to make file manipulation a bit easier.
PathLists are lazy. When given a list of glob patterns for possible files to be included in the file list, instead of searching the file structures to find the files, a PathList holds the pattern for latter use.
This allows us to define a number of PathList to match any number of files, but only search out the actual files when then PathList itself is actually used. The key is that the first time an element of the PathList/Array is requested, the pending patterns are resolved into a real list of file names.
| ARRAY_METHODS | = | Array.instance_methods - Object.instance_methods unless defined? ARRAY_METHODS | List of array methods (that are not in Object) that need to be delegated. | |
| MUST_DEFINE | = | %w[to_a inspect] unless defined? MUST_DEFINE | List of additional methods that must be delegated. | |
| MUST_NOT_DEFINE | = | %w[to_a to_ary partition *] unless defined? MUST_NOT_DEFINE | List of methods that should not be delegated here (we define special versions of them explicitly below). | |
| SPECIAL_RETURN | = | %w[ map collect sort sort_by select find_all reject grep compact flatten uniq values_at + - & | ] unless defined? SPECIAL_RETURN | List of delegated methods that return new array values which need wrapping. | |
| DELEGATING_METHODS | = | (ARRAY_METHODS + MUST_DEFINE - MUST_NOT_DEFINE).sort.uniq \ unless defined? DELEGATING_METHODS | ||
| DEFAULT_IGNORE_PATTERNS | = | [ /(^|[\/\\])CVS([\/\\]|$)/, /(^|[\/\\])\.svn([\/\\]|$)/, /\.bak$/, /~$/, /(^|[\/\\])core$/ |
| each | -> | traditional_each |
| <<<< | ||
Clear the ignore patterns.
# File lib/path_list.rb, line 464 def clear_ignore_patterns @exclude_patterns = [ /^$/ ] end
Create a file list from the globbable patterns given. If you wish to perform multiple includes or excludes at object build time, use the "yield self" pattern.
Example:
file_list = PathList.new['lib/**/*.rb', 'test/test*.rb']
pkg_files = PathList.new['lib/**/*'] do |fl|
fl.exclude(/\bCVS\b/)
end
# File lib/path_list.rb, line 136 def initialize(*patterns) @pending_add = [] @pending = false @exclude_patterns = DEFAULT_IGNORE_PATTERNS.dup @exclude_re = nil @items = [] patterns.each { |pattern| include(pattern) } yield self if block_given? end
Set the ignore patterns back to the default value. The default patterns will ignore files
Note that file names beginning with "." are automatically ignored by Ruby‘s glob patterns and are not specifically listed in the ignore patterns.
# File lib/path_list.rb, line 459 def select_default_ignore_patterns @exclude_patterns = DEFAULT_IGNORE_PATTERNS.dup end
# File lib/path_list.rb, line 243 def calculate_exclude_regexp ignores = [] @exclude_patterns.each do |pat| case pat when Regexp ignores << pat when /[*.]/ Dir[pat].each do |p| ignores << p end else ignores << Regexp.quote(pat) end end if ignores.empty? @exclude_re = /^$/ else re_str = ignores.collect { |p| "(" + p.to_s + ")" }.join("|") @exclude_re = Regexp.new(re_str) end end
# File lib/path_list.rb, line 51 def clone sibling = self.class.new instance_variables.each do |ivar| value = self.instance_variable_get(ivar) sibling.instance_variable_set(ivar, value.try_dup) end sibling end
# File lib/path_list.rb, line 471 def each ( &block ) traditional_each do |x| if x.is_a? PathAndMatchData block[x, *x.match.to_a[1..-1]] else block[x] end end end
Register a list of file name patterns that should be excluded from the list. Patterns may be regular expressions, glob patterns or regular strings.
Note that glob patterns are expanded against the file system. If a file is explicitly added to a file list, but does not exist in the file system, then an glob pattern in the exclude list will not exclude the file.
Examples:
PathList['a.c', 'b.c'].exclude("a.c") => ['b.c']
PathList['a.c', 'b.c'].exclude(/^a/) => ['b.c']
If "a.c" is a file, then …
PathList['a.c', 'b.c'].exclude("a.*") => ['b.c']
If "a.c" is not a file, then …
PathList['a.c', 'b.c'].exclude("a.*") => ['a.c', 'b.c']
# File lib/path_list.rb, line 186 def exclude(*patterns) patterns.each do |pat| @exclude_patterns << pat end if ! @pending calculate_exclude_regexp reject! { |fn| fn.to_s =~ @exclude_re } end self end
Should the given file name be excluded?
# File lib/path_list.rb, line 414 def exclude?(fn) calculate_exclude_regexp unless @exclude_re # <<<< fn.to_s =~ @exclude_re # >>>> end
Return a new PathList with the results of running gsub against each element of the original list.
Example:
PathList['lib/test/file', 'x/y'].gsub(/\//, "\\")
=> ['lib\\test\\file', 'x\\y']
# File lib/path_list.rb, line 314 def gsub(*args, &block) inject(PathList.new) { |res, fn| res << fn.gsub(*args, &block) } end
Add file names defined by glob patterns to the file list. If an array is given, add each element of the array.
Example:
file_list.include("*.java", "*.cfg")
file_list.include %w( math.c lib.h *.o )
# File lib/path_list.rb, line 153 def include(*filenames) # TODO: check for pending filenames.each do |fn| if fn.respond_to? :to_ary include(*fn.to_ary) else @pending_add << fn end end @pending = true self end
Resolve all the pending adds now.
# File lib/path_list.rb, line 233 def resolve if @pending @pending = false @pending_add.each do |fn| resolve_add(fn) end @pending_add = [] resolve_exclude end self end
# File lib/path_list.rb, line 263 def resolve_add(fn) case fn when Array fn.each { |f| self.resolve_add(f) } when %r{[*?\{]} add_matching(fn) else # >>>> self << fn.to_path # <<<< end end
# File lib/path_list.rb, line 276 def resolve_exclude @exclude_patterns.each do |pat| case pat when Regexp # <<<< reject! { |fn| fn.to_s =~ pat } # >>>> when /[*.]/ # <<<< reject_list = Pathname.glob(pat) # >>>> reject! { |fn| reject_list.include?(fn) } else # <<<< reject! { |fn| fn.to_s == pat.to_s } # >>>> end end self end
# File lib/path_list.rb, line 366 def to_yaml ( opts={} ) resolve if @pending to_a.to_yaml(opts) end
Add matching glob patterns.
# File lib/path_list.rb, line 385 def add_matching(pattern) # <<<< pattern = pattern.to_s re = Regexp.quote(pattern) while re =~ /\\\{/ re.gsub!(/\\\{([^{}]+)\\\}/) do '(?:' + $1.gsub(/,/, '|') + ')' end end re.gsub!(/\\\*\\\*\//, '(?:.+/)?') re.gsub!(/\\\*/, '[^/]*') re.gsub!(/\\\?/, '[^/]') re.gsub!(/\\([()])/, '\1') pattern.gsub!(/[()]/, '') Pathname.glob(pattern) do |fn| # >>>> next if exclude?(fn) if match_data = Regexp.new(re).match(fn.to_s) self << PathAndMatchData.new(fn, match_data) else self << fn end end end