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.
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$/, /~$/ | ||
DEFAULT_IGNORE_PROCS | = | [ proc { |fn| fn =~ /(^|[\/\\])core$/ && ! File.directory?(fn) } |
Clear the ignore patterns.
# 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
# File lib/rake.rb, line 1040 1040: def initialize(*patterns) 1041: @pending_add = [] 1042: @pending = false 1043: @exclude_patterns = DEFAULT_IGNORE_PATTERNS.dup 1044: @exclude_procs = DEFAULT_IGNORE_PROCS.dup 1045: @exclude_re = nil 1046: @items = [] 1047: patterns.each { |pattern| include(pattern) } 1048: yield self if block_given? 1049: end
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 1331 1331: def select_default_ignore_patterns 1332: @exclude_patterns = DEFAULT_IGNORE_PATTERNS.dup 1333: end
# File lib/rake.rb, line 1152 1152: def calculate_exclude_regexp 1153: ignores = [] 1154: @exclude_patterns.each do |pat| 1155: case pat 1156: when Regexp 1157: ignores << pat 1158: when /[*?]/ 1159: Dir[pat].each do |p| ignores << p end 1160: else 1161: ignores << Regexp.quote(pat) 1162: end 1163: end 1164: if ignores.empty? 1165: @exclude_re = /^$/ 1166: else 1167: re_str = ignores.collect { |p| "(" + p.to_s + ")" }.join("|") 1168: @exclude_re = Regexp.new(re_str) 1169: end 1170: 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.
# File lib/rake.rb, line 1247 1247: def egrep(pattern) 1248: each do |fn| 1249: open(fn) do |inf| 1250: count = 0 1251: inf.each do |line| 1252: count += 1 1253: if pattern.match(line) 1254: if block_given? 1255: yield fn, count, line 1256: else 1257: puts "#{fn}:#{count}:#{line}" 1258: end 1259: end 1260: end 1261: end 1262: end 1263: 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']
# File lib/rake.rb, line 1093 1093: def exclude(*patterns, &block) 1094: patterns.each do |pat| 1095: @exclude_patterns << pat 1096: end 1097: if block_given? 1098: @exclude_procs << block 1099: end 1100: resolve_exclude if ! @pending 1101: self 1102: end
Should the given file name be excluded?
# 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 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 1237 1237: def ext(newext='') 1238: collect { |fn| fn.ext(newext) } 1239: 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']
# File lib/rake.rb, line 1206 1206: def gsub(pat, rep) 1207: inject(FileList.new) { |res, fn| res << fn.gsub(pat,rep) } 1208: 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 )
# File lib/rake.rb, line 1058 1058: def include(*filenames) 1059: # TODO: check for pending 1060: filenames.each do |fn| 1061: if fn.respond_to? :to_ary 1062: include(*fn.to_ary) 1063: else 1064: @pending_add << fn 1065: end 1066: end 1067: @pending = true 1068: self 1069: end
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 1225 1225: def pathmap(spec=nil) 1226: collect { |fn| fn.pathmap(spec) } 1227: end
Resolve all the pending adds now.
# File lib/rake.rb, line 1142 1142: def resolve 1143: if @pending 1144: @pending = false 1145: @pending_add.each do |fn| resolve_add(fn) end 1146: @pending_add = [] 1147: resolve_exclude 1148: end 1149: self 1150: end
Return the internal array object.
# File lib/rake.rb, line 1119 1119: def to_a 1120: resolve 1121: @items 1122: end