Class: FileList
Overview
FileList
A FileList is essentially an array with a few helper methods
defined to make file manipulation a bit easier. It works in
a fashion similar to Dir.glob.
== Synopsis
require 'trix/filelist'
fl = FileList.new
fl.include('./**/*')
fl.exclude('./*~')
== Legal
Ported from Rake
Copyright (C) 2002 Jim Welch
You may distribute this software under the same terms as Ruby (see the file
COPYING that was distributed with this library).
== History
$id: filelist.rb 2004/11/27 10:00:01 transami Exp $
Constant Summary collapse
- DEFAULT_IGNORE_PATTERNS =
[ /(^|[\/\\])CVS([\/\\]|$)/, /\.bak$/, /~$/, /(^|[\/\\])core$/ ]
Class Method Summary collapse
-
.[](*args) ⇒ Object
Create a new file list including the files listed.
-
.clear_ignore_patterns ⇒ Object
Clear the ignore patterns.
-
.select_default_ignore_patterns ⇒ Object
Set the ignore patterns back to the default value.
Instance Method Summary collapse
- #calculate_exclude_regexp ⇒ Object
- #clear_exclude ⇒ Object
-
#exclude(*patterns) ⇒ Object
Register a list of file name patterns that should be excluded from the list.
-
#exclude?(fn) ⇒ Boolean
Should the given file name be excluded?.
-
#gsub(pat, rep) ⇒ Object
Return a new FileList with the results of running
gsub
against each element of the original list. -
#gsub!(pat, rep) ⇒ Object
Same as
gsub
except that the original file list is modified. -
#include(*filenames) ⇒ Object
(also: #add)
Add file names defined by glob patterns to the file list.
-
#initialize(*patterns) {|_self| ... } ⇒ FileList
constructor
Create a file list from the globbable patterns given.
-
#resolve ⇒ Object
Resolve all the pending adds now.
- #resolve_add(fn) ⇒ Object
- #resolve_exclude ⇒ Object
-
#sub(pat, rep) ⇒ Object
Return a new FileList with the results of running
sub
against each element of the oringal list. -
#sub!(pat, rep) ⇒ Object
Same as
sub
except that the oringal file list is modified. -
#to_s ⇒ Object
Convert a FileList to a string by joining all elements with a space.
-
#to_str ⇒ Object
Convert a FileList to a string prior to resolving.
Methods inherited from Array
#[], #apply_snapshot, #at, #take_snapshot, #values_at
Constructor Details
#initialize(*patterns) {|_self| ... } ⇒ FileList
58 59 60 61 62 63 64 65 66 |
# File 'lib/carat/filelist.rb', line 58 def initialize(*patterns) @pending_add = [] @pending = false @exclude_patterns = DEFAULT_IGNORE_PATTERNS.dup @exclude_re = nil # @extent_only = false patterns.each { |pattern| include(pattern) } yield self if block_given? end |
Class Method Details
.[](*args) ⇒ Object
253 254 255 |
# File 'lib/carat/filelist.rb', line 253 def [](*args) new(*args) end |
.clear_ignore_patterns ⇒ Object
Clear the ignore patterns.
272 273 274 |
# File 'lib/carat/filelist.rb', line 272 def clear_ignore_patterns @exclude_patterns = [ /^$/ ] end |
.select_default_ignore_patterns ⇒ Object
Set the ignore patterns back to the default value. The default patterns will ignore files
-
containing “CVS” 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.
267 268 269 |
# File 'lib/carat/filelist.rb', line 267 def select_default_ignore_patterns @exclude_patterns = DEFAULT_IGNORE_PATTERNS.dup end |
Instance Method Details
#calculate_exclude_regexp ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/carat/filelist.rb', line 135 def calculate_exclude_regexp ignores = [] @exclude_patterns.each do |pat| case pat when Regexp ignores << pat when /[*.]/ Dir[pat].each do |p| ignores << p end else ignores << Regexp.quote(pat) end end if ignores.empty? @exclude_re = /^$/ else re_str = ignores.collect { |p| "(" + p.to_s + ")" }.join("|") @exclude_re = Regexp.new(re_str) end end |
#clear_exclude ⇒ Object
121 122 123 124 |
# File 'lib/carat/filelist.rb', line 121 def clear_exclude @exclude_patterns = [] calculate_exclude_regexp if ! @pending end |
#exclude(*patterns) ⇒ Object
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']
111 112 113 114 115 116 117 118 |
# File 'lib/carat/filelist.rb', line 111 def exclude(*patterns) # TODO: check for pending patterns.each do |pat| @exclude_patterns << pat end if ! @pending calculate_exclude_regexp reject! { |fn| fn =~ @exclude_re } end end |
#exclude?(fn) ⇒ Boolean
Should the given file name be excluded?
236 237 238 239 |
# File 'lib/carat/filelist.rb', line 236 def exclude?(fn) calculate_exclude_regexp unless @exclude_re fn =~ @exclude_re end |
#gsub(pat, rep) ⇒ Object
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']
200 201 202 |
# File 'lib/carat/filelist.rb', line 200 def gsub(pat, rep) inject(FileList.new) { |res, fn| res << fn.gsub(pat,rep) } end |
#gsub!(pat, rep) ⇒ Object
Same as gsub
except that the original file list is modified.
211 212 213 214 |
# File 'lib/carat/filelist.rb', line 211 def gsub!(pat, rep) each_with_index { |fn, i| self[i] = fn.gsub(pat,rep) } self end |
#include(*filenames) ⇒ Object Also known as: add
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 )
75 76 77 78 79 80 |
# File 'lib/carat/filelist.rb', line 75 def include(*filenames) # TODO: check for pending filenames.each do |fn| @pending_add << fn end @pending = true self end |
#resolve ⇒ Object
Resolve all the pending adds now.
127 128 129 130 131 132 133 |
# File 'lib/carat/filelist.rb', line 127 def resolve @pending = false @pending_add.each do |fn| resolve_add(fn) end @pending_add = [] resolve_exclude self end |
#resolve_add(fn) ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/carat/filelist.rb', line 155 def resolve_add(fn) case fn when NilClass # ignore when Array fn.each { |f| self.resolve_add(f) } when %r{[*?]} add_matching(fn) else self << fn if FileTest.exists?( fn ) end end |
#resolve_exclude ⇒ Object
168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/carat/filelist.rb', line 168 def resolve_exclude @exclude_patterns.each do |pat| case pat when Regexp reject! { |fn| fn =~ pat } when /[*.]/ reject_list = Dir[pat] reject! { |fn| reject_list.include?(fn) } else reject! { |fn| fn == pat } end end self end |
#sub(pat, rep) ⇒ Object
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']
189 190 191 |
# File 'lib/carat/filelist.rb', line 189 def sub(pat, rep) inject(FileList.new) { |res, fn| res << fn.sub(pat,rep) } end |
#sub!(pat, rep) ⇒ Object
Same as sub
except that the oringal file list is modified.
205 206 207 208 |
# File 'lib/carat/filelist.rb', line 205 def sub!(pat, rep) each_with_index { |fn, i| self[i] = fn.sub(pat,rep) } self end |
#to_s ⇒ Object
Convert a FileList to a string by joining all elements with a space.
217 218 219 220 |
# File 'lib/carat/filelist.rb', line 217 def to_s resolve self.join(' ') end |
#to_str ⇒ Object
Convert a FileList to a string prior to resolving.
223 224 225 |
# File 'lib/carat/filelist.rb', line 223 def to_str self.join(' ') end |