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).sort.uniq
DEFAULT_IGNORE_PATTERNS = [ /(^|[\/\\])CVS([\/\\]|$)/, /(^|[\/\\])\.svn([\/\\]|$)/, /\.bak$/, /~$/, /(^|[\/\\])core$/
Public Class methods
[](*args)

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

  FileList.new(*args)
      # File lib/rake.rb, line 1314
1314:       def [](*args)
1315:         new(*args)
1316:       end
clear_ignore_patterns()

Clear the ignore patterns.

      # File lib/rake.rb, line 1334
1334:       def clear_ignore_patterns
1335:         @exclude_patterns = [ /^$/ ]
1336:       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 1036
1036:     def initialize(*patterns)
1037:       @pending_add = []
1038:       @pending = false
1039:       @exclude_patterns = DEFAULT_IGNORE_PATTERNS.dup
1040:       @exclude_re = nil
1041:       @items = []
1042:       patterns.each { |pattern| include(pattern) }
1043:       yield self if block_given?
1044:     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 1329
1329:       def select_default_ignore_patterns
1330:         @exclude_patterns = DEFAULT_IGNORE_PATTERNS.dup
1331:       end
Public Instance methods
*(other)

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

      # File lib/rake.rb, line 1120
1120:     def *(other)
1121:       result = @items * other
1122:       case result
1123:       when Array
1124:         FileList.new.import(result)
1125:       else
1126:         result
1127:       end
1128:     end
==(array)

Define equality.

      # File lib/rake.rb, line 1103
1103:     def ==(array)
1104:       to_ary == array
1105:     end
add(*filenames)

Alias for include

calculate_exclude_regexp()
      # File lib/rake.rb, line 1141
1141:     def calculate_exclude_regexp
1142:       ignores = []
1143:       @exclude_patterns.each do |pat|
1144:         case pat
1145:         when Regexp
1146:           ignores << pat
1147:         when /[*.]/
1148:           Dir[pat].each do |p| ignores << p end
1149:         else
1150:           ignores << Regexp.quote(pat)
1151:         end
1152:       end
1153:       if ignores.empty?
1154:         @exclude_re = /^$/
1155:       else
1156:         re_str = ignores.collect { |p| "(" + p.to_s + ")" }.join("|")
1157:         @exclude_re = Regexp.new(re_str)
1158:       end
1159:     end
clear_exclude()

Clear all the exclude patterns so that we exclude nothing.

      # File lib/rake.rb, line 1097
1097:     def clear_exclude
1098:       @exclude_patterns = []
1099:       calculate_exclude_regexp if ! @pending
1100:     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 1247
1247:     def egrep(pattern)
1248:       each do |fn|
1249:         open(fn) do |inf|
1250:           count = 0
1251:           inf.each do |line|
1252:             count += 1
1253:             if pattern.match(line)
1254:               if block_given?
1255:                 yield fn, count, line
1256:               else
1257:                 puts "#{fn}:#{count}:#{line}"
1258:               end              
1259:             end
1260:           end
1261:         end
1262:       end
1263:     end
exclude(*patterns)

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/rake.rb, line 1086
1086:     def exclude(*patterns)
1087:       patterns.each do |pat| @exclude_patterns << pat end
1088:       if ! @pending
1089:         calculate_exclude_regexp
1090:         reject! { |fn| fn =~ @exclude_re }
1091:       end
1092:       self
1093:     end
exclude?(fn)

Should the given file name be excluded?

      # File lib/rake.rb, line 1291
1291:     def exclude?(fn)
1292:       calculate_exclude_regexp unless @exclude_re
1293:       fn =~ @exclude_re
1294:     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 1237
1237:     def ext(newext='')
1238:       collect { |fn| fn.ext(newext) }
1239:     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 1206
1206:     def gsub(pat, rep)
1207:       inject(FileList.new) { |res, fn| res << fn.gsub(pat,rep) }
1208:     end
gsub!(pat, rep)

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

      # File lib/rake.rb, line 1217
1217:     def gsub!(pat, rep)
1218:       each_with_index { |fn, i| self[i] = fn.gsub(pat,rep) }
1219:       self
1220:     end
import(array)
      # File lib/rake.rb, line 1305
1305:     def import(array)
1306:       @items = array
1307:       self
1308:     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 1053
1053:     def include(*filenames)
1054:       # TODO: check for pending
1055:       filenames.each do |fn|
1056:         if fn.respond_to? :to_ary
1057:           include(*fn.to_ary)
1058:         else
1059:           @pending_add << fn
1060:         end
1061:       end
1062:       @pending = true
1063:       self
1064:     end
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 1225
1225:     def pathmap(spec=nil)
1226:       collect { |fn| fn.pathmap(spec) }
1227:     end
resolve()

Resolve all the pending adds now.

      # File lib/rake.rb, line 1131
1131:     def resolve
1132:       if @pending
1133:         @pending = false
1134:         @pending_add.each do |fn| resolve_add(fn) end
1135:         @pending_add = []
1136:         resolve_exclude
1137:       end
1138:       self
1139:     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 1195
1195:     def sub(pat, rep)
1196:       inject(FileList.new) { |res, fn| res << fn.sub(pat,rep) }
1197:     end
sub!(pat, rep)

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

      # File lib/rake.rb, line 1211
1211:     def sub!(pat, rep)
1212:       each_with_index { |fn, i| self[i] = fn.sub(pat,rep) }
1213:       self
1214:     end
to_a()

Return the internal array object.

      # File lib/rake.rb, line 1108
1108:     def to_a
1109:       resolve
1110:       @items
1111:     end
to_ary()

Return the internal array object.

      # File lib/rake.rb, line 1114
1114:     def to_ary
1115:       resolve
1116:       @items
1117:     end
to_s()

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

      # File lib/rake.rb, line 1277
1277:     def to_s
1278:       resolve if @pending
1279:       self.join(' ')
1280:     end