Class FileList
In: lib/more/facets/filelist.rb
Parent: Object

FileList

A FileList is essentially an array with helper methods to make file manipulation 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.

  fl = FileList.new
  fl.include('./**/*')
  fl.exclude('./*~')

Methods

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([\/\\]|$)/, /(^|[\/\\])_darcs([\/\\]|$)/, /\.bak$/, /~$/, /(^|[\/\\])core$/

Public Class methods

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

  FileList.new(*args)

[Source]

     # File lib/more/facets/filelist.rb, line 439
439:     def [](*args)
440:       new(*args)
441:     end

Clear the ignore patterns.

[Source]

     # File lib/more/facets/filelist.rb, line 460
460:     def clear_ignore_patterns
461:       @exclude_patterns = [ /^$/ ]
462:     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/more/facets/filelist.rb, line 169
169:   def initialize(*patterns)
170:     @pending_add = []
171:     @pending = false
172:     @exclude_patterns = DEFAULT_IGNORE_PATTERNS.dup
173:     @exclude_re = nil
174:     @items = []
175:     patterns.each { |pattern| include(pattern) }
176:     yield self if block_given?
177:   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
  • containing "_darcs" 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/more/facets/filelist.rb, line 455
455:     def select_default_ignore_patterns
456:       @exclude_patterns = DEFAULT_IGNORE_PATTERNS.dup
457:     end

Public Instance methods

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

[Source]

     # File lib/more/facets/filelist.rb, line 252
252:   def *(other)
253:     result = @items * other
254:     case result
255:     when Array
256:       FileList.new.import(result)
257:     else
258:       result
259:     end
260:   end

Define equality.

[Source]

     # File lib/more/facets/filelist.rb, line 235
235:   def ==(array)
236:     to_ary == array
237:   end
add(*filenames)

Alias for include

[Source]

     # File lib/more/facets/filelist.rb, line 273
273:   def calculate_exclude_regexp
274:     ignores = []
275:     @exclude_patterns.each do |pat|
276:       case pat
277:       when Regexp
278:         ignores << pat
279:       when /[*.]/
280:         Dir[pat].each do |p| ignores << p end
281:       else
282:         ignores << Regexp.quote(pat)
283:       end
284:     end
285:     if ignores.empty?
286:       @exclude_re = /^$/
287:     else
288:       re_str = ignores.collect { |p| "(" + p.to_s + ")" }.join("|")
289:       @exclude_re = Regexp.new(re_str)
290:     end
291:   end

Clear all the exclude patterns so that we exclude nothing.

[Source]

     # File lib/more/facets/filelist.rb, line 229
229:   def clear_exclude
230:     @exclude_patterns = []
231:     calculate_exclude_regexp if ! @pending
232:   end

include Cloneable

[Source]

    # File lib/more/facets/filelist.rb, line 85
85:   def clone
86:     sibling = self.class.new
87:     instance_variables.each do |ivar|
88:       value = self.instance_variable_get(ivar)
89:       sibling.instance_variable_set(ivar, value.rake_dup)
90:     end
91:     sibling
92:   end
dup()

Alias for clone

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/more/facets/filelist.rb, line 369
369:   def egrep(pattern)
370:     each do |fn|
371:       open(fn) do |inf|
372:         count = 0
373: 
374:         inf.each do |line|
375:           count += 1
376:           if pattern.match(line)
377:             if block_given?
378:               yield fn, count, line
379:             else
380:               puts "#{fn}:#{count}:#{line}"
381:             end
382:           end
383:         end
384: 
385:       end
386:     end
387:   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.

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/more/facets/filelist.rb, line 219
219:   def exclude(*patterns)
220:     patterns.each do |pat| @exclude_patterns << pat end
221:     if ! @pending
222:       calculate_exclude_regexp
223:       reject! { |fn| fn =~ @exclude_re }
224:     end
225:     self
226:   end

Should the given file name be excluded?

[Source]

     # File lib/more/facets/filelist.rb, line 415
415:   def exclude?(fn)
416:     calculate_exclude_regexp unless @exclude_re
417:     fn =~ @exclude_re
418:   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/more/facets/filelist.rb, line 360
360:   def ext(newext='')
361:     collect { |fn| fn.ext(newext) }
362:   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/more/facets/filelist.rb, line 336
336:   def gsub(pat, rep)
337:     inject(FileList.new) { |res, fn| res << fn.gsub(pat,rep) }
338:   end

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

[Source]

     # File lib/more/facets/filelist.rb, line 347
347:   def gsub!(pat, rep)
348:     each_with_index { |fn, i| self[i] = fn.gsub(pat,rep) }
349:     self
350:   end

[Source]

     # File lib/more/facets/filelist.rb, line 430
430:   def import(array)
431:     @items = array
432:     self
433:   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/more/facets/filelist.rb, line 186
186:   def include(*filenames)
187:     # TODO: check for pending
188:     filenames.each do |fn|
189:       if fn.respond_to? :to_ary
190:         include(*fn.to_ary)
191:       else
192:         @pending_add << fn
193:       end
194:     end
195:     @pending = true
196:     self
197:   end

Resolve all the pending adds now.

[Source]

     # File lib/more/facets/filelist.rb, line 263
263:   def resolve
264:     if @pending
265:       @pending = false
266:       @pending_add.each do |fn| resolve_add(fn) end
267:       @pending_add = []
268:       resolve_exclude
269:     end
270:     self
271:   end

[Source]

     # File lib/more/facets/filelist.rb, line 293
293:   def resolve_add(fn)
294:     case fn
295:     when Array
296:       fn.each { |f| self.resolve_add(f) }
297:     when %r{[*?]}
298:       add_matching(fn)
299:     else
300:       self << fn
301:     end
302:   end

[Source]

     # File lib/more/facets/filelist.rb, line 304
304:   def resolve_exclude
305:     @exclude_patterns.each do |pat|
306:       case pat
307:       when Regexp
308:         reject! { |fn| fn =~ pat }
309:       when /[*.]/
310:         reject_list = Dir[pat]
311:         reject! { |fn| reject_list.include?(fn) }
312:       else
313:         reject! { |fn| fn == pat }
314:       end
315:     end
316:     self
317:   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/more/facets/filelist.rb, line 325
325:   def sub(pat, rep)
326:     inject(FileList.new) { |res, fn| res << fn.sub(pat,rep) }
327:   end

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

[Source]

     # File lib/more/facets/filelist.rb, line 341
341:   def sub!(pat, rep)
342:     each_with_index { |fn, i| self[i] = fn.sub(pat,rep) }
343:     self
344:   end

Return the internal array object.

[Source]

     # File lib/more/facets/filelist.rb, line 240
240:   def to_a
241:     resolve
242:     @items
243:   end

Return the internal array object.

[Source]

     # File lib/more/facets/filelist.rb, line 246
246:   def to_ary
247:     resolve
248:     @items
249:   end

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

[Source]

     # File lib/more/facets/filelist.rb, line 401
401:   def to_s
402:     resolve if @pending
403:     self.join(' ')
404:   end

Private Instance methods

Add matching glob patterns.

[Source]

     # File lib/more/facets/filelist.rb, line 407
407:   def add_matching(pattern)
408:     Dir[pattern].each do |fn|
409:       self << fn unless exclude?(fn)
410:     end
411:   end

[Validate]