Class Rake::FileList
In: lib/rake.rb
Parent: Object

######################################################################### 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.

Methods

*   ==   []   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  

Included Modules

Cloneable

Constants

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) }

Public Class methods

Create a new file list including the files listed. Similar to:

  FileList.new(*args)

[Source]

      # File lib/rake.rb, line 1566
1566:       def [](*args)
1567:         new(*args)
1568:       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

[Source]

      # File lib/rake.rb, line 1272
1272:     def initialize(*patterns)
1273:       @pending_add = []
1274:       @pending = false
1275:       @exclude_patterns = DEFAULT_IGNORE_PATTERNS.dup
1276:       @exclude_procs = DEFAULT_IGNORE_PROCS.dup
1277:       @exclude_re = nil
1278:       @items = []
1279:       patterns.each { |pattern| include(pattern) }
1280:       yield self if block_given?
1281:     end

Public Instance methods

Redefine * to return either a string or a new file list.

[Source]

      # File lib/rake.rb, line 1367
1367:     def *(other)
1368:       result = @items * other
1369:       case result
1370:       when Array
1371:         FileList.new.import(result)
1372:       else
1373:         result
1374:       end
1375:     end

Define equality.

[Source]

      # File lib/rake.rb, line 1345
1345:     def ==(array)
1346:       to_ary == array
1347:     end
add(*filenames)

Alias for include

[Source]

      # File lib/rake.rb, line 1388
1388:     def calculate_exclude_regexp
1389:       ignores = []
1390:       @exclude_patterns.each do |pat|
1391:         case pat
1392:         when Regexp
1393:           ignores << pat
1394:         when /[*?]/
1395:           Dir[pat].each do |p| ignores << p end
1396:         else
1397:           ignores << Regexp.quote(pat)
1398:         end
1399:       end
1400:       if ignores.empty?
1401:         @exclude_re = /^$/
1402:       else
1403:         re_str = ignores.collect { |p| "(" + p.to_s + ")" }.join("|")
1404:         @exclude_re = Regexp.new(re_str)
1405:       end
1406:     end

Clear all the exclude patterns so that we exclude nothing.

[Source]

      # File lib/rake.rb, line 1337
1337:     def clear_exclude
1338:       @exclude_patterns = []
1339:       @exclude_procs = []
1340:       calculate_exclude_regexp if ! @pending
1341:       self
1342:     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.

[Source]

      # File lib/rake.rb, line 1483
1483:     def egrep(pattern)
1484:       each do |fn|
1485:         open(fn) do |inf|
1486:           count = 0
1487:           inf.each do |line|
1488:             count += 1
1489:             if pattern.match(line)
1490:               if block_given?
1491:                 yield fn, count, line
1492:               else
1493:                 puts "#{fn}:#{count}:#{line}"
1494:               end
1495:             end
1496:           end
1497:         end
1498:       end
1499:     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']

[Source]

      # File lib/rake.rb, line 1324
1324:     def exclude(*patterns, &block)
1325:       patterns.each do |pat|
1326:         @exclude_patterns << pat
1327:       end
1328:       if block_given?
1329:         @exclude_procs << block
1330:       end
1331:       resolve_exclude if ! @pending
1332:       self
1333:     end

Should the given file name be excluded?

[Source]

      # File lib/rake.rb, line 1541
1541:     def exclude?(fn)
1542:       calculate_exclude_regexp unless @exclude_re
1543:       fn =~ @exclude_re || @exclude_procs.any? { |p| p.call(fn) }
1544:     end

Return a new file list that only contains file names from the current file list that exist on the file system.

[Source]

      # File lib/rake.rb, line 1503
1503:     def existing
1504:       select { |fn| File.exist?(fn) }
1505:     end

Modify the current file list so that it contains only file name that exist on the file system.

[Source]

      # File lib/rake.rb, line 1509
1509:     def existing!
1510:       resolve
1511:       @items = @items.select { |fn| File.exist?(fn) }
1512:       self
1513:     end

Return a new file list 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.

[Source]

      # File lib/rake.rb, line 1473
1473:     def ext(newext='')
1474:       collect { |fn| fn.ext(newext) }
1475:     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']

[Source]

      # File lib/rake.rb, line 1442
1442:     def gsub(pat, rep)
1443:       inject(FileList.new) { |res, fn| res << fn.gsub(pat,rep) }
1444:     end

Same as gsub except that the original file list is modified.

[Source]

      # File lib/rake.rb, line 1453
1453:     def gsub!(pat, rep)
1454:       each_with_index { |fn, i| self[i] = fn.gsub(pat,rep) }
1455:       self
1456:     end

@exclude_patterns = DEFAULT_IGNORE_PATTERNS.dup

[Source]

      # File lib/rake.rb, line 1557
1557:     def import(array)
1558:       @items = array
1559:       self
1560:     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 )

[Source]

      # File lib/rake.rb, line 1290
1290:     def include(*filenames)
1291:       # TODO: check for pending
1292:       filenames.each do |fn|
1293:         if fn.respond_to? :to_ary
1294:           include(*fn.to_ary)
1295:         else
1296:           @pending_add << fn
1297:         end
1298:       end
1299:       @pending = true
1300:       self
1301:     end

Lie about our class.

[Source]

      # File lib/rake.rb, line 1361
1361:     def is_a?(klass)
1362:       klass == Array || super(klass)
1363:     end
kind_of?(klass)

Alias for is_a?

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.)

[Source]

      # File lib/rake.rb, line 1461
1461:     def pathmap(spec=nil)
1462:       collect { |fn| fn.pathmap(spec) }
1463:     end

Resolve all the pending adds now.

[Source]

      # File lib/rake.rb, line 1378
1378:     def resolve
1379:       if @pending
1380:         @pending = false
1381:         @pending_add.each do |fn| resolve_add(fn) end
1382:         @pending_add = []
1383:         resolve_exclude
1384:       end
1385:       self
1386:     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']

[Source]

      # File lib/rake.rb, line 1431
1431:     def sub(pat, rep)
1432:       inject(FileList.new) { |res, fn| res << fn.sub(pat,rep) }
1433:     end

Same as sub except that the oringal file list is modified.

[Source]

      # File lib/rake.rb, line 1447
1447:     def sub!(pat, rep)
1448:       each_with_index { |fn, i| self[i] = fn.sub(pat,rep) }
1449:       self
1450:     end

Return the internal array object.

[Source]

      # File lib/rake.rb, line 1350
1350:     def to_a
1351:       resolve
1352:       @items
1353:     end

Return the internal array object.

[Source]

      # File lib/rake.rb, line 1356
1356:     def to_ary
1357:       to_a
1358:     end

Convert a FileList to a string by joining all elements with a space.

[Source]

      # File lib/rake.rb, line 1527
1527:     def to_s
1528:       resolve
1529:       self.join(' ')
1530:     end

[Validate]