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.rb, line 1296 1296: def [](*args) 1297: new(*args) 1298: end
clear_ignore_patterns() |
Clear the ignore patterns.
# File lib/rake.rb, line 1316 1316: def clear_ignore_patterns 1317: @exclude_patterns = [ /^$/ ] 1318: 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 1018 1018: def initialize(*patterns) 1019: @pending_add = [] 1020: @pending = false 1021: @exclude_patterns = DEFAULT_IGNORE_PATTERNS.dup 1022: @exclude_re = nil 1023: @items = [] 1024: patterns.each { |pattern| include(pattern) } 1025: yield self if block_given? 1026: end
select_default_ignore_patterns() |
Set the ignore patterns back to the default value. The default patterns will ignore files
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 1311 1311: def select_default_ignore_patterns 1312: @exclude_patterns = DEFAULT_IGNORE_PATTERNS.dup 1313: end
Public Instance methods |
*(other) |
Redefine * to return either a string or a new file list.
# File lib/rake.rb, line 1102 1102: def *(other) 1103: result = @items * other 1104: case result 1105: when Array 1106: FileList.new.import(result) 1107: else 1108: result 1109: end 1110: end
==(array) |
Define equality.
# File lib/rake.rb, line 1085 1085: def ==(array) 1086: to_ary == array 1087: end
add(*filenames) |
Alias for include
calculate_exclude_regexp() |
# File lib/rake.rb, line 1123 1123: def calculate_exclude_regexp 1124: ignores = [] 1125: @exclude_patterns.each do |pat| 1126: case pat 1127: when Regexp 1128: ignores << pat 1129: when /[*.]/ 1130: Dir[pat].each do |p| ignores << p end 1131: else 1132: ignores << Regexp.quote(pat) 1133: end 1134: end 1135: if ignores.empty? 1136: @exclude_re = /^$/ 1137: else 1138: re_str = ignores.collect { |p| "(" + p.to_s + ")" }.join("|") 1139: @exclude_re = Regexp.new(re_str) 1140: end 1141: end
clear_exclude() |
Clear all the exclude patterns so that we exclude nothing.
# File lib/rake.rb, line 1079 1079: def clear_exclude 1080: @exclude_patterns = [] 1081: calculate_exclude_regexp if ! @pending 1082: 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 1229 1229: def egrep(pattern) 1230: each do |fn| 1231: open(fn) do |inf| 1232: count = 0 1233: inf.each do |line| 1234: count += 1 1235: if pattern.match(line) 1236: if block_given? 1237: yield fn, count, line 1238: else 1239: puts "#{fn}:#{count}:#{line}" 1240: end 1241: end 1242: end 1243: end 1244: end 1245: 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 1068 1068: def exclude(*patterns) 1069: patterns.each do |pat| @exclude_patterns << pat end 1070: if ! @pending 1071: calculate_exclude_regexp 1072: reject! { |fn| fn =~ @exclude_re } 1073: end 1074: self 1075: end
exclude?(fn) |
Should the given file name be excluded?
# File lib/rake.rb, line 1273 1273: def exclude?(fn) 1274: calculate_exclude_regexp unless @exclude_re 1275: fn =~ @exclude_re 1276: 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 1219 1219: def ext(newext='') 1220: collect { |fn| fn.ext(newext) } 1221: 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 1188 1188: def gsub(pat, rep) 1189: inject(FileList.new) { |res, fn| res << fn.gsub(pat,rep) } 1190: end
gsub!(pat, rep) |
Same as gsub except that the original file list is modified.
# File lib/rake.rb, line 1199 1199: def gsub!(pat, rep) 1200: each_with_index { |fn, i| self[i] = fn.gsub(pat,rep) } 1201: self 1202: end
import(array) |
# File lib/rake.rb, line 1287 1287: def import(array) 1288: @items = array 1289: self 1290: 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.rb, line 1035 1035: def include(*filenames) 1036: # TODO: check for pending 1037: filenames.each do |fn| 1038: if fn.respond_to? :to_ary 1039: include(*fn.to_ary) 1040: else 1041: @pending_add << fn 1042: end 1043: end 1044: @pending = true 1045: self 1046: 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 1207 1207: def pathmap(spec=nil) 1208: collect { |fn| fn.pathmap(spec) } 1209: end
resolve() |
Resolve all the pending adds now.
# File lib/rake.rb, line 1113 1113: def resolve 1114: if @pending 1115: @pending = false 1116: @pending_add.each do |fn| resolve_add(fn) end 1117: @pending_add = [] 1118: resolve_exclude 1119: end 1120: self 1121: 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 1177 1177: def sub(pat, rep) 1178: inject(FileList.new) { |res, fn| res << fn.sub(pat,rep) } 1179: end
sub!(pat, rep) |
Same as sub except that the oringal file list is modified.
# File lib/rake.rb, line 1193 1193: def sub!(pat, rep) 1194: each_with_index { |fn, i| self[i] = fn.sub(pat,rep) } 1195: self 1196: end
to_a() |
Return the internal array object.
# File lib/rake.rb, line 1090 1090: def to_a 1091: resolve 1092: @items 1093: end
to_ary() |
Return the internal array object.
# File lib/rake.rb, line 1096 1096: def to_ary 1097: resolve 1098: @items 1099: end
to_s() |
Convert a FileList to a string by joining all elements with a space.
# File lib/rake.rb, line 1259 1259: def to_s 1260: resolve if @pending 1261: self.join(' ') 1262: end