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 1134
1134:       def [](*args)
1135:         new(*args)
1136:       end
clear_ignore_patterns()

Clear the ignore patterns.

      # File lib/rake.rb, line 1154
1154:       def clear_ignore_patterns
1155:         @exclude_patterns = [ /^$/ ]
1156:       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 863
863:     def initialize(*patterns)
864:       @pending_add = []
865:       @pending = false
866:       @exclude_patterns = DEFAULT_IGNORE_PATTERNS.dup
867:       @exclude_re = nil
868:       @items = []
869:       patterns.each { |pattern| include(pattern) }
870:       yield self if block_given?
871:     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 1149
1149:       def select_default_ignore_patterns
1150:         @exclude_patterns = DEFAULT_IGNORE_PATTERNS.dup
1151:       end
Public Instance methods
*(other)

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

     # File lib/rake.rb, line 947
947:     def *(other)
948:       result = @items * other
949:       case result
950:       when Array
951:         FileList.new.import(result)
952:       else
953:         result
954:       end
955:     end
==(array)

Define equality.

     # File lib/rake.rb, line 930
930:     def ==(array)
931:       to_ary == array
932:     end
add(*filenames)

Alias for include

calculate_exclude_regexp()
     # File lib/rake.rb, line 968
968:     def calculate_exclude_regexp
969:       ignores = []
970:       @exclude_patterns.each do |pat|
971:         case pat
972:         when Regexp
973:           ignores << pat
974:         when /[*.]/
975:           Dir[pat].each do |p| ignores << p end
976:         else
977:           ignores << Regexp.quote(pat)
978:         end
979:       end
980:       if ignores.empty?
981:         @exclude_re = /^$/
982:       else
983:         re_str = ignores.collect { |p| "(" + p.to_s + ")" }.join("|")
984:         @exclude_re = Regexp.new(re_str)
985:       end
986:     end
clear_exclude()

Clear all the exclude patterns so that we exclude nothing.

     # File lib/rake.rb, line 924
924:     def clear_exclude
925:       @exclude_patterns = []
926:       calculate_exclude_regexp if ! @pending
927:     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 1067
1067:     def egrep(pattern)
1068:       each do |fn|
1069:         open(fn) do |inf|
1070:           count = 0
1071:           inf.each do |line|
1072:             count += 1
1073:             if pattern.match(line)
1074:               if block_given?
1075:                 yield fn, count, line
1076:               else
1077:                 puts "#{fn}:#{count}:#{line}"
1078:               end              
1079:             end
1080:           end
1081:         end
1082:       end
1083:     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 913
913:     def exclude(*patterns)
914:       patterns.each do |pat| @exclude_patterns << pat end
915:       if ! @pending
916:         calculate_exclude_regexp
917:         reject! { |fn| fn =~ @exclude_re }
918:       end
919:       self
920:     end
exclude?(fn)

Should the given file name be excluded?

      # File lib/rake.rb, line 1111
1111:     def exclude?(fn)
1112:       calculate_exclude_regexp unless @exclude_re
1113:       fn =~ @exclude_re
1114:     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 1057
1057:     def ext(newext='')
1058:       collect { |fn| fn.ext(newext) }
1059:     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 1033
1033:     def gsub(pat, rep)
1034:       inject(FileList.new) { |res, fn| res << fn.gsub(pat,rep) }
1035:     end
gsub!(pat, rep)

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

      # File lib/rake.rb, line 1044
1044:     def gsub!(pat, rep)
1045:       each_with_index { |fn, i| self[i] = fn.gsub(pat,rep) }
1046:       self
1047:     end
import(array)
      # File lib/rake.rb, line 1125
1125:     def import(array)
1126:       @items = array
1127:       self
1128:     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 880
880:     def include(*filenames)
881:       # TODO: check for pending
882:       filenames.each do |fn|
883:         if fn.respond_to? :to_ary
884:           include(*fn.to_ary)
885:         else
886:           @pending_add << fn
887:         end
888:       end
889:       @pending = true
890:       self
891:     end
resolve()

Resolve all the pending adds now.

     # File lib/rake.rb, line 958
958:     def resolve
959:       if @pending
960:         @pending = false
961:         @pending_add.each do |fn| resolve_add(fn) end
962:         @pending_add = []
963:         resolve_exclude
964:       end
965:       self
966:     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 1022
1022:     def sub(pat, rep)
1023:       inject(FileList.new) { |res, fn| res << fn.sub(pat,rep) }
1024:     end
sub!(pat, rep)

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

      # File lib/rake.rb, line 1038
1038:     def sub!(pat, rep)
1039:       each_with_index { |fn, i| self[i] = fn.sub(pat,rep) }
1040:       self
1041:     end
to_a()

Return the internal array object.

     # File lib/rake.rb, line 935
935:     def to_a
936:       resolve
937:       @items
938:     end
to_ary()

Return the internal array object.

     # File lib/rake.rb, line 941
941:     def to_ary
942:       resolve
943:       @items
944:     end
to_s()

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

      # File lib/rake.rb, line 1097
1097:     def to_s
1098:       resolve if @pending
1099:       self.join(' ')
1100:     end