Class: Buildr::Filter
Overview
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.
Defined Under Namespace
Classes: Mapper
Instance Attribute Summary collapse
-
#sources ⇒ Object
readonly
Returns the list of source directories (each being a file task).
Instance Method Summary collapse
-
#clear ⇒ Object
:call-seq: clear => self.
-
#exclude(*files) ⇒ Object
:call-seq: exclude(*files) => self.
-
#from(*sources) ⇒ Object
:call-seq: from(*sources) => self.
-
#include(*files) ⇒ Object
(also: #add)
:call-seq: include(*files) => self.
-
#initialize ⇒ Filter
constructor
:nodoc:.
-
#into(dir) ⇒ Object
:call-seq: into(dir) => self.
-
#mapper ⇒ Object
The mapper to use.
-
#mapping ⇒ Object
The mapping.
-
#run ⇒ Object
:call-seq: run => boolean.
-
#target ⇒ Object
The target directory as a file task.
-
#to_s ⇒ Object
Returns the target directory.
-
#using(*args, &block) ⇒ Object
:call-seq: using(mapping) => self using { |file_name, contents| … } => self.
Constructor Details
#initialize ⇒ Filter
:nodoc:
51 52 53 |
# File 'lib/buildr/core/filter.rb', line 51 def initialize #:nodoc: clear end |
Instance Attribute Details
#sources ⇒ Object (readonly)
Returns the list of source directories (each being a file task).
56 57 58 |
# File 'lib/buildr/core/filter.rb', line 56 def sources @sources end |
Instance Method Details
#clear ⇒ Object
:call-seq:
clear => self
Clear filter sources and include/exclude patterns
62 63 64 65 66 67 68 |
# File 'lib/buildr/core/filter.rb', line 62 def clear @include = [] @exclude = [] @sources = FileList[] @mapper = Mapper.new self end |
#exclude(*files) ⇒ Object
:call-seq:
exclude(*files) => self
Specifies files to exclude and returns self. See FileList#exclude.
124 125 126 127 |
# File 'lib/buildr/core/filter.rb', line 124 def exclude(*files) @exclude += files.flatten self end |
#from(*sources) ⇒ Object
:call-seq:
from(*sources) => self
Adds additional directories from which to copy resources.
For example:
filter.from('src').into('target').using('build'=>Time.now)
77 78 79 80 |
# File 'lib/buildr/core/filter.rb', line 77 def from(*sources) @sources |= sources.flatten.map { |dir| file(File.(dir.to_s)) } self end |
#include(*files) ⇒ Object Also known as: add
:call-seq:
include(*files) => self
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.
114 115 116 117 |
# File 'lib/buildr/core/filter.rb', line 114 def include(*files) @include += files.flatten self end |
#into(dir) ⇒ Object
:call-seq:
into(dir) => self
Sets the target directory into which files are copied and returns self.
For example:
filter.from('src').into('target').using('build'=>Time.now)
101 102 103 104 105 |
# File 'lib/buildr/core/filter.rb', line 101 def into(dir) @target_dir = dir.to_s @target = nil self end |
#mapper ⇒ Object
The mapper to use. See #using.
135 136 137 |
# File 'lib/buildr/core/filter.rb', line 135 def mapper #:nodoc: @mapper.mapper_type end |
#mapping ⇒ Object
The mapping. See #using.
130 131 132 |
# File 'lib/buildr/core/filter.rb', line 130 def mapping #:nodoc: @mapper.config end |
#run ⇒ Object
:call-seq:
run => boolean
Runs the filter.
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/buildr/core/filter.rb', line 174 def run copy_map = copy_map() mkpath target.to_s return false if copy_map.empty? copy_map.each do |path, source| dest = File.(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 |
#target ⇒ Object
The target directory as a file task.
83 84 85 86 87 88 89 90 91 92 |
# File 'lib/buildr/core/filter.rb', line 83 def target return nil unless @target_dir unless @target @target = file(File.(@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 |
#to_s ⇒ Object
Returns the target directory.
200 201 202 |
# File 'lib/buildr/core/filter.rb', line 200 def to_s target.to_s end |
#using(*args, &block) ⇒ Object
:call-seq:
using(mapping) => self
using { |file_name, contents| ... } => self
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.
165 166 167 168 |
# File 'lib/buildr/core/filter.rb', line 165 def using(*args, &block) @mapper.using(*args, &block) self end |