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.
List of array methods (that are not in Object) that need to be delegated.
List of additional methods that must be delegated.
List of methods that should not be delegated here (we define special versions of them explicitly below).
List of delegated methods that return new array values which need wrapping.
Create a new file list including the files listed. Similar to:
FileList.new(*args)
# File lib/rake.rb, line 1589 1589: def [](*args) 1590: new(*args) 1591: 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/rake.rb, line 1295 1295: def initialize(*patterns) 1296: @pending_add = [] 1297: @pending = false 1298: @exclude_patterns = DEFAULT_IGNORE_PATTERNS.dup 1299: @exclude_procs = DEFAULT_IGNORE_PROCS.dup 1300: @exclude_re = nil 1301: @items = [] 1302: patterns.each { |pattern| include(pattern) } 1303: yield self if block_given? 1304: end
Redefine * to return either a string or a new file list.
# File lib/rake.rb, line 1390 1390: def *(other) 1391: result = @items * other 1392: case result 1393: when Array 1394: FileList.new.import(result) 1395: else 1396: result 1397: end 1398: end
Define equality.
# File lib/rake.rb, line 1368 1368: def ==(array) 1369: to_ary == array 1370: end
# File lib/rake.rb, line 1411 1411: def calculate_exclude_regexp 1412: ignores = [] 1413: @exclude_patterns.each do |pat| 1414: case pat 1415: when Regexp 1416: ignores << pat 1417: when /[*?]/ 1418: Dir[pat].each do |p| ignores << p end 1419: else 1420: ignores << Regexp.quote(pat) 1421: end 1422: end 1423: if ignores.empty? 1424: @exclude_re = /^$/ 1425: else 1426: re_str = ignores.collect { |p| "(" + p.to_s + ")" }.join("|") 1427: @exclude_re = Regexp.new(re_str) 1428: end 1429: end
Clear all the exclude patterns so that we exclude nothing.
# File lib/rake.rb, line 1360 1360: def clear_exclude 1361: @exclude_patterns = [] 1362: @exclude_procs = [] 1363: calculate_exclude_regexp if ! @pending 1364: self 1365: 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/rake.rb, line 1506 1506: def egrep(pattern, *options) 1507: each do |fn| 1508: open(fn, "rb", *options) do |inf| 1509: count = 0 1510: inf.each do |line| 1511: count += 1 1512: if pattern.match(line) 1513: if block_given? 1514: yield fn, count, line 1515: else 1516: puts "#{fn}:#{count}:#{line}" 1517: end 1518: end 1519: end 1520: end 1521: end 1522: 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']
# File lib/rake.rb, line 1347 1347: def exclude(*patterns, &block) 1348: patterns.each do |pat| 1349: @exclude_patterns << pat 1350: end 1351: if block_given? 1352: @exclude_procs << block 1353: end 1354: resolve_exclude if ! @pending 1355: self 1356: end
Should the given file name be excluded?
# File lib/rake.rb, line 1564 1564: def exclude?(fn) 1565: calculate_exclude_regexp unless @exclude_re 1566: fn =~ @exclude_re || @exclude_procs.any? { |p| p.call(fn) } 1567: end
Return a new file list that only contains file names from the current file list that exist on the file system.
# File lib/rake.rb, line 1526 1526: def existing 1527: select { |fn| File.exist?(fn) } 1528: end
Modify the current file list so that it contains only file name that exist on the file system.
# File lib/rake.rb, line 1532 1532: def existing! 1533: resolve 1534: @items = @items.select { |fn| File.exist?(fn) } 1535: self 1536: 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.
# File lib/rake.rb, line 1496 1496: def ext(newext='') 1497: collect { |fn| fn.ext(newext) } 1498: 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/rake.rb, line 1465 1465: def gsub(pat, rep) 1466: inject(FileList.new) { |res, fn| res << fn.gsub(pat,rep) } 1467: end
Same as gsub except that the original file list is modified.
# File lib/rake.rb, line 1476 1476: def gsub!(pat, rep) 1477: each_with_index { |fn, i| self[i] = fn.gsub(pat,rep) } 1478: self 1479: end
@exclude_patterns = DEFAULT_IGNORE_PATTERNS.dup
# File lib/rake.rb, line 1580 1580: def import(array) 1581: @items = array 1582: self 1583: 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/rake.rb, line 1313 1313: def include(*filenames) 1314: # TODO: check for pending 1315: filenames.each do |fn| 1316: if fn.respond_to? :to_ary 1317: include(*fn.to_ary) 1318: else 1319: @pending_add << fn 1320: end 1321: end 1322: @pending = true 1323: self 1324: end
Lie about our class.
# File lib/rake.rb, line 1384 1384: def is_a?(klass) 1385: klass == Array || super(klass) 1386: 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.)
# File lib/rake.rb, line 1484 1484: def pathmap(spec=nil) 1485: collect { |fn| fn.pathmap(spec) } 1486: end
Resolve all the pending adds now.
# File lib/rake.rb, line 1401 1401: def resolve 1402: if @pending 1403: @pending = false 1404: @pending_add.each do |fn| resolve_add(fn) end 1405: @pending_add = [] 1406: resolve_exclude 1407: end 1408: self 1409: 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/rake.rb, line 1454 1454: def sub(pat, rep) 1455: inject(FileList.new) { |res, fn| res << fn.sub(pat,rep) } 1456: end
Same as sub except that the oringal file list is modified.
# File lib/rake.rb, line 1470 1470: def sub!(pat, rep) 1471: each_with_index { |fn, i| self[i] = fn.sub(pat,rep) } 1472: self 1473: end
Return the internal array object.
# File lib/rake.rb, line 1373 1373: def to_a 1374: resolve 1375: @items 1376: end
Return the internal array object.
# File lib/rake.rb, line 1379 1379: def to_ary 1380: to_a 1381: end
Convert a FileList to a string by joining all elements with a space.
# File lib/rake.rb, line 1550 1550: def to_s 1551: resolve 1552: self.join(' ') 1553: end
Add matching glob patterns.
# File lib/rake.rb, line 1556 1556: def add_matching(pattern) 1557: Dir[pattern].each do |fn| 1558: self << fn unless exclude?(fn) 1559: end 1560: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.