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   clear_ignore_patterns   egrep   exclude   exclude?   existing   existing!   ext   gsub   gsub!   import   include   is_a?   kind_of?   new   pathmap   resolve   select_default_ignore_patterns   sub   sub!   to_a   to_ary   to_s  

Included Modules

Cloneable

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

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

  FileList.new(*args)

[Source]

      # File lib/rake.rb, line 1316
1316:       def [](*args)
1317:         new(*args)
1318:       end

Clear the ignore patterns.

[Source]

      # File lib/rake.rb, line 1336
1336:       def clear_ignore_patterns
1337:         @exclude_patterns = [ /^$/ ]
1338:       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 1022
1022:     def initialize(*patterns)
1023:       @pending_add = []
1024:       @pending = false
1025:       @exclude_patterns = DEFAULT_IGNORE_PATTERNS.dup
1026:       @exclude_procs = DEFAULT_IGNORE_PROCS.dup
1027:       @exclude_re = nil
1028:       @items = []
1029:       patterns.each { |pattern| include(pattern) }
1030:       yield self if block_given?
1031:     end

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.

[Source]

      # File lib/rake.rb, line 1331
1331:       def select_default_ignore_patterns
1332:         @exclude_patterns = DEFAULT_IGNORE_PATTERNS.dup
1333:       end

Public Instance methods

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

[Source]

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

Define equality.

[Source]

      # File lib/rake.rb, line 1095
1095:     def ==(array)
1096:       to_ary == array
1097:     end
add(*filenames)

Alias for include

[Source]

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

Clear all the exclude patterns so that we exclude nothing.

[Source]

      # File lib/rake.rb, line 1087
1087:     def clear_exclude
1088:       @exclude_patterns = []
1089:       @exclude_procs = []
1090:       calculate_exclude_regexp if ! @pending
1091:       self
1092:     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 1233
1233:     def egrep(pattern)
1234:       each do |fn|
1235:         open(fn) do |inf|
1236:           count = 0
1237:           inf.each do |line|
1238:             count += 1
1239:             if pattern.match(line)
1240:               if block_given?
1241:                 yield fn, count, line
1242:               else
1243:                 puts "#{fn}:#{count}:#{line}"
1244:               end               
1245:             end
1246:           end
1247:         end
1248:       end
1249:     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 1074
1074:     def exclude(*patterns, &block)
1075:       patterns.each do |pat|
1076:         @exclude_patterns << pat 
1077:       end
1078:       if block_given?
1079:         @exclude_procs << block
1080:       end        
1081:       resolve_exclude if ! @pending
1082:       self
1083:     end

Should the given file name be excluded?

[Source]

      # File lib/rake.rb, line 1291
1291:     def exclude?(fn)
1292:       calculate_exclude_regexp unless @exclude_re
1293:       fn =~ @exclude_re || @exclude_procs.any? { |p| p.call(fn) }
1294:     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 1253
1253:     def existing
1254:       select { |fn| File.exists?(fn) }
1255:     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 1259
1259:     def existing!
1260:       resolve
1261:       @items = @items.select { |fn| File.exists?(fn) }
1262:       self
1263:     end

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.

[Source]

      # File lib/rake.rb, line 1223
1223:     def ext(newext='')
1224:       collect { |fn| fn.ext(newext) }
1225:     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 1192
1192:     def gsub(pat, rep)
1193:       inject(FileList.new) { |res, fn| res << fn.gsub(pat,rep) }
1194:     end

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

[Source]

      # File lib/rake.rb, line 1203
1203:     def gsub!(pat, rep)
1204:       each_with_index { |fn, i| self[i] = fn.gsub(pat,rep) }
1205:       self
1206:     end

[Source]

      # File lib/rake.rb, line 1307
1307:     def import(array)
1308:       @items = array
1309:       self
1310:     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 1040
1040:     def include(*filenames)
1041:       # TODO: check for pending
1042:       filenames.each do |fn|
1043:         if fn.respond_to? :to_ary
1044:           include(*fn.to_ary)
1045:         else
1046:           @pending_add << fn
1047:         end
1048:       end
1049:       @pending = true
1050:       self
1051:     end

Lie about our class.

[Source]

      # File lib/rake.rb, line 1111
1111:     def is_a?(klass)
1112:       klass == Array || super(klass)
1113:     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 1211
1211:     def pathmap(spec=nil)
1212:       collect { |fn| fn.pathmap(spec) }
1213:     end

Resolve all the pending adds now.

[Source]

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

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

[Source]

      # File lib/rake.rb, line 1197
1197:     def sub!(pat, rep)
1198:       each_with_index { |fn, i| self[i] = fn.sub(pat,rep) }
1199:       self
1200:     end

Return the internal array object.

[Source]

      # File lib/rake.rb, line 1100
1100:     def to_a
1101:       resolve
1102:       @items
1103:     end

Return the internal array object.

[Source]

      # File lib/rake.rb, line 1106
1106:     def to_ary
1107:       to_a
1108:     end

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

[Source]

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

[Validate]