A FileList is essentially an array with a few helper methods defined to make file manipulation a bit 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.
- *
- ==
- []
- add
- calculate_exclude_regexp
- clear_exclude
- egrep
- exclude
- exclude?
- existing
- existing!
- ext
- gsub
- gsub!
- import
- include
- is_a?
- kind_of?
- new
- pathmap
- resolve
- sub
- sub!
- to_a
- to_ary
- to_s
| ARRAY_METHODS | = | (Array.instance_methods - Object.instance_methods).map { |n| n.to_s } |
| 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).collect{ |s| s.to_s }.sort.uniq |
| DEFAULT_IGNORE_PATTERNS | = | [ /(^|[\/\\])CVS([\/\\]|$)/, /(^|[\/\\])\.svn([\/\\]|$)/, /\.bak$/, /~$/ |
| DEFAULT_IGNORE_PROCS | = | [ proc { |fn| fn =~ /(^|[\/\\])core$/ && ! File.directory?(fn) } |
Create a new file list including the files listed. Similar to:
FileList.new(*args)
[ show source ]
# File lib/rake.rb, line 1590
1590: def [](*args)
1591: new(*args)
1592: 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
[ show source ]
# File lib/rake.rb, line 1296
1296: def initialize(*patterns)
1297: @pending_add = []
1298: @pending = false
1299: @exclude_patterns = DEFAULT_IGNORE_PATTERNS.dup
1300: @exclude_procs = DEFAULT_IGNORE_PROCS.dup
1301: @exclude_re = nil
1302: @items = []
1303: patterns.each { |pattern| include(pattern) }
1304: yield self if block_given?
1305: end
Redefine * to return either a string or a new file list.
[ show source ]
# File lib/rake.rb, line 1391
1391: def *(other)
1392: result = @items * other
1393: case result
1394: when Array
1395: FileList.new.import(result)
1396: else
1397: result
1398: end
1399: end
Define equality.
[ show source ]
# File lib/rake.rb, line 1369
1369: def ==(array)
1370: to_ary == array
1371: end
Alias for include
[ show source ]
# File lib/rake.rb, line 1412
1412: def calculate_exclude_regexp
1413: ignores = []
1414: @exclude_patterns.each do |pat|
1415: case pat
1416: when Regexp
1417: ignores << pat
1418: when /[*?]/
1419: Dir[pat].each do |p| ignores << p end
1420: else
1421: ignores << Regexp.quote(pat)
1422: end
1423: end
1424: if ignores.empty?
1425: @exclude_re = /^$/
1426: else
1427: re_str = ignores.collect { |p| "(" + p.to_s + ")" }.join("|")
1428: @exclude_re = Regexp.new(re_str)
1429: end
1430: end
[ show source ]
# File lib/rake.rb, line 1361
1361: def clear_exclude
1362: @exclude_patterns = []
1363: @exclude_procs = []
1364: calculate_exclude_regexp if ! @pending
1365: self
1366: 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.
[ show source ]
# File lib/rake.rb, line 1507
1507: def egrep(pattern, *options)
1508: each do |fn|
1509: open(fn, "rb", *options) do |inf|
1510: count = 0
1511: inf.each do |line|
1512: count += 1
1513: if pattern.match(line)
1514: if block_given?
1515: yield fn, count, line
1516: else
1517: puts "#{fn}:#{count}:#{line}"
1518: end
1519: end
1520: end
1521: end
1522: end
1523: 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. In addition, a block given to exclude will remove entries that return true when given to the block.
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']
[ show source ]
# File lib/rake.rb, line 1348
1348: def exclude(*patterns, &block)
1349: patterns.each do |pat|
1350: @exclude_patterns << pat
1351: end
1352: if block_given?
1353: @exclude_procs << block
1354: end
1355: resolve_exclude if ! @pending
1356: self
1357: end
Should the given file name be excluded?
[ show source ]
# File lib/rake.rb, line 1565
1565: def exclude?(fn)
1566: calculate_exclude_regexp unless @exclude_re
1567: fn =~ @exclude_re || @exclude_procs.any? { |p| p.call(fn) }
1568: end
Return a new file list that only contains file names from the current file list that exist on the file system.
[ show source ]
# File lib/rake.rb, line 1527
1527: def existing
1528: select { |fn| File.exist?(fn) }
1529: end
Modify the current file list so that it contains only file name that exist on the file system.
[ show source ]
# File lib/rake.rb, line 1533
1533: def existing!
1534: resolve
1535: @items = @items.select { |fn| File.exist?(fn) }
1536: self
1537: end
Return a new FileList 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.
[ show source ]
# File lib/rake.rb, line 1497
1497: def ext(newext='')
1498: collect { |fn| fn.ext(newext) }
1499: 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']
[ show source ]
# File lib/rake.rb, line 1466
1466: def gsub(pat, rep)
1467: inject(FileList.new) { |res, fn| res << fn.gsub(pat,rep) }
1468: end
Same as gsub except that the original file list is modified.
[ show source ]
# File lib/rake.rb, line 1477
1477: def gsub!(pat, rep)
1478: each_with_index { |fn, i| self[i] = fn.gsub(pat,rep) }
1479: self
1480: end
@exclude_patterns = DEFAULT_IGNORE_PATTERNS.dup
[ show source ]
# File lib/rake.rb, line 1581
1581: def import(array)
1582: @items = array
1583: self
1584: 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 )
[ show source ]
# File lib/rake.rb, line 1314
1314: def include(*filenames)
1315: # TODO: check for pending
1316: filenames.each do |fn|
1317: if fn.respond_to? :to_ary
1318: include(*fn.to_ary)
1319: else
1320: @pending_add << fn
1321: end
1322: end
1323: @pending = true
1324: self
1325: end
Lie about our class.
[ show source ]
# File lib/rake.rb, line 1385
1385: def is_a?(klass)
1386: klass == Array || super(klass)
1387: end
Apply the pathmap spec to each of the included file names, returning a new file list with the modified paths. (See String#pathmap for details.)
[ show source ]
# File lib/rake.rb, line 1485
1485: def pathmap(spec=nil)
1486: collect { |fn| fn.pathmap(spec) }
1487: end
Resolve all the pending adds now.
[ show source ]
# File lib/rake.rb, line 1402
1402: def resolve
1403: if @pending
1404: @pending = false
1405: @pending_add.each do |fn| resolve_add(fn) end
1406: @pending_add = []
1407: resolve_exclude
1408: end
1409: self
1410: 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']
[ show source ]
# File lib/rake.rb, line 1455
1455: def sub(pat, rep)
1456: inject(FileList.new) { |res, fn| res << fn.sub(pat,rep) }
1457: end
Same as sub except that the oringal file list is modified.
[ show source ]
# File lib/rake.rb, line 1471
1471: def sub!(pat, rep)
1472: each_with_index { |fn, i| self[i] = fn.sub(pat,rep) }
1473: self
1474: end
Return the internal array object.
[ show source ]
# File lib/rake.rb, line 1374
1374: def to_a
1375: resolve
1376: @items
1377: end
Return the internal array object.
[ show source ]
# File lib/rake.rb, line 1380
1380: def to_ary
1381: to_a
1382: end
Convert a FileList to a string by joining all elements with a space.
[ show source ]
# File lib/rake.rb, line 1551
1551: def to_s
1552: resolve
1553: self.join(' ')
1554: end