Class | FileList |
In: |
lib/more/facets/filelist.rb
|
Parent: | Object |
A FileList is essentially an array with helper methods to make file manipulation easier.
FileLists 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 FileList holds the pattern for latter use.
This allows us to define a number of FileList to match any number of files, but only search out the actual files when then FileList itself is actually used. The key is that the first time an element of the FileList/Array is requested, the pending patterns are resolved into a real list of file names.
fl = FileList.new fl.include('./**/*') fl.exclude('./*~')
ARRAY_METHODS | = | Array.instance_methods - Object.instance_methods | List of array methods (that are not in Object) that need to be delegated. | |
MUST_DEFINE | = | %w[to_a inspect] | List of additional methods that must be delegated. | |
MUST_NOT_DEFINE | = | %w[to_a to_ary partition *] | 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 + - & | ] | List of delegated methods that return new array values which need wrapping. | |
DELEGATING_METHODS | = | (ARRAY_METHODS + MUST_DEFINE - MUST_NOT_DEFINE).sort.uniq | ||
DEFAULT_IGNORE_PATTERNS | = | [ /(^|[\/\\])CVS([\/\\]|$)/, /(^|[\/\\])\.svn([\/\\]|$)/, /(^|[\/\\])_darcs([\/\\]|$)/, /\.bak$/, /~$/, /(^|[\/\\])core$/ |
Clear the ignore patterns.
# File lib/more/facets/filelist.rb, line 460 460: def clear_ignore_patterns 461: @exclude_patterns = [ /^$/ ] 462: 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 = FileList.new['lib/**/*.rb', 'test/test*.rb'] pkg_files = FileList.new['lib/**/*'] do |fl| fl.exclude(/\bCVS\b/) end
# File lib/more/facets/filelist.rb, line 169 169: def initialize(*patterns) 170: @pending_add = [] 171: @pending = false 172: @exclude_patterns = DEFAULT_IGNORE_PATTERNS.dup 173: @exclude_re = nil 174: @items = [] 175: patterns.each { |pattern| include(pattern) } 176: yield self if block_given? 177: 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/more/facets/filelist.rb, line 455 455: def select_default_ignore_patterns 456: @exclude_patterns = DEFAULT_IGNORE_PATTERNS.dup 457: end
Define equality.
# File lib/more/facets/filelist.rb, line 235 235: def ==(array) 236: to_ary == array 237: end
# File lib/more/facets/filelist.rb, line 273 273: def calculate_exclude_regexp 274: ignores = [] 275: @exclude_patterns.each do |pat| 276: case pat 277: when Regexp 278: ignores << pat 279: when /[*.]/ 280: Dir[pat].each do |p| ignores << p end 281: else 282: ignores << Regexp.quote(pat) 283: end 284: end 285: if ignores.empty? 286: @exclude_re = /^$/ 287: else 288: re_str = ignores.collect { |p| "(" + p.to_s + ")" }.join("|") 289: @exclude_re = Regexp.new(re_str) 290: end 291: end
Grep each of the files in the filelist using the given pattern. If a block is given, call the block on each matching line, passing the file name, line number, and the matching line of text. If no block is given, a standard emac style file:linenumber:line message will be printed to standard out.
# File lib/more/facets/filelist.rb, line 369 369: def egrep(pattern) 370: each do |fn| 371: open(fn) do |inf| 372: count = 0 373: 374: inf.each do |line| 375: count += 1 376: if pattern.match(line) 377: if block_given? 378: yield fn, count, line 379: else 380: puts "#{fn}:#{count}:#{line}" 381: end 382: end 383: end 384: 385: end 386: end 387: 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:
FileList['a.c', 'b.c'].exclude("a.c") => ['b.c'] FileList['a.c', 'b.c'].exclude(/^a/) => ['b.c']
If "a.c" is a file, then …
FileList['a.c', 'b.c'].exclude("a.*") => ['b.c']
If "a.c" is not a file, then …
FileList['a.c', 'b.c'].exclude("a.*") => ['a.c', 'b.c']
# File lib/more/facets/filelist.rb, line 219 219: def exclude(*patterns) 220: patterns.each do |pat| @exclude_patterns << pat end 221: if ! @pending 222: calculate_exclude_regexp 223: reject! { |fn| fn =~ @exclude_re } 224: end 225: self 226: end
Should the given file name be excluded?
# File lib/more/facets/filelist.rb, line 415 415: def exclude?(fn) 416: calculate_exclude_regexp unless @exclude_re 417: fn =~ @exclude_re 418: end
Return a new array with String#ext method applied to each member of the array.
This method is a shortcut for:
array.collect { |item| item.ext(newext) }
ext is a user added method for the Array class.
# File lib/more/facets/filelist.rb, line 360 360: def ext(newext='') 361: collect { |fn| fn.ext(newext) } 362: end
Return a new FileList with the results of running gsub against each element of the original list.
Example:
FileList['lib/test/file', 'x/y'].gsub(/\//, "\\") => ['lib\\test\\file', 'x\\y']
# File lib/more/facets/filelist.rb, line 336 336: def gsub(pat, rep) 337: inject(FileList.new) { |res, fn| res << fn.gsub(pat,rep) } 338: end
# File lib/more/facets/filelist.rb, line 430 430: def import(array) 431: @items = array 432: self 433: 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/more/facets/filelist.rb, line 186 186: def include(*filenames) 187: # TODO: check for pending 188: filenames.each do |fn| 189: if fn.respond_to? :to_ary 190: include(*fn.to_ary) 191: else 192: @pending_add << fn 193: end 194: end 195: @pending = true 196: self 197: end
Resolve all the pending adds now.
# File lib/more/facets/filelist.rb, line 263 263: def resolve 264: if @pending 265: @pending = false 266: @pending_add.each do |fn| resolve_add(fn) end 267: @pending_add = [] 268: resolve_exclude 269: end 270: self 271: end
# File lib/more/facets/filelist.rb, line 293 293: def resolve_add(fn) 294: case fn 295: when Array 296: fn.each { |f| self.resolve_add(f) } 297: when %r{[*?]} 298: add_matching(fn) 299: else 300: self << fn 301: end 302: end
# File lib/more/facets/filelist.rb, line 304 304: def resolve_exclude 305: @exclude_patterns.each do |pat| 306: case pat 307: when Regexp 308: reject! { |fn| fn =~ pat } 309: when /[*.]/ 310: reject_list = Dir[pat] 311: reject! { |fn| reject_list.include?(fn) } 312: else 313: reject! { |fn| fn == pat } 314: end 315: end 316: self 317: end
Return a new FileList with the results of running sub against each element of the oringal list.
Example:
FileList['a.c', 'b.c'].sub(/\.c$/, '.o') => ['a.o', 'b.o']
# File lib/more/facets/filelist.rb, line 325 325: def sub(pat, rep) 326: inject(FileList.new) { |res, fn| res << fn.sub(pat,rep) } 327: end
Return the internal array object.
# File lib/more/facets/filelist.rb, line 240 240: def to_a 241: resolve 242: @items 243: end
Return the internal array object.
# File lib/more/facets/filelist.rb, line 246 246: def to_ary 247: resolve 248: @items 249: end