|
Class Rake::FileList |
|
######################################################################### 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 |
| Public Class methods |
| [](*args) |
Create a new file list including the files listed. Similar to:
FileList.new(*args)
# File lib/rake/file_list.rb, line 382
382: def [](*args)
383: new(*args)
384: 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/file_list.rb, line 96
96: def initialize(*patterns)
97: @pending_add = []
98: @pending = false
99: @exclude_patterns = DEFAULT_IGNORE_PATTERNS.dup
100: @exclude_procs = DEFAULT_IGNORE_PROCS.dup
101: @items = []
102: patterns.each { |pattern| include(pattern) }
103: yield self if block_given?
104: end
| Public Instance methods |
| *(other) |
Redefine * to return either a string or a new file list.
# File lib/rake/file_list.rb, line 189
189: def *(other)
190: result = @items * other
191: case result
192: when Array
193: FileList.new.import(result)
194: else
195: result
196: end
197: end
| ==(array) |
Define equality.
# File lib/rake/file_list.rb, line 167
167: def ==(array)
168: to_ary == array
169: end
| add(*filenames) |
Alias for include
| clear_exclude() |
# File lib/rake/file_list.rb, line 160
160: def clear_exclude
161: @exclude_patterns = []
162: @exclude_procs = []
163: self
164: end
| egrep(pattern, *options) {|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. Returns the number of matched items.
# File lib/rake/file_list.rb, line 284
284: def egrep(pattern, *options)
285: matched = 0
286: each do |fn|
287: begin
288: open(fn, "rb", *options) do |inf|
289: count = 0
290: inf.each do |line|
291: count += 1
292: if pattern.match(line)
293: matched += 1
294: if block_given?
295: yield fn, count, line
296: else
297: puts "#{fn}:#{count}:#{line}"
298: end
299: end
300: end
301: end
302: rescue StandardError => ex
303: puts "Error while processing '#{fn}': #{ex}"
304: end
305: end
306: matched
307: 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/file_list.rb, line 147
147: def exclude(*patterns, &block)
148: patterns.each do |pat|
149: @exclude_patterns << pat
150: end
151: if block_given?
152: @exclude_procs << block
153: end
154: resolve_exclude if ! @pending
155: self
156: end
| exclude?(fn) |
Should the given file name be excluded?
# File lib/rake/file_list.rb, line 349
349: def exclude?(fn)
350: return true if @exclude_patterns.any? do |pat|
351: case pat
352: when Regexp
353: fn =~ pat
354: when /[*?]/
355: File.fnmatch?(pat, fn, File::FNM_PATHNAME)
356: else
357: fn == pat
358: end
359: end
360: @exclude_procs.any? { |p| p.call(fn) }
361: 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/file_list.rb, line 311
311: def existing
312: select { |fn| File.exist?(fn) }
313: end
| existing!() |
Modify the current file list so that it contains only file name that exist on the file system.
# File lib/rake/file_list.rb, line 317
317: def existing!
318: resolve
319: @items = @items.select { |fn| File.exist?(fn) }
320: self
321: end
| ext(newext='') |
Return a new FileList 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/file_list.rb, line 274
274: def ext(newext='')
275: collect { |fn| fn.ext(newext) }
276: 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/file_list.rb, line 243
243: def gsub(pat, rep)
244: inject(FileList.new) { |res, fn| res << fn.gsub(pat,rep) }
245: end
| gsub!(pat, rep) |
Same as gsub except that the original file list is modified.
# File lib/rake/file_list.rb, line 254
254: def gsub!(pat, rep)
255: each_with_index { |fn, i| self[i] = fn.gsub(pat,rep) }
256: self
257: end
| import(array) |
# File lib/rake/file_list.rb, line 373
373: def import(array)
374: @items = array
375: self
376: 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 )
# File lib/rake/file_list.rb, line 113
113: def include(*filenames)
114: # TODO: check for pending
115: filenames.each do |fn|
116: if fn.respond_to? :to_ary
117: include(*fn.to_ary)
118: else
119: @pending_add << fn
120: end
121: end
122: @pending = true
123: self
124: end
| is_a?(klass) |
Lie about our class.
# File lib/rake/file_list.rb, line 183
183: def is_a?(klass)
184: klass == Array || super(klass)
185: 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/file_list.rb, line 262
262: def pathmap(spec=nil)
263: collect { |fn| fn.pathmap(spec) }
264: end
| resolve() |
Resolve all the pending adds now.
# File lib/rake/file_list.rb, line 200
200: def resolve
201: if @pending
202: @pending = false
203: @pending_add.each do |fn| resolve_add(fn) end
204: @pending_add = []
205: resolve_exclude
206: end
207: self
208: end
| sub(pat, rep) |
Return a new FileList with the results of running sub against each element of the orignal list.
Example:
FileList['a.c', 'b.c'].sub(/\.c$/, '.o') => ['a.o', 'b.o']
# File lib/rake/file_list.rb, line 232
232: def sub(pat, rep)
233: inject(FileList.new) { |res, fn| res << fn.sub(pat,rep) }
234: end
| sub!(pat, rep) |
Same as sub except that the oringal file list is modified.
# File lib/rake/file_list.rb, line 248
248: def sub!(pat, rep)
249: each_with_index { |fn, i| self[i] = fn.sub(pat,rep) }
250: self
251: end
| to_a() |
Return the internal array object.
# File lib/rake/file_list.rb, line 172
172: def to_a
173: resolve
174: @items
175: end
| to_ary() |
Return the internal array object.
# File lib/rake/file_list.rb, line 178
178: def to_ary
179: to_a
180: end
| to_s() |
Convert a FileList to a string by joining all elements with a space.
# File lib/rake/file_list.rb, line 335
335: def to_s
336: resolve
337: self.join(' ')
338: end