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
Included Modules
Constants
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).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
[](*args)

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

  FileList.new(*args)
      # File lib/rake.rb, line 1336
1336:       def [](*args)
1337:         new(*args)
1338:       end
clear_ignore_patterns()

Clear the ignore patterns.

      # File lib/rake.rb, line 1356
1356:       def clear_ignore_patterns
1357:         @exclude_patterns = [ /^$/ ]
1358:       end
new(*patterns) {|self if block_given?| ...}

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 1041
1041:     def initialize(*patterns)
1042:       @pending_add = []
1043:       @pending = false
1044:       @exclude_patterns = DEFAULT_IGNORE_PATTERNS.dup
1045:       @exclude_procs = DEFAULT_IGNORE_PROCS.dup
1046:       @exclude_re = nil
1047:       @items = []
1048:       patterns.each { |pattern| include(pattern) }
1049:       yield self if block_given?
1050:     end
select_default_ignore_patterns()

Set the ignore patterns back to the default value. The default patterns will ignore files

  • containing "CVS" in the file path
  • containing ".svn" in the file path
  • ending with ".bak"
  • ending with "~"
  • named "core"

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/rake.rb, line 1351
1351:       def select_default_ignore_patterns
1352:         @exclude_patterns = DEFAULT_IGNORE_PATTERNS.dup
1353:       end
Public Instance methods
*(other)

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

      # File lib/rake.rb, line 1137
1137:     def *(other)
1138:       result = @items * other
1139:       case result
1140:       when Array
1141:         FileList.new.import(result)
1142:       else
1143:         result
1144:       end
1145:     end
==(array)

Define equality.

      # File lib/rake.rb, line 1115
1115:     def ==(array)
1116:       to_ary == array
1117:     end
add(*filenames)

Alias for include

calculate_exclude_regexp()
      # File lib/rake.rb, line 1158
1158:     def calculate_exclude_regexp
1159:       ignores = []
1160:       @exclude_patterns.each do |pat|
1161:         case pat
1162:         when Regexp
1163:           ignores << pat
1164:         when /[*?]/
1165:           Dir[pat].each do |p| ignores << p end
1166:         else
1167:           ignores << Regexp.quote(pat)
1168:         end
1169:       end
1170:       if ignores.empty?
1171:         @exclude_re = /^$/
1172:       else
1173:         re_str = ignores.collect { |p| "(" + p.to_s + ")" }.join("|")
1174:         @exclude_re = Regexp.new(re_str)
1175:       end
1176:     end
clear_exclude()

Clear all the exclude patterns so that we exclude nothing.

      # File lib/rake.rb, line 1107
1107:     def clear_exclude
1108:       @exclude_patterns = []
1109:       @exclude_procs = []
1110:       calculate_exclude_regexp if ! @pending
1111:       self
1112:     end
egrep(pattern) {|fn, count, line| ...}

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 1253
1253:     def egrep(pattern)
1254:       each do |fn|
1255:         open(fn) do |inf|
1256:           count = 0
1257:           inf.each do |line|
1258:             count += 1
1259:             if pattern.match(line)
1260:               if block_given?
1261:                 yield fn, count, line
1262:               else
1263:                 puts "#{fn}:#{count}:#{line}"
1264:               end               
1265:             end
1266:           end
1267:         end
1268:       end
1269:     end
exclude(*patterns, &block)

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 1094
1094:     def exclude(*patterns, &block)
1095:       patterns.each do |pat|
1096:         @exclude_patterns << pat 
1097:       end
1098:       if block_given?
1099:         @exclude_procs << block
1100:       end        
1101:       resolve_exclude if ! @pending
1102:       self
1103:     end
exclude?(fn)

Should the given file name be excluded?

      # File lib/rake.rb, line 1311
1311:     def exclude?(fn)
1312:       calculate_exclude_regexp unless @exclude_re
1313:       fn =~ @exclude_re || @exclude_procs.any? { |p| p.call(fn) }
1314:     end
existing()

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 1273
1273:     def existing
1274:       select { |fn| File.exists?(fn) }
1275:     end
existing!()

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

      # File lib/rake.rb, line 1279
1279:     def existing!
1280:       resolve
1281:       @items = @items.select { |fn| File.exists?(fn) }
1282:       self
1283:     end
ext(newext='')

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/rake.rb, line 1243
1243:     def ext(newext='')
1244:       collect { |fn| fn.ext(newext) }
1245:     end
gsub(pat, rep)

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 1212
1212:     def gsub(pat, rep)
1213:       inject(FileList.new) { |res, fn| res << fn.gsub(pat,rep) }
1214:     end
gsub!(pat, rep)

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

      # File lib/rake.rb, line 1223
1223:     def gsub!(pat, rep)
1224:       each_with_index { |fn, i| self[i] = fn.gsub(pat,rep) }
1225:       self
1226:     end
import(array)
      # File lib/rake.rb, line 1327
1327:     def import(array)
1328:       @items = array
1329:       self
1330:     end
include(*filenames)

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 )
This method is also aliased as add
      # File lib/rake.rb, line 1059
1059:     def include(*filenames)
1060:       # TODO: check for pending
1061:       filenames.each do |fn|
1062:         if fn.respond_to? :to_ary
1063:           include(*fn.to_ary)
1064:         else
1065:           @pending_add << fn
1066:         end
1067:       end
1068:       @pending = true
1069:       self
1070:     end
is_a?(klass)

Lie about our class.

This method is also aliased as kind_of?
      # File lib/rake.rb, line 1131
1131:     def is_a?(klass)
1132:       klass == Array || super(klass)
1133:     end
kind_of?(klass)

Alias for is_a?

pathmap(spec=nil)

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 1231
1231:     def pathmap(spec=nil)
1232:       collect { |fn| fn.pathmap(spec) }
1233:     end
resolve()

Resolve all the pending adds now.

      # File lib/rake.rb, line 1148
1148:     def resolve
1149:       if @pending
1150:         @pending = false
1151:         @pending_add.each do |fn| resolve_add(fn) end
1152:         @pending_add = []
1153:         resolve_exclude
1154:       end
1155:       self
1156:     end
sub(pat, rep)

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 1201
1201:     def sub(pat, rep)
1202:       inject(FileList.new) { |res, fn| res << fn.sub(pat,rep) }
1203:     end
sub!(pat, rep)

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

      # File lib/rake.rb, line 1217
1217:     def sub!(pat, rep)
1218:       each_with_index { |fn, i| self[i] = fn.sub(pat,rep) }
1219:       self
1220:     end
to_a()

Return the internal array object.

      # File lib/rake.rb, line 1120
1120:     def to_a
1121:       resolve
1122:       @items
1123:     end
to_ary()

Return the internal array object.

      # File lib/rake.rb, line 1126
1126:     def to_ary
1127:       to_a
1128:     end
to_s()

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

      # File lib/rake.rb, line 1297
1297:     def to_s
1298:       resolve if @pending
1299:       self.join(' ')
1300:     end