Class: Rake::FileList
- Inherits:
-
Array
- Object
- Array
- Rake::FileList
- Defined in:
- lib/rake.rb
Overview
A FileList is essentially an array with a few helper methods defined to make file manipulation a bit easier.
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.
Constructor Details
#initialize(*patterns) {|_self| ... } ⇒ FileList
599 600 601 602 603 604 605 606 |
# File 'lib/rake.rb', line 599 def initialize(*patterns) @pending_add = [] @pending = false @exclude_patterns = DEFAULT_IGNORE_PATTERNS.dup @exclude_re = nil patterns.each { |pattern| include(pattern) } yield self if block_given? end |
Class Method Details
.[](*args) ⇒ Object
778 779 780 |
# File 'lib/rake.rb', line 778 def [](*args) new(*args) end |
.clear_ignore_patterns ⇒ Object
Clear the ignore patterns.
797 798 799 |
# File 'lib/rake.rb', line 797 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.
792 793 794 |
# File 'lib/rake.rb', line 792 def select_default_ignore_patterns @exclude_patterns = DEFAULT_IGNORE_PATTERNS.dup end |
Instance Method Details
#calculate_exclude_regexp ⇒ Object
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 |
# File 'lib/rake.rb', line 667 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
653 654 655 656 |
# File 'lib/rake.rb', line 653 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']
642 643 644 645 646 647 648 649 650 |
# File 'lib/rake.rb', line 642 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 self end |
#exclude?(fn) ⇒ Boolean
Should the given file name be excluded?
761 762 763 764 |
# File 'lib/rake.rb', line 761 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']
730 731 732 |
# File 'lib/rake.rb', line 730 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.
741 742 743 744 |
# File 'lib/rake.rb', line 741 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 )
615 616 617 618 619 620 |
# File 'lib/rake.rb', line 615 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.
659 660 661 662 663 664 665 |
# File 'lib/rake.rb', line 659 def resolve @pending = false @pending_add.each do |fn| resolve_add(fn) end @pending_add = [] resolve_exclude self end |
#resolve_add(fn) ⇒ Object
687 688 689 690 691 692 693 694 695 696 |
# File 'lib/rake.rb', line 687 def resolve_add(fn) case fn when Array fn.each { |f| self.resolve_add(f) } when %r{[*?]} add_matching(fn) else self << fn end end |
#resolve_exclude ⇒ Object
698 699 700 701 702 703 704 705 706 707 708 709 710 711 |
# File 'lib/rake.rb', line 698 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']
719 720 721 |
# File 'lib/rake.rb', line 719 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.
735 736 737 738 |
# File 'lib/rake.rb', line 735 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.
747 748 749 750 |
# File 'lib/rake.rb', line 747 def to_s resolve if @pending self.join(' ') end |