Class: Rake::FileList

Inherits:
Array
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(*patterns) {|_self| ... } ⇒ FileList

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

Yields:

  • (_self)

Yield Parameters:



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

Create a new file list including the files listed. Similar to:

FileList.new(*args)


778
779
780
# File 'lib/rake.rb', line 778

def [](*args)
	new(*args)
end

.clear_ignore_patternsObject

Clear the ignore patterns.



797
798
799
# File 'lib/rake.rb', line 797

def clear_ignore_patterns
	@exclude_patterns = [ /^$/ ]
end

.select_default_ignore_patternsObject

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_regexpObject



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_excludeObject



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?

Returns:

  • (Boolean)


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

#resolveObject

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_excludeObject



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_sObject

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