class Buildr::Filter
A filter knows how to copy files from one directory to another, applying mappings to the contents of these files.
You can specify the mapping using a Hash, and it will map ${key} fields found in each source file into the appropriate value in the target file. For example:
filter.using 'version'=>'1.2', 'build'=>Time.now
will replace all occurrences of ${version}
with
1.2
, and ${build}
with the current date/time.
You can also specify the mapping by passing a proc or a method, that will be called for each source file, with the file name and content, returning the modified content.
Without any mapping, the filter simply copies files from the source directory into the target directory.
A filter has one target directory, but you can specify any number of source directories, either when creating the filter or calling from. Include/exclude patterns are specified relative to the source directories, so:
filter.include '*.png'
will only include PNG files from any of the source directories. In the same way, you can use regular expressions, so:
filter.include /picture_.*\.png/
will only include PNG files starting with picture_ from any of the sources directories.
See Buildr#filter.
Attributes
Returns the list of source directories (each being a file task).
Public Instance Methods
Clear filter sources and include/exclude patterns
# File lib/buildr/core/filter.rb, line 58 def clear @include = [] @exclude = [] @sources = FileList[] @mapper = Mapper.new self end
Specifies files to exclude and returns self. See FileList#exclude.
# File lib/buildr/core/filter.rb, line 120 def exclude(*files) @exclude += files.flatten self end
Adds additional directories from which to copy resources.
For example:
filter.from('src').into('target').using('build'=>Time.now)
# File lib/buildr/core/filter.rb, line 73 def from(*sources) @sources |= sources.flatten.map { |dir| file(File.expand_path(dir.to_s)) } self end
Specifies files to include and returns self. See FileList#include.
By default all files are included. You can use this method to only include specific files from the source directory.
# File lib/buildr/core/filter.rb, line 110 def include(*files) @include += files.flatten self end
Sets the target directory into which files are copied and returns self.
For example:
filter.from('src').into('target').using('build'=>Time.now)
# File lib/buildr/core/filter.rb, line 97 def into(dir) @target_dir = dir.to_s @target = nil self end
Runs the filter.
# File lib/buildr/core/filter.rb, line 170 def run copy_map = copy_map() mkpath target.to_s return false if copy_map.empty? copy_map.each do |path, source| dest = File.expand_path(path, target.to_s) if File.directory?(source) mkpath dest else mkpath File.dirname(dest) if @mapper.mapper_type mapped = @mapper.transform(File.open(source, 'rb') { |file| file.read }, path) File.open(dest, 'wb') { |file| file.write mapped } else # no mapping cp source, dest end end File.chmod(File.stat(source).mode | 0200, dest) end touch target.to_s true end
The target directory as a file task.
# File lib/buildr/core/filter.rb, line 79 def target return nil unless @target_dir unless @target @target = file(File.expand_path(@target_dir)) { |task| run if @target == task } @target.enhance @include.select {|f| f.is_a?(Rake::FileTask)} @target.enhance @exclude.select {|f| f.is_a?(Rake::FileTask)} @target.enhance copy_map.values end @target end
Returns the target directory.
# File lib/buildr/core/filter.rb, line 196 def to_s target.to_s end
Specifies the mapping to use and returns self.
The most typical mapping uses a Hash, and the
default mapping uses the Maven style, so ${key}
are mapped to
the values. You can change that by passing a different format as the first
argument. Currently supports:
-
:ant – Map
@key@
. -
:maven – Map
${key}
(default). -
:ruby – Map
#{key}
. -
:erb – Map
<%= key %>
. -
Regexp – Maps the matched data (e.g.
/=(.*?)=/
For example:
filter.using 'version'=>'1.2'
Is the same as:
filter.using :maven, 'version'=>'1.2'
You can also pass a proc or method. It will be called with the file name and content, to return the mapped content.
Without any mapping, all files are copied as is.
To register new mapping type see the Mapper class.
# File lib/buildr/core/filter.rb, line 161 def using(*args, &block) @mapper.using(*args, &block) self end
Protected Instance Methods
This method returns true if the file name matches the pattern. The pattern may be a String, a Regexp or a Proc.
# File lib/buildr/core/filter.rb, line 208 def pattern_match(file, pattern) case when pattern.is_a?(Regexp) return file.match(pattern) when pattern.is_a?(String) return File.fnmatch(pattern, file) when pattern.is_a?(Proc) return pattern.call(file) when pattern.is_a?(Rake::FileTask) return pattern.to_s.match(file) else raise "Cannot interpret pattern #{pattern}" end end